Search in sources :

Example 16 with Value

use of com.github.javaparser.symbolsolver.model.resolution.Value in project javaparser by javaparser.

the class CatchClauseContext method solveSymbolAsValue.

@Override
public final Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) {
    SymbolDeclarator sb = JavaParserFactory.getSymbolDeclarator(wrappedNode.getParameter(), typeSolver);
    Optional<Value> symbolReference = solveWithAsValue(sb, name, typeSolver);
    if (symbolReference.isPresent()) {
        // Perform parameter type substitution as needed
        return symbolReference;
    }
    // if nothing is found we should ask the parent context
    return getParent().solveSymbolAsValue(name, typeSolver);
}
Also used : SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) Value(com.github.javaparser.symbolsolver.model.resolution.Value)

Example 17 with Value

use of com.github.javaparser.symbolsolver.model.resolution.Value 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 18 with Value

use of com.github.javaparser.symbolsolver.model.resolution.Value in project javaparser by javaparser.

the class LambdaExprContext method solveSymbolAsValue.

@Override
public Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) {
    for (Parameter parameter : wrappedNode.getParameters()) {
        SymbolDeclarator sb = JavaParserFactory.getSymbolDeclarator(parameter, typeSolver);
        int index = 0;
        for (ResolvedValueDeclaration decl : sb.getSymbolDeclarations()) {
            if (decl.getName().equals(name)) {
                if (requireParentNode(wrappedNode) instanceof MethodCallExpr) {
                    MethodCallExpr methodCallExpr = (MethodCallExpr) requireParentNode(wrappedNode);
                    MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(methodCallExpr);
                    int i = pos(methodCallExpr, wrappedNode);
                    ResolvedType lambdaType = methodUsage.getParamTypes().get(i);
                    // Get the functional method in order for us to resolve it's type arguments properly
                    Optional<MethodUsage> functionalMethodOpt = FunctionalInterfaceLogic.getFunctionalMethod(lambdaType);
                    if (functionalMethodOpt.isPresent()) {
                        MethodUsage functionalMethod = functionalMethodOpt.get();
                        InferenceContext inferenceContext = new InferenceContext(MyObjectProvider.INSTANCE);
                        // Resolve each type variable of the lambda, and use this later to infer the type of each
                        // implicit parameter
                        inferenceContext.addPair(lambdaType, new ReferenceTypeImpl(lambdaType.asReferenceType().getTypeDeclaration(), typeSolver));
                        // Find the position of this lambda argument
                        boolean found = false;
                        int lambdaParamIndex;
                        for (lambdaParamIndex = 0; lambdaParamIndex < wrappedNode.getParameters().size(); lambdaParamIndex++) {
                            if (wrappedNode.getParameter(lambdaParamIndex).getName().getIdentifier().equals(name)) {
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            return Optional.empty();
                        }
                        // Now resolve the argument type using the inference context
                        ResolvedType argType = inferenceContext.resolve(inferenceContext.addSingle(functionalMethod.getParamType(lambdaParamIndex)));
                        ResolvedLambdaConstraintType conType;
                        if (argType.isWildcard()) {
                            conType = ResolvedLambdaConstraintType.bound(argType.asWildcard().getBoundedType());
                        } else {
                            conType = ResolvedLambdaConstraintType.bound(argType);
                        }
                        Value value = new Value(conType, name);
                        return Optional.of(value);
                    } else {
                        return Optional.empty();
                    }
                } else if (requireParentNode(wrappedNode) instanceof VariableDeclarator) {
                    VariableDeclarator variableDeclarator = (VariableDeclarator) requireParentNode(wrappedNode);
                    ResolvedType t = JavaParserFacade.get(typeSolver).convertToUsageVariableType(variableDeclarator);
                    Optional<MethodUsage> functionalMethod = FunctionalInterfaceLogic.getFunctionalMethod(t);
                    if (functionalMethod.isPresent()) {
                        ResolvedType lambdaType = functionalMethod.get().getParamType(index);
                        // Replace parameter from declarator
                        Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>();
                        if (lambdaType.isReferenceType()) {
                            for (com.github.javaparser.utils.Pair<ResolvedTypeParameterDeclaration, ResolvedType> entry : lambdaType.asReferenceType().getTypeParametersMap()) {
                                if (entry.b.isTypeVariable() && entry.b.asTypeParameter().declaredOnType()) {
                                    ResolvedType ot = t.asReferenceType().typeParametersMap().getValue(entry.a);
                                    lambdaType = lambdaType.replaceTypeVariables(entry.a, ot, inferredTypes);
                                }
                            }
                        } else if (lambdaType.isTypeVariable() && lambdaType.asTypeParameter().declaredOnType()) {
                            lambdaType = t.asReferenceType().typeParametersMap().getValue(lambdaType.asTypeParameter());
                        }
                        Value value = new Value(lambdaType, name);
                        return Optional.of(value);
                    } else {
                        throw new UnsupportedOperationException();
                    }
                } else {
                    throw new UnsupportedOperationException();
                }
            }
            index++;
        }
    }
    // if nothing is found we should ask the parent context
    return getParent().solveSymbolAsValue(name, typeSolver);
}
Also used : SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) InferenceContext(com.github.javaparser.symbolsolver.logic.InferenceContext) ResolvedLambdaConstraintType(com.github.javaparser.resolution.types.ResolvedLambdaConstraintType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Value(com.github.javaparser.symbolsolver.model.resolution.Value) Parameter(com.github.javaparser.ast.body.Parameter) MethodUsage(com.github.javaparser.resolution.MethodUsage) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 19 with Value

use of com.github.javaparser.symbolsolver.model.resolution.Value in project javaparser by javaparser.

the class ClassOrInterfaceDeclarationContextResolutionTest method solveSymbolAsValueReferringToInheritedStaticField.

@Test
public void solveSymbolAsValueReferringToInheritedStaticField() {
    CompilationUnit cu = parseSample("ClassWithSymbols");
    ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
    Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("m", new MemoryTypeSolver());
    assertEquals(true, ref.isPresent());
    assertEquals("char", ref.get().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) Value(com.github.javaparser.symbolsolver.model.resolution.Value) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 20 with Value

use of com.github.javaparser.symbolsolver.model.resolution.Value in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveSymbolAsValueReferringToStaticallyImportedUsingAsteriskValue.

@Test
public void solveSymbolAsValueReferringToStaticallyImportedUsingAsteriskValue() throws ParseException, IOException {
    CompilationUnit cu = parseSample("CompilationUnitSymbols");
    Context context = new CompilationUnitContext(cu, typeSolver);
    CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    typeSolver.add(new ReflectionTypeSolver());
    typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    Optional<Value> ref = context.solveSymbolAsValue("err", typeSolver);
    assertEquals(true, ref.isPresent());
    assertEquals("java.io.PrintStream", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

Value (com.github.javaparser.symbolsolver.model.resolution.Value)26 CompilationUnit (com.github.javaparser.ast.CompilationUnit)21 Test (org.junit.Test)20 Context (com.github.javaparser.symbolsolver.core.resolution.Context)16 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)15 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)12 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)11 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)9 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)9 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)8 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)7 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)5 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)4 SymbolDeclarator (com.github.javaparser.symbolsolver.resolution.SymbolDeclarator)4 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)4 Parameter (com.github.javaparser.ast.body.Parameter)3 EnumDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.EnumDeclarationContext)3 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)2