Search in sources :

Example 31 with Context

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);
}
Also used : Context(com.github.javaparser.symbolsolver.core.resolution.Context) SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) NodeWithStatements(com.github.javaparser.ast.nodeTypes.NodeWithStatements) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) IfStmt(com.github.javaparser.ast.stmt.IfStmt) Value(com.github.javaparser.symbolsolver.model.resolution.Value)

Example 32 with Context

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);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 33 with Context

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();
    }
}
Also used : CtMethod(javassist.CtMethod) java.util(java.util) BadBytecode(javassist.bytecode.BadBytecode) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) SignatureAttribute(javassist.bytecode.SignatureAttribute) CtClass(javassist.CtClass) Collectors(java.util.stream.Collectors) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) Context(com.github.javaparser.symbolsolver.core.resolution.Context) com.github.javaparser.resolution.types(com.github.javaparser.resolution.types) ResolvedTypeParametrizable(com.github.javaparser.resolution.declarations.ResolvedTypeParametrizable) NotFoundException(javassist.NotFoundException) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver)

Example 34 with Context

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();
}
Also used : CtMethod(javassist.CtMethod) java.util(java.util) BadBytecode(javassist.bytecode.BadBytecode) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) SignatureAttribute(javassist.bytecode.SignatureAttribute) CtClass(javassist.CtClass) Collectors(java.util.stream.Collectors) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) Context(com.github.javaparser.symbolsolver.core.resolution.Context) com.github.javaparser.resolution.types(com.github.javaparser.resolution.types) ResolvedTypeParametrizable(com.github.javaparser.resolution.declarations.ResolvedTypeParametrizable) NotFoundException(javassist.NotFoundException) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) NotFoundException(javassist.NotFoundException) BadBytecode(javassist.bytecode.BadBytecode) SignatureAttribute(javassist.bytecode.SignatureAttribute) CtClass(javassist.CtClass) MethodUsage(com.github.javaparser.resolution.MethodUsage) CtMethod(javassist.CtMethod)

Example 35 with Context

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) MemoryTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ClassOrInterfaceDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

Context (com.github.javaparser.symbolsolver.core.resolution.Context)72 CompilationUnit (com.github.javaparser.ast.CompilationUnit)65 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)63 Test (org.junit.Test)63 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)51 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)40 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)37 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)32 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)26 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)17 Value (com.github.javaparser.symbolsolver.model.resolution.Value)16 MethodUsage (com.github.javaparser.resolution.MethodUsage)14 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)11 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)10 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)9 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)7 JarTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver)7 EnumDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext)6 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)6 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)5