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 (+7 lines)
Lines 3228-3233 Link Here
3228
					CompilerOptions.OPTION_ReportMissingOverrideAnnotationForInterfaceMethodImplementation,
3228
					CompilerOptions.OPTION_ReportMissingOverrideAnnotationForInterfaceMethodImplementation,
3229
					isEnabling ? CompilerOptions.ENABLED : CompilerOptions.DISABLED);
3229
					isEnabling ? CompilerOptions.ENABLED : CompilerOptions.DISABLED);
3230
				return;
3230
				return;
3231
			} else if (token.equals("all-static-method")) { //$NON-NLS-1$
3232
				setSeverity(CompilerOptions.OPTION_ReportMethodCanBeStatic, severity, isEnabling);
3233
				setSeverity(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, severity, isEnabling);
3234
				return;
3231
			}
3235
			}
3232
			break;
3236
			break;
3233
		case 'b' :
3237
		case 'b' :
Lines 3459-3464 Link Here
3459
			} else if (token.equals("super")) { //$NON-NLS-1$
3463
			} else if (token.equals("super")) { //$NON-NLS-1$
3460
				setSeverity(CompilerOptions.OPTION_ReportOverridingMethodWithoutSuperInvocation, severity, isEnabling);
3464
				setSeverity(CompilerOptions.OPTION_ReportOverridingMethodWithoutSuperInvocation, severity, isEnabling);
3461
				return;
3465
				return;
3466
			} else if (token.equals("static-method")) { //$NON-NLS-1$
3467
				setSeverity(CompilerOptions.OPTION_ReportMethodCanBeStatic, severity, isEnabling);
3468
				return;
3462
			}
3469
			}
3463
			break;
3470
			break;
3464
		case 't' :
3471
		case 't' :
(-)batch/org/eclipse/jdt/internal/compiler/batch/messages.properties (+2 lines)
Lines 267-272 Link Here
267
\      allDeprecation       deprecation including inside deprecated code\n\
267
\      allDeprecation       deprecation including inside deprecated code\n\
268
\      allJavadoc           invalid or missing javadoc\n\
268
\      allJavadoc           invalid or missing javadoc\n\
269
\      allOver-ann          all missing @Override annotations\n\
269
\      allOver-ann          all missing @Override annotations\n\
270
\      all-static-method    all method can be declared as static warnings\n\
270
\      assertIdentifier   + ''assert'' used as identifier\n\
271
\      assertIdentifier   + ''assert'' used as identifier\n\
271
\      boxing               autoboxing conversion\n\
272
\      boxing               autoboxing conversion\n\
272
\      charConcat         + char[] in String concat\n\
273
\      charConcat         + char[] in String concat\n\
Lines 309-314 Link Here
309
\      semicolon            unnecessary semicolon, empty statement\n\
310
\      semicolon            unnecessary semicolon, empty statement\n\
310
\      serial             + missing serialVersionUID\n\
311
\      serial             + missing serialVersionUID\n\
311
\      specialParamHiding   constructor or setter parameter hiding a field\n\
312
\      specialParamHiding   constructor or setter parameter hiding a field\n\
313
\      static-method        method can be declared as static\n\
312
\      static-access        macro for indirectStatic and staticReceiver\n\
314
\      static-access        macro for indirectStatic and staticReceiver\n\
313
\      staticReceiver     + non-static reference to static member\n\
315
\      staticReceiver     + non-static reference to static member\n\
314
\      super                overriding a method without making a super invocation\n\
316
\      super                overriding a method without making a super invocation\n\
(-)compiler/org/eclipse/jdt/core/compiler/IProblem.java (+4 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;
406
	/** @since 3.7 */
407
	int MethodCanBePotentiallyStatic = Internal + MethodRelated + 122;
404
408
405
	// constructors
409
	// constructors
406
	/** @since 3.4 */
410
	/** @since 3.4 */
(-)compiler/org/eclipse/jdt/internal/compiler/ast/ASTNode.java (-1 / +2 lines)
Lines 32-38 Link Here
32
	public final static int Bit6 = 0x20;					// depth (name ref, msg) | ignore need cast check (cast expression) | error in signature (method declaration/ initializer) | is recovered (annotation reference)
32
	public final static int Bit6 = 0x20;					// depth (name ref, msg) | ignore need cast check (cast expression) | error in signature (method declaration/ initializer) | is recovered (annotation reference)
33
	public final static int Bit7 = 0x40;					// depth (name ref, msg) | operator (operator) | need runtime checkcast (cast expression) | label used (labelStatement) | needFreeReturn (AbstractMethodDeclaration)
33
	public final static int Bit7 = 0x40;					// depth (name ref, msg) | operator (operator) | need runtime checkcast (cast expression) | label used (labelStatement) | needFreeReturn (AbstractMethodDeclaration)
34
	public final static int Bit8 = 0x80;					// depth (name ref, msg) | operator (operator) | unsafe cast (cast expression) | is default constructor (constructor declaration) | isElseStatementUnreachable (if statement)
34
	public final static int Bit8 = 0x80;					// depth (name ref, msg) | operator (operator) | unsafe cast (cast expression) | is default constructor (constructor declaration) | isElseStatementUnreachable (if statement)
35
	public final static int Bit9 = 0x100;				// depth (name ref, msg) | operator (operator) | is local type (type decl) | isThenStatementUnreachable (if statement)
35
	public final static int Bit9 = 0x100;				// depth (name ref, msg) | operator (operator) | is local type (type decl) | isThenStatementUnreachable (if statement) | can be static
36
	public final static int Bit10= 0x200;				// depth (name ref, msg) | operator (operator) | is anonymous type (type decl)
36
	public final static int Bit10= 0x200;				// depth (name ref, msg) | operator (operator) | is anonymous type (type decl)
37
	public final static int Bit11 = 0x400;				// depth (name ref, msg) | operator (operator) | is member type (type decl)
37
	public final static int Bit11 = 0x400;				// depth (name ref, msg) | operator (operator) | is member type (type decl)
38
	public final static int Bit12 = 0x800;				// depth (name ref, msg) | operator (operator) | has abstract methods (type decl)
38
	public final static int Bit12 = 0x800;				// depth (name ref, msg) | operator (operator) | has abstract methods (type decl)
Lines 169-174 Link Here
169
	// for block and method declaration
169
	// for block and method declaration
170
	public static final int UndocumentedEmptyBlock = Bit4;
170
	public static final int UndocumentedEmptyBlock = Bit4;
171
	public static final int OverridingMethodWithSupercall = Bit5;
171
	public static final int OverridingMethodWithSupercall = Bit5;
172
	public static final int CanBeStatic = Bit9;   // used to flag a method that can be declared static
172
173
173
	// for initializer and method declaration
174
	// for initializer and method declaration
174
	public static final int ErrorInSignature = Bit6;
175
	public static final int ErrorInSignature = Bit6;
(-)compiler/org/eclipse/jdt/internal/compiler/ast/FieldReference.java (+21 lines)
Lines 108-113 Link Here
108
			currentScope.problemReporter().cannotAssignToFinalField(this.binding, this);
108
			currentScope.problemReporter().cannotAssignToFinalField(this.binding, this);
109
		}
109
		}
110
	}
110
	}
111
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
112
	if (!this.binding.isStatic()) {
113
		if (this.receiver.isThis()) {
114
			currentScope.resetEnclosingMethodStaticFlag();
115
		}
116
	} else if (this.receiver.isThis()) {
117
		if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
118
			// explicit this, not allowed in static context
119
			currentScope.resetEnclosingMethodStaticFlag();
120
		}
121
	}
111
	return flowInfo;
122
	return flowInfo;
