View | Details | Raw Unified | Return to bug 318682 | Differences between
and this patch

Collapse All | Expand All

(-)batch/org/eclipse/jdt/internal/compiler/batch/Main.java (+3 lines)
Lines 3461-3466 Link Here
3461
			} else if (token.equals("super")) { //$NON-NLS-1$
3461
			} else if (token.equals("super")) { //$NON-NLS-1$
3462
				setSeverity(CompilerOptions.OPTION_ReportOverridingMethodWithoutSuperInvocation, severity, isEnabling);
3462
				setSeverity(CompilerOptions.OPTION_ReportOverridingMethodWithoutSuperInvocation, severity, isEnabling);
3463
				return;
3463
				return;
3464
			} else if (token.equals("static-method")) { //$NON-NLS-1$
3465
				setSeverity(CompilerOptions.OPTION_ReportMethodCanBeStatic, severity, isEnabling);
3466
				return;
3464
			}
3467
			}
3465
			break;
3468
			break;
3466
		case 't' :
3469
		case 't' :
(-)batch/org/eclipse/jdt/internal/compiler/batch/messages.properties (+1 lines)
Lines 309-314 Link Here
309
\      semicolon            unnecessary semicolon, empty statement\n\
309
\      semicolon            unnecessary semicolon, empty statement\n\
310
\      serial             + missing serialVersionUID\n\
310
\      serial             + missing serialVersionUID\n\
311
\      specialParamHiding   constructor or setter parameter hiding a field\n\
311
\      specialParamHiding   constructor or setter parameter hiding a field\n\
312
\      static-method        method can be declared as static\n\
312
\      static-access        macro for indirectStatic and staticReceiver\n\
313
\      static-access        macro for indirectStatic and staticReceiver\n\
313
\      staticReceiver     + non-static reference to static member\n\
314
\      staticReceiver     + non-static reference to static member\n\
314
\      super                overriding a method without making a super invocation\n\
315
\      super                overriding a method without making a super invocation\n\
(-)compiler/org/eclipse/jdt/core/compiler/IProblem.java (+2 lines)
Lines 401-406 Link Here
401
	int IndirectAccessToStaticMethod = Internal + MethodRelated + 119;
401
	int IndirectAccessToStaticMethod = Internal + MethodRelated + 119;
402
	/** @since 3.4 */
402
	/** @since 3.4 */
403
	int MissingTypeInMethod = MethodRelated + 120;
403
	int MissingTypeInMethod = MethodRelated + 120;
404
	/** @since 3.7 */
405
	int MethodCanBeStatic = Internal + MethodRelated + 121;
404
406
405
	// constructors
407
	// constructors
406
	/** @since 3.4 */
408
	/** @since 3.4 */
(-)compiler/org/eclipse/jdt/internal/compiler/ast/ASTNode.java (+1 lines)
Lines 146-151 Link Here
146
	// for type, method and field declarations
146
	// for type, method and field declarations
147
	public static final int HasLocalType = Bit2; // cannot conflict with AddAssertionMASK
147
	public static final int HasLocalType = Bit2; // cannot conflict with AddAssertionMASK
148
	public static final int HasBeenResolved = Bit5; // field decl only (to handle forward references)
148
	public static final int HasBeenResolved = Bit5; // field decl only (to handle forward references)
149
	public static final int CanBeStatic = Bit9;
149
150
150
	// for expression
151
	// for expression
151
	public static final int ParenthesizedSHIFT = 21; // Bit22 -> Bit29
152
	public static final int ParenthesizedSHIFT = 21; // Bit22 -> Bit29
(-)compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java (+10 lines)
Lines 61-66 Link Here
61
	flowInfo = this.receiver.analyseCode(currentScope, flowContext, flowInfo, nonStatic).unconditionalInits();
61
	flowInfo = this.receiver.analyseCode(currentScope, flowContext, flowInfo, nonStatic).unconditionalInits();
62
	if (nonStatic) {
62
	if (nonStatic) {
63
		this.receiver.checkNPE(currentScope, flowContext, flowInfo);
63
		this.receiver.checkNPE(currentScope, flowContext, flowInfo);
64
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
65
		if (this.receiver.isThis()) {
66
			// accessing non-static method without an object
67
			currentScope.resetEnclosingMethodStaticFlag();
68
		}
69
	} else if (this.receiver.isThis()) {
70
		if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
71
			// explicit this receiver, not allowed in static context
72
			currentScope.resetEnclosingMethodStaticFlag();
73
		}
64
	}
74
	}
65
75
66
	if (this.arguments != null) {
76
	if (this.arguments != null) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java (-1 / +7 lines)
Lines 80-85 Link Here
80
					currentScope.problemReporter().uninitializedBlankFinalField(lastFieldBinding, this);
80
					currentScope.problemReporter().uninitializedBlankFinalField(lastFieldBinding, this);
81
				}
81
				}
82
			}
82
			}
83
			if (!lastFieldBinding.isStatic()) {
84
				currentScope.resetEnclosingMethodStaticFlag();
85
			}
83
			break;
86
			break;
84
		case Binding.LOCAL :
87
		case Binding.LOCAL :
85
			// first binding is a local variable
88
			// first binding is a local variable
Lines 175-182 Link Here
175
			if (needValue || complyTo14) {
178
			if (needValue || complyTo14) {
176
				manageSyntheticAccessIfNecessary(currentScope, (FieldBinding) this.binding, 0, flowInfo);
179
				manageSyntheticAccessIfNecessary(currentScope, (FieldBinding) this.binding, 0, flowInfo);
177
			}
180
			}
181
			FieldBinding fieldBinding = (FieldBinding) this.binding;
