diff options
| author | Jesper S Moller | 2013-03-12 11:21:56 +0000 |
|---|---|---|
| committer | Jayaprakash Arthanareeswaran | 2013-04-03 09:03:14 +0000 |
| commit | 97db7e90ccc840cc657b10cff644c6263b14fb3a (patch) | |
| tree | bd170f5cd853278fc8adcbbc7eaa8ed4340a7f7b | |
| parent | c17d3895a0e0525887a24ddd3e8adeeb66ba12c9 (diff) | |
| download | eclipse.jdt.core-97db7e90ccc840cc657b10cff644c6263b14fb3a.tar.gz eclipse.jdt.core-97db7e90ccc840cc657b10cff644c6263b14fb3a.tar.xz eclipse.jdt.core-97db7e90ccc840cc657b10cff644c6263b14fb3a.zip | |
Fixed Bug 378674 - "The method can be declared as static" is wrong
8 files changed, 28 insertions, 72 deletions
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 7f6393d4e2..e195511a3b 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 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl 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 8dd6c14938..0393635c77 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 @@ -24,6 +24,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; @@ -57,42 +59,6 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl 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 726fcfee38..0ef3fc3da0 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 @@ -31,6 +31,8 @@ * bug 382069 - [null] Make the null analysis consider JUnit's assertNotNull similarly to assertions * bug 382350 - [1.8][compiler] Unable to invoke inherited default method via I.super.m() syntax * bug 404649 - [1.8][compiler] detect illegal reference to indirect or redundant super + * Jesper S Moller - Contributions for + * Bug 378674 - "The method can be declared as static" is wrong *******************************************************************************/ package org.eclipse.jdt.internal.compiler.ast; @@ -132,11 +134,6 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl // 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 b9e3535155..c68500dd4e 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 @@ -57,7 +57,7 @@ public class MethodDeclaration extends AbstractMethodDeclaration { */ 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) { @@ -276,7 +276,7 @@ public class MethodDeclaration extends AbstractMethodDeclaration { 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 5444d2adc2..6dafc924f8 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 @@ public class SuperReference extends ThisReference { 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 b7f8d4d3be..d5c019ad0f 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; @@ -135,13 +137,4 @@ public class ThisReference extends Reference { 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 b5a51ea750..ddefbf9414 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 @@ -17,7 +17,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; @@ -1748,6 +1748,7 @@ public abstract class Scope { ProblemFieldBinding foundInsideProblem = null; // inside Constructor call or inside static context Scope scope = this; + MethodScope methodScope = null; int depth = 0; int foundDepth = 0; boolean shouldTrackOuterLocals = false; @@ -1755,7 +1756,7 @@ public abstract class Scope { 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; @@ -1823,6 +1824,8 @@ public abstract class Scope { fieldBinding.declaringClass, name, ProblemReasons.NonStaticReferenceInStaticContext); + } else if (methodScope != null) { + methodScope.resetEnclosingMethodStaticFlag(); } } if (receiverType == fieldBinding.declaringClass || compilerOptions().complianceLevel >= ClassFileConstants.JDK1_4) { @@ -2127,6 +2130,7 @@ public abstract class Scope { 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; @@ -2135,7 +2139,7 @@ public abstract class Scope { 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; @@ -2162,6 +2166,8 @@ public abstract class Scope { 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 bf04e4d700..53b874a265 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 @@ public class CodeSnippetThisReference extends ThisReference implements Evaluatio methodScope.problemReporter().errorThisSuperInStatic(this); return false; } + methodScope.resetEnclosingMethodStaticFlag(); return true; } |
