package a; import junit.framework.TestCase; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.IAnnotationBinding; import org.eclipse.jdt.core.dom.IBinding; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.internal.junit.buildpath.JUnitContainerInitializer; import org.eclipse.jdt.launching.JavaRuntime; public class SnippedBug156352 extends TestCase { public void testNPE() throws Exception { IProject project= ResourcesPlugin.getWorkspace().getRoot().getProject("Test"); project.create(null); try { project.open(null); IProjectDescription description = project.getDescription(); description.setNatureIds(new String[] { JavaCore.NATURE_ID } ); project.setDescription(description, null); IJavaProject javaProject= JavaCore.create(project); IClasspathEntry[] cpentry= new IClasspathEntry[] { JavaCore.newSourceEntry(javaProject.getPath()), JavaRuntime.getDefaultJREContainerEntry(), JavaCore.newContainerEntry(JUnitContainerInitializer.JUNIT4_PATH) }; javaProject.setRawClasspath(cpentry, javaProject.getPath(), null); IPackageFragmentRoot root= javaProject.getPackageFragmentRoot(project); IPackageFragment p= root.createPackageFragment("p", true, null); StringBuffer buf= new StringBuffer(); buf.append("package p;\n"); buf.append("\n"); buf.append("import org.junit.Test;\n"); buf.append("\n"); buf.append("public class Test1 {\n"); buf.append(" @Test public void testFoo() {\n"); buf.append(" }\n"); buf.append("}\n"); p.createCompilationUnit("Test1.java", buf.toString(), false, null); buf= new StringBuffer(); buf.append("package p;\n"); buf.append("\n"); buf.append("import org.junit.runner.RunWith;\n"); buf.append("import org.junit.runners.Suite;\n"); buf.append("import org.junit.runners.Suite.SuiteClasses;\n"); buf.append("\n"); buf.append("@RunWith(Suite.class)\n"); buf.append("@SuiteClasses(Test1.class)\n"); buf.append("public class Test2 {\n"); buf.append(" \n"); buf.append("}\n"); p.createCompilationUnit("Test2.java", buf.toString(), false, null); buf= new StringBuffer(); buf.append("package p;\n"); buf.append("\n"); buf.append("public class Test3 extends Test2 {\n"); buf.append(" \n"); buf.append("}\n"); IType type= p.createCompilationUnit("Test3.java", buf.toString(), false, null).getType("Test3"); ASTParser parser= ASTParser.newParser(AST.JLS3); parser.setProject(type.getJavaProject()); IBinding[] bindings= parser.createBindings(new IJavaElement[] { type }, null); if (bindings.length == 1 && bindings[0] instanceof ITypeBinding) { ITypeBinding binding= (ITypeBinding) bindings[0]; while (binding != null) { IAnnotationBinding[] annotations= binding.getAnnotations(); // NPE here on 'Test1' binding= binding.getSuperclass(); } } } finally { project.delete(true, null); } } }