Skip to content

Commit

Permalink
Bug 435571: ImportReferencesCollector needs to support old AST levels
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeller committed May 26, 2014
1 parent 0acb1c1 commit 28a197a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*******************************************************************************/
package org.eclipse.jdt.ui.tests.core.source;

import static org.junit.Assert.assertArrayEquals;

import java.util.Hashtable;

import junit.framework.Test;
Expand All @@ -36,6 +38,8 @@
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.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IMethodBinding;
Expand Down Expand Up @@ -349,6 +353,64 @@ public void testInsertAt() throws Exception {
}
}

/**
* @throws Exception
* @deprecated tests deprecated API
*/
public void testJLS3() throws Exception {
doTestOldAstLevel(AST.JLS3);
}

/**
* @throws Exception
* @deprecated tests deprecated API
*/
public void testJLS4() throws Exception {
doTestOldAstLevel(AST.JLS4);
}

public void testJLS8() throws Exception {
doTestOldAstLevel(AST.JLS8);
}

/**
* @param astLevel AST.JLS*
* @throws Exception
* @deprecated tests deprecated API
*/
public void doTestOldAstLevel(int astLevel) throws Exception {
ICompilationUnit cu= fPackage.getCompilationUnit("Test1.java");
IType testClass= cu.createType(
"public class Test1 extends A implements B {\n"
+ " @Deprecated\n"
+ " java.util.List<String>[][] getArray() throws RuntimeException {\n"
+ " return (ArrayList<String>[][]) new ArrayList<?>[1][2];\n"
+ " }\n"
+ "}\n", null, true, null);
cu.createImport("java.util.ArrayList", null, null);

RefactoringASTParser parser= new RefactoringASTParser(astLevel);
CompilationUnit unit= parser.parse(cu, true);
AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) ASTNodes.getParent(NodeFinder.perform(unit, testClass.getNameRange()), AbstractTypeDeclaration.class);
assertNotNull("Could not find type declaration node", declaration);
ITypeBinding binding= declaration.resolveBinding();
assertNotNull("Binding for type declaration could not be resolved", binding);

IMethodBinding[] overridableMethods= StubUtility2.getOverridableMethods(unit.getAST(), binding, false);

AddUnimplementedMethodsOperation op= new AddUnimplementedMethodsOperation(unit, binding, overridableMethods, -1, true, true, true);
op.run(new NullProgressMonitor());

IMethod[] methods= testClass.getMethods();
checkMethods(new String[] { "a", "b", "c", "getArray", "equals", "clone", "toString", "finalize", "hashCode" }, methods);

IImportDeclaration[] imports= cu.getImports();
checkImports(new String[] { "java.util.Date", "java.util.Hashtable", "java.util.Vector", "java.util.ArrayList" }, imports);

IProblem[] problems= parser.parse(cu, true).getProblems();
assertArrayEquals(new IProblem[0], problems);
}

private void testHelper(IType testClass) throws JavaModelException, CoreException {
testHelper(testClass, -1, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
package org.eclipse.jdt.internal.corext.codemanipulation;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.text.Region;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.AnnotatableType;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.CreationReference;
Expand Down Expand Up @@ -200,17 +203,17 @@ protected boolean visitNode(ASTNode node) {
@Override
public boolean visit(SimpleType node) {
typeRefFound(node.getName());
doVisitChildren(node.annotations());
visitAnnotations(node);
return false;
}

/*
* @see ASTVisitor#visit(NameQualifiedType)
*/
@Override
public boolean visit(NameQualifiedType node) {
possibleTypeRefFound(node.getQualifier());
doVisitChildren(node.annotations());
visitAnnotations(node);
return false;
}

Expand All @@ -220,10 +223,16 @@ public boolean visit(NameQualifiedType node) {
@Override
public boolean visit(QualifiedType node) {
doVisitNode(node.getQualifier());
doVisitChildren(node.annotations());
visitAnnotations(node);
return false;
}

private void visitAnnotations(AnnotatableType node) {
if (node.getAST().apiLevel() >= AST.JLS8) {
doVisitChildren(node.annotations());
}
}

/*
* @see ASTVisitor#visit(QualifiedName)
*/
Expand Down Expand Up @@ -420,18 +429,38 @@ public boolean visit(MethodDeclaration node) {
doVisitNode(node.getReturnType2());
}
// name not visited
doVisitNode(node.getReceiverType());

int apiLevel= node.getAST().apiLevel();
if (apiLevel >= AST.JLS8) {
doVisitNode(node.getReceiverType());
}
// receiverQualifier not visited:
// Enclosing class names cannot be shadowed by an import (qualification is always redundant).
doVisitChildren(node.parameters());
doVisitChildren(node.extraDimensions());
doVisitChildren(node.thrownExceptionTypes());
if (apiLevel >= AST.JLS8) {
doVisitChildren(node.extraDimensions());
doVisitChildren(node.thrownExceptionTypes());
} else {
Iterator<Name> iter= getThrownExceptions(node).iterator();
while (iter.hasNext()) {
typeRefFound(iter.next());
}
}
if (!fSkipMethodBodies) {
doVisitNode(node.getBody());
}
return false;
}

/**
* @param decl method declaration
* @return thrown exception names
* @deprecated to avoid deprecation warnings
*/
private static List<Name> getThrownExceptions(MethodDeclaration decl) {
return decl.thrownExceptions();
}

@Override
public boolean visit(TagElement node) {
String tagName= node.getTagName();
Expand Down

0 comments on commit 28a197a

Please sign in to comment.