112
}
123
}
113
124
Lines 120-125 Link Here
120
	this.receiver.analyseCode(currentScope, flowContext, flowInfo, nonStatic);
131
	this.receiver.analyseCode(currentScope, flowContext, flowInfo, nonStatic);
121
	if (nonStatic) {
132
	if (nonStatic) {
122
		this.receiver.checkNPE(currentScope, flowContext, flowInfo);
133
		this.receiver.checkNPE(currentScope, flowContext, flowInfo);
134
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
135
		if (this.receiver.isThis()) {
136
			currentScope.resetEnclosingMethodStaticFlag();
137
		}
138
	} else if (this.receiver.isThis()) {
139
		if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
140
			// explicit this receiver, not allowed in static context
141
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
142
			currentScope.resetEnclosingMethodStaticFlag();
143
		}
123
	}
144
	}
124
145
125
	if (valueRequired || currentScope.compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4) {
146
	if (valueRequired || currentScope.compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java (+24 lines)
Lines 39-44 Link Here
39
	if ((flowInfo.tagBits & FlowInfo.UNREACHABLE) == 0) {
39
	if ((flowInfo.tagBits & FlowInfo.UNREACHABLE) == 0) {
40
		this.bits |= ASTNode.IsLocalDeclarationReachable; // only set if actually reached
40
		this.bits |= ASTNode.IsLocalDeclarationReachable; // only set if actually reached
41
	}
41
	}
42
	if (this.binding != null && this.type.resolvedType instanceof TypeVariableBinding) {
43
		MethodScope methodScope= this.binding.declaringScope.methodScope();
44
		AbstractMethodDeclaration methodDeclaration = methodScope.referenceMethod();
45
		if (methodDeclaration != null && ((methodDeclaration.bits & ASTNode.CanBeStatic) != 0) && methodDeclaration.binding != null) {
46
			TypeVariableBinding[] typeVariables = methodDeclaration.binding.typeVariables();
47
			if (typeVariables == Binding.NO_TYPE_VARIABLES) {
48
				// Method declares no type variables.
49
				currentScope.resetEnclosingMethodStaticFlag();
50
			} else {
51
				// to check whether the resolved type for this is declared by enclosing method as a type variable
52
				boolean usesEnclosingTypeVar = false; 
53
				for (int i = 0; i < typeVariables.length ; i ++) {
54
					if (typeVariables[i] == this.type.resolvedType){
55
						usesEnclosingTypeVar = true;
56
						break;
57
					}
58
				}
59
				if (!usesEnclosingTypeVar) {
60
					// uses a type variable not declared by enclosing method
61
					currentScope.resetEnclosingMethodStaticFlag();
62
				}
63
			}
64
		}
65
	}
42
	if (this.initialization == null) {
66
	if (this.initialization == null) {
43
		return flowInfo;
67
		return flowInfo;
44
	}
68
	}
(-)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/MethodDeclaration.java (-2 / +42 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 18-28 Link Here
18
import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
18
import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
19
import org.eclipse.jdt.internal.compiler.flow.InitializationFlowContext;
19
import org.eclipse.jdt.internal.compiler.flow.InitializationFlowContext;
20
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
20
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
21
import org.eclipse.jdt.internal.compiler.lookup.Binding;
21
import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
22
import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
22
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
23
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
24
import org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding;
25
import org.eclipse.jdt.internal.compiler.lookup.MemberTypeBinding;
23
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
26
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
24
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
27
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
25
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
28
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
29
import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
26
import org.eclipse.jdt.internal.compiler.parser.Parser;
30
import org.eclipse.jdt.internal.compiler.parser.Parser;
27
import org.eclipse.jdt.internal.compiler.problem.AbortMethod;
31
import org.eclipse.jdt.internal.compiler.problem.AbortMethod;
28
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
32
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
Lines 78-85 Link Here
78
			if (this.arguments != null) {
82
			if (this.arguments != null) {
79
				for (int i = 0, count = this.arguments.length; i < count; i++) {
83
				for (int i = 0, count = this.arguments.length; i < count; i++) {
80
					flowInfo.markAsDefinitelyAssigned(this.arguments[i].binding);
84
					flowInfo.markAsDefinitelyAssigned(this.arguments[i].binding);
85
					// if this method uses a type parameter declared by the declaring class,
86
					// it can't be static. https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
87
					if (this.arguments[i].binding != null && (this.arguments[i].binding.type instanceof TypeVariableBinding)) {
88
						Binding declaringElement = ((TypeVariableBinding)this.arguments[i].binding.type).declaringElement;
89
						if (this.binding != null && this.binding.declaringClass == declaringElement)
90
							this.bits &= ~ASTNode.CanBeStatic;
91
					}
81
				}
92
				}
82
			}
93
			}
94
			if (this.binding.declaringClass instanceof MemberTypeBinding && !this.binding.declaringClass.isStatic()) {
95
				// method of a non-static member type can't be static.
96
				this.bits &= ~ASTNode.CanBeStatic;
97
			}
83
			// propagate to statements
98
			// propagate to statements
84
			if (this.statements != null) {
99
			if (this.statements != null) {
85
				int complaintLevel = (flowInfo.reachMode() & FlowInfo.UNREACHABLE) == 0 ? Statement.NOT_COMPLAINED : Statement.COMPLAINED_FAKE_REACHABLE;
100
				int complaintLevel = (flowInfo.reachMode() & FlowInfo.UNREACHABLE) == 0 ? Statement.NOT_COMPLAINED : Statement.COMPLAINED_FAKE_REACHABLE;
Lines 89-94 Link Here
89
						flowInfo = stat.analyseCode(this.scope, methodContext, flowInfo);
104
						flowInfo = stat.analyseCode(this.scope, methodContext, flowInfo);
90
					}
105
					}
91
				}
106
				}
107
			} else {
108
				// method with empty body should not be flagged as static.
109
				this.bits &= ~ASTNode.CanBeStatic;
92
			}
110
			}
93
			// check for missing returning path
111
			// check for missing returning path
94
			TypeBinding returnTypeBinding = this.binding.returnType;
112
			TypeBinding returnTypeBinding = this.binding.returnType;
Lines 105-110 Link Here
105
			methodContext.complainIfUnusedExceptionHandlers(this);
123
			methodContext.complainIfUnusedExceptionHandlers(this);
106
			// check unused parameters
124
			// check unused parameters
107
			this.scope.checkUnusedParameters(this.binding);
125
			this.scope.checkUnusedParameters(this.binding);
126
			// check if the method could have been static
127
			if (!this.binding.isStatic() && (this.bits & ASTNode.CanBeStatic) != 0) {
128
				if(!this.binding.isOverriding() && !this.binding.isImplementing()) {
129
					if (this.binding.isPrivate() || this.binding.isFinal() || this.binding.declaringClass.isFinal()) {
130
						this.scope.problemReporter().methodCanBeDeclaredStatic(this);
131
					} else {
132
						this.scope.problemReporter().methodCanBePotentiallyDeclaredStatic(this);
133
					}
134
				}
135
					
136
			}
108
		} catch (AbortMethod e) {
137
		} catch (AbortMethod e) {
109
			this.ignoreFurtherInvestigation = true;
138
			this.ignoreFurtherInvestigation = true;
110
		}
139
		}
Lines 134-143 Link Here
134
		if (CharOperation.equals(this.scope.enclosingSourceType().sourceName, this.selector)) {
163
		if (CharOperation.equals(this.scope.enclosingSourceType().sourceName, this.selector)) {
135
			this.scope.problemReporter().methodWithConstructorName(this);
164
			this.scope.problemReporter().methodWithConstructorName(this);
136
		}
165
		}
137
166
		// to check whether the method returns a type parameter not declared by it.
