Fixed Bug 378674 - "The method can be declared as static" is wrong
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FieldReference.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FieldReference.java
index fa04bbf..b356327 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FieldReference.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FieldReference.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -11,6 +11,8 @@
* bug 185682 - Increment/decrement operators mark local variables as read
* bug 331649 - [compiler][null] consider null annotations for fields
* bug 383368 - [compiler][null] syntactic null analysis for field references
+ * Jesper S Moller - Contributions for
+ * Bug 378674 - "The method can be declared as static" is wrong
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;
@@ -148,12 +150,6 @@
if (this.receiver.isThis()) {
currentScope.resetDeclaringClassMethodStaticFlag(this.binding.declaringClass);
}
- } else if (this.receiver.isThis()) {
- if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
- // explicit this receiver, not allowed in static context
- // https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
- currentScope.resetEnclosingMethodStaticFlag();
- }
}
if (valueRequired || currentScope.compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4) {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java
index 988bc5c..942ee02 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -20,6 +20,8 @@
* bug 394768 - [compiler][resource] Incorrect resource leak warning when creating stream in conditional
* bug 395002 - Self bound generic class doesn't resolve bounds properly for wildcards for certain parametrisation.
* bug 383368 - [compiler][null] syntactic null analysis for field references
+ * Jesper S Moller - Contributions for
+ * Bug 378674 - "The method can be declared as static" is wrong
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;
@@ -50,42 +52,6 @@
if ((flowInfo.tagBits & FlowInfo.UNREACHABLE_OR_DEAD) == 0) {
this.bits |= ASTNode.IsLocalDeclarationReachable; // only set if actually reached
}
- if (this.binding != null && this.type.resolvedType instanceof TypeVariableBinding) {
- TypeVariableBinding typeVariableBinding = (TypeVariableBinding) this.type.resolvedType;
- MethodScope methodScope= this.binding.declaringScope.methodScope();
- if (methodScope != null && methodScope.referenceContext instanceof TypeDeclaration) {
- // initialization scope
- methodScope = methodScope.enclosingMethodScope();
- }
- AbstractMethodDeclaration methodDeclaration = (methodScope != null) ? methodScope.referenceMethod() : null;
- if (methodDeclaration != null && methodDeclaration.binding != null) {
- TypeVariableBinding[] typeVariables = methodDeclaration.binding.typeVariables();
- if (typeVariables == null) typeVariables = Binding.NO_TYPE_VARIABLES;
- if (typeVariables == Binding.NO_TYPE_VARIABLES) {
- // Method declares no type variables.
- if (typeVariableBinding != null && typeVariableBinding.declaringElement instanceof TypeBinding)
- currentScope.resetDeclaringClassMethodStaticFlag((TypeBinding) typeVariableBinding.declaringElement);
- else
- currentScope.resetEnclosingMethodStaticFlag();
- } else {
- // to check whether the resolved type for this is declared by enclosing method as a type variable
- boolean usesEnclosingTypeVar = false;
- for (int i = 0; i < typeVariables.length ; i ++) {
- if (typeVariables[i] == this.type.resolvedType){
- usesEnclosingTypeVar = true;
- break;
- }
- }
- if (!usesEnclosingTypeVar) {
- // uses a type variable not declared by enclosing method
- if (typeVariableBinding != null && typeVariableBinding.declaringElement instanceof TypeBinding)
- currentScope.resetDeclaringClassMethodStaticFlag((TypeBinding) typeVariableBinding.declaringElement);
- else
- currentScope.resetEnclosingMethodStaticFlag();
- }
- }
- }
- }
if (this.initialization == null) {
return flowInfo;
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java
index a6e6bdb..eccddf4 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java
@@ -24,6 +24,8 @@
* bug 331649 - [compiler][null] consider null annotations for fields
* bug 383368 - [compiler][null] syntactic null analysis for field references
* bug 382069 - [null] Make the null analysis consider JUnit's assertNotNull similarly to assertions
+ * Jesper S Moller - Contributions for
+ * Bug 378674 - "The method can be declared as static" is wrong
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;
@@ -122,11 +124,6 @@
// accessing non-static method without an object
currentScope.resetDeclaringClassMethodStaticFlag(this.actualReceiverType);
}
- } else if (this.receiver.isThis()) {
- if ((this.receiver.bits & ASTNode.IsImplicitThis) == 0) {
- // explicit this receiver, not allowed in static context
- currentScope.resetEnclosingMethodStaticFlag();
- }
}
if (this.arguments != null) {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java
index 5c61125..3675b46 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java
@@ -48,7 +48,7 @@
*/
public MethodDeclaration(CompilationResult compilationResult) {
super(compilationResult);
- this.bits |= ASTNode.CanBeStatic; // Start with this assumption, will course correct during resolve.
+ this.bits |= ASTNode.CanBeStatic; // Start with this assumption, will course correct during resolve and analyseCode.
}
public void analyseCode(ClassScope classScope, FlowContext flowContext, FlowInfo flowInfo) {
@@ -253,7 +253,7 @@
if ((this.modifiers & ClassFileConstants.AccAbstract) == 0)
this.scope.problemReporter().methodNeedBody(this);
} else {
- // the method HAS a body --> abstract native modifiers are forbiden
+ // the method HAS a body --> abstract native modifiers are forbidden
if (((this.modifiers & ClassFileConstants.AccNative) != 0) || ((this.modifiers & ClassFileConstants.AccAbstract) != 0))
this.scope.problemReporter().methodNeedingNoBody(this);
else if (this.binding == null || this.binding.isStatic() || (this.binding.declaringClass instanceof LocalTypeBinding) || returnsUndeclTypeVar) {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SuperReference.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SuperReference.java
index 5444d2a..6dafc92 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SuperReference.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SuperReference.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,12 +7,12 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Jesper S Moller - Contributions for
+ * Bug 378674 - "The method can be declared as static" is wrong
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;
import org.eclipse.jdt.internal.compiler.ASTVisitor;
-import org.eclipse.jdt.internal.compiler.flow.FlowContext;
-import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
import org.eclipse.jdt.internal.compiler.impl.Constant;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
@@ -68,9 +68,4 @@
visitor.visit(this, blockScope);
visitor.endVisit(this, blockScope);
}
-
- public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo, boolean valueRequired) {
- currentScope.resetEnclosingMethodStaticFlag();
- return analyseCode(currentScope, flowContext, flowInfo);
- }
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ThisReference.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ThisReference.java
index fc05dc4..9fc8015 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ThisReference.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ThisReference.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2011 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -10,6 +10,8 @@
* Stephan Herrmann - Contribution for
* bug 331649 - [compiler][null] consider null annotations for fields
* bug 383368 - [compiler][null] syntactic null analysis for field references
+ * Jesper S Moller - Contributions for
+ * Bug 378674 - "The method can be declared as static" is wrong
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;
@@ -131,13 +133,4 @@
visitor.visit(this, blockScope);
visitor.endVisit(this, blockScope);
}
-
- public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
- if (!isImplicitThis()) {
- // explicit this reference, not allowed in static context
- // https://bugs.eclipse.org/bugs/show_bug.cgi?id=335780
- currentScope.resetEnclosingMethodStaticFlag();
- }
- return super.analyseCode(currentScope, flowContext, flowInfo);
- }
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java
index 18faf32..63ffa15 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/Scope.java
@@ -13,7 +13,7 @@
* bug 395002 - Self bound generic class doesn't resolve bounds properly for wildcards for certain parametrisation.
* bug 401456 - Code compiles from javac/intellij, but fails from eclipse
* Jesper S Moller - Contributions for
- * bug 382721 - [1.8][compiler] Effectively final variables needs special treatment
+ * Bug 378674 - "The method can be declared as static" is wrong
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.lookup;
@@ -1712,13 +1712,14 @@
ProblemFieldBinding foundInsideProblem = null;
// inside Constructor call or inside static context
Scope scope = this;
+ MethodScope methodScope = null;
int depth = 0;
int foundDepth = 0;
ReferenceBinding foundActualReceiverType = null;
done : while (true) { // done when a COMPILATION_UNIT_SCOPE is found
switch (scope.kind) {
case METHOD_SCOPE :
- MethodScope methodScope = (MethodScope) scope;
+ methodScope = (MethodScope) scope;
insideStaticContext |= methodScope.isStatic;
insideConstructorCall |= methodScope.isConstructorCall;
insideTypeAnnotation = methodScope.insideTypeAnnotation;
@@ -1777,6 +1778,8 @@
fieldBinding.declaringClass,
name,
ProblemReasons.NonStaticReferenceInStaticContext);
+ } else if (methodScope != null) {
+ methodScope.resetEnclosingMethodStaticFlag();
}
}
if (receiverType == fieldBinding.declaringClass || compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4) {
@@ -2078,6 +2081,7 @@
MethodBinding foundProblem = null;
boolean foundProblemVisible = false;
Scope scope = this;
+ MethodScope methodScope = null;
int depth = 0;
// in 1.4 mode (inherited visible shadows enclosing)
CompilerOptions options;
@@ -2086,7 +2090,7 @@
done : while (true) { // done when a COMPILATION_UNIT_SCOPE is found
switch (scope.kind) {
case METHOD_SCOPE :
- MethodScope methodScope = (MethodScope) scope;
+ methodScope = (MethodScope) scope;
insideStaticContext |= methodScope.isStatic;
insideConstructorCall |= methodScope.isConstructorCall;
insideTypeAnnotation = methodScope.insideTypeAnnotation;
@@ -2113,6 +2117,8 @@
insideConstructorCall
? ProblemReasons.NonStaticReferenceInConstructorInvocation
: ProblemReasons.NonStaticReferenceInStaticContext);
+ } else if (!methodBinding.isStatic() && methodScope != null) {
+ methodScope.resetDeclaringClassMethodStaticFlag(receiverType);
}
if (inheritedHasPrecedence
|| receiverType == methodBinding.declaringClass
diff --git a/org.eclipse.jdt.core/eval/org/eclipse/jdt/internal/eval/CodeSnippetThisReference.java b/org.eclipse.jdt.core/eval/org/eclipse/jdt/internal/eval/CodeSnippetThisReference.java
index bf04e4d..53b874a 100644
--- a/org.eclipse.jdt.core/eval/org/eclipse/jdt/internal/eval/CodeSnippetThisReference.java
+++ b/org.eclipse.jdt.core/eval/org/eclipse/jdt/internal/eval/CodeSnippetThisReference.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2013 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,8 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Jesper S Moller - Contributions for
+ * Bug 378674 - "The method can be declared as static" is wrong
*******************************************************************************/
package org.eclipse.jdt.internal.eval;
@@ -54,6 +56,7 @@
methodScope.problemReporter().errorThisSuperInStatic(this);
return false;
}
+ methodScope.resetEnclosingMethodStaticFlag();
return true;
}