use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration 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());
}
use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class ContextTest method resolveReferenceToClassInJavaLang.
@Test
public void resolveReferenceToClassInJavaLang() {
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(1);
ResolvedClassDeclaration stringDecl = mock(ResolvedClassDeclaration.class);
when(stringDecl.getName()).thenReturn("String");
when(stringDecl.getQualifiedName()).thenReturn("java.lang.String");
TypeSolver typeSolver = mock(TypeSolver.class);
when(typeSolver.tryToSolveType("me.tomassetti.symbolsolver.javaparser.String")).thenReturn(SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class));
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("java.lang.String")).thenReturn(SymbolReference.solved(stringDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("String", param);
assertEquals(true, ref.isSolved());
assertEquals("String", ref.getCorrespondingDeclaration().getName());
assertEquals("java.lang.String", ref.getCorrespondingDeclaration().getQualifiedName());
}
use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class InferenceContextTest method setup.
@Before
public void setup() {
typeSolver = new ReflectionTypeSolver();
string = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
object = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
listOfString = listOf(string);
tpE = mock(ResolvedTypeParameterDeclaration.class);
when(tpE.getName()).thenReturn("T");
listOfE = listOf(new ResolvedTypeVariable(tpE));
}
use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class PrimitiveTypeTest 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));
booleanBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Boolean.class, typeSolver), typeSolver);
characterBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Character.class, typeSolver), typeSolver);
byteBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Byte.class, typeSolver), typeSolver);
shortBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Short.class, typeSolver), typeSolver);
integerBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Integer.class, typeSolver), typeSolver);
longBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Long.class, typeSolver), typeSolver);
floatBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Float.class, typeSolver), typeSolver);
doubleBox = new ReferenceTypeImpl(new ReflectionClassDeclaration(Double.class, typeSolver), typeSolver);
}
use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class ReferenceTypeTest method testGetAllAncestorsConsideringGenericsCases.
@Test
public void testGetAllAncestorsConsideringGenericsCases() {
ReferenceTypeImpl foo = new ReferenceTypeImpl(new ReflectionClassDeclaration(Foo.class, typeSolver), typeSolver);
ReferenceTypeImpl bar = new ReferenceTypeImpl(new ReflectionClassDeclaration(Bar.class, typeSolver), typeSolver);
ReferenceTypeImpl left, right;
// YES MoreBazzing<Foo, Bar> e1 = new MoreBazzing<Foo, Bar>();
assertEquals(true, new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(foo, bar), typeSolver).isAssignableBy(new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(foo, bar), typeSolver)));
// YES MoreBazzing<? extends Foo, Bar> e2 = new MoreBazzing<Foo, Bar>();
assertEquals(true, new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(ResolvedWildcard.extendsBound(foo), bar), typeSolver).isAssignableBy(new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(foo, bar), typeSolver)));
// YES MoreBazzing<Foo, ? extends Bar> e3 = new MoreBazzing<Foo, Bar>();
assertEquals(true, new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(foo, ResolvedWildcard.extendsBound(bar)), typeSolver).isAssignableBy(new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(foo, bar), typeSolver)));
// YES MoreBazzing<? extends Foo, ? extends Foo> e4 = new MoreBazzing<Foo, Bar>();
assertEquals(true, new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(ResolvedWildcard.extendsBound(foo), ResolvedWildcard.extendsBound(foo)), typeSolver).isAssignableBy(new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(foo, bar), typeSolver)));
// YES MoreBazzing<? extends Foo, ? extends Foo> e5 = new MoreBazzing<Bar, Bar>();
left = new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(ResolvedWildcard.extendsBound(foo), ResolvedWildcard.extendsBound(foo)), typeSolver);
right = new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(bar, bar), typeSolver);
assertEquals(true, left.isAssignableBy(right));
// YES Bazzer<Object, String, String> e6 = new MoreBazzing<String, Object>();
left = new ReferenceTypeImpl(new ReflectionClassDeclaration(Bazzer.class, typeSolver), ImmutableList.of(object, string, string), typeSolver);
right = new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(string, object), typeSolver);
// To debug the following
List<ResolvedReferenceType> ancestors = right.getAllAncestors();
ResolvedReferenceType moreBazzingAncestor = ancestors.stream().filter(a -> a.getQualifiedName().endsWith("Bazzer")).findFirst().get();
assertEquals(true, left.isAssignableBy(right));
// YES Bazzer<String,String,String> e7 = new MoreBazzing<String, String>();
assertEquals(true, new ReferenceTypeImpl(new ReflectionClassDeclaration(Bazzer.class, typeSolver), ImmutableList.of(string, string, string), typeSolver).isAssignableBy(new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(string, string), typeSolver)));
// YES Bazzer<Bar,String,Foo> e8 = new MoreBazzing<Foo, Bar>();
assertEquals(true, new ReferenceTypeImpl(new ReflectionClassDeclaration(Bazzer.class, typeSolver), ImmutableList.of(bar, string, foo), typeSolver).isAssignableBy(new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(foo, bar), typeSolver)));
// YES Bazzer<Foo,String,Bar> e9 = new MoreBazzing<Bar, Foo>();
assertEquals(true, new ReferenceTypeImpl(new ReflectionClassDeclaration(Bazzer.class, typeSolver), ImmutableList.of(foo, string, bar), typeSolver).isAssignableBy(new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(bar, foo), typeSolver)));
// NO Bazzer<Bar,String,Foo> n1 = new MoreBazzing<Bar, Foo>();
assertEquals(false, new ReferenceTypeImpl(new ReflectionClassDeclaration(Bazzer.class, typeSolver), ImmutableList.of(bar, string, foo), typeSolver).isAssignableBy(new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(bar, foo), typeSolver)));
// NO Bazzer<Bar,String,Bar> n2 = new MoreBazzing<Bar, Foo>();
assertEquals(false, new ReferenceTypeImpl(new ReflectionClassDeclaration(Bazzer.class, typeSolver), ImmutableList.of(bar, string, foo), typeSolver).isAssignableBy(new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(bar, foo), typeSolver)));
// NO Bazzer<Foo,Object,Bar> n3 = new MoreBazzing<Bar, Foo>();
assertEquals(false, new ReferenceTypeImpl(new ReflectionClassDeclaration(Bazzer.class, typeSolver), ImmutableList.of(foo, object, bar), typeSolver).isAssignableBy(new ReferenceTypeImpl(new ReflectionClassDeclaration(MoreBazzing.class, typeSolver), ImmutableList.of(bar, foo), typeSolver)));
}
Aggregations