167
		boolean returnsUndeclTypeVar = false;
168
		if (this.returnType != null && this.returnType.resolvedType instanceof TypeVariableBinding) {
169
			returnsUndeclTypeVar = true;
170
		}
138
		if (this.typeParameters != null) {
171
		if (this.typeParameters != null) {
139
			for (int i = 0, length = this.typeParameters.length; i < length; i++) {
172
			for (int i = 0, length = this.typeParameters.length; i < length; i++) {
140
				this.typeParameters[i].resolve(this.scope);
173
				this.typeParameters[i].resolve(this.scope);
174
				if (returnsUndeclTypeVar && this.typeParameters[i].binding == this.returnType.resolvedType) {
175
					returnsUndeclTypeVar = false;
176
				}
141
			}
177
			}
142
		}
178
		}
143
179
Lines 207-212 Link Here
207
					// the method HAS a body --> abstract native modifiers are forbiden
243
					// the method HAS a body --> abstract native modifiers are forbiden
208
					if (((this.modifiers & ClassFileConstants.AccNative) != 0) || ((this.modifiers & ClassFileConstants.AccAbstract) != 0))
244
					if (((this.modifiers & ClassFileConstants.AccNative) != 0) || ((this.modifiers & ClassFileConstants.AccAbstract) != 0))
209
						this.scope.problemReporter().methodNeedingNoBody(this);
245
						this.scope.problemReporter().methodNeedingNoBody(this);
246
					else if (this.binding != null && !this.binding.isStatic() && !(this.binding.declaringClass instanceof LocalTypeBinding) && !returnsUndeclTypeVar) {
247
						// Not a method of local type - can be static
248
						this.bits |= ASTNode.CanBeStatic;
249
					}
210
				}
250
				}
211
		}
251
		}
212
		super.resolveStatements();
252
		super.resolveStatements();
(-)compiler/org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java (-1 / +7 lines)
Lines 81-86 Link Here
81
					currentScope.problemReporter().uninitializedBlankFinalField(lastFieldBinding, this);
81
					currentScope.problemReporter().uninitializedBlankFinalField(lastFieldBinding, this);
82
				}
82
				}
83
			}
83
			}
84
			if (!lastFieldBinding.isStatic()) {
85
				currentScope.resetEnclosingMethodStaticFlag();
86
			}
84
			break;
87
			break;
85
		case Binding.LOCAL :
88
		case Binding.LOCAL :
86
			// first binding is a local variable
89
			// first binding is a local variable
Lines 176-183 Link Here
176
			if (needValue || complyTo14) {
179
			if (needValue || complyTo14) {
177
				manageSyntheticAccessIfNecessary(currentScope, (FieldBinding) this.binding, 0, flowInfo);
180
				manageSyntheticAccessIfNecessary(currentScope, (FieldBinding) this.binding, 0, flowInfo);
178
			}
181
			}
182
			FieldBinding fieldBinding = (FieldBinding) this.binding;
179
			if (this.indexOfFirstFieldBinding == 1) { // was an implicit reference to the first field binding
183
			if (this.indexOfFirstFieldBinding == 1) { // was an implicit reference to the first field binding
180
				FieldBinding fieldBinding = (FieldBinding) this.binding;
181
				// check if reading a final blank field
184
				// check if reading a final blank field
182
				if (fieldBinding.isBlankFinal()
185
				if (fieldBinding.isBlankFinal()
183
						&& currentScope.needBlankFinalFieldInitializationCheck(fieldBinding)) {
186
						&& currentScope.needBlankFinalFieldInitializationCheck(fieldBinding)) {
Lines 187-192 Link Here
187
					}
190
					}
188
				}
191
				}
189
			}
192
			}
193
			if (!fieldBinding.isStatic()) {
194
				currentScope.resetEnclosingMethodStaticFlag();
195
			}
190
			break;
196
			break;
191
		case Binding.LOCAL : // reading a local variable
197
		case Binding.LOCAL : // reading a local variable
192
			LocalVariableBinding localBinding;
198
			LocalVariableBinding localBinding;
(-)compiler/org/eclipse/jdt/internal/compiler/ast/SingleNameReference.java (-2 / +14 lines)
Lines 63-76 Link Here
63
	if (isCompound) { // check the variable part is initialized if blank final
63
	if (isCompound) { // check the variable part is initialized if blank final
64
		switch (this.bits & ASTNode.RestrictiveFlagMASK) {
64
		switch (this.bits & ASTNode.RestrictiveFlagMASK) {
65
			case Binding.FIELD : // reading a field
65
			case Binding.FIELD : // reading a field
66
				FieldBinding fieldBinding;
66
				FieldBinding fieldBinding = (FieldBinding) this.binding;
67
				if ((fieldBinding = (FieldBinding) this.binding).isBlankFinal()
67
				if (fieldBinding.isBlankFinal()
68
						&& currentScope.needBlankFinalFieldInitializationCheck(fieldBinding)) {
68
						&& currentScope.needBlankFinalFieldInitializationCheck(fieldBinding)) {
69
					FlowInfo fieldInits = flowContext.getInitsForFinalBlankInitializationCheck(fieldBinding.declaringClass.original(), flowInfo);
69
					FlowInfo fieldInits = flowContext.getInitsForFinalBlankInitializationCheck(fieldBinding.declaringClass.original(), flowInfo);
70
					if (!fieldInits.isDefinitelyAssigned(fieldBinding)) {
70
					if (!fieldInits.isDefinitelyAssigned(fieldBinding)) {
71
						currentScope.problemReporter().uninitializedBlankFinalField(fieldBinding, this);
71
						currentScope.problemReporter().uninitializedBlankFinalField(fieldBinding, this);
72
					}
72
					}
73
				}
73
				}
74
				if (!fieldBinding.isStatic()) {
75
					// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
76
					currentScope.resetEnclosingMethodStaticFlag();
77
				}
74
				manageSyntheticAccessIfNecessary(currentScope, flowInfo, true /*read-access*/);
78
				manageSyntheticAccessIfNecessary(currentScope, flowInfo, true /*read-access*/);
75
				break;
79
				break;
76
			case Binding.LOCAL : // reading a local variable
80
			case Binding.LOCAL : // reading a local variable
Lines 115-120 Link Here
115
					currentScope.problemReporter().cannotAssignToFinalField(fieldBinding, this);
119
					currentScope.problemReporter().cannotAssignToFinalField(fieldBinding, this);
116
				}
120
				}
117
			}
121
			}
122
			if (!fieldBinding.isStatic()) {
123
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
124
				currentScope.resetEnclosingMethodStaticFlag();
125
			}
118
			break;
126
			break;
119
		case Binding.LOCAL : // assigning to a local variable
127
		case Binding.LOCAL : // assigning to a local variable
120
			LocalVariableBinding localBinding = (LocalVariableBinding) this.binding;
128
			LocalVariableBinding localBinding = (LocalVariableBinding) this.binding;
Lines 164-169 Link Here
164
					currentScope.problemReporter().uninitializedBlankFinalField(fieldBinding, this);
172
					currentScope.problemReporter().uninitializedBlankFinalField(fieldBinding, this);
165
				}
173
				}
166
			}
174
			}
175
			if (!fieldBinding.isStatic()) {
176
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
177
				currentScope.resetEnclosingMethodStaticFlag();
178
			}
167
			break;
179
			break;
168
		case Binding.LOCAL : // reading a local variable
180
		case Binding.LOCAL : // reading a local variable
169
			LocalVariableBinding localBinding;
181
			LocalVariableBinding localBinding;
