Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Loskutov2019-10-07 11:52:51 +0000
committerStephan Herrmann2019-11-14 16:28:59 +0000
commita33aa29393230e8178f4d055c2d4705783641e02 (patch)
tree07b5c11ca5c31684e8e79e8ef3eaaf54c332e438
parent6b460f84922c92a4184b44f3133ca5d7a24c7cf5 (diff)
downloadeclipse.jdt.core-a33aa29393230e8178f4d055c2d4705783641e02.tar.gz
eclipse.jdt.core-a33aa29393230e8178f4d055c2d4705783641e02.tar.xz
eclipse.jdt.core-a33aa29393230e8178f4d055c2d4705783641e02.zip
Bug 551426 - NPE at org.eclipse.jdt.core.dom.TypeBinding.getAnnotations
Change-Id: Ibb15184d28adfc030536914b0a8f1bc761ecc2f2 Signed-off-by: Andrey Loskutov <loskutov@gmx.de> Also-by: Stephan Herrmann <stephan.herrmann@berlin.de>
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/NullAnnotationModelTests.java32
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java45
2 files changed, 55 insertions, 22 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/NullAnnotationModelTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/NullAnnotationModelTests.java
index 7dbfd9d928..118ee5bd4d 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/NullAnnotationModelTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/NullAnnotationModelTests.java
@@ -19,8 +19,10 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.io.ByteArrayInputStream;
import java.net.URL;
+import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
+import java.util.Map;
import junit.framework.Test;
@@ -43,13 +45,19 @@ import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.IProblem;
+import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
+import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.FieldDeclaration;
+import org.eclipse.jdt.core.dom.IAnnotationBinding;
+import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
+import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.tests.util.Util;
import org.osgi.framework.Bundle;
@@ -1061,4 +1069,28 @@ public class NullAnnotationModelTests extends ReconcilerTests {
deleteProject(project);
}
}
+
+ // was: NPE in SourceTypeBinding.getAnnotationTagBits
+ public void testBug551426() throws CoreException, Exception {
+ ASTParser astParser = ASTParser.newParser(AST.JLS8);
+ Map<String, String> options = new HashMap<>();
+ astParser.setResolveBindings(true);
+ astParser.setEnvironment(new String[] {}, new String[] {}, new String[] {}, true);
+ options.put(JavaCore.COMPILER_SOURCE, "1.8");
+ options.put(JavaCore.COMPILER_COMPLIANCE, "1.8");
+ astParser.setCompilerOptions(options);
+ astParser.setUnitName("C.java");
+ String source =
+ "class C {\n" +
+ " public static final Object f = new Object() {};\n" +
+ "}\n";
+ astParser.setSource(source.toCharArray());
+ CompilationUnit astNode = (CompilationUnit) astParser.createAST(null);
+ AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration) astNode.types().get(0);
+ FieldDeclaration fieldDeclaration = (FieldDeclaration) typeDeclaration.bodyDeclarations().get(0);
+ VariableDeclarationFragment fragment = (VariableDeclarationFragment) fieldDeclaration.fragments().get(0);
+ ITypeBinding typeBinding = fragment.getInitializer().resolveTypeBinding();
+ IAnnotationBinding[] annotations = typeBinding.getAnnotations();
+ assertEquals(0, annotations.length);
+ }
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java
index a1e02094e1..f2eb5efa0e 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java
@@ -1035,33 +1035,34 @@ public long getAnnotationTagBits() {
if (!isPrototype())
return this.prototype.getAnnotationTagBits();
- if ((this.tagBits & TagBits.EndHierarchyCheck) == 0) {
- CompilationUnitScope pkgCUS = this.scope.compilationUnitScope();
- boolean current = pkgCUS.connectingHierarchy;
- pkgCUS.connectingHierarchy = true;
- try {
- return internalGetAnnotationTagBits();
- } finally {
- pkgCUS.connectingHierarchy = current;
- }
- }
- return internalGetAnnotationTagBits();
-}
-private long internalGetAnnotationTagBits() {
if ((this.tagBits & TagBits.AnnotationResolved) == 0 && this.scope != null) {
- TypeDeclaration typeDecl = this.scope.referenceContext;
- boolean old = typeDecl.staticInitializerScope.insideTypeAnnotation;
- try {
- typeDecl.staticInitializerScope.insideTypeAnnotation = true;
- ASTNode.resolveAnnotations(typeDecl.staticInitializerScope, typeDecl.annotations, this);
- } finally {
- typeDecl.staticInitializerScope.insideTypeAnnotation = old;
+ if ((this.tagBits & TagBits.EndHierarchyCheck) == 0) {
+ CompilationUnitScope pkgCUS = this.scope.compilationUnitScope();
+ boolean current = pkgCUS.connectingHierarchy;
+ pkgCUS.connectingHierarchy = true;
+ try {
+ initAnnotationTagBits();
+ } finally {
+ pkgCUS.connectingHierarchy = current;
+ }
+ } else {
+ initAnnotationTagBits();
}
- if ((this.tagBits & TagBits.AnnotationDeprecated) != 0)
- this.modifiers |= ClassFileConstants.AccDeprecated;
}
return this.tagBits;
}
+private void initAnnotationTagBits() {
+ TypeDeclaration typeDecl = this.scope.referenceContext;
+ boolean old = typeDecl.staticInitializerScope.insideTypeAnnotation;
+ try {
+ typeDecl.staticInitializerScope.insideTypeAnnotation = true;
+ ASTNode.resolveAnnotations(typeDecl.staticInitializerScope, typeDecl.annotations, this);
+ } finally {
+ typeDecl.staticInitializerScope.insideTypeAnnotation = old;
+ }
+ if ((this.tagBits & TagBits.AnnotationDeprecated) != 0)
+ this.modifiers |= ClassFileConstants.AccDeprecated;
+}
public MethodBinding[] getDefaultAbstractMethods() {
if (!isPrototype())
return this.prototype.getDefaultAbstractMethods();

Back to the top