Search in sources :

Example 1 with ResolvedInterfaceDeclaration

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

Example 2 with ResolvedInterfaceDeclaration

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

Example 3 with ResolvedInterfaceDeclaration

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());
}
Also used : List(java.util.List) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) Map(java.util.Map) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) Assert.assertEquals(org.junit.Assert.assertEquals) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) Test(org.junit.Test)

Example 4 with ResolvedInterfaceDeclaration

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());
}
Also used : List(java.util.List) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) Map(java.util.Map) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) Assert.assertEquals(org.junit.Assert.assertEquals) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) Test(org.junit.Test)

Example 5 with ResolvedInterfaceDeclaration

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());
}
Also used : List(java.util.List) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) Map(java.util.Map) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) Assert.assertEquals(org.junit.Assert.assertEquals) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) Test(org.junit.Test)

Aggregations

ResolvedInterfaceDeclaration (com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration)8 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)8 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)8 Test (org.junit.Test)8 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)5 ResolvedClassDeclaration (com.github.javaparser.resolution.declarations.ResolvedClassDeclaration)4 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)4 ReflectionInterfaceDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration)4 List (java.util.List)3 Map (java.util.Map)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)2 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)1 ResolvedTypeVariable (com.github.javaparser.resolution.types.ResolvedTypeVariable)1 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)1 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)1 HashMap (java.util.HashMap)1