use of com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration in project javaparser by javaparser.
the class ReferenceTypeTest method testGetAllAncestorsOnTypeWithSpecifiedTypeParametersForInterface.
@Test
public void testGetAllAncestorsOnTypeWithSpecifiedTypeParametersForInterface() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedInterfaceDeclaration list = new ReflectionInterfaceDeclaration(List.class, typeResolver);
ResolvedType string = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeResolver), typeResolver);
ResolvedReferenceType listOfString = new ReferenceTypeImpl(list, ImmutableList.of(string), typeResolver);
Map<String, ResolvedReferenceType> ancestors = new HashMap<>();
listOfString.getAllAncestors().forEach(a -> ancestors.put(a.getQualifiedName(), a));
assertEquals(3, ancestors.size());
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Collection.class, typeResolver), ImmutableList.of(string), typeResolver), ancestors.get("java.util.Collection"));
assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeResolver), typeResolver), ancestors.get("java.lang.Object"));
assertEquals(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(Iterable.class, typeResolver), ImmutableList.of(string), typeResolver), ancestors.get("java.lang.Iterable"));
}
use of com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration in project javaparser by javaparser.
the class ReferenceTypeTest method testReplaceTypeVariables.
@Test
public void testReplaceTypeVariables() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedInterfaceDeclaration streamInterface = new ReflectionInterfaceDeclaration(Stream.class, typeResolver);
ResolvedReferenceType stream = new ReferenceTypeImpl(streamInterface, typeResolver);
ResolvedMethodDeclaration streamMap = streamInterface.getDeclaredMethods().stream().filter(m -> m.getName().equals("map")).findFirst().get();
ResolvedTypeParameterDeclaration streamMapR = streamMap.findTypeParameter("T").get();
ResolvedTypeVariable typeVariable = new ResolvedTypeVariable(streamMapR);
stream = stream.deriveTypeParameters(stream.typeParametersMap().toBuilder().setValue(stream.getTypeDeclaration().getTypeParameters().get(0), typeVariable).build());
ResolvedTypeParameterDeclaration tpToReplace = streamInterface.getTypeParameters().get(0);
ResolvedType replaced = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeResolver), typeResolver);
ResolvedType streamReplaced = stream.replaceTypeVariables(tpToReplace, replaced);
assertEquals("java.util.stream.Stream<java.lang.String>", streamReplaced.describe());
}
use of com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration 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.ResolvedInterfaceDeclaration in project javaparser by javaparser.
the class ReflectionMethodDeclarationTest method testGetGenericParameters.
@Test
public void testGetGenericParameters() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedInterfaceDeclaration map = new ReflectionInterfaceDeclaration(Map.class, typeResolver);
ResolvedMethodDeclaration put = map.getAllMethods().stream().filter(m -> m.getName().equals("put")).findFirst().get().getDeclaration();
assertEquals(true, put.getParam(0).getType().isTypeVariable());
assertEquals(true, put.getParam(0).getType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.K", put.getParam(0).getType().asTypeParameter().getQualifiedName());
assertEquals(true, put.getParam(1).getType().isTypeVariable());
assertEquals(true, put.getParam(1).getType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.V", put.getParam(1).getType().asTypeParameter().getQualifiedName());
}
use of com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration in project javaparser by javaparser.
the class ReflectionMethodDeclarationTest method testGetGenericReturnType.
@Test
public void testGetGenericReturnType() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedInterfaceDeclaration map = new ReflectionInterfaceDeclaration(Map.class, typeResolver);
ResolvedMethodDeclaration put = map.getAllMethods().stream().filter(m -> m.getName().equals("put")).findFirst().get().getDeclaration();
assertEquals(true, put.getReturnType().isTypeVariable());
assertEquals(true, put.getReturnType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.V", put.getReturnType().asTypeParameter().getQualifiedName());
}
Aggregations