178
			if (this.indexOfFirstFieldBinding == 1) { // was an implicit reference to the first field binding
182
			if (this.indexOfFirstFieldBinding == 1) { // was an implicit reference to the first field binding
179
				FieldBinding fieldBinding = (FieldBinding) this.binding;
180
				// check if reading a final blank field
183
				// check if reading a final blank field
181
				if (fieldBinding.isBlankFinal()
184
				if (fieldBinding.isBlankFinal()
182
						&& currentScope.needBlankFinalFieldInitializationCheck(fieldBinding)) {
185
						&& currentScope.needBlankFinalFieldInitializationCheck(fieldBinding)) {
Lines 186-191 Link Here
186
					}
189
					}
187
				}
190
				}
188
			}
191
			}
192
			if (!fieldBinding.isStatic()) {
193
				currentScope.resetEnclosingMethodStaticFlag();
194
			}
189
			break;
195
			break;
190
		case Binding.LOCAL : // reading a local variable
196
		case Binding.LOCAL : // reading a local variable
191
			LocalVariableBinding localBinding;
197
			LocalVariableBinding localBinding;
(-)compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java (+13 lines)
Lines 133-138 Link Here
133
	public static final String OPTION_ReportTasks = "org.eclipse.jdt.core.compiler.problem.tasks"; //$NON-NLS-1$
133
	public static final String OPTION_ReportTasks = "org.eclipse.jdt.core.compiler.problem.tasks"; //$NON-NLS-1$
134
	public static final String OPTION_ReportUnusedObjectAllocation = "org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation";  //$NON-NLS-1$
134
	public static final String OPTION_ReportUnusedObjectAllocation = "org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation";  //$NON-NLS-1$
135
	public static final String OPTION_IncludeNullInfoFromAsserts = "org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts";  //$NON-NLS-1$
135
	public static final String OPTION_IncludeNullInfoFromAsserts = "org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts";  //$NON-NLS-1$
136
	public static final String OPTION_ReportMethodCanBeStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic";  //$NON-NLS-1$
136
	// Backward compatibility
137
	// Backward compatibility
137
	public static final String OPTION_ReportInvalidAnnotation = "org.eclipse.jdt.core.compiler.problem.invalidAnnotation"; //$NON-NLS-1$
138
	public static final String OPTION_ReportInvalidAnnotation = "org.eclipse.jdt.core.compiler.problem.invalidAnnotation"; //$NON-NLS-1$
138
	public static final String OPTION_ReportMissingAnnotation = "org.eclipse.jdt.core.compiler.problem.missingAnnotation"; //$NON-NLS-1$
139
	public static final String OPTION_ReportMissingAnnotation = "org.eclipse.jdt.core.compiler.problem.missingAnnotation"; //$NON-NLS-1$
Lines 238-243 Link Here
238
	public static final int DeadCode = IrritantSet.GROUP2 | ASTNode.Bit2;
239
	public static final int DeadCode = IrritantSet.GROUP2 | ASTNode.Bit2;
239
	public static final int Tasks = IrritantSet.GROUP2 | ASTNode.Bit3;
240
	public static final int Tasks = IrritantSet.GROUP2 | ASTNode.Bit3;
240
	public static final int UnusedObjectAllocation = IrritantSet.GROUP2 | ASTNode.Bit4;
241
	public static final int UnusedObjectAllocation = IrritantSet.GROUP2 | ASTNode.Bit4;
242
	public static final int MethodCanBeStatic = IrritantSet.GROUP2 | ASTNode.Bit5;
241
243
242
	// Severity level for handlers
244
	// Severity level for handlers
