Lines 30-36
Link Here
|
30 |
// Static initializer to specify tests subset using TESTS_* static variables |
30 |
// Static initializer to specify tests subset using TESTS_* static variables |
31 |
// All specified tests which does not belong to the class are skipped... |
31 |
// All specified tests which does not belong to the class are skipped... |
32 |
static { |
32 |
static { |
33 |
// TESTS_NAMES = new String[] { "test127" }; |
33 |
// TESTS_NAMES = new String[] { "test120" }; |
34 |
// TESTS_NUMBERS = new int[] { 113 }; |
34 |
// TESTS_NUMBERS = new int[] { 113 }; |
35 |
// TESTS_RANGE = new int[] { 108, -1 }; |
35 |
// TESTS_RANGE = new int[] { 108, -1 }; |
36 |
} |
36 |
} |
Lines 5892-5895
Link Here
|
5892 |
false, |
5892 |
false, |
5893 |
null); |
5893 |
null); |
5894 |
} |
5894 |
} |
|
|
5895 |
|
5896 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
5897 |
// QualifiedNameReference, SingleNameReference and MessageSend |
5898 |
// Can be static warning shown |
5899 |
public void test114() { |
5900 |
Map compilerOptions = getCompilerOptions(); |
5901 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
5902 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
5903 |
this.runNegativeTest( |
5904 |
new String[] { |
5905 |
"X.java", |
5906 |
"public class X {\n" + |
5907 |
" public static int field1;\n" + |
5908 |
" public static int field2;\n" + |
5909 |
" public void bar(int i) {\n" + |
5910 |
" System.out.println(foo());\n" + |
5911 |
" foo();" + |
5912 |
" System.out.println(X.field1);\n" + |
5913 |
" System.out.println(field2);\n" + |
5914 |
" field2 = 1;\n" + |
5915 |
" }\n" + |
5916 |
" public final void bar2(int i) {\n" + |
5917 |
" System.out.println(foo());\n" + |
5918 |
" foo();" + |
5919 |
" System.out.println(X.field1);\n" + |
5920 |
" System.out.println(field2);\n" + |
5921 |
" field2 = 1;\n" + |
5922 |
" }\n" + |
5923 |
" private void bar3(int i) {\n" + |
5924 |
" System.out.println(foo());\n" + |
5925 |
" foo();" + |
5926 |
" System.out.println(X.field1);\n" + |
5927 |
" System.out.println(field2);\n" + |
5928 |
" field2 = 1;\n" + |
5929 |
" }\n" + |
5930 |
" private static String foo() {\n" + |
5931 |
" return null;\n" + |
5932 |
" }\n" + |
5933 |
"}" |
5934 |
}, |
5935 |
"----------\n" + |
5936 |
"1. ERROR in X.java (at line 4)\n" + |
5937 |
" public void bar(int i) {\n" + |
5938 |
" ^^^^^^^^^^\n" + |
5939 |
"The method bar(int) from the type X can potentially be declared as static\n" + |
5940 |
"----------\n" + |
5941 |
"2. ERROR in X.java (at line 10)\n" + |
5942 |
" public final void bar2(int i) {\n" + |
5943 |
" ^^^^^^^^^^^\n" + |
5944 |
"The method bar2(int) from the type X can be declared as static\n" + |
5945 |
"----------\n" + |
5946 |
"3. WARNING in X.java (at line 16)\n" + |
5947 |
" private void bar3(int i) {\n" + |
5948 |
" ^^^^^^^^^^^\n" + |
5949 |
"The method bar3(int) from the type X is never used locally\n" + |
5950 |
"----------\n" + |
5951 |
"4. ERROR in X.java (at line 16)\n" + |
5952 |
" private void bar3(int i) {\n" + |
5953 |
" ^^^^^^^^^^^\n" + |
5954 |
"The method bar3(int) from the type X can be declared as static\n" + |
5955 |
"----------\n", |
5956 |
null /* no extra class libraries */, |
5957 |
true /* flush output directory */, |
5958 |
compilerOptions /* custom options */ |
5959 |
); |
5960 |
} |
5961 |
|
5962 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
5963 |
// FieldReference and MessageSend |
5964 |
// Can be static warning shown |
5965 |
public void test115() { |
5966 |
Map compilerOptions = getCompilerOptions(); |
5967 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
5968 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
5969 |
this.runNegativeTest( |
5970 |
new String[] { |
5971 |
"X.java", |
5972 |
"public class X extends B{\n" + |
5973 |
" public static int field1;\n" + |
5974 |
" public static int field2;\n" + |
5975 |
" public void bar(int i) {\n" + |
5976 |
" System.out.println(foo());\n" + |
5977 |
" X.field2 = 2;\n" + |
5978 |
" System.out.println(field1);\n" + |
5979 |
" A a = new A();\n" + |
5980 |
" a.a1();\n" + |
5981 |
" }\n" + |
5982 |
" private static String foo() {\n" + |
5983 |
" return null;\n" + |
5984 |
" }\n" + |
5985 |
"}\n" + |
5986 |
"class A{\n" + |
5987 |
" public void a1() {\n" + |
5988 |
" }\n" + |
5989 |
"}\n" + |
5990 |
"class B{\n" + |
5991 |
" public void b1(){\n" + |
5992 |
" }\n" + |
5993 |
"}" |
5994 |
}, |
5995 |
"----------\n" + |
5996 |
"1. ERROR in X.java (at line 4)\n" + |
5997 |
" public void bar(int i) {\n" + |
5998 |
" ^^^^^^^^^^\n" + |
5999 |
"The method bar(int) from the type X can potentially be declared as static\n" + |
6000 |
"----------\n", |
6001 |
null /* no extra class libraries */, |
6002 |
true /* flush output directory */, |
6003 |
compilerOptions /* custom options */ |
6004 |
); |
6005 |
} |
6006 |
|
6007 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
6008 |
// MessageSend in different ways |
6009 |
public void test116a() { |
6010 |
Map compilerOptions = getCompilerOptions(); |
6011 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
6012 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
6013 |
compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); |
6014 |
this.runNegativeTest( |
6015 |
new String[] { |
6016 |
"X.java", |
6017 |
"public class X extends B{\n" + |
6018 |
" public static int field1;\n" + |
6019 |
" public X xfield;\n" + |
6020 |
" public void bar1(int i) {\n" + |
6021 |
" baz();\n" + |
6022 |
" }\n" + |
6023 |
" public void bar2(int i) {\n" + |
6024 |
" this.baz();\n" + |
6025 |
" }\n" + |
6026 |
" public void bar3(int i) {\n" + |
6027 |
" this.xfield.baz();\n" + |
6028 |
" }\n" + |
6029 |
" public void bar4(int i) {\n" + |
6030 |
" xfield.baz();\n" + |
6031 |
" }\n" + |
6032 |
" public void bar5(int i) {\n" + |
6033 |
" X x = new X();\n" + |
6034 |
" x.baz();\n" + |
6035 |
" }\n" + |
6036 |
" public void bar6(int i) {\n" + |
6037 |
" A.xA.baz();\n" + |
6038 |
" }\n" + |
6039 |
" public void bar7(int i) {\n" + |
6040 |
" b1();\n" + |
6041 |
" }\n" + |
6042 |
" public void bar8(int i) {\n" + |
6043 |
" this.b1();\n" + |
6044 |
" }\n" + |
6045 |
" public void bar9(int i) {\n" + |
6046 |
" new X().b1();\n" + |
6047 |
" }\n" + |
6048 |
" public void baz() {\n" + |
6049 |
" }\n" + |
6050 |
"}\n" + |
6051 |
"class A{\n" + |
6052 |
" public static X xA;\n" + |
6053 |
"}\n" + |
6054 |
"class B{\n" + |
6055 |
" public void b1(){\n" + |
6056 |
" }\n" + |
6057 |
"}", |
6058 |
}, |
6059 |
"----------\n" + |
6060 |
"1. ERROR in X.java (at line 16)\n" + |
6061 |
" public void bar5(int i) {\n" + |
6062 |
" ^^^^^^^^^^^\n" + |
6063 |
"The method bar5(int) from the type X can potentially be declared as static\n" + |
6064 |
"----------\n" + |
6065 |
"2. ERROR in X.java (at line 20)\n" + |
6066 |
" public void bar6(int i) {\n" + |
6067 |
" ^^^^^^^^^^^\n" + |
6068 |
"The method bar6(int) from the type X can potentially be declared as static\n" + |
6069 |
"----------\n" + |
6070 |
"3. ERROR in X.java (at line 29)\n" + |
6071 |
" public void bar9(int i) {\n" + |
6072 |
" ^^^^^^^^^^^\n" + |
6073 |
"The method bar9(int) from the type X can potentially be declared as static\n" + |
6074 |
"----------\n", |
6075 |
null /* no extra class libraries */, |
6076 |
true /* flush output directory */, |
6077 |
compilerOptions /* custom options */ |
6078 |
); |
6079 |
} |
6080 |
|
6081 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
6082 |
// MessageSend in different ways, referencing a static method. |
6083 |
public void test116b() { |
6084 |
Map compilerOptions = getCompilerOptions(); |
6085 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
6086 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
6087 |
compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); |
6088 |
this.runNegativeTest( |
6089 |
new String[] { |
6090 |
"X.java", |
6091 |
"public class X extends B{\n" + |
6092 |
" public static int field1;\n" + |
6093 |
" public static X xfield;\n" + |
6094 |
" public void bar1(int i) {\n" + |
6095 |
" baz();\n" + |
6096 |
" }\n" + |
6097 |
" public void bar2(int i) {\n" + |
6098 |
" this.baz();\n" + |
6099 |
" }\n" + |
6100 |
" public void bar3(int i) {\n" + |
6101 |
" this.xfield.baz();\n" + |
6102 |
" }\n" + |
6103 |
" public void bar4(int i) {\n" + |
6104 |
" xfield.baz();\n" + |
6105 |
" }\n" + |
6106 |
" public void bar5(int i) {\n" + |
6107 |
" X x = new X();\n" + |
6108 |
" x.baz();\n" + |
6109 |
" }\n" + |
6110 |
" public void bar6(int i) {\n" + |
6111 |
" A.xA.baz();\n" + |
6112 |
" }\n" + |
6113 |
" public void bar7(int i) {\n" + |
6114 |
" b1();\n" + |
6115 |
" }\n" + |
6116 |
" public void bar8(int i) {\n" + |
6117 |
" this.b1();\n" + |
6118 |
" }\n" + |
6119 |
" public void bar9(int i) {\n" + |
6120 |
" new X().b1();\n" + |
6121 |
" }\n" + |
6122 |
" public static void baz() {\n" + |
6123 |
" }\n" + |
6124 |
"}\n" + |
6125 |
"class A{\n" + |
6126 |
" public static X xA;\n" + |
6127 |
"}\n" + |
6128 |
"class B{\n" + |
6129 |
" public static void b1(){\n" + |
6130 |
" }\n" + |
6131 |
"}", |
6132 |
}, |
6133 |
"----------\n" + |
6134 |
"1. ERROR in X.java (at line 4)\n" + |
6135 |
" public void bar1(int i) {\n" + |
6136 |
" ^^^^^^^^^^^\n" + |
6137 |
"The method bar1(int) from the type X can potentially be declared as static\n" + |
6138 |
"----------\n" + |
6139 |
"2. ERROR in X.java (at line 13)\n" + |
6140 |
" public void bar4(int i) {\n" + |
6141 |
" ^^^^^^^^^^^\n" + |
6142 |
"The method bar4(int) from the type X can potentially be declared as static\n" + |
6143 |
"----------\n" + |
6144 |
"3. ERROR in X.java (at line 16)\n" + |
6145 |
" public void bar5(int i) {\n" + |
6146 |
" ^^^^^^^^^^^\n" + |
6147 |
"The method bar5(int) from the type X can potentially be declared as static\n" + |
6148 |
"----------\n" + |
6149 |
"4. ERROR in X.java (at line 20)\n" + |
6150 |
" public void bar6(int i) {\n" + |
6151 |
" ^^^^^^^^^^^\n" + |
6152 |
"The method bar6(int) from the type X can potentially be declared as static\n" + |
6153 |
"----------\n" + |
6154 |
"5. ERROR in X.java (at line 23)\n" + |
6155 |
" public void bar7(int i) {\n" + |
6156 |
" ^^^^^^^^^^^\n" + |
6157 |
"The method bar7(int) from the type X can potentially be declared as static\n" + |
6158 |
"----------\n" + |
6159 |
"6. ERROR in X.java (at line 29)\n" + |
6160 |
" public void bar9(int i) {\n" + |
6161 |
" ^^^^^^^^^^^\n" + |
6162 |
"The method bar9(int) from the type X can potentially be declared as static\n" + |
6163 |
"----------\n", |
6164 |
null /* no extra class libraries */, |
6165 |
true /* flush output directory */, |
6166 |
compilerOptions /* custom options */ |
6167 |
); |
6168 |
} |
6169 |
|
6170 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
6171 |
// Referring a field in different ways, accessing non-static field. |
6172 |
public void test117a() { |
6173 |
Map compilerOptions = getCompilerOptions(); |
6174 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
6175 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
6176 |
compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); |
6177 |
this.runNegativeTest( |
6178 |
new String[] { |
6179 |
"X.java", |
6180 |
"public class X extends B{\n" + |
6181 |
" public int field1;\n" + |
6182 |
" public X xfield;\n" + |
6183 |
" public void bar1(int i) {\n" + |
6184 |
" field1 = 1;\n" + |
6185 |
" }\n" + |
6186 |
" public void bar2(int i) {\n" + |
6187 |
" this.field1 = 1;\n" + |
6188 |
" }\n" + |
6189 |
" public void bar3(int i) {\n" + |
6190 |
" System.out.println(field1);\n" + |
6191 |
" }\n" + |
6192 |
" public void bar4(int i) {\n" + |
6193 |
" System.out.println(this.field1);\n" + |
6194 |
" }\n" + |
6195 |
" public void bar5(int i) {\n" + |
6196 |
" X x = new X();\n" + |
6197 |
" x.field1 = 1;\n" + |
6198 |
" }\n" + |
6199 |
" public void bar6(int i) {\n" + |
6200 |
" A.xA.field1 = 1;\n" + |
6201 |
" }\n" + |
6202 |
" public void bar7(int i) {\n" + |
6203 |
" b1 = 1;\n" + |
6204 |
" }\n" + |
6205 |
" public void bar8(int i) {\n" + |
6206 |
" this.b1 = 1;\n" + |
6207 |
" }\n" + |
6208 |
" public void bar9(int i) {\n" + |
6209 |
" new X().b1 = 1;\n" + |
6210 |
" }\n" + |
6211 |
" public void bar10(int i) {\n" + |
6212 |
" this.xfield.field1 = 1;\n" + |
6213 |
" }\n" + |
6214 |
" public void bar11(int i) {\n" + |
6215 |
" System.out.println(this.xfield.field1);\n" + |
6216 |
" }\n" + |
6217 |
" public void bar12(int i) {\n" + |
6218 |
" System.out.println(new X().b1);\n" + |
6219 |
" }\n" + |
6220 |
" public void bar13(int i) {\n" + |
6221 |
" System.out.println(b1);\n" + |
6222 |
" }\n" + |
6223 |
" public void bar14(int i) {\n" + |
6224 |
" System.out.println(this.b1);\n" + |
6225 |
" }\n" + |
6226 |
" public void bar15(int i) {\n" + |
6227 |
" xfield.field1 = 1;\n" + |
6228 |
" }\n" + |
6229 |
" public void bar16(int i) {\n" + |
6230 |
" System.out.println(xfield.field1);\n" + |
6231 |
" }\n" + |
6232 |
" public void baz() {\n" + |
6233 |
" }\n" + |
6234 |
"}\n" + |
6235 |
"class A{\n" + |
6236 |
" public static X xA;\n" + |
6237 |
"}\n" + |
6238 |
"class B{\n" + |
6239 |
" public int b1;\n" + |
6240 |
"}", |
6241 |
}, |
6242 |
"----------\n" + |
6243 |
"1. ERROR in X.java (at line 16)\n" + |
6244 |
" public void bar5(int i) {\n" + |
6245 |
" ^^^^^^^^^^^\n" + |
6246 |
"The method bar5(int) from the type X can potentially be declared as static\n" + |
6247 |
"----------\n" + |
6248 |
"2. ERROR in X.java (at line 20)\n" + |
6249 |
" public void bar6(int i) {\n" + |
6250 |
" ^^^^^^^^^^^\n" + |
6251 |
"The method bar6(int) from the type X can potentially be declared as static\n" + |
6252 |
"----------\n" + |
6253 |
"3. ERROR in X.java (at line 29)\n" + |
6254 |
" public void bar9(int i) {\n" + |
6255 |
" ^^^^^^^^^^^\n" + |
6256 |
"The method bar9(int) from the type X can potentially be declared as static\n" + |
6257 |
"----------\n" + |
6258 |
"4. ERROR in X.java (at line 38)\n" + |
6259 |
" public void bar12(int i) {\n" + |
6260 |
" ^^^^^^^^^^^^\n" + |
6261 |
"The method bar12(int) from the type X can potentially be declared as static\n" + |
6262 |
"----------\n", |
6263 |
null /* no extra class libraries */, |
6264 |
true /* flush output directory */, |
6265 |
compilerOptions /* custom options */ |
6266 |
); |
6267 |
} |
6268 |
|
6269 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
6270 |
// Referring a field in different ways, accessing non-static field. |
6271 |
public void test117b() { |
6272 |
Map compilerOptions = getCompilerOptions(); |
6273 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
6274 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
6275 |
compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); |
6276 |
this.runNegativeTest( |
6277 |
new String[] { |
6278 |
"X.java", |
6279 |
"public class X extends B{\n" + |
6280 |
" public static int field1;\n" + |
6281 |
" public static X xfield;\n" + |
6282 |
" public void bar1(int i) {\n" + |
6283 |
" field1 = 1;\n" + |
6284 |
" }\n" + |
6285 |
" public void bar2(int i) {\n" + |
6286 |
" this.field1 = 1;\n" + |
6287 |
" }\n" + |
6288 |
" public void bar3(int i) {\n" + |
6289 |
" System.out.println(field1);\n" + |
6290 |
" }\n" + |
6291 |
" public void bar4(int i) {\n" + |
6292 |
" System.out.println(this.field1);\n" + |
6293 |
" }\n" + |
6294 |
" public void bar5(int i) {\n" + |
6295 |
" X x = new X();\n" + |
6296 |
" x.field1 = 1;\n" + |
6297 |
" }\n" + |
6298 |
" public void bar6(int i) {\n" + |
6299 |
" A.xA.field1 = 1;\n" + |
6300 |
" }\n" + |
6301 |
" public void bar7(int i) {\n" + |
6302 |
" b1 = 1;\n" + |
6303 |
" }\n" + |
6304 |
" public void bar8(int i) {\n" + |
6305 |
" this.b1 = 1;\n" + |
6306 |
" }\n" + |
6307 |
" public void bar9(int i) {\n" + |
6308 |
" new X().b1 = 1;\n" + |
6309 |
" }\n" + |
6310 |
" public void bar10(int i) {\n" + |
6311 |
" this.xfield.field1 = 1;\n" + |
6312 |
" }\n" + |
6313 |
" public void bar11(int i) {\n" + |
6314 |
" System.out.println(this.xfield.field1);\n" + |
6315 |
" }\n" + |
6316 |
" public void bar12(int i) {\n" + |
6317 |
" System.out.println(new X().b1);\n" + |
6318 |
" }\n" + |
6319 |
" public void bar13(int i) {\n" + |
6320 |
" System.out.println(b1);\n" + |
6321 |
" }\n" + |
6322 |
" public void bar14(int i) {\n" + |
6323 |
" System.out.println(this.b1);\n" + |
6324 |
" }\n" + |
6325 |
" public void bar15(int i) {\n" + |
6326 |
" xfield.field1 = 1;\n" + |
6327 |
" }\n" + |
6328 |
" public void bar16(int i) {\n" + |
6329 |
" System.out.println(xfield.field1);\n" + |
6330 |
" }\n" + |
6331 |
" public void baz() {\n" + |
6332 |
" }\n" + |
6333 |
"}\n" + |
6334 |
"class A{\n" + |
6335 |
" public static X xA;\n" + |
6336 |
"}\n" + |
6337 |
"class B{\n" + |
6338 |
" public static int b1;\n" + |
6339 |
"}", |
6340 |
}, |
6341 |
"----------\n" + |
6342 |
"1. ERROR in X.java (at line 4)\n" + |
6343 |
" public void bar1(int i) {\n" + |
6344 |
" ^^^^^^^^^^^\n" + |
6345 |
"The method bar1(int) from the type X can potentially be declared as static\n" + |
6346 |
"----------\n" + |
6347 |
"2. ERROR in X.java (at line 10)\n" + |
6348 |
" public void bar3(int i) {\n" + |
6349 |
" ^^^^^^^^^^^\n" + |
6350 |
"The method bar3(int) from the type X can potentially be declared as static\n" + |
6351 |
"----------\n" + |
6352 |
"3. ERROR in X.java (at line 16)\n" + |
6353 |
" public void bar5(int i) {\n" + |
6354 |
" ^^^^^^^^^^^\n" + |
6355 |
"The method bar5(int) from the type X can potentially be declared as static\n" + |
6356 |
"----------\n" + |
6357 |
"4. ERROR in X.java (at line 20)\n" + |
6358 |
" public void bar6(int i) {\n" + |
6359 |
" ^^^^^^^^^^^\n" + |
6360 |
"The method bar6(int) from the type X can potentially be declared as static\n" + |
6361 |
"----------\n" + |
6362 |
"5. ERROR in X.java (at line 23)\n" + |
6363 |
" public void bar7(int i) {\n" + |
6364 |
" ^^^^^^^^^^^\n" + |
6365 |
"The method bar7(int) from the type X can potentially be declared as static\n" + |
6366 |
"----------\n" + |
6367 |
"6. ERROR in X.java (at line 29)\n" + |
6368 |
" public void bar9(int i) {\n" + |
6369 |
" ^^^^^^^^^^^\n" + |
6370 |
"The method bar9(int) from the type X can potentially be declared as static\n" + |
6371 |
"----------\n" + |
6372 |
"7. ERROR in X.java (at line 38)\n" + |
6373 |
" public void bar12(int i) {\n" + |
6374 |
" ^^^^^^^^^^^^\n" + |
6375 |
"The method bar12(int) from the type X can potentially be declared as static\n" + |
6376 |
"----------\n" + |
6377 |
"8. ERROR in X.java (at line 41)\n" + |
6378 |
" public void bar13(int i) {\n" + |
6379 |
" ^^^^^^^^^^^^\n" + |
6380 |
"The method bar13(int) from the type X can potentially be declared as static\n" + |
6381 |
"----------\n" + |
6382 |
"9. ERROR in X.java (at line 47)\n" + |
6383 |
" public void bar15(int i) {\n" + |
6384 |
" ^^^^^^^^^^^^\n" + |
6385 |
"The method bar15(int) from the type X can potentially be declared as static\n" + |
6386 |
"----------\n" + |
6387 |
"10. ERROR in X.java (at line 50)\n" + |
6388 |
" public void bar16(int i) {\n" + |
6389 |
" ^^^^^^^^^^^^\n" + |
6390 |
"The method bar16(int) from the type X can potentially be declared as static\n" + |
6391 |
"----------\n", |
6392 |
null /* no extra class libraries */, |
6393 |
true /* flush output directory */, |
6394 |
compilerOptions /* custom options */ |
6395 |
); |
6396 |
} |
6397 |
|
6398 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
6399 |
// Final class -> can be static (and not potentially be static) warning shown |
6400 |
public void test118() { |
6401 |
Map compilerOptions = getCompilerOptions(); |
6402 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
6403 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
6404 |
this.runNegativeTest( |
6405 |
new String[] { |
6406 |
"X.java", |
6407 |
"final public class X {\n" + |
6408 |
" public static int field1;\n" + |
6409 |
" public static int field2;\n" + |
6410 |
" public void bar(int i) {\n" + |
6411 |
" System.out.println(foo());\n" + |
6412 |
" foo();" + |
6413 |
" System.out.println(X.field1);\n" + |
6414 |
" System.out.println(field2);\n" + |
6415 |
" field2 = 1;\n" + |
6416 |
" }\n" + |
6417 |
" public static int foo(){ return 1;}\n" + |
6418 |
"}" |
6419 |
}, |
6420 |
"----------\n" + |
6421 |
"1. ERROR in X.java (at line 4)\n" + |
6422 |
" public void bar(int i) {\n" + |
6423 |
" ^^^^^^^^^^\n" + |
6424 |
"The method bar(int) from the type X can be declared as static\n" + |
6425 |
"----------\n", |
6426 |
null /* no extra class libraries */, |
6427 |
true /* flush output directory */, |
6428 |
compilerOptions /* custom options */ |
6429 |
); |
6430 |
} |
6431 |
|
6432 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
6433 |
// Method of a local class -> can't be static, so no warning |
6434 |
// Also method with such a local class accessing a member of the outer class can't be static |
6435 |
public void test119() { |
6436 |
Map compilerOptions = getCompilerOptions(); |
6437 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
6438 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
6439 |
this.runNegativeTest( |
6440 |
new String[] { |
6441 |
"X.java", |
6442 |
"public class X {\n" + |
6443 |
" public static int field1;\n" + |
6444 |
" public int field2;\n" + |
6445 |
" public void bar(int i) {\n" + // don't warn |
6446 |
" (new Object() {\n" + |
6447 |
" public boolean foo1() {\n" + // don't warn for foo1 |
6448 |
" return X.this.field2 == 1;\n" + |
6449 |
" }\n" + |
6450 |
" }).foo1();\n" + |
6451 |
" System.out.println(X.field1);\n" + |
6452 |
" }\n" + |
6453 |
" public void bar2(int i) {\n" + // warn |
6454 |
" (new Object() {\n" + |
6455 |
" public boolean foo1() {\n" + // don't warn for foo1 |
6456 |
" System.out.println(X.field1);\n" + |
6457 |
" return true;" + |
6458 |
" }\n" + |
6459 |
" }).foo1();\n" + |
6460 |
" System.out.println(X.field1);\n" + |
6461 |
" }\n" + |
6462 |
"}" |
6463 |
}, |
6464 |
"----------\n" + |
6465 |
"1. ERROR in X.java (at line 12)\n" + |
6466 |
" public void bar2(int i) {\n" + |
6467 |
" ^^^^^^^^^^^\n" + |
6468 |
"The method bar2(int) from the type X can potentially be declared as static\n" + |
6469 |
"----------\n", |
6470 |
null /* no extra class libraries */, |
6471 |
true /* flush output directory */, |
6472 |
compilerOptions /* custom options */ |
6473 |
); |
6474 |
} |
6475 |
|
6476 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
6477 |
// Method using type parameters declared by enclosing class can't be static, so don't warn |
6478 |
public void test120() { |
6479 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) |
6480 |
return; |
6481 |
Map compilerOptions = getCompilerOptions(); |
6482 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
6483 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
6484 |
this.runNegativeTest( |
6485 |
new String[] { |
6486 |
"X.java", |
6487 |
"public class X<T> {\n" + |
6488 |
" public static int field1;\n" + |
6489 |
" public int field2;\n" + |
6490 |
" public void bar(T t) {\n" + // don't warn |
6491 |
" X.field1 = 1;\n" + |
6492 |
" System.out.println(t);\n" + |
6493 |
" }\n" + |
6494 |
" public <E> void bar2(E e) {\n" + // warn |
6495 |
" X.field1 = 1;\n" + |
6496 |
" System.out.println(e);\n" + |
6497 |
" }\n" + |
6498 |
" public <E> void bar3() {\n" + // don't warn |
6499 |
" T a;\n" + |
6500 |
" System.out.println();\n" + |
6501 |
" }\n" + |
6502 |
"}" |
6503 |
}, |
6504 |
"----------\n" + |
6505 |
"1. ERROR in X.java (at line 8)\n" + |
6506 |
" public <E> void bar2(E e) {\n" + |
6507 |
" ^^^^^^^^^\n" + |
6508 |
"The method bar2(E) from the type X<T> can potentially be declared as static\n" + |
6509 |
"----------\n", |
6510 |
null /* no extra class libraries */, |
6511 |
true /* flush output directory */, |
6512 |
compilerOptions /* custom options */ |
6513 |
); |
6514 |
} |
6515 |
|
6516 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
6517 |
// Access to super in a method disqualifies it from being static |
6518 |
public void test121() { |
6519 |
Map compilerOptions = getCompilerOptions(); |
6520 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
6521 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
6522 |
compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); |
6523 |
this.runNegativeTest( |
6524 |
new String[] { |
6525 |
"X.java", |
6526 |
"public class X extends A{\n" + |
6527 |
" public static int field1;\n" + |
6528 |
" public int field2;\n" + |
6529 |
" public void methodA() {\n" + // don't warn |
6530 |
" super.methodA();\n" + |
6531 |
" }\n" + |
6532 |
" public void bar() {\n" + // don't warn |
6533 |
" super.fieldA = 1;\n" + |
6534 |
" }\n" + |
6535 |
" public void bar2() {\n" + // don't warn |
6536 |
" System.out.println(super.fieldA);\n" + |
6537 |
" }\n" + |
6538 |
" public void bar3() {\n" + // warn |
6539 |
" System.out.println(X.fieldA);\n" + |
6540 |
" }\n" + |
6541 |
"}\n" + |
6542 |
"class A{\n" + |
6543 |
" public static int fieldA;\n" + |
6544 |
" public void methodA(){\n" + |
6545 |
" }\n" + |
6546 |
"}" |
6547 |
}, |
6548 |
"----------\n" + |
6549 |
"1. ERROR in X.java (at line 13)\n" + |
6550 |
" public void bar3() {\n" + |
6551 |
" ^^^^^^\n" + |
6552 |
"The method bar3() from the type X can potentially be declared as static\n" + |
6553 |
"----------\n", |
6554 |
null /* no extra class libraries */, |
6555 |
true /* flush output directory */, |
6556 |
compilerOptions /* custom options */ |
6557 |
); |
6558 |
} |
6559 |
|
6560 |
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 |
6561 |
// Methods of non-static member types can't be static |
6562 |
public void test122() { |
6563 |
Map compilerOptions = getCompilerOptions(); |
6564 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); |
6565 |
compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); |
6566 |
compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE); |
6567 |
this.runNegativeTest( |
6568 |
new String[] { |
6569 |
"X.java", |
6570 |
"public class X {\n" + |
6571 |
" class A{\n" + |
6572 |
" void methodA() {\n" + // don't warn |
6573 |
" System.out.println();\n" + |
6574 |
" }\n" + |
6575 |
" }\n" + |
6576 |
" static class B{\n" + |
6577 |
" void methodB() {\n" + // warn |
6578 |
" System.out.println();\n" + |
6579 |
" }\n" + |
6580 |
" }\n" + |
6581 |
"}" |
6582 |
}, |
6583 |
"----------\n" + |
6584 |
"1. ERROR in X.java (at line 8)\n" + |
6585 |
" void methodB() {\n" + |
6586 |
" ^^^^^^^^^\n" + |
6587 |
"The method methodB() from the type X.B can potentially be declared as static\n" + |
6588 |
"----------\n", |
6589 |
null /* no extra class libraries */, |
6590 |
true /* flush output directory */, |
6591 |
compilerOptions /* custom options */ |
6592 |
); |
6593 |
} |
5895 |
} |
6594 |
} |