use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration 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.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class ReferenceTypeTest method testGetFieldTypeUnexisting.
@Test
public void testGetFieldTypeUnexisting() {
class Foo<A> {
List<A> elements;
}
TypeSolver typeSolver = new ReflectionTypeSolver();
ReferenceTypeImpl ref = new ReferenceTypeImpl(new ReflectionClassDeclaration(Foo.class, typeSolver), typeSolver);
assertEquals(false, ref.getFieldType("bar").isPresent());
ref = new ReferenceTypeImpl(new ReflectionClassDeclaration(Foo.class, typeSolver), ImmutableList.of(new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver)), typeSolver);
assertEquals(false, ref.getFieldType("bar").isPresent());
}
use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration 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.symbolsolver.reflectionmodel.ReflectionClassDeclaration 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.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class ReferenceTypeTest method testIsAssignableByBoxedPrimitive.
@Test
public void testIsAssignableByBoxedPrimitive() {
ResolvedReferenceType numberType = new ReferenceTypeImpl(new ReflectionClassDeclaration(Number.class, typeSolver), typeSolver);
ResolvedReferenceType intType = new ReferenceTypeImpl(new ReflectionClassDeclaration(Integer.class, typeSolver), typeSolver);
ResolvedReferenceType doubleType = new ReferenceTypeImpl(new ReflectionClassDeclaration(Double.class, typeSolver), typeSolver);
assertEquals(true, numberType.isAssignableBy(ResolvedPrimitiveType.INT));
assertEquals(true, numberType.isAssignableBy(ResolvedPrimitiveType.DOUBLE));
assertEquals(true, numberType.isAssignableBy(ResolvedPrimitiveType.SHORT));
assertEquals(true, numberType.isAssignableBy(ResolvedPrimitiveType.LONG));
assertEquals(true, numberType.isAssignableBy(ResolvedPrimitiveType.FLOAT));
assertEquals(false, numberType.isAssignableBy(ResolvedPrimitiveType.BOOLEAN));
assertEquals(true, intType.isAssignableBy(ResolvedPrimitiveType.INT));
assertEquals(true, doubleType.isAssignableBy(ResolvedPrimitiveType.DOUBLE));
}
Aggregations