243
	/** 
245
	/** 
Lines 375-380 Link Here
375
		"rawtypes", //$NON-NLS-1$
377
		"rawtypes", //$NON-NLS-1$
376
		"serial", //$NON-NLS-1$
378
		"serial", //$NON-NLS-1$
377
		"static-access", //$NON-NLS-1$
379
		"static-access", //$NON-NLS-1$
380
		"static-method", //$NON-NLS-1$
378
		"super", //$NON-NLS-1$
381
		"super", //$NON-NLS-1$
379
		"synthetic-access", //$NON-NLS-1$
382
		"synthetic-access", //$NON-NLS-1$
380
		"unchecked", //$NON-NLS-1$
383
		"unchecked", //$NON-NLS-1$
Lines 539-544 Link Here
539
				return OPTION_ReportDeadCode;
542
				return OPTION_ReportDeadCode;
540
			case UnusedObjectAllocation:
543
			case UnusedObjectAllocation:
541
				return OPTION_ReportUnusedObjectAllocation;
544
				return OPTION_ReportUnusedObjectAllocation;
545
			case MethodCanBeStatic :
546
				return OPTION_ReportMethodCanBeStatic;
542
		}
547
		}
543
		return null;
548
		return null;
544
	}
549
	}
Lines 638-643 Link Here
638
			OPTION_ReportInvalidJavadoc,
643
			OPTION_ReportInvalidJavadoc,
639
			OPTION_ReportLocalVariableHiding,
644
			OPTION_ReportLocalVariableHiding,
640
			OPTION_ReportMethodWithConstructorName,
645
			OPTION_ReportMethodWithConstructorName,
646
			OPTION_ReportMethodCanBeStatic,
641
			OPTION_ReportMissingDeprecatedAnnotation,
647
			OPTION_ReportMissingDeprecatedAnnotation,
642
			OPTION_ReportMissingJavadocComments,
648
			OPTION_ReportMissingJavadocComments,
643
			OPTION_ReportMissingJavadocTagDescription,
649
			OPTION_ReportMissingJavadocTagDescription,
Lines 739-744 Link Here
739
				return "fallthrough"; //$NON-NLS-1$
745
				return "fallthrough"; //$NON-NLS-1$
740
			case OverridingMethodWithoutSuperInvocation :
746
			case OverridingMethodWithoutSuperInvocation :
741
				return "super"; //$NON-NLS-1$
747
				return "super"; //$NON-NLS-1$
748
			case MethodCanBeStatic :
749
				return "static-method"; //$NON-NLS-1$
742
		}
750
		}
743
		return null;
751
		return null;
744
	}
752
	}
Lines 796-801 Link Here
796
					return IrritantSet.SERIAL;
804
					return IrritantSet.SERIAL;
797
				if ("static-access".equals(warningToken)) //$NON-NLS-1$
805
				if ("static-access".equals(warningToken)) //$NON-NLS-1$
798
					return IrritantSet.STATIC_ACCESS;
806
					return IrritantSet.STATIC_ACCESS;
807
				if ("static-method".equals(warningToken)) //$NON-NLS-1$
808
					return IrritantSet.STATIC_METHOD;
799
				if ("synthetic-access".equals(warningToken)) //$NON-NLS-1$
809
				if ("synthetic-access".equals(warningToken)) //$NON-NLS-1$
800
					return IrritantSet.SYNTHETIC_ACCESS;
810
					return IrritantSet.SYNTHETIC_ACCESS;
801
				if ("super".equals(warningToken)) { //$NON-NLS-1$
811
				if ("super".equals(warningToken)) { //$NON-NLS-1$
Lines 921-926 Link Here
921
		optionsMap.put(OPTION_ReportTasks, getSeverityString(Tasks));
931
		optionsMap.put(OPTION_ReportTasks, getSeverityString(Tasks));
922
		optionsMap.put(OPTION_ReportUnusedObjectAllocation, getSeverityString(UnusedObjectAllocation));
932
		optionsMap.put(OPTION_ReportUnusedObjectAllocation, getSeverityString(UnusedObjectAllocation));
923
		optionsMap.put(OPTION_IncludeNullInfoFromAsserts, this.includeNullInfoFromAsserts ? ENABLED : DISABLED);
933
		optionsMap.put(OPTION_IncludeNullInfoFromAsserts, this.includeNullInfoFromAsserts ? ENABLED : DISABLED);
934
		optionsMap.put(OPTION_ReportMethodCanBeStatic, getSeverityString(MethodCanBeStatic));
924
		return optionsMap;
935
		return optionsMap;
925
	}
936
	}
926
937
Lines 1339-1344 Link Here
1339
		if ((optionValue = optionsMap.get(OPTION_ReportDeadCode)) != null) updateSeverity(DeadCode, optionValue);
1350
		if ((optionValue = optionsMap.get(OPTION_ReportDeadCode)) != null) updateSeverity(DeadCode, optionValue);
1340
		if ((optionValue = optionsMap.get(OPTION_ReportTasks)) != null) updateSeverity(Tasks, optionValue);
1351
		if ((optionValue = optionsMap.get(OPTION_ReportTasks)) != null) updateSeverity(Tasks, optionValue);
1341
		if ((optionValue = optionsMap.get(OPTION_ReportUnusedObjectAllocation)) != null) updateSeverity(UnusedObjectAllocation, optionValue);
1352
		if ((optionValue = optionsMap.get(OPTION_ReportUnusedObjectAllocation)) != null) updateSeverity(UnusedObjectAllocation, optionValue);
1353
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBeStatic)) != null) updateSeverity(MethodCanBeStatic, optionValue);
1342
1354
1343
		// Javadoc options
1355
		// Javadoc options
1344
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
1356
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
Lines 1551-1556 Link Here
1551
		buf.append("\n\t- dead code in trivial if statement: ").append(this.reportDeadCodeInTrivialIfStatement ? ENABLED : DISABLED); //$NON-NLS-1$
1563
		buf.append("\n\t- dead code in trivial if statement: ").append(this.reportDeadCodeInTrivialIfStatement ? ENABLED : DISABLED); //$NON-NLS-1$
1552
		buf.append("\n\t- tasks severity: ").append(getSeverityString(Tasks)); //$NON-NLS-1$
1564
		buf.append("\n\t- tasks severity: ").append(getSeverityString(Tasks)); //$NON-NLS-1$
1553
		buf.append("\n\t- unused object allocation: ").append(getSeverityString(UnusedObjectAllocation)); //$NON-NLS-1$
1565
		buf.append("\n\t- unused object allocation: ").append(getSeverityString(UnusedObjectAllocation)); //$NON-NLS-1$
1566
		buf.append("\n\t- method can be static: ").append(getSeverityString(MethodCanBeStatic)); //$NON-NLS-1$
1554
		return buf.toString();
1567
		return buf.toString();
1555
	}
1568
	}
1556
	
1569
	
(-)compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java (-1 / +5 lines)
Lines 51-56 Link Here
51
	public static final IrritantSet RESTRICTION = new IrritantSet(CompilerOptions.ForbiddenReference);
51
	public static final IrritantSet RESTRICTION = new IrritantSet(CompilerOptions.ForbiddenReference);
52
	public static final IrritantSet SERIAL = new IrritantSet(CompilerOptions.MissingSerialVersion);
52
	public static final IrritantSet SERIAL = new IrritantSet(CompilerOptions.MissingSerialVersion);
53
	public static final IrritantSet STATIC_ACCESS = new IrritantSet(CompilerOptions.IndirectStaticAccess);
53
	public static final IrritantSet STATIC_ACCESS = new IrritantSet(CompilerOptions.IndirectStaticAccess);
54
	public static final IrritantSet STATIC_METHOD = new IrritantSet(CompilerOptions.MethodCanBeStatic);
54
	public static final IrritantSet SYNTHETIC_ACCESS = new IrritantSet(CompilerOptions.AccessEmulation);
55
	public static final IrritantSet SYNTHETIC_ACCESS = new IrritantSet(CompilerOptions.AccessEmulation);
55
	public static final IrritantSet SUPER = new IrritantSet(CompilerOptions.OverridingMethodWithoutSuperInvocation);
56
	public static final IrritantSet SUPER = new IrritantSet(CompilerOptions.OverridingMethodWithoutSuperInvocation);
56
	public static final IrritantSet UNUSED = new IrritantSet(CompilerOptions.UnusedLocalVariable);
57
	public static final IrritantSet UNUSED = new IrritantSet(CompilerOptions.UnusedLocalVariable);
Lines 118-124 Link Here
118
			.set(CompilerOptions.UnusedTypeArguments)
119
			.set(CompilerOptions.UnusedTypeArguments)
119
			.set(CompilerOptions.RedundantSuperinterface)
120
			.set(CompilerOptions.RedundantSuperinterface)
120
			.set(CompilerOptions.DeadCode)
121
			.set(CompilerOptions.DeadCode)
121
			.set(CompilerOptions.UnusedObjectAllocation);
122
			.set(CompilerOptions.UnusedObjectAllocation)
123
			.set(CompilerOptions.MethodCanBeStatic);
124
		STATIC_METHOD
125
			.set(CompilerOptions.MethodCanBeStatic);
122
		String suppressRawWhenUnchecked = System.getProperty("suppressRawWhenUnchecked"); //$NON-NLS-1$
126
		String suppressRawWhenUnchecked = System.getProperty("suppressRawWhenUnchecked"); //$NON-NLS-1$
123
		if (suppressRawWhenUnchecked != null && "true".equalsIgnoreCase(suppressRawWhenUnchecked)) { //$NON-NLS-1$
127
		if (suppressRawWhenUnchecked != null && "true".equalsIgnoreCase(suppressRawWhenUnchecked)) { //$NON-NLS-1$
124
			UNCHECKED.set(CompilerOptions.RawTypeReference);
128
			UNCHECKED.set(CompilerOptions.RawTypeReference);
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java (+8 lines)
Lines 940-943 Link Here
940
			s += ((BlockScope) this.subscopes[i]).toString(tab + 1) + "\n"; //$NON-NLS-1$
940
			s += ((BlockScope) this.subscopes[i]).toString(tab + 1) + "\n"; //$NON-NLS-1$
941
	return s;
941
	return s;
942
}
942
}
943
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
944
public void resetEnclosingMethodStaticFlag() {
945
	MethodScope methodScope = methodScope();
946
	if (methodScope != null && methodScope.referenceContext instanceof MethodDeclaration) {
947
		MethodDeclaration methodDeclaration= (MethodDeclaration) methodScope.referenceContext;
948
		methodDeclaration.bits &= ~ASTNode.CanBeStatic;
949
	}
950
}
943
}
951
}
(-)compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties (+1 lines)
Lines 102-107 Link Here
102
118 = The method {1}({2}) from the type {0} is never used locally
102
118 = The method {1}({2}) from the type {0} is never used locally
103
119 = The static method {1}({2}) from the type {0} should be accessed directly 
103
119 = The static method {1}({2}) from the type {0} should be accessed directly 
104
120 = The method {1}({2}) from the type {0} refers to the missing type {3}
104
120 = The method {1}({2}) from the type {0} refers to the missing type {3}
105
121 = The method {1}({2}) from the type {0} can be declared as static
105
106
106
129 = The constructor {0}({1}) refers to the missing type {2}
107
129 = The constructor {0}({1}) refers to the missing type {2}
107
130 = The constructor {0}({1}) is undefined
108
130 = The constructor {0}({1}) is undefined
(-)src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java (-1 / +540 lines)
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[] { "test118" };
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 5887-5890 Link Here
5887
		false,
5887
		false,
5888
		null);
5888
		null);
5889
}
5889
}
5890
5891
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
5892
// QualifiedNameReference, SingleNameReference and MessageSend
5893
// Can be static warning shown
5894
public void test114() {
5895
	Map compilerOptions = getCompilerOptions();
5896
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
5897
	this.runNegativeTest(
5898
		new String[] {
5899
			"X.java", 
5900
			"public class X {\n" +
5901
			"	public static int field1;\n" +
5902
			"	public static int field2;\n" + 
5903
			"	public void bar(int i) {\n" + 
5904
			"		System.out.println(foo());\n" +
5905
			"		foo();" +
5906
			"		System.out.println(X.field1);\n" +
5907
			"		System.out.println(field2);\n" +
5908
			"		field2 = 1;\n" +
5909
			"	}\n" + 
5910
			"	private static String foo() {\n" + 
5911
			"		return null;\n" + 
5912
			"	}\n" + 
5913
			"}"
5914
		},
5915
		"----------\n" + 
5916
		"1. ERROR in X.java (at line 4)\n" + 
5917
		"	public void bar(int i) {\n" + 
5918
		"	            ^^^^^^^^^^\n" + 
5919
		"The method bar(int) from the type X can be declared as static\n" + 
5920
		"----------\n",
5921
		null /* no extra class libraries */,
