use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.
the class StatementContext method solveSymbolAsValue.
@Override
public Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) {
// if we're in a multiple Variable declaration line (for ex: double a=0, b=a;)
SymbolDeclarator symbolDeclarator = JavaParserFactory.getSymbolDeclarator(wrappedNode, typeSolver);
Optional<Value> symbolReference = solveWithAsValue(symbolDeclarator, name, typeSolver);
if (symbolReference.isPresent()) {
return symbolReference;
}
// we should look in all the statements preceding, treating them as SymbolDeclarators
if (requireParentNode(wrappedNode) instanceof com.github.javaparser.ast.body.MethodDeclaration) {
return getParent().solveSymbolAsValue(name, typeSolver);
}
if (requireParentNode(wrappedNode) instanceof LambdaExpr) {
return getParent().solveSymbolAsValue(name, typeSolver);
}
if (requireParentNode(wrappedNode) instanceof IfStmt) {
return getParent().solveSymbolAsValue(name, typeSolver);
}
if (!(requireParentNode(wrappedNode) instanceof NodeWithStatements)) {
return getParent().solveSymbolAsValue(name, typeSolver);
}
NodeWithStatements<?> nodeWithStmt = (NodeWithStatements<?>) requireParentNode(wrappedNode);
int position = -1;
for (int i = 0; i < nodeWithStmt.getStatements().size(); i++) {
if (nodeWithStmt.getStatements().get(i).equals(wrappedNode)) {
position = i;
}
}
if (position == -1) {
throw new RuntimeException();
}
for (int i = position - 1; i >= 0; i--) {
symbolDeclarator = JavaParserFactory.getSymbolDeclarator(nodeWithStmt.getStatements().get(i), typeSolver);
symbolReference = solveWithAsValue(symbolDeclarator, name, typeSolver);
if (symbolReference.isPresent()) {
return symbolReference;
}
}
// if nothing is found we should ask the parent context
Context parentContext = getParent();
return parentContext.solveSymbolAsValue(name, typeSolver);
}
use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.
the class Issue232 method issue232.
@Test
public void issue232() {
CompilationUnit cu = parseSample("Issue232");
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "OfDouble");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
Context context = JavaParserFactory.getContext(cls, typeSolver);
SymbolReference<ResolvedTypeDeclaration> reference = context.solveType("OfPrimitive<Double, DoubleConsumer, OfDouble>", typeSolver);
}
use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.
the class JavassistUtils method parseTypeParameters.
private static List<ResolvedType> parseTypeParameters(String signature, TypeSolver typeSolver, Context invokationContext) {
String originalSignature = signature;
if (signature.contains("<")) {
signature = signature.substring(signature.indexOf('<') + 1);
if (!signature.endsWith(">")) {
throw new IllegalArgumentException();
}
signature = signature.substring(0, signature.length() - 1);
if (signature.contains(",")) {
throw new UnsupportedOperationException();
}
if (signature.startsWith("?")) {
// TODO: check bounds
List<ResolvedType> types = new ArrayList<>();
types.add(ResolvedWildcard.UNBOUNDED);
return types;
}
List<ResolvedType> typeParameters = parseTypeParameters(signature, typeSolver, invokationContext);
if (signature.contains("<")) {
signature = signature.substring(0, signature.indexOf('<'));
}
if (signature.contains(">")) {
throw new UnsupportedOperationException();
}
ResolvedType type = new SymbolSolver(typeSolver).solveTypeUsage(signature, invokationContext);
if (type.isReferenceType() && typeParameters.size() > 0) {
type = type.asReferenceType().transformTypeParameters(tp -> typeParameters.remove(0));
}
List<ResolvedType> types = new ArrayList<>();
types.add(type);
return types;
} else {
return Collections.emptyList();
}
}
use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.
the class JavassistUtils method getMethodUsage.
static Optional<MethodUsage> getMethodUsage(CtClass ctClass, String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext) {
// TODO avoid bridge and synthetic methods
for (CtMethod method : ctClass.getDeclaredMethods()) {
if (method.getName().equals(name)) {
// TODO check typeParametersValues
MethodUsage methodUsage = new MethodUsage(new JavassistMethodDeclaration(method, typeSolver));
if (argumentsTypes.size() < methodUsage.getNoParams()) {
// this method cannot be a good candidate (except if variadic ?)
continue;
}
try {
if (method.getGenericSignature() != null) {
SignatureAttribute.MethodSignature methodSignature = SignatureAttribute.toMethodSignature(method.getGenericSignature());
List<ResolvedType> parametersOfReturnType = parseTypeParameters(methodSignature.getReturnType().toString(), typeSolver, invokationContext);
ResolvedType newReturnType = methodUsage.returnType();
// consume one parametersOfReturnType at the time
if (newReturnType.isReferenceType() && parametersOfReturnType.size() > 0) {
newReturnType = newReturnType.asReferenceType().transformTypeParameters(tp -> parametersOfReturnType.remove(0));
}
methodUsage = methodUsage.replaceReturnType(newReturnType);
}
return Optional.of(methodUsage);
} catch (BadBytecode e) {
throw new RuntimeException(e);
}
}
}
try {
CtClass superClass = ctClass.getSuperclass();
if (superClass != null) {
Optional<MethodUsage> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
if (ref.isPresent()) {
return ref;
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
try {
for (CtClass interfaze : ctClass.getInterfaces()) {
Optional<MethodUsage> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethodAsUsage(name, argumentsTypes, typeSolver, invokationContext, null);
if (ref.isPresent()) {
return ref;
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
}
return Optional.empty();
}
use of com.github.javaparser.symbolsolver.core.resolution.Context in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationContextResolutionTest method solveSymbolReferringToInterfaceInheritedInstanceField.
@Test
public void solveSymbolReferringToInterfaceInheritedInstanceField() {
CompilationUnit cu = parseSample("ClassWithSymbols");
ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
SymbolReference<? extends ResolvedValueDeclaration> ref = context.solveSymbol("o", new MemoryTypeSolver());
assertEquals(true, ref.isSolved());
assertEquals("int", ref.getCorrespondingDeclaration().getType().describe());
}
Aggregations