use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.
the class JavaParserFacade method solveMethodAsUsage.
public MethodUsage solveMethodAsUsage(MethodCallExpr call) {
List<ResolvedType> params = new ArrayList<>();
if (call.getArguments() != null) {
for (Expression param : call.getArguments()) {
// getTypeConcrete(Node node, boolean solveLambdas)
try {
params.add(getType(param, false));
} catch (Exception e) {
throw new RuntimeException(String.format("Error calculating the type of parameter %s of method call %s", param, call), e);
}
// params.add(getTypeConcrete(param, false));
}
}
Context context = JavaParserFactory.getContext(call, typeSolver);
Optional<MethodUsage> methodUsage = context.solveMethodAsUsage(call.getName().getId(), params, typeSolver);
if (!methodUsage.isPresent()) {
throw new RuntimeException("Method '" + call.getName() + "' cannot be resolved in context " + call + " (line: " + call.getRange().map(r -> "" + r.begin.line).orElse("??") + ") " + context + ". Parameter types: " + params);
}
return methodUsage.get();
}
use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.
the class MethodCallExprContext method solveMethodAsUsage.
@Override
public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver) {
if (wrappedNode.getScope().isPresent()) {
Expression scope = wrappedNode.getScope().get();
// Consider static method calls
if (scope instanceof NameExpr) {
String className = ((NameExpr) scope).getName().getId();
SymbolReference<ResolvedTypeDeclaration> ref = solveType(className, typeSolver);
if (ref.isSolved()) {
SymbolReference<ResolvedMethodDeclaration> m = MethodResolutionLogic.solveMethodInType(ref.getCorrespondingDeclaration(), name, argumentsTypes, typeSolver);
if (m.isSolved()) {
MethodUsage methodUsage = new MethodUsage(m.getCorrespondingDeclaration());
methodUsage = resolveMethodTypeParametersFromExplicitList(typeSolver, methodUsage);
methodUsage = resolveMethodTypeParameters(methodUsage, argumentsTypes);
return Optional.of(methodUsage);
} else {
throw new UnsolvedSymbolException(ref.getCorrespondingDeclaration().toString(), "Method '" + name + "' with parameterTypes " + argumentsTypes);
}
}
}
ResolvedType typeOfScope = JavaParserFacade.get(typeSolver).getType(scope);
// we can replace the parameter types from the scope into the typeParametersValues
Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>();
for (int i = 0; i < argumentsTypes.size(); i++) {
// by replacing types I can also find new equivalences
// for example if I replace T=U with String because I know that T=String I can derive that also U equal String
ResolvedType originalArgumentType = argumentsTypes.get(i);
ResolvedType updatedArgumentType = usingParameterTypesFromScope(typeOfScope, originalArgumentType, inferredTypes);
argumentsTypes.set(i, updatedArgumentType);
}
for (int i = 0; i < argumentsTypes.size(); i++) {
ResolvedType updatedArgumentType = applyInferredTypes(argumentsTypes.get(i), inferredTypes);
argumentsTypes.set(i, updatedArgumentType);
}
return solveMethodAsUsage(typeOfScope, name, argumentsTypes, typeSolver, this);
} else {
Context parentContext = getParent();
while (parentContext instanceof MethodCallExprContext) {
parentContext = parentContext.getParent();
}
return parentContext.solveMethodAsUsage(name, argumentsTypes, typeSolver);
}
}
use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationContextResolutionTest method solveSymbolAsValueReferringToDeclaredInstanceField.
@Test
public void solveSymbolAsValueReferringToDeclaredInstanceField() {
CompilationUnit cu = parseSample("ClassWithSymbols");
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
Optional<Value> ref = context.solveSymbolAsValue("i", new MemoryTypeSolver());
assertEquals(true, ref.isPresent());
assertEquals("int", ref.get().getType().describe());
}
use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationContextResolutionTest method solveSymbolAsValueReferringToUnknownElement.
@Test
public void solveSymbolAsValueReferringToUnknownElement() {
CompilationUnit cu = parseSample("ClassWithSymbols");
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
Optional<Value> ref = context.solveSymbolAsValue("zzz", new MemoryTypeSolver());
assertEquals(false, ref.isPresent());
}
use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationContextResolutionTest method solveTypeRefToQualifiedInternalOfInternalClass.
@Test
public void solveTypeRefToQualifiedInternalOfInternalClass() {
CompilationUnit cu = parseSample("ClassWithTypes");
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
SymbolReference<ResolvedTypeDeclaration> ref = context.solveType("B.C", new MemoryTypeSolver());
assertEquals(true, ref.isSolved());
}
Aggregations