Search in sources :

Example 6 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class NullTypeTest method setup.

@Before
public void setup() {
    typeSolver = new ReflectionTypeSolver();
    OBJECT = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
    STRING = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
    arrayOfBooleans = new ResolvedArrayType(ResolvedPrimitiveType.BOOLEAN);
    arrayOfListOfA = new ResolvedArrayType(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(List.class, typeSolver), ImmutableList.of(new ResolvedTypeVariable(ResolvedTypeParameterDeclaration.onType("A", "foo.Bar", Collections.emptyList()))), typeSolver));
}
Also used : ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) ReflectionInterfaceDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Before(org.junit.Before)

Example 7 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class ReferenceTypeTest method testDerivationOfTypeParameters.

@Test
public void testDerivationOfTypeParameters() {
    ReflectionTypeSolver typeSolver = new ReflectionTypeSolver();
    ReferenceTypeImpl ref1 = new ReferenceTypeImpl(typeSolver.solveType(LinkedList.class.getCanonicalName()), typeSolver);
    assertEquals(1, ref1.typeParametersValues().size());
    assertEquals(true, ref1.typeParametersValues().get(0).isTypeVariable());
    assertEquals("E", ref1.typeParametersValues().get(0).asTypeParameter().getName());
}
Also used : ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test)

Example 8 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class ReferenceTypeTest method testGetFieldTypeExisting.

@Test
public void testGetFieldTypeExisting() {
    class Foo<A> {

        List<A> elements;
    }
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ReferenceTypeImpl ref = new ReferenceTypeImpl(new ReflectionClassDeclaration(Foo.class, typeSolver), typeSolver);
    assertEquals(true, ref.getFieldType("elements").isPresent());
    assertEquals(true, ref.getFieldType("elements").get().isReferenceType());
    assertEquals(List.class.getCanonicalName(), ref.getFieldType("elements").get().asReferenceType().getQualifiedName());
    assertEquals(1, ref.getFieldType("elements").get().asReferenceType().typeParametersValues().size());
    assertEquals(true, ref.getFieldType("elements").get().asReferenceType().typeParametersValues().get(0).isTypeVariable());
    assertEquals("A", ref.getFieldType("elements").get().asReferenceType().typeParametersValues().get(0).asTypeParameter().getName());
    ref = new ReferenceTypeImpl(new ReflectionClassDeclaration(Foo.class, typeSolver), ImmutableList.of(new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver)), typeSolver);
    assertEquals(true, ref.getFieldType("elements").isPresent());
    assertEquals(true, ref.getFieldType("elements").get().isReferenceType());
    assertEquals(List.class.getCanonicalName(), ref.getFieldType("elements").get().asReferenceType().getQualifiedName());
    assertEquals(1, ref.getFieldType("elements").get().asReferenceType().typeParametersValues().size());
    assertEquals(true, ref.getFieldType("elements").get().asReferenceType().typeParametersValues().get(0).isReferenceType());
    assertEquals(String.class.getCanonicalName(), ref.getFieldType("elements").get().asReferenceType().typeParametersValues().get(0).asReferenceType().getQualifiedName());
}
Also used : TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ImmutableList(com.google.common.collect.ImmutableList) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test)

Example 9 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver 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"));
}
Also used : TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) ReflectionInterfaceDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test)

Example 10 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver 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());
}
Also used : TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ImmutableList(com.google.common.collect.ImmutableList) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test)

Aggregations

ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)206 Test (org.junit.Test)165 CompilationUnit (com.github.javaparser.ast.CompilationUnit)128 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)81 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)77 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)75 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)57 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)57 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)52 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)47 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)41 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)31 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)28 Expression (com.github.javaparser.ast.expr.Expression)26 Before (org.junit.Before)26 Context (com.github.javaparser.symbolsolver.core.resolution.Context)25 MethodUsage (com.github.javaparser.resolution.MethodUsage)23 File (java.io.File)23 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)22 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)22