(-)compiler/org/eclipse/jdt/internal/compiler/ast/SuperReference.java (-1 / +8 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 11-16 Link Here
11
package org.eclipse.jdt.internal.compiler.ast;
11
package org.eclipse.jdt.internal.compiler.ast;
12
12
13
import org.eclipse.jdt.internal.compiler.ASTVisitor;
13
import org.eclipse.jdt.internal.compiler.ASTVisitor;
14
import org.eclipse.jdt.internal.compiler.flow.FlowContext;
15
import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
14
import org.eclipse.jdt.internal.compiler.impl.Constant;
16
import org.eclipse.jdt.internal.compiler.impl.Constant;
15
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
17
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
16
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
18
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
Lines 66-69 Link Here
66
		visitor.visit(this, blockScope);
68
		visitor.visit(this, blockScope);
67
		visitor.endVisit(this, blockScope);
69
		visitor.endVisit(this, blockScope);
68
	}
70
	}
71
	
72
	public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo, boolean valueRequired) {
73
		currentScope.resetEnclosingMethodStaticFlag();
74
		return analyseCode(currentScope, flowContext, flowInfo);
75
	}
69
}
76
}
(-)compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java (+22 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$
137
	public static final String OPTION_ReportMethodCanBePotentiallyStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic";  //$NON-NLS-1$
136
	// Backward compatibility
138
	// Backward compatibility
137
	public static final String OPTION_ReportInvalidAnnotation = "org.eclipse.jdt.core.compiler.problem.invalidAnnotation"; //$NON-NLS-1$
139
	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$
140
	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;
240
	public static final int DeadCode = IrritantSet.GROUP2 | ASTNode.Bit2;
239
	public static final int Tasks = IrritantSet.GROUP2 | ASTNode.Bit3;
241
	public static final int Tasks = IrritantSet.GROUP2 | ASTNode.Bit3;
240
	public static final int UnusedObjectAllocation = IrritantSet.GROUP2 | ASTNode.Bit4;
242
	public static final int UnusedObjectAllocation = IrritantSet.GROUP2 | ASTNode.Bit4;
243
	public static final int MethodCanBeStatic = IrritantSet.GROUP2 | ASTNode.Bit5;
244
	public static final int MethodCanBePotentiallyStatic = IrritantSet.GROUP2 | ASTNode.Bit6;
241
245
242
	// Severity level for handlers
246
	// Severity level for handlers
243
	/** 
247
	/** 
Lines 375-380 Link Here
375
		"rawtypes", //$NON-NLS-1$
379
		"rawtypes", //$NON-NLS-1$
376
		"serial", //$NON-NLS-1$
380
		"serial", //$NON-NLS-1$
377
		"static-access", //$NON-NLS-1$
381
		"static-access", //$NON-NLS-1$
382
		"static-method", //$NON-NLS-1$
378
		"super", //$NON-NLS-1$
383
		"super", //$NON-NLS-1$
379
		"synthetic-access", //$NON-NLS-1$
384
		"synthetic-access", //$NON-NLS-1$
380
		"unchecked", //$NON-NLS-1$
385
		"unchecked", //$NON-NLS-1$
Lines 539-544 Link Here
539
				return OPTION_ReportDeadCode;
544
				return OPTION_ReportDeadCode;
540
			case UnusedObjectAllocation:
545
			case UnusedObjectAllocation:
541
				return OPTION_ReportUnusedObjectAllocation;
546
				return OPTION_ReportUnusedObjectAllocation;
547
			case MethodCanBeStatic :
548
				return OPTION_ReportMethodCanBeStatic;
549
			case MethodCanBePotentiallyStatic :
550
				return OPTION_ReportMethodCanBePotentiallyStatic;
542
		}
551
		}
543
		return null;
552
		return null;
544
	}
553
	}
Lines 638-643 Link Here
638
			OPTION_ReportInvalidJavadoc,
647
			OPTION_ReportInvalidJavadoc,
639
			OPTION_ReportLocalVariableHiding,
648
			OPTION_ReportLocalVariableHiding,
640
			OPTION_ReportMethodWithConstructorName,
649
			OPTION_ReportMethodWithConstructorName,
650
			OPTION_ReportMethodCanBeStatic,
651
			OPTION_ReportMethodCanBePotentiallyStatic,
641
			OPTION_ReportMissingDeprecatedAnnotation,
652
			OPTION_ReportMissingDeprecatedAnnotation,
642
			OPTION_ReportMissingJavadocComments,
653
			OPTION_ReportMissingJavadocComments,
643
			OPTION_ReportMissingJavadocTagDescription,
654
			OPTION_ReportMissingJavadocTagDescription,
Lines 739-744 Link Here
739
				return "fallthrough"; //$NON-NLS-1$
750
				return "fallthrough"; //$NON-NLS-1$
740
			case OverridingMethodWithoutSuperInvocation :
751
			case OverridingMethodWithoutSuperInvocation :
741
				return "super"; //$NON-NLS-1$
752
				return "super"; //$NON-NLS-1$
753
			case MethodCanBeStatic :
754
			case MethodCanBePotentiallyStatic :
755
				return "static-method"; //$NON-NLS-1$
742
		}
756
		}
743
		return null;
757
		return null;
744
	}
758
	}
Lines 796-801 Link Here
796
					return IrritantSet.SERIAL;
810
					return IrritantSet.SERIAL;
797
				if ("static-access".equals(warningToken)) //$NON-NLS-1$
811
				if ("static-access".equals(warningToken)) //$NON-NLS-1$
798
					return IrritantSet.STATIC_ACCESS;
812
					return IrritantSet.STATIC_ACCESS;
813
				if ("static-method".equals(warningToken)) //$NON-NLS-1$
814
					return IrritantSet.STATIC_METHOD;
799
				if ("synthetic-access".equals(warningToken)) //$NON-NLS-1$
815
				if ("synthetic-access".equals(warningToken)) //$NON-NLS-1$
800
					return IrritantSet.SYNTHETIC_ACCESS;
816
					return IrritantSet.SYNTHETIC_ACCESS;
801
				if ("super".equals(warningToken)) { //$NON-NLS-1$
817
				if ("super".equals(warningToken)) { //$NON-NLS-1$
Lines 921-926 Link Here
921
		optionsMap.put(OPTION_ReportTasks, getSeverityString(Tasks));
937
		optionsMap.put(OPTION_ReportTasks, getSeverityString(Tasks));
922
		optionsMap.put(OPTION_ReportUnusedObjectAllocation, getSeverityString(UnusedObjectAllocation));
938
		optionsMap.put(OPTION_ReportUnusedObjectAllocation, getSeverityString(UnusedObjectAllocation));
923
		optionsMap.put(OPTION_IncludeNullInfoFromAsserts, this.includeNullInfoFromAsserts ? ENABLED : DISABLED);
939
		optionsMap.put(OPTION_IncludeNullInfoFromAsserts, this.includeNullInfoFromAsserts ? ENABLED : DISABLED);
940
		optionsMap.put(OPTION_ReportMethodCanBeStatic, getSeverityString(MethodCanBeStatic));
941
		optionsMap.put(OPTION_ReportMethodCanBePotentiallyStatic, getSeverityString(MethodCanBePotentiallyStatic));
924
		return optionsMap;
942
		return optionsMap;
925
	}
943
	}
926
944
Lines 1339-1344 Link Here
1339
		if ((optionValue = optionsMap.get(OPTION_ReportDeadCode)) != null) updateSeverity(DeadCode, optionValue);
1357
		if ((optionValue = optionsMap.get(OPTION_ReportDeadCode)) != null) updateSeverity(DeadCode, optionValue);
1340
		if ((optionValue = optionsMap.get(OPTION_ReportTasks)) != null) updateSeverity(Tasks, optionValue);
1358
		if ((optionValue = optionsMap.get(OPTION_ReportTasks)) != null) updateSeverity(Tasks, optionValue);
1341
		if ((optionValue = optionsMap.get(OPTION_ReportUnusedObjectAllocation)) != null) updateSeverity(UnusedObjectAllocation, optionValue);
1359
		if ((optionValue = optionsMap.get(OPTION_ReportUnusedObjectAllocation)) != null) updateSeverity(UnusedObjectAllocation, optionValue);
1360
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBeStatic)) != null) updateSeverity(MethodCanBeStatic, optionValue);
1361
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBePotentiallyStatic)) != null) updateSeverity(MethodCanBePotentiallyStatic, optionValue);
1342
1362
1343
		// Javadoc options
1363
		// Javadoc options
1344
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
1364
		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$
1571
		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$
1572
		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$
1573
		buf.append("\n\t- unused object allocation: ").append(getSeverityString(UnusedObjectAllocation)); //$NON-NLS-1$
1574
		buf.append("\n\t- method can be static: ").append(getSeverityString(MethodCanBeStatic)); //$NON-NLS-1$
1575
		buf.append("\n\t- method can be potentially static: ").append(getSeverityString(MethodCanBePotentiallyStatic)); //$NON-NLS-1$
1554
		return buf.toString();
1576
		return buf.toString();
1555
	}
1577
	}
1556
	
1578
	
(-)compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java (+3 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 119-124 Link Here
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
		STATIC_METHOD
124
		    .set(CompilerOptions.MethodCanBePotentiallyStatic);
122
		String suppressRawWhenUnchecked = System.getProperty("suppressRawWhenUnchecked"); //$NON-NLS-1$
125
		String suppressRawWhenUnchecked = System.getProperty("suppressRawWhenUnchecked"); //$NON-NLS-1$
123
		if (suppressRawWhenUnchecked != null && "true".equalsIgnoreCase(suppressRawWhenUnchecked)) { //$NON-NLS-1$
126
		if (suppressRawWhenUnchecked != null && "true".equalsIgnoreCase(suppressRawWhenUnchecked)) { //$NON-NLS-1$
124
			UNCHECKED.set(CompilerOptions.RawTypeReference);
127
			UNCHECKED.set(CompilerOptions.RawTypeReference);
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java (+14 lines)
Lines 941-944 Link Here
941
			s += ((BlockScope) this.subscopes[i]).toString(tab + 1) + "\n"; //$NON-NLS-1$
941
			s += ((BlockScope) this.subscopes[i]).toString(tab + 1) + "\n"; //$NON-NLS-1$
942
	return s;
942
	return s;
943
}
943
}
944
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
945
public void resetEnclosingMethodStaticFlag() {
946
	MethodScope methodScope = methodScope();
947
	while (methodScope != null && methodScope.referenceContext instanceof MethodDeclaration) {
948
		MethodDeclaration methodDeclaration= (MethodDeclaration) methodScope.referenceContext;
949
		methodDeclaration.bits &= ~ASTNode.CanBeStatic;
950
		ClassScope enclosingClassScope = methodScope.enclosingClassScope();
951
		if (enclosingClassScope != null) {
952
			methodScope = enclosingClassScope.methodScope();
953
		} else {
954
			break;
955
		}
956
	}
957
}
944
}
958
}
(-)compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java (+50 lines)
Lines 418-423 Link Here
418
418
419
		case IProblem.UnusedObjectAllocation:
419
		case IProblem.UnusedObjectAllocation:
420
			return CompilerOptions.UnusedObjectAllocation;
420
			return CompilerOptions.UnusedObjectAllocation;
421
			
422
		case IProblem.MethodCanBeStatic:
423
			return CompilerOptions.MethodCanBeStatic;
424
			
425
		case IProblem.MethodCanBePotentiallyStatic:
426
			return CompilerOptions.MethodCanBePotentiallyStatic;
421
	}
427
	}
422
	return 0;
428
	return 0;
423
}
429
}
Lines 448-453 Link Here
448
			case CompilerOptions.MissingOverrideAnnotation :
454
			case CompilerOptions.MissingOverrideAnnotation :
449
			case CompilerOptions.MissingDeprecatedAnnotation :
455
			case CompilerOptions.MissingDeprecatedAnnotation :
450
			case CompilerOptions.ParameterAssignment :
456
			case CompilerOptions.ParameterAssignment :
457
			case CompilerOptions.MethodCanBeStatic :
458
			case CompilerOptions.MethodCanBePotentiallyStatic :
451
				return CategorizedProblem.CAT_CODE_STYLE;
459
				return CategorizedProblem.CAT_CODE_STYLE;
452
460
453
			case CompilerOptions.MaskedCatchBlock :
461
			case CompilerOptions.MaskedCatchBlock :
Lines 5020-5025 Link Here
5020
		methodDecl.sourceEnd);
5028
		methodDecl.sourceEnd);
5021
}
5029
}
5022
5030
5031
public void methodCanBeDeclaredStatic(MethodDeclaration methodDecl) {
5032
	int severity = computeSeverity(IProblem.MethodCanBeStatic);
5033
	if (severity == ProblemSeverities.Ignore) return;
5034
	MethodBinding method = methodDecl.binding;
5035
	this.handle(
5036
			IProblem.MethodCanBeStatic,
5037
		new String[] {
5038
			new String(method.declaringClass.readableName()),
5039
			new String(method.selector),
5040
			typesAsString(method.isVarargs(), method.parameters, false)
5041
		 },
5042
		new String[] {
5043
			new String(method.declaringClass.shortReadableName()),
5044
			new String(method.selector),
5045
			typesAsString(method.isVarargs(), method.parameters, true)
5046
		 },
5047
		severity,
5048
		methodDecl.sourceStart,
5049
		methodDecl.sourceEnd);
5050
}
5051
5052
public void methodCanBePotentiallyDeclaredStatic(MethodDeclaration methodDecl) {
5053
	int severity = computeSeverity(IProblem.MethodCanBePotentiallyStatic);
5054
	if (severity == ProblemSeverities.Ignore) return;
5055
	MethodBinding method = methodDecl.binding;
5056
	this.handle(
5057
			IProblem.MethodCanBePotentiallyStatic,
5058
		new String[] {
5059
			new String(method.declaringClass.readableName()),
5060
			new String(method.selector),
5061
			typesAsString(method.isVarargs(), method.parameters, false)
5062
		 },
5063
		new String[] {
5064
			new String(method.declaringClass.shortReadableName()),
5065
			new String(method.selector),
5066
			typesAsString(method.isVarargs(), method.parameters, true)
5067
		 },
5068
		severity,
5069
		methodDecl.sourceStart,
5070
		methodDecl.sourceEnd);
5071
}
5072
5023
public void missingDeprecatedAnnotationForField(FieldDeclaration field) {
5073
public void missingDeprecatedAnnotationForField(FieldDeclaration field) {
5024
	int severity = computeSeverity(IProblem.FieldMissingDeprecatedAnnotation);
5074
	int severity = computeSeverity(IProblem.FieldMissingDeprecatedAnnotation);
5025
	if (severity == ProblemSeverities.Ignore) return;
5075
	if (severity == ProblemSeverities.Ignore) return;
(-)compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties (+2 lines)
Lines 103-108 Link Here
103
118 = The method {1}({2}) from the type {0} is never used locally
103
118 = The method {1}({2}) from the type {0} is never used locally
104
119 = The static method {1}({2}) from the type {0} should be accessed directly 
104
119 = The static method {1}({2}) from the type {0} should be accessed directly 
105
120 = The method {1}({2}) from the type {0} refers to the missing type {3}
105
120 = The method {1}({2}) from the type {0} refers to the missing type {3}
106
121 = The method {1}({2}) from the type {0} can be declared as static
107
122 = The method {1}({2}) from the type {0} can potentially be declared as static
106
108
107
129 = The constructor {0}({1}) refers to the missing type {2}
109
129 = The constructor {0}({1}) refers to the missing type {2}
108
130 = The constructor {0}({1}) is undefined
110
130 = The constructor {0}({1}) is undefined
(-)formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java (+2 lines)
Lines 338-343 Link Here
338
			optionsMap.put(CompilerOptions.OPTION_ReportSpecialParameterHidingField, CompilerOptions.DISABLED);
338
			optionsMap.put(CompilerOptions.OPTION_ReportSpecialParameterHidingField, CompilerOptions.DISABLED);
339
			optionsMap.put(CompilerOptions.OPTION_MaxProblemPerUnit, String.valueOf(100));
339
			optionsMap.put(CompilerOptions.OPTION_MaxProblemPerUnit, String.valueOf(100));
340
			optionsMap.put(CompilerOptions.OPTION_InlineJsr, CompilerOptions.DISABLED);
340
			optionsMap.put(CompilerOptions.OPTION_InlineJsr, CompilerOptions.DISABLED);
341
			optionsMap.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.IGNORE);
342
			optionsMap.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.IGNORE);
341
			this.defaultCompilerOptions = optionsMap;
343
			this.defaultCompilerOptions = optionsMap;
342
		}
344
		}
343
		Object sourceOption = this.options.get(CompilerOptions.OPTION_Source);
345
		Object sourceOption = this.options.get(CompilerOptions.OPTION_Source);
(-)model/org/eclipse/jdt/core/JavaCore.java (+27 lines)
Lines 1314-1319 Link Here
1314
	 */
1314
	 */
1315
	public static final String COMPILER_PB_PARAMETER_ASSIGNMENT = PLUGIN_ID + ".compiler.problem.parameterAssignment"; //$NON-NLS-1$
1315
	public static final String COMPILER_PB_PARAMETER_ASSIGNMENT = PLUGIN_ID + ".compiler.problem.parameterAssignment"; //$NON-NLS-1$
1316
	/**
1316
	/**
1317
	 * Compiler option ID: Reporting a method that qualifies as static, but not declared static.
1318
	 * <p>When enabled, the compiler will issue an error or a warning if a method has
1319
	 *    not been declared as <code>static</code>, even though it qualifies as one.
1320
	 * <dl>
1321
	 * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic"</code></dd>
1322
	 * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd>
1323
	 * <dt>Default:</dt><dd><code>"ignore"</code></dd>
1324
	 * </dl>
1325
	 * @since 3.7
1326
	 * @category CompilerOptionID
1327
	 */
1328
	public static final String COMPILER_PB_MISSING_STATIC_ON_METHOD = PLUGIN_ID + ".compiler.problem.reportMethodCanBeStatic"; //$NON-NLS-1$
1329
	/**
1330
	 * Compiler option ID: Reporting a method that may qualify as static, but not declared static.
1331
	 * <p>When enabled, the compiler will issue an error or a warning if a method has
1332
	 *    not been declared as <code>static</code>, even though it may qualify as one,
1333
	 *    when another method doesn't override it.
1334
	 * <dl>
1335
	 * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic"</code></dd>
1336
	 * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd>
1337
	 * <dt>Default:</dt><dd><code>"ignore"</code></dd>
1338
	 * </dl>
1339
	 * @since 3.7
1340
	 * @category CompilerOptionID
1341
	 */
1342
	public static final String COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD = PLUGIN_ID + ".compiler.problem.reportMethodCanBePotentiallyStatic"; //$NON-NLS-1$
1343
	/**
1317
	 * Compiler option ID: Setting Source Compatibility Mode.
1344
	 * Compiler option ID: Setting Source Compatibility Mode.
1318
	 * <p>Specify whether which source level compatibility is used. From 1.4 on, <code>'assert'</code> is a keyword
1345
	 * <p>Specify whether which source level compatibility is used. From 1.4 on, <code>'assert'</code> is a keyword
1319
	 *    reserved for assertion support. Also note, than when toggling to 1.4 mode, the target VM
1346
	 *    reserved for assertion support. Also note, than when toggling to 1.4 mode, the target VM
(-)src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java (-1 / +125 lines)
Lines 48-54 Link Here
48
	private static final Main MAIN = new Main(null/*outWriter*/, null/*errWriter*/, false/*systemExit*/, null/*options*/, null/*progress*/);
