winningCandidate = other; } else { if (winningCandidate.declaringType().getQualifiedName() .equals(other.declaringType().getQualifiedName())) { possibleAmbiguity = true; } else {
winningCandidate = other; } else { if (winningCandidate.declaringType().getQualifiedName() .equals(other.declaringType().getQualifiedName())) { possibleAmbiguity = true; } else {
@Override public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) { // TODO consider generic types if (this.getQualifiedName().equals(other.getQualifiedName())) { return true; } ResolvedClassDeclaration superclass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration(); if (superclass != null) { // We want to avoid infinite recursion in case of Object having Object as ancestor if (Object.class.getCanonicalName().equals(superclass.getQualifiedName())) { return true; } if (superclass.canBeAssignedTo(other)) { return true; } } if (this.wrappedNode.getImplementedTypes() != null) { for (ClassOrInterfaceType type : wrappedNode.getImplementedTypes()) { ResolvedReferenceTypeDeclaration ancestor = (ResolvedReferenceTypeDeclaration) new SymbolSolver(typeSolver).solveType(type); if (ancestor.canBeAssignedTo(other)) { return true; } } } return false; }
@Override public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) { // TODO consider generic types if (this.getQualifiedName().equals(other.getQualifiedName())) { return true; } ResolvedClassDeclaration superclass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration(); if (superclass != null) { // We want to avoid infinite recursion in case of Object having Object as ancestor if (Object.class.getCanonicalName().equals(superclass.getQualifiedName())) { return true; } if (superclass.canBeAssignedTo(other)) { return true; } } if (this.wrappedNode.getImplementedTypes() != null) { for (ClassOrInterfaceType type : wrappedNode.getImplementedTypes()) { ResolvedReferenceTypeDeclaration ancestor = (ResolvedReferenceTypeDeclaration) new SymbolSolver(typeSolver).solveType(type); if (ancestor.canBeAssignedTo(other)) { return true; } } } return false; }
@Test public void testGetQualifiedName() { TypeSolver typeResolver = new ReflectionTypeSolver(); ResolvedClassDeclaration arraylist = new ReflectionClassDeclaration(ArrayList.class, typeResolver); assertEquals("java.util.ArrayList", arraylist.getQualifiedName()); ResolvedClassDeclaration string = new ReflectionClassDeclaration(String.class, typeResolver); assertEquals("java.lang.String", string.getQualifiedName()); }
@Override public boolean canBeAssignedTo(ResolvedReferenceTypeDeclaration other) { // TODO consider generic types if (this.getQualifiedName().equals(other.getQualifiedName())) { return true; } ResolvedClassDeclaration superclass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration(); if (superclass != null) { // We want to avoid infinite recursion in case of Object having Object as ancestor if (Object.class.getCanonicalName().equals(superclass.getQualifiedName())) { return true; } if (superclass.canBeAssignedTo(other)) { return true; } } if (this.wrappedNode.getImplementedTypes() != null) { for (ClassOrInterfaceType type : wrappedNode.getImplementedTypes()) { ResolvedReferenceTypeDeclaration ancestor = (ResolvedReferenceTypeDeclaration) new SymbolSolver(typeSolver).solveType(type); if (ancestor.canBeAssignedTo(other)) { return true; } } } return false; }
@Test public void resolveReferenceToClassesInTheSamePackage() throws ParseException { CompilationUnit cu = parseSample("Navigator3"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator"); MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType"); Parameter param = method.getParameters().get(0); ResolvedClassDeclaration compilationUnitDecl = createMock(ResolvedClassDeclaration.class); expect(compilationUnitDecl.getName()).andReturn("CompilationUnit"); expect(compilationUnitDecl.getQualifiedName()).andReturn("my.packagez.CompilationUnit"); TypeSolver typeSolver = createMock(TypeSolver.class); expect(typeSolver.getRoot()).andReturn(typeSolver); expect(typeSolver.solveType("java.lang.Object")).andReturn(new ReflectionClassDeclaration(Object.class, typeSolver)); expect(typeSolver.tryToSolveType("my.packagez.CompilationUnit")).andReturn(SymbolReference.solved(compilationUnitDecl)); SymbolSolver symbolSolver = new SymbolSolver(typeSolver); replay(typeSolver, compilationUnitDecl); SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("CompilationUnit", param); assertEquals(true, ref.isSolved()); assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName()); assertEquals("my.packagez.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName()); verify(typeSolver, compilationUnitDecl); }
@Test public void resolveReferenceToImportedType() throws ParseException { CompilationUnit cu = parseSample("Navigator"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator"); MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType"); Parameter param = method.getParameters().get(0); ResolvedClassDeclaration compilationUnitDecl = createMock(ResolvedClassDeclaration.class); expect(compilationUnitDecl.getName()).andReturn("CompilationUnit"); expect(compilationUnitDecl.getQualifiedName()).andReturn("com.github.javaparser.ast.CompilationUnit"); TypeSolver typeSolver = createMock(TypeSolver.class); expect(typeSolver.getRoot()).andReturn(typeSolver); expect(typeSolver.solveType("java.lang.Object")).andReturn(new ReflectionClassDeclaration(Object.class, typeSolver)); expect(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).andReturn(SymbolReference.solved(compilationUnitDecl)); SymbolSolver symbolSolver = new SymbolSolver(typeSolver); replay(typeSolver, compilationUnitDecl); SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("CompilationUnit", param); assertEquals(true, ref.isSolved()); assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName()); assertEquals("com.github.javaparser.ast.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName()); verify(typeSolver, compilationUnitDecl); }
@Test public void resolveReferenceToClassInJavaLang() throws ParseException { CompilationUnit cu = parseSample("Navigator"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator"); MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType"); Parameter param = method.getParameters().get(1); ResolvedClassDeclaration stringDecl = createMock(ResolvedClassDeclaration.class); expect(stringDecl.getName()).andReturn("String"); expect(stringDecl.getQualifiedName()).andReturn("java.lang.String"); TypeSolver typeSolver = createMock(TypeSolver.class); expect(typeSolver.tryToSolveType("me.tomassetti.symbolsolver.javaparser.String")).andReturn(SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class)); expect(typeSolver.getRoot()).andReturn(typeSolver); expect(typeSolver.solveType("java.lang.Object")).andReturn(new ReflectionClassDeclaration(Object.class, typeSolver)); expect(typeSolver.tryToSolveType("java.lang.String")).andReturn(SymbolReference.solved(stringDecl)); SymbolSolver symbolSolver = new SymbolSolver(typeSolver); replay(typeSolver, stringDecl); SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("String", param); assertEquals(true, ref.isSolved()); assertEquals("String", ref.getCorrespondingDeclaration().getName()); assertEquals("java.lang.String", ref.getCorrespondingDeclaration().getQualifiedName()); verify(typeSolver, stringDecl); }
@Test public void resolveReferenceUsingQualifiedName() throws ParseException { CompilationUnit cu = parseSample("Navigator2"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator"); MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType"); Parameter param = method.getParameters().get(0); ResolvedClassDeclaration compilationUnitDecl = createMock(ResolvedClassDeclaration.class); expect(compilationUnitDecl.getName()).andReturn("CompilationUnit"); expect(compilationUnitDecl.getQualifiedName()).andReturn("com.github.javaparser.ast.CompilationUnit"); TypeSolver typeSolver = createMock(TypeSolver.class); //expect(typeSolver.tryToSolveType("java.lang.com.github.javaparser.ast.CompilationUnit")).andReturn(SymbolReference.unsolved(ClassDeclaration.class)); expect(typeSolver.getRoot()).andReturn(typeSolver); expect(typeSolver.solveType("java.lang.Object")).andReturn(new ReflectionClassDeclaration(Object.class, typeSolver)); expect(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).andReturn(SymbolReference.solved(compilationUnitDecl)); SymbolSolver symbolSolver = new SymbolSolver(typeSolver); replay(typeSolver, compilationUnitDecl); SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("com.github.javaparser.ast.CompilationUnit", param); assertEquals(true, ref.isSolved()); assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName()); assertEquals("com.github.javaparser.ast.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName()); verify(typeSolver, compilationUnitDecl); }