use of com.github.javaparser.resolution.declarations.ResolvedClassDeclaration in project javaparser by javaparser.
the class ReferenceTypeTest method testGetAllAncestorsOnRawType.
@Test
public void testGetAllAncestorsOnRawType() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedClassDeclaration arraylist = new ReflectionClassDeclaration(ArrayList.class, typeResolver);
ResolvedReferenceType rawArrayList = new ReferenceTypeImpl(arraylist, typeResolver);
Map<String, ResolvedReferenceType> ancestors = new HashMap<>();
rawArrayList.getAllAncestors().forEach(a -> ancestors.put(a.getQualifiedName(), a));
assertEquals(9, ancestors.size());
ResolvedTypeVariable tv = new ResolvedTypeVariable(arraylist.getTypeParameters().get(0));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(RandomAccess.class, typeResolver), typeResolver), ancestors.get("java.util.RandomAccess"));
assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(AbstractCollection.class, typeResolver), ImmutableList.of(tv), typeResolver), ancestors.get("java.util.AbstractCollection"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(List.class, typeResolver), ImmutableList.of(tv), typeResolver), ancestors.get("java.util.List"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Cloneable.class, typeResolver), typeResolver), ancestors.get("java.lang.Cloneable"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Collection.class, typeResolver), ImmutableList.of(tv), typeResolver), ancestors.get("java.util.Collection"));
assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(AbstractList.class, typeResolver), ImmutableList.of(tv), typeResolver), ancestors.get("java.util.AbstractList"));
assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeResolver), typeResolver), ancestors.get("java.lang.Object"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Iterable.class, typeResolver), ImmutableList.of(tv), typeResolver), ancestors.get("java.lang.Iterable"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Serializable.class, typeResolver), typeResolver), ancestors.get("java.io.Serializable"));
}
use of com.github.javaparser.resolution.declarations.ResolvedClassDeclaration in project javaparser by javaparser.
the class ContextTest method resolveReferenceUsingQualifiedName.
@Test
public void resolveReferenceUsingQualifiedName() {
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 = mock(ResolvedClassDeclaration.class);
when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
when(compilationUnitDecl.getQualifiedName()).thenReturn("com.github.javaparser.ast.CompilationUnit");
TypeSolver typeSolver = mock(TypeSolver.class);
// when(typeSolver.tryToSolveType("java.lang.com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.unsolved(ClassDeclaration.class));
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
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());
}
use of com.github.javaparser.resolution.declarations.ResolvedClassDeclaration in project javaparser by javaparser.
the class ContextTest method resolveReferenceToImportedType.
@Test
public void resolveReferenceToImportedType() {
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 = mock(ResolvedClassDeclaration.class);
when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
when(compilationUnitDecl.getQualifiedName()).thenReturn("com.github.javaparser.ast.CompilationUnit");
TypeSolver typeSolver = mock(TypeSolver.class);
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
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());
}
use of com.github.javaparser.resolution.declarations.ResolvedClassDeclaration in project javaparser by javaparser.
the class ReflectionMethodDeclarationTest method testGetSignature.
@Test
public void testGetSignature() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedClassDeclaration object = new ReflectionClassDeclaration(Object.class, typeResolver);
ResolvedInterfaceDeclaration list = new ReflectionInterfaceDeclaration(List.class, typeResolver);
ResolvedMethodDeclaration hashCode = object.getAllMethods().stream().filter(m -> m.getName().equals("hashCode")).findFirst().get().getDeclaration();
ResolvedMethodDeclaration equals = object.getAllMethods().stream().filter(m -> m.getName().equals("equals")).findFirst().get().getDeclaration();
ResolvedMethodDeclaration containsAll = list.getAllMethods().stream().filter(m -> m.getName().equals("containsAll")).findFirst().get().getDeclaration();
ResolvedMethodDeclaration subList = list.getAllMethods().stream().filter(m -> m.getName().equals("subList")).findFirst().get().getDeclaration();
assertEquals("hashCode()", hashCode.getSignature());
assertEquals("equals(java.lang.Object)", equals.getSignature());
assertEquals("containsAll(java.util.Collection<? extends java.lang.Object>)", containsAll.getSignature());
assertEquals("subList(int, int)", subList.getSignature());
}
use of com.github.javaparser.resolution.declarations.ResolvedClassDeclaration in project javaparser by javaparser.
the class ContextTest method resolveReferenceToClassesInTheSamePackage.
@Test
public void resolveReferenceToClassesInTheSamePackage() {
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 = mock(ResolvedClassDeclaration.class);
when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
when(compilationUnitDecl.getQualifiedName()).thenReturn("my.packagez.CompilationUnit");
TypeSolver typeSolver = mock(TypeSolver.class);
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("my.packagez.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("CompilationUnit", param);
assertEquals(true, ref.isSolved());
assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName());
assertEquals("my.packagez.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());
}
Aggregations