5922
		true /* flush output directory */,
5923
		compilerOptions /* custom options */
5924
	);
5925
}
5926
5927
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
5928
// FieldReference and MessageSend
5929
// Can be static warning shown
5930
public void test115() {
5931
	Map compilerOptions = getCompilerOptions();
5932
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
5933
	this.runNegativeTest(
5934
		new String[] {
5935
			"X.java",
5936
			"public class X extends B{\n" +
5937
			"	public static int field1;\n" +
5938
			"	public static int field2;\n" +
5939
			"	public void bar(int i) {\n" + 
5940
			"		System.out.println(foo());\n" +
5941
			"		X.field2 = 2;\n" +
5942
			"		System.out.println(field1);\n" +
5943
			"		A a = new A();\n" +
5944
			"		a.a1();\n" +
5945
			"	}\n" + 
5946
			"	private static String foo() {\n" + 
5947
			"		return null;\n" + 
5948
			"	}\n" + 
5949
			"}\n" +
5950
			"class A{\n" +
5951
			"	public void a1() {\n" +
5952
			"	}\n" +
5953
			"}\n" +
5954
			"class B{\n" +
5955
			"	public void b1(){\n" +
5956
			"	}\n" +
5957
			"}"
5958
		},
5959
		"----------\n" + 
5960
		"1. ERROR in X.java (at line 4)\n" + 
5961
		"	public void bar(int i) {\n" + 
5962
		"	            ^^^^^^^^^^\n" + 
5963
		"The method bar(int) from the type X can be declared as static\n" + 
5964
		"----------\n" + 
5965
		"2. ERROR in X.java (at line 16)\n" + 
5966
		"	public void a1() {\n" + 
5967
		"	            ^^^^\n" + 
5968
		"The method a1() from the type A can be declared as static\n" + 
5969
		"----------\n" + 
5970
		"3. ERROR in X.java (at line 20)\n" + 
5971
		"	public void b1(){\n" + 
5972
		"	            ^^^^\n" + 
5973
		"The method b1() from the type B can be declared as static\n" + 
5974
		"----------\n",
5975
		null /* no extra class libraries */,
5976
		true /* flush output directory */,
5977
		compilerOptions /* custom options */
5978
	);
5979
}
5980
5981
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
5982
// MessageSend in different ways
5983
public void test116a() {
5984
	Map compilerOptions = getCompilerOptions();
5985
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
5986
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
5987
	this.runNegativeTest(
5988
		new String[] {
5989
			"X.java",
5990
			"public class X extends B{\n" +
5991
			"	public static int field1;\n" +
5992
			"	public X xfield;\n" +
5993
			"	public void bar1(int i) {\n" + 
5994
			"		baz();\n" +
5995
			"	}\n" +
5996
			"	public void bar2(int i) {\n" + 
5997
			"		this.baz();\n" +
5998
			"	}\n" +
5999
			"	public void bar3(int i) {\n" + 
6000
			"		this.xfield.baz();\n" +
6001
			"	}\n" +
6002
			"	public void bar4(int i) {\n" + 
6003
			"		xfield.baz();\n" +
6004
			"	}\n" +
6005
			"	public void bar5(int i) {\n" + 
6006
			"		X x = new X();\n" +
6007
			"		x.baz();\n" +
6008
			"	}\n" +
6009
			"	public void bar6(int i) {\n" + 
6010
			"		A.xA.baz();\n" +
6011
			"	}\n" +
6012
			"	public void bar7(int i) {\n" + 
6013
			"		b1();\n" +
6014
			"	}\n" +
6015
			"	public void bar8(int i) {\n" + 
6016
			"		this.b1();\n" +
6017
			"	}\n" +
6018
			"	public void bar9(int i) {\n" + 
6019
			"		new X().b1();\n" +
6020
			"	}\n" +
6021
			"	public void baz() {\n" +
6022
			"	}\n" + 
6023
			"}\n" +
6024
			"class A{\n" +
6025
			"	public static X xA;\n" +
6026
			"}\n" +
6027
			"class B{\n" +
6028
			"	public void b1(){\n" +
6029
			"	}\n" +
6030
			"}",
6031
		},
6032
		"----------\n" + 
6033
		"1. ERROR in X.java (at line 16)\n" + 
6034
		"	public void bar5(int i) {\n" + 
6035
		"	            ^^^^^^^^^^^\n" + 
6036
		"The method bar5(int) from the type X can be declared as static\n" + 
6037
		"----------\n" + 
6038
		"2. ERROR in X.java (at line 20)\n" + 
6039
		"	public void bar6(int i) {\n" + 
6040
		"	            ^^^^^^^^^^^\n" + 
6041
		"The method bar6(int) from the type X can be declared as static\n" + 
6042
		"----------\n" + 
6043
		"3. ERROR in X.java (at line 29)\n" + 
6044
		"	public void bar9(int i) {\n" + 
6045
		"	            ^^^^^^^^^^^\n" + 
6046
		"The method bar9(int) from the type X can be declared as static\n" + 
6047
		"----------\n" + 
6048
		"4. ERROR in X.java (at line 32)\n" + 
6049
		"	public void baz() {\n" + 
6050
		"	            ^^^^^\n" + 
6051
		"The method baz() from the type X can be declared as static\n" + 
6052
		"----------\n" + 
6053
		"5. ERROR in X.java (at line 39)\n" + 
6054
		"	public void b1(){\n" + 
6055
		"	            ^^^^\n" + 
6056
		"The method b1() from the type B can be declared as static\n" + 
6057
		"----------\n",
6058
		null /* no extra class libraries */,
6059
		true /* flush output directory */,
6060
		compilerOptions /* custom options */
6061
	);
6062
}
6063
6064
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
6065
// MessageSend in different ways, referencing a static method.
6066
public void test116b() {
6067
	Map compilerOptions = getCompilerOptions();
6068
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6069
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6070
	this.runNegativeTest(
6071
		new String[] {
6072
			"X.java",
6073
			"public class X extends B{\n" +
6074
			"	public static int field1;\n" +
6075
			"	public static X xfield;\n" +
6076
			"	public void bar1(int i) {\n" + 
6077
			"		baz();\n" +
6078
			"	}\n" +
6079
			"	public void bar2(int i) {\n" + 
6080
			"		this.baz();\n" +
6081
			"	}\n" +
6082
			"	public void bar3(int i) {\n" + 
6083
			"		this.xfield.baz();\n" +
6084
			"	}\n" +
6085
			"	public void bar4(int i) {\n" + 
6086
			"		xfield.baz();\n" +
6087
			"	}\n" +
6088
			"	public void bar5(int i) {\n" + 
6089
			"		X x = new X();\n" +
6090
			"		x.baz();\n" +
6091
			"	}\n" +
6092
			"	public void bar6(int i) {\n" + 
6093
			"		A.xA.baz();\n" +
6094
			"	}\n" +
6095
			"	public void bar7(int i) {\n" + 
6096
			"		b1();\n" +
6097
			"	}\n" +
6098
			"	public void bar8(int i) {\n" + 
6099
			"		this.b1();\n" +
6100
			"	}\n" +
6101
			"	public void bar9(int i) {\n" + 
6102
			"		new X().b1();\n" +
6103
			"	}\n" +
6104
			"	public static void baz() {\n" +
6105
			"	}\n" + 
6106
			"}\n" +
6107
			"class A{\n" +
6108
			"	public static X xA;\n" +
6109
			"}\n" +
6110
			"class B{\n" +
6111
			"	public static void b1(){\n" +
6112
			"	}\n" +
6113
			"}",
6114
		},
6115
		"----------\n" + 
6116
		"1. ERROR in X.java (at line 4)\n" + 
6117
		"	public void bar1(int i) {\n" + 
6118
		"	            ^^^^^^^^^^^\n" + 
6119
		"The method bar1(int) from the type X can be declared as static\n" + 
6120
		"----------\n" + 
6121
		"2. ERROR in X.java (at line 13)\n" + 
6122
		"	public void bar4(int i) {\n" + 
6123
		"	            ^^^^^^^^^^^\n" + 
6124
		"The method bar4(int) from the type X can be declared as static\n" + 
6125
		"----------\n" + 
6126
		"3. ERROR in X.java (at line 16)\n" + 
6127
		"	public void bar5(int i) {\n" + 
6128
		"	            ^^^^^^^^^^^\n" + 
6129
		"The method bar5(int) from the type X can be declared as static\n" + 
6130
		"----------\n" + 
6131
		"4. ERROR in X.java (at line 20)\n" + 
6132
		"	public void bar6(int i) {\n" + 
6133
		"	            ^^^^^^^^^^^\n" + 
6134
		"The method bar6(int) from the type X can be declared as static\n" + 
6135
		"----------\n" + 
6136
		"5. ERROR in X.java (at line 23)\n" + 
6137
		"	public void bar7(int i) {\n" + 
6138
		"	            ^^^^^^^^^^^\n" + 
6139
		"The method bar7(int) from the type X can be declared as static\n" + 
6140
		"----------\n" + 
6141
		"6. ERROR in X.java (at line 29)\n" + 
6142
		"	public void bar9(int i) {\n" + 
6143
		"	            ^^^^^^^^^^^\n" + 
6144
		"The method bar9(int) from the type X can be declared as static\n" + 
6145
		"----------\n",
6146
		null /* no extra class libraries */,
6147
		true /* flush output directory */,
6148
		compilerOptions /* custom options */
6149
	);
6150
}
6151
6152
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
6153
// Referring a field in different ways, accessing non-static field.
6154
public void test117a() {
6155
	Map compilerOptions = getCompilerOptions();
6156
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6157
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6158
	this.runNegativeTest(
6159
		new String[] {
6160
			"X.java",
6161
			"public class X extends B{\n" +
6162
			"	public int field1;\n" +
6163
			"	public X xfield;\n" +
6164
			"	public void bar1(int i) {\n" + 
6165
			"		field1 = 1;\n" +
6166
			"	}\n" +
6167
			"	public void bar2(int i) {\n" + 
6168
			"		this.field1 = 1;\n" +
6169
			"	}\n" +
6170
			"	public void bar3(int i) {\n" + 
6171
			"		System.out.println(field1);\n" +
6172
			"	}\n" +
6173
			"	public void bar4(int i) {\n" + 
6174
			"		System.out.println(this.field1);\n" +
6175
			"	}\n" +
6176
			"	public void bar5(int i) {\n" + 
6177
			"		X x = new X();\n" +
6178
			"		x.field1 = 1;\n" +
6179
			"	}\n" +
6180
			"	public void bar6(int i) {\n" + 
6181
			"		A.xA.field1 = 1;\n" +
6182
			"	}\n" +
6183
			"	public void bar7(int i) {\n" + 
6184
			"		b1 = 1;\n" +
6185
			"	}\n" +
6186
			"	public void bar8(int i) {\n" + 
6187
			"		this.b1 = 1;\n" +
6188
			"	}\n" +
6189
			"	public void bar9(int i) {\n" + 
6190
			"		new X().b1 = 1;\n" +
6191
			"	}\n" +
6192
			"	public void bar10(int i) {\n" + 
6193
			"		this.xfield.field1 = 1;\n" +
6194
			"	}\n" +
6195
			"	public void bar11(int i) {\n" + 
6196
			"		System.out.println(this.xfield.field1);\n" +
6197
			"	}\n" +
6198
			"	public void bar12(int i) {\n" + 
6199
			"		System.out.println(new X().b1);\n" +
6200
			"	}\n" +
6201
			"	public void bar13(int i) {\n" + 
6202
			"		System.out.println(b1);\n" +
6203
			"	}\n" +
6204
			"	public void bar14(int i) {\n" + 
6205
			"		System.out.println(this.b1);\n" +
6206
			"	}\n" +
6207
			"	public void bar15(int i) {\n" + 
6208
			"		xfield.field1 = 1;\n" +
6209
			"	}\n" +
6210
			"	public void bar16(int i) {\n" + 
6211
			"		System.out.println(xfield.field1);\n" +
6212
			"	}\n" +
6213
			"	public void baz() {\n" +
6214
			"	}\n" + 
6215
			"}\n" +
6216
			"class A{\n" +
6217
			"	public static X xA;\n" +
6218
			"}\n" +
6219
			"class B{\n" +
6220
			"	public int b1;\n" +
6221
			"}",
6222
		},
6223
		"----------\n" + 
6224
		"1. ERROR in X.java (at line 16)\n" + 
6225
		"	public void bar5(int i) {\n" + 
6226
		"	            ^^^^^^^^^^^\n" + 
6227
		"The method bar5(int) from the type X can be declared as static\n" + 
6228
		"----------\n" + 
6229
		"2. ERROR in X.java (at line 20)\n" + 
6230
		"	public void bar6(int i) {\n" + 
6231
		"	            ^^^^^^^^^^^\n" + 
6232
		"The method bar6(int) from the type X can be declared as static\n" + 
6233
		"----------\n" + 
6234
		"3. ERROR in X.java (at line 29)\n" + 
6235
		"	public void bar9(int i) {\n" + 
6236
		"	            ^^^^^^^^^^^\n" + 
6237
		"The method bar9(int) from the type X can be declared as static\n" + 
6238
		"----------\n" + 
6239
		"4. ERROR in X.java (at line 38)\n" + 
6240
		"	public void bar12(int i) {\n" + 
6241
		"	            ^^^^^^^^^^^^\n" + 
6242
		"The method bar12(int) from the type X can be declared as static\n" + 
6243
		"----------\n" + 
6244
		"5. ERROR in X.java (at line 53)\n" + 
6245
		"	public void baz() {\n" + 
6246
		"	            ^^^^^\n" + 
6247
		"The method baz() from the type X can be declared as static\n" + 
6248
		"----------\n",
6249
		null /* no extra class libraries */,
6250
		true /* flush output directory */,
6251
		compilerOptions /* custom options */
6252
	);
6253
}
6254
6255
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
6256
// Referring a field in different ways, accessing non-static field.
6257
public void test117b() {
6258
	Map compilerOptions = getCompilerOptions();
6259
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6260
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6261
	this.runNegativeTest(
6262
		new String[] {
6263
			"X.java",
6264
			"public class X extends B{\n" +
6265
			"	public static int field1;\n" +
6266
			"	public static X xfield;\n" +
6267
			"	public void bar1(int i) {\n" + 
6268
			"		field1 = 1;\n" +
6269
			"	}\n" +
6270
			"	public void bar2(int i) {\n" + 
6271
			"		this.field1 = 1;\n" +
6272
			"	}\n" +
6273
			"	public void bar3(int i) {\n" + 
6274
			"		System.out.println(field1);\n" +
6275
			"	}\n" +
6276
			"	public void bar4(int i) {\n" + 
6277
			"		System.out.println(this.field1);\n" +
6278
			"	}\n" +
6279
			"	public void bar5(int i) {\n" + 
6280
			"		X x = new X();\n" +
6281
			"		x.field1 = 1;\n" +
6282
			"	}\n" +
6283
			"	public void bar6(int i) {\n" + 
6284
			"		A.xA.field1 = 1;\n" +
6285
			"	}\n" +
6286
			"	public void bar7(int i) {\n" + 
6287
			"		b1 = 1;\n" +
6288
			"	}\n" +
6289
			"	public void bar8(int i) {\n" + 
6290
			"		this.b1 = 1;\n" +
6291
			"	}\n" +
6292
			"	public void bar9(int i) {\n" + 
6293
			"		new X().b1 = 1;\n" +
6294
			"	}\n" +
6295
			"	public void bar10(int i) {\n" + 
6296
			"		this.xfield.field1 = 1;\n" +
6297
			"	}\n" +
6298
			"	public void bar11(int i) {\n" + 
6299
			"		System.out.println(this.xfield.field1);\n" +
6300
			"	}\n" +
6301
			"	public void bar12(int i) {\n" + 
6302
			"		System.out.println(new X().b1);\n" +
6303
			"	}\n" +
6304
			"	public void bar13(int i) {\n" + 
6305
			"		System.out.println(b1);\n" +
6306
			"	}\n" +
6307
			"	public void bar14(int i) {\n" + 
6308
			"		System.out.println(this.b1);\n" +
6309
			"	}\n" +
6310
			"	public void bar15(int i) {\n" + 
6311
			"		xfield.field1 = 1;\n" +
6312
			"	}\n" +
6313
			"	public void bar16(int i) {\n" + 
6314
			"		System.out.println(xfield.field1);\n" +
6315
			"	}\n" +
6316
			"	public void baz() {\n" +
6317
			"	}\n" + 
6318
			"}\n" +
6319
			"class A{\n" +
6320
			"	public static X xA;\n" +
6321
			"}\n" +
6322
			"class B{\n" +
6323
			"	public static int b1;\n" +
6324
			"}",
6325
		},
6326
		"----------\n" + 
6327
		"1. ERROR in X.java (at line 4)\n" + 
6328
		"	public void bar1(int i) {\n" + 
6329
		"	            ^^^^^^^^^^^\n" + 
6330
		"The method bar1(int) from the type X can be declared as static\n" + 
6331
		"----------\n" + 
6332
		"2. ERROR in X.java (at line 10)\n" + 
6333
		"	public void bar3(int i) {\n" + 
6334
		"	            ^^^^^^^^^^^\n" + 
6335
		"The method bar3(int) from the type X can be declared as static\n" + 
6336
		"----------\n" + 
6337
		"3. ERROR in X.java (at line 16)\n" + 
6338
		"	public void bar5(int i) {\n" + 
6339
		"	            ^^^^^^^^^^^\n" + 
6340
		"The method bar5(int) from the type X can be declared as static\n" + 
6341
		"----------\n" + 
6342
		"4. ERROR in X.java (at line 20)\n" + 
6343
		"	public void bar6(int i) {\n" + 
6344
		"	            ^^^^^^^^^^^\n" + 
6345
		"The method bar6(int) from the type X can be declared as static\n" + 
6346
		"----------\n" + 
6347
		"5. ERROR in X.java (at line 23)\n" + 
6348
		"	public void bar7(int i) {\n" + 
6349
		"	            ^^^^^^^^^^^\n" + 
6350
		"The method bar7(int) from the type X can be declared as static\n" + 
6351
		"----------\n" + 
6352
		"6. ERROR in X.java (at line 29)\n" + 
6353
		"	public void bar9(int i) {\n" + 
6354
		"	            ^^^^^^^^^^^\n" + 
6355
		"The method bar9(int) from the type X can be declared as static\n" + 
6356
		"----------\n" + 
6357
		"7. ERROR in X.java (at line 38)\n" + 
6358
		"	public void bar12(int i) {\n" + 
6359
		"	            ^^^^^^^^^^^^\n" + 
6360
		"The method bar12(int) from the type X can be declared as static\n" + 
6361
		"----------\n" + 
6362
		"8. ERROR in X.java (at line 41)\n" + 
6363
		"	public void bar13(int i) {\n" + 
6364
		"	            ^^^^^^^^^^^^\n" + 
6365
		"The method bar13(int) from the type X can be declared as static\n" + 
6366
		"----------\n" + 
6367
		"9. ERROR in X.java (at line 47)\n" + 
6368
		"	public void bar15(int i) {\n" + 
6369
		"	            ^^^^^^^^^^^^\n" + 
6370
		"The method bar15(int) from the type X can be declared as static\n" + 
6371
		"----------\n" + 
6372
		"10. ERROR in X.java (at line 50)\n" + 
6373
		"	public void bar16(int i) {\n" + 
6374
		"	            ^^^^^^^^^^^^\n" + 
6375
		"The method bar16(int) from the type X can be declared as static\n" + 
6376
		"----------\n" + 
6377
		"11. ERROR in X.java (at line 53)\n" + 
6378
		"	public void baz() {\n" + 
6379
		"	            ^^^^^\n" + 
6380
		"The method baz() from the type X can be declared as static\n" + 
6381
		"----------\n",
6382
		null /* no extra class libraries */,
6383
		true /* flush output directory */,
6384
		compilerOptions /* custom options */
6385
	);
6386
}
6387
6388
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
6389
// Verify that can be static is not raised on overriden or static methods.
6390
public void test118() {
6391
	Map compilerOptions = getCompilerOptions();
6392
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6393
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6394
	this.runNegativeTest(
6395
		new String[] {
6396
			"X.java",
6397
			"public class X extends B implements A{\n" +
6398
			"	public static int field1 = 1;\n" +
6399
			"	public static void methodX(){\n" +
6400
			"	}\n" +
6401
			"	public void methodB() {\n" +	// overriding method, dont warn
6402
			"		System.out.println(X.field1);\n" +
6403
			"		X.methodX();\n" +
6404
			"	}\n" +
6405
			"	public void methodA() {\n" +	// implementing method, dont warn
6406
			"		System.out.println();\n" +
6407
			"	}\n" +
6408
			"}\n" +
6409
			"interface A{\n" +
6410
			"	public abstract void methodA();\n" +
6411
			"}\n" +
6412
			"class B{\n" +
6413
			"	public void methodB() {\n" +
6414
			"		System.out.println();\n" +
6415
			"	}\n" +
6416
			"}",
6417
		},
6418
		"----------\n" + 
6419
		"1. ERROR in X.java (at line 17)\n" + 
6420
		"	public void methodB() {\n" + 
6421
		"	            ^^^^^^^^^\n" + 
6422
		"The method methodB() from the type B can be declared as static\n" + 
6423
		"----------\n",
6424
		null /* no extra class libraries */,
6425
		true /* flush output directory */,
6426
		compilerOptions /* custom options */
6427
	);
6428
}
5890
}
6429
}
(-)guide/jdt_api_options.htm (+15 lines)
Lines 666-671 Link Here
666
</tr>
666
</tr>
667
667
668
<tr>
668
<tr>
669
<td colspan=2><b>Reporting that a method can be declared static</b> (<b><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#COMPILER_PB_METHOD_CAN_BE_STATIC">COMPILER_PB_METHOD_CAN_BE_STATIC</a></b>)</td>
670
</tr>
671
<tr valign="top">
672
<td rowspan=3>When enabled, the compiler will issue an error or a warning if a method has
673
not be declared as <code>static</code>, even though it qualifies as one.</td>
674
<td><b><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#ERROR">ERROR</a></b></td>
675
</tr>
676
<tr valign="top">
677
<td><b><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#WARNING">WARNING</a></b></td>
678
</tr>
679
<tr valign="top">
680
<td><b><i><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#IGNORE">IGNORE</a></i></b></td>
681
</tr>
682
683
<tr>
669
<td colspan=2><b>Reporting Method With Constructor Name</b> (<b><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#COMPILER_PB_METHOD_WITH_CONSTRUCTOR_NAME">COMPILER_PB_METHOD_WITH_CONSTRUCTOR_NAME</a></b>)</td>
684
<td colspan=2><b>Reporting Method With Constructor Name</b> (<b><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#COMPILER_PB_METHOD_WITH_CONSTRUCTOR_NAME">COMPILER_PB_METHOD_WITH_CONSTRUCTOR_NAME</a></b>)</td>
670
</tr>
685
</tr>
671
<tr valign="top">
686
<tr valign="top">

Return to bug 318682