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

Return to bug 318682