use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class ContextTest method resolveReferenceToImportedType.
@Test
public void resolveReferenceToImportedType() {
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(0);
ResolvedClassDeclaration compilationUnitDecl = mock(ResolvedClassDeclaration.class);
when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
when(compilationUnitDecl.getQualifiedName()).thenReturn("com.github.javaparser.ast.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("com.github.javaparser.ast.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("com.github.javaparser.ast.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());
}
use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationContextResolutionTest method solveMethodAsUsageWithMoreSpecializedParameter.
@Test
public void solveMethodAsUsageWithMoreSpecializedParameter() {
CompilationUnit cu = parseSample("ClassWithMethods");
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
ResolvedType stringType = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
Optional<MethodUsage> ref = context.solveMethodAsUsage("foo4", ImmutableList.of(stringType), new ReflectionTypeSolver());
assertEquals(true, ref.isPresent());
assertEquals("A", ref.get().declaringType().getName());
assertEquals(1, ref.get().getNoParams());
}
use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class VarTypeTest method resolveAReferenceType.
@Test
public void resolveAReferenceType() {
CompilationUnit ast = javaParser.parse(ParseStart.COMPILATION_UNIT, provider("class X{void x(){var abc = \"\";}}")).getResult().get();
VarType varType = ast.findFirst(VarType.class).get();
ResolvedType resolvedType = varType.resolve();
assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver), resolvedType);
}
use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class AbstractJavaParserContext method findTypeDeclarations.
protected Collection<ResolvedReferenceTypeDeclaration> findTypeDeclarations(Optional<Expression> optScope, TypeSolver typeSolver) {
if (optScope.isPresent()) {
Expression scope = optScope.get();
// consider static methods
if (scope instanceof NameExpr) {
NameExpr scopeAsName = (NameExpr) scope;
SymbolReference<ResolvedTypeDeclaration> symbolReference = this.solveType(scopeAsName.getName().getId(), typeSolver);
if (symbolReference.isSolved() && symbolReference.getCorrespondingDeclaration().isType()) {
return singletonList(symbolReference.getCorrespondingDeclaration().asReferenceType());
}
}
ResolvedType typeOfScope;
try {
typeOfScope = JavaParserFacade.get(typeSolver).getType(scope);
} catch (Exception e) {
throw new RuntimeException("Issue calculating the type of the scope of " + this, e);
}
if (typeOfScope.isWildcard()) {
if (typeOfScope.asWildcard().isExtends() || typeOfScope.asWildcard().isSuper()) {
return singletonList(typeOfScope.asWildcard().getBoundedType().asReferenceType().getTypeDeclaration());
} else {
return singletonList(new ReflectionClassDeclaration(Object.class, typeSolver).asReferenceType());
}
} else if (typeOfScope.isArray()) {
// method call on array are Object methods
return singletonList(new ReflectionClassDeclaration(Object.class, typeSolver).asReferenceType());
} else if (typeOfScope.isTypeVariable()) {
Collection<ResolvedReferenceTypeDeclaration> result = new ArrayList<>();
for (ResolvedTypeParameterDeclaration.Bound bound : typeOfScope.asTypeParameter().getBounds()) {
result.add(bound.getType().asReferenceType().getTypeDeclaration());
}
return result;
} else if (typeOfScope.isConstraint()) {
return singletonList(typeOfScope.asConstraintType().getBound().asReferenceType().getTypeDeclaration());
}
return singletonList(typeOfScope.asReferenceType().getTypeDeclaration());
}
ResolvedType typeOfScope = JavaParserFacade.get(typeSolver).getTypeOfThisIn(wrappedNode);
return singletonList(typeOfScope.asReferenceType().getTypeDeclaration());
}
use of com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationContextResolutionTest method solveMethodWithMoreSpecializedParameter.
@Test
public void solveMethodWithMoreSpecializedParameter() {
CompilationUnit cu = parseSample("ClassWithMethods");
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
ResolvedType stringType = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
SymbolReference<ResolvedMethodDeclaration> ref = context.solveMethod("foo4", ImmutableList.of(stringType), false, new ReflectionTypeSolver());
assertEquals(true, ref.isSolved());
assertEquals("A", ref.getCorrespondingDeclaration().declaringType().getName());
assertEquals(1, ref.getCorrespondingDeclaration().getNumberOfParams());
}
Aggregations