48
	private static final Main MAIN = new Main(null/*outWriter*/, null/*errWriter*/, false/*systemExit*/, null/*options*/, null/*progress*/);
49
49
50
	static {
50
	static {
51
//		TESTS_NAMES = new String[] { "test292_warn_options" };
51
//		TESTS_NAMES = new String[] { "test295_warn_options" };
52
//		TESTS_NUMBERS = new int[] { 306 };
52
//		TESTS_NUMBERS = new int[] { 306 };
53
//		TESTS_RANGE = new int[] { 298, -1 };
53
//		TESTS_RANGE = new int[] { 298, -1 };
54
	}
54
	}
Lines 1663-1668 Link Here
1663
        "      allDeprecation       deprecation including inside deprecated code\n" + 
1663
        "      allDeprecation       deprecation including inside deprecated code\n" + 
1664
        "      allJavadoc           invalid or missing javadoc\n" + 
1664
        "      allJavadoc           invalid or missing javadoc\n" + 
1665
        "      allOver-ann          all missing @Override annotations\n" + 
1665
        "      allOver-ann          all missing @Override annotations\n" + 
1666
        "      all-static-method    all method can be declared as static warnings\n" +
1666
        "      assertIdentifier   + ''assert'' used as identifier\n" + 
1667
        "      assertIdentifier   + ''assert'' used as identifier\n" + 
1667
        "      boxing               autoboxing conversion\n" + 
1668
        "      boxing               autoboxing conversion\n" + 
1668
        "      charConcat         + char[] in String concat\n" + 
1669
        "      charConcat         + char[] in String concat\n" + 
Lines 1705-1710 Link Here
1705
        "      semicolon            unnecessary semicolon, empty statement\n" + 
1706
        "      semicolon            unnecessary semicolon, empty statement\n" + 
1706
        "      serial             + missing serialVersionUID\n" + 
1707
        "      serial             + missing serialVersionUID\n" + 
1707
        "      specialParamHiding   constructor or setter parameter hiding a field\n" + 
1708
        "      specialParamHiding   constructor or setter parameter hiding a field\n" + 
1709
        "      static-method        method can be declared as static\n" + 
1708
        "      static-access        macro for indirectStatic and staticReceiver\n" + 
1710
        "      static-access        macro for indirectStatic and staticReceiver\n" + 
1709
        "      staticReceiver     + non-static reference to static member\n" + 
1711
        "      staticReceiver     + non-static reference to static member\n" + 
1710
        "      super                overriding a method without making a super invocation\n" + 
1712
        "      super                overriding a method without making a super invocation\n" + 
Lines 1857-1862 Link Here
1857
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.rawTypeReference\" value=\"warning\"/>\n" + 
1859
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.rawTypeReference\" value=\"warning\"/>\n" + 
1858
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantNullCheck\" value=\"ignore\"/>\n" + 
1860
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantNullCheck\" value=\"ignore\"/>\n" + 
1859
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantSuperinterface\" value=\"ignore\"/>\n" + 
1861
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantSuperinterface\" value=\"ignore\"/>\n" + 
1862
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic\" value=\"ignore\"/>\n" + 
1863
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic\" value=\"ignore\"/>\n" + 
1860
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.specialParameterHidingField\" value=\"disabled\"/>\n" + 
1864
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.specialParameterHidingField\" value=\"disabled\"/>\n" + 
1861
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.staticAccessReceiver\" value=\"warning\"/>\n" + 
1865
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.staticAccessReceiver\" value=\"warning\"/>\n" + 
1862
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors\" value=\"disabled\"/>\n" + 
1866
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors\" value=\"disabled\"/>\n" + 
Lines 11195-11200 Link Here
11195
		"4 problems (4 warnings)", 
11199
		"4 problems (4 warnings)", 
11196
		true);
11200
		true);
11197
}
11201
}
11202
11203
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
11204
// -warn option - regression test to check option static-method
11205
// Method can be static warning should be given
11206
public void test294_warn_options() {
11207
	this.runConformTest(
11208
		new String[] {
11209
			"X.java",
11210
			"public class X {\n" +
11211
			"	public static int field1;\n" +
11212
			"	public static int field2;\n" + 
11213
			"	public void bar(int i) {\n" + 
11214
			"		System.out.println(foo());\n" +
11215
			"		foo();" +
11216
			"		System.out.println(X.field1);\n" +
11217
			"		System.out.println(field2);\n" +
11218
			"		field2 = 1;\n" +
11219
			"	}\n" + 
11220
			"	private static String foo() {\n" + 
11221
			"		return null;\n" + 
11222
			"	}\n" +
11223
			"	private void foo1() {\n" +
11224
			"		System.out.println();\n" +
11225
			"	}\n" +
11226
			"	public final void foo2() {\n" +
11227
			"		System.out.println();\n" +
11228
			"	}\n" + 
11229
			"}\n" +
11230
			"final class A {" +
11231
			"	public void foo() {\n" +
11232
			"		System.out.println();\n" +
11233
			"	}\n" + 
11234
			"}\n",
11235
		},
11236
		"\"" + OUTPUT_DIR +  File.separator + "X.java\""
11237
		+ " -sourcepath \"" + OUTPUT_DIR + "\""
11238
		+ " -warn:static-method -proc:none -d \"" + OUTPUT_DIR + "\"",
11239
		"",
11240
		"----------\n" + 
11241
		"1. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 13)\n" + 
11242
		"	private void foo1() {\n" + 
11243
		"	             ^^^^^^\n" + 
11244
		"The method foo1() from the type X can be declared as static\n" + 
11245
		"----------\n" + 
11246
		"2. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 16)\n" + 
11247
		"	public final void foo2() {\n" + 
11248
		"	                  ^^^^^^\n" + 
11249
		"The method foo2() from the type X can be declared as static\n" + 
11250
		"----------\n" + 
11251
		"3. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 20)\n" + 
11252
		"	final class A {	public void foo() {\n" + 
11253
		"	               	            ^^^^^\n" + 
11254
		"The method foo() from the type A can be declared as static\n" + 
11255
		"----------\n" + 
11256
		"3 problems (3 warnings)", 
11257
		true);
11258
}
11259
11260
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
11261
// -warn option - regression test to check option all-static-method
11262
// Method can be static warning should be given
11263
public void test295_warn_options() {
11264
	this.runConformTest(
11265
		new String[] {
11266
			"X.java",
11267
			"public class X {\n" +
11268
			"	public static int field1;\n" +
11269
			"	public static int field2;\n" + 
11270
			"	public void bar(int i) {\n" + 
11271
			"		System.out.println(foo());\n" +
11272
			"		foo();" +
11273
			"		System.out.println(X.field1);\n" +
11274
			"		System.out.println(field2);\n" +
11275
			"		field2 = 1;\n" +
11276
			"	}\n" + 
11277
			"	private static String foo() {\n" + 
11278
			"		return null;\n" + 
11279
			"	}\n" +
11280
			"	private void foo1() {\n" +
11281
			"		System.out.println();\n" +
11282
			"	}\n" +
11283
			"	public final void foo2() {\n" +
11284
			"		System.out.println();\n" +
11285
			"	}\n" + 
11286
			"}\n" +
11287
			"final class A {" +
11288
			"	public void foo() {\n" +
11289
			"		System.out.println();\n" +
11290
			"	}\n" + 
11291
			"}\n",
11292
		},
11293
		"\"" + OUTPUT_DIR +  File.separator + "X.java\""
11294
		+ " -sourcepath \"" + OUTPUT_DIR + "\""
11295
		+ " -warn:all-static-method -proc:none -d \"" + OUTPUT_DIR + "\"",
11296
		"",
11297
		"----------\n" + 
11298
		"1. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 4)\n" + 
11299
		"	public void bar(int i) {\n" + 
11300
		"	            ^^^^^^^^^^\n" + 
11301
		"The method bar(int) from the type X can potentially be declared as static\n" + 
11302
		"----------\n" + 
11303
		"2. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 13)\n" + 
11304
		"	private void foo1() {\n" + 
11305
		"	             ^^^^^^\n" + 
11306
		"The method foo1() from the type X can be declared as static\n" + 
11307
		"----------\n" + 
11308
		"3. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 16)\n" + 
11309
		"	public final void foo2() {\n" + 
11310
		"	                  ^^^^^^\n" + 
11311
		"The method foo2() from the type X can be declared as static\n" + 
11312
		"----------\n" + 
11313
		"4. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 20)\n" + 
11314
		"	final class A {	public void foo() {\n" + 
11315
		"	               	            ^^^^^\n" + 
11316
		"The method foo() from the type A can be declared as static\n" + 
11317
		"----------\n" + 
11318
		"4 problems (4 warnings)", 
11319
		true);
11320
}
11321
11198
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=280784
11322
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=280784
11199
public void test293(){
11323
public void test293(){
11200
	createCascadedJars();
11324
	createCascadedJars();
(-)src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java (+4 lines)
Lines 655-660 Link Here
655
		expectedProblemAttributes.put("LocalVariableMayBeNull", DEPRECATED);
655
		expectedProblemAttributes.put("LocalVariableMayBeNull", DEPRECATED);
656
		expectedProblemAttributes.put("MaskedCatch", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
656
		expectedProblemAttributes.put("MaskedCatch", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
657
		expectedProblemAttributes.put("MethodButWithConstructorName", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
657
		expectedProblemAttributes.put("MethodButWithConstructorName", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
658
		expectedProblemAttributes.put("MethodCanBeStatic", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
659
		expectedProblemAttributes.put("MethodCanBePotentiallyStatic", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
658
		expectedProblemAttributes.put("MethodMissingDeprecatedAnnotation", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
660
		expectedProblemAttributes.put("MethodMissingDeprecatedAnnotation", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
659
		expectedProblemAttributes.put("MethodMustOverride", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
661
		expectedProblemAttributes.put("MethodMustOverride", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
660
		expectedProblemAttributes.put("MethodMustOverrideOrImplement", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
662
		expectedProblemAttributes.put("MethodMustOverrideOrImplement", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
Lines 1290-1295 Link Here
1290
		expectedProblemAttributes.put("LocalVariableMayBeNull", SKIP);
1292
		expectedProblemAttributes.put("LocalVariableMayBeNull", SKIP);
1291
		expectedProblemAttributes.put("MaskedCatch", new ProblemAttributes(JavaCore.COMPILER_PB_HIDDEN_CATCH_BLOCK));
1293
		expectedProblemAttributes.put("MaskedCatch", new ProblemAttributes(JavaCore.COMPILER_PB_HIDDEN_CATCH_BLOCK));
1292
		expectedProblemAttributes.put("MethodButWithConstructorName", new ProblemAttributes(JavaCore.COMPILER_PB_METHOD_WITH_CONSTRUCTOR_NAME));
1294
		expectedProblemAttributes.put("MethodButWithConstructorName", new ProblemAttributes(JavaCore.COMPILER_PB_METHOD_WITH_CONSTRUCTOR_NAME));
1295
		expectedProblemAttributes.put("MethodCanBeStatic", new ProblemAttributes(JavaCore.COMPILER_PB_MISSING_STATIC_ON_METHOD));
1296
		expectedProblemAttributes.put("MethodCanBePotentiallyStatic", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD));
1293
		expectedProblemAttributes.put("MethodMissingDeprecatedAnnotation", new ProblemAttributes(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION));
1297
		expectedProblemAttributes.put("MethodMissingDeprecatedAnnotation", new ProblemAttributes(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION));
1294
		expectedProblemAttributes.put("MethodMustOverride", SKIP);
1298
		expectedProblemAttributes.put("MethodMustOverride", SKIP);
1295
		expectedProblemAttributes.put("MethodMustOverrideOrImplement", SKIP);
1299
		expectedProblemAttributes.put("MethodMustOverrideOrImplement", SKIP);
(-)src/org/eclipse/jdt/core/tests/compiler/regression/ProblemTypeAndMethodTest.java (-1 / +742 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[] { "test123" };
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
				"	public <E,Y> void bar4() {\n" + 	// warn
6503
				"		Y a;\n" +
6504
				"		System.out.println();\n" +
6505
				"	}\n" + 
6506
				"}"
6507
		},
6508
		"----------\n" + 
6509
		"1. ERROR in X.java (at line 8)\n" + 
6510
		"	public <E> void bar2(E e) {\n" + 
6511
		"	                ^^^^^^^^^\n" + 
6512
		"The method bar2(E) from the type X<T> can potentially be declared as static\n" + 
6513
		"----------\n" + 
6514
		"2. ERROR in X.java (at line 16)\n" + 
6515
		"	public <E,Y> void bar4() {\n" + 
6516
		"	                  ^^^^^^\n" + 
6517
		"The method bar4() from the type X<T> can potentially be declared as static\n" + 
6518
		"----------\n",
6519
		null /* no extra class libraries */,
6520
		true /* flush output directory */,
6521
		compilerOptions /* custom options */
6522
	);
6523
}
6524
6525
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
6526
// Access to super in a method disqualifies it from being static
6527
public void test121() {
6528
	Map compilerOptions = getCompilerOptions();
6529
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6530
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
6531
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6532
	this.runNegativeTest(
6533
		new String[] {
6534
				"X.java", 
6535
				"public class X extends A{\n" +
6536
				"	public static int field1;\n" +
6537
				"	public int field2;\n" + 
6538
				"	public void methodA() {\n" + 	// don't warn
6539
				"		super.methodA();\n" +
6540
				"	}\n" + 
6541
				"	public void bar() {\n" + 	// don't warn
6542
				"		super.fieldA = 1;\n" +
6543
				"	}\n" +
6544
				"	public void bar2() {\n" + 	// don't warn
6545
				"		System.out.println(super.fieldA);\n" +
6546
				"	}\n" +
6547
				"	public void bar3() {\n" + 	// warn
6548
				"		System.out.println(X.fieldA);\n" +
6549
				"	}\n" +
6550
				"}\n" +
6551
				"class A{\n" +
6552
				"	public static int fieldA;\n" +
6553
				"   public void methodA(){\n" +
6554
				"   }\n" +
6555
				"}"
6556
		},
6557
		"----------\n" + 
6558
		"1. ERROR in X.java (at line 13)\n" + 
6559
		"	public void bar3() {\n" + 
6560
		"	            ^^^^^^\n" + 
6561
		"The method bar3() from the type X can potentially be declared as static\n" + 
6562
		"----------\n",
6563
		null /* no extra class libraries */,
6564
		true /* flush output directory */,
6565
		compilerOptions /* custom options */
6566
	);
6567
}
6568
6569
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
6570
// Methods of non-static member types can't be static
6571
public void test122() {
6572
	Map compilerOptions = getCompilerOptions();
6573
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6574
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
6575
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6576
	this.runNegativeTest(
6577
		new String[] {
6578
				"X.java", 
6579
				"public class X {\n" +
6580
				"	class A{\n" +
6581
				"   	void methodA() {\n" +	// don't warn
6582
				"			System.out.println();\n" +
6583
				"		}\n" +
6584
				"   }\n" +
6585
				"	static class B{\n" +
6586
				"   	void methodB() {\n" +	// warn
6587
				"			System.out.println();\n" +
6588
				"		}\n" +
6589
				"   }\n" +
6590
				"}"
6591
		},
6592
		"----------\n" + 
6593
		"1. ERROR in X.java (at line 8)\n" + 
6594
		"	void methodB() {\n" + 
6595
		"	     ^^^^^^^^^\n" + 
6596
		"The method methodB() from the type X.B can potentially be declared as static\n" + 
6597
		"----------\n",
6598
		null /* no extra class libraries */,
6599
		true /* flush output directory */,
6600
		compilerOptions /* custom options */
6601
	);
6602
}
6603
6604
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
6605
// If method returns type parameter not declared by it, it cannot be static
6606
public void test123() {
6607
	if (this.complianceLevel < ClassFileConstants.JDK1_5)
6608
		return;
6609
	Map compilerOptions = getCompilerOptions();
6610
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR);
6611
	compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR);
6612
	compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
6613
	this.runNegativeTest(
6614
		new String[] {
6615
				"X.java", 
6616
				"public class X<T> {\n" +
6617
				"	<E,Y> T method1() {\n" + 	// don't warn
6618
				"		return null;\n" +
6619
				"	}\n" + 
6620
				"	<E,Y> E method2() {\n" + 	// warn
6621
				"		return null;\n" +
6622
				"	}\n" + 
6623
				"}"
6624
		},
6625
		"----------\n" + 
6626
		"1. ERROR in X.java (at line 5)\n" + 
6627
		"	<E,Y> E method2() {\n" + 
6628
		"	        ^^^^^^^^^\n" + 
6629
		"The method method2() from the type X<T> can potentially be declared as static\n" + 
6630
		"----------\n",
6631
		null /* no extra class libraries */,
6632
		true /* flush output directory */,
6633
		compilerOptions /* custom options */
6634
	);
6635
}
5895
}
6636
}
(-)guide/jdt_api_options.htm (+31 lines)
Lines 666-671 Link Here
666
</tr>
666
</tr>
667
667
668
<tr>
668
<tr>
669
<td colspan=2><b>Reporting a method that qualifies as static, but not declared static</b> (<b><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#COMPILER_PB_MISSING_STATIC_ON_METHOD">COMPILER_PB_MISSING_STATIC_ON_METHOD</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 been 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>
684
<td colspan=2><b>Reporting a method that may qualify as static, but not declared static</b> (<b><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD">COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD</a></b>)</td>
685
</tr>
686
<tr valign="top">
687
<td rowspan=3>When enabled, the compiler will issue an error or a warning if a method has
688
not been declared as <code>static</code>, even though it may qualify as one,
689
when another method doesn't override it.</td>
690
<td><b><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#ERROR">ERROR</a></b></td>
691
</tr>
692
<tr valign="top">
693
<td><b><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#WARNING">WARNING</a></b></td>
694
</tr>
695
<tr valign="top">
696
<td><b><i><a href="../reference/api/org/eclipse/jdt/core/JavaCore.html#IGNORE">IGNORE</a></i></b></td>
697
</tr>
698
699
<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>
700
<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>
701
</tr>
671
<tr valign="top">
702
<tr valign="top">

Return to bug 318682