Search in sources :

Example 71 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType 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 72 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class SwitchEntryContext method solveSymbol.

@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
    SwitchStmt switchStmt = (SwitchStmt) requireParentNode(wrappedNode);
    ResolvedType type = JavaParserFacade.get(typeSolver).getType(switchStmt.getSelector());
    if (type.isReferenceType() && type.asReferenceType().getTypeDeclaration().isEnum()) {
        if (type instanceof ReferenceTypeImpl) {
            ReferenceTypeImpl typeUsageOfTypeDeclaration = (ReferenceTypeImpl) type;
            if (typeUsageOfTypeDeclaration.getTypeDeclaration().hasField(name)) {
                return SymbolReference.solved(typeUsageOfTypeDeclaration.getTypeDeclaration().getField(name));
            }
        } else {
            throw new UnsupportedOperationException();
        }
    }
    // look for declaration in other switch statements
    for (SwitchEntryStmt seStmt : switchStmt.getEntries()) {
        if (!seStmt.equals(wrappedNode)) {
            for (Statement stmt : seStmt.getStatements()) {
                SymbolDeclarator symbolDeclarator = JavaParserFactory.getSymbolDeclarator(stmt, typeSolver);
                SymbolReference<? extends ResolvedValueDeclaration> symbolReference = solveWith(symbolDeclarator, name);
                if (symbolReference.isSolved()) {
                    return symbolReference;
                }
            }
        }
    }
    return getParent().solveSymbol(name, typeSolver);
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) Statement(com.github.javaparser.ast.stmt.Statement) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 73 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class JavassistClassDeclaration method solveMethod.

@Deprecated
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) {
    List<ResolvedMethodDeclaration> candidates = new ArrayList<>();
    Predicate<CtMethod> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
    for (CtMethod method : ctClass.getDeclaredMethods()) {
        boolean isSynthetic = method.getMethodInfo().getAttribute(SyntheticAttribute.tag) != null;
        boolean isNotBridge = (method.getMethodInfo().getAccessFlags() & AccessFlag.BRIDGE) == 0;
        if (method.getName().equals(name) && !isSynthetic && isNotBridge && staticOnlyCheck.test(method)) {
            candidates.add(new JavassistMethodDeclaration(method, typeSolver));
        }
    }
    try {
        CtClass superClass = ctClass.getSuperclass();
        if (superClass != null) {
            SymbolReference<ResolvedMethodDeclaration> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethod(name, argumentsTypes, staticOnly);
            if (ref.isSolved()) {
                candidates.add(ref.getCorrespondingDeclaration());
            }
        }
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    try {
        for (CtClass interfaze : ctClass.getInterfaces()) {
            SymbolReference<ResolvedMethodDeclaration> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethod(name, argumentsTypes, staticOnly);
            if (ref.isSolved()) {
                candidates.add(ref.getCorrespondingDeclaration());
            }
        }
    } catch (NotFoundException e) {
        throw new RuntimeException(e);
    }
    return MethodResolutionLogic.findMostApplicable(candidates, name, argumentsTypes, typeSolver);
}
Also used : CtMethod(javassist.CtMethod) java.util(java.util) AccessFlag(javassist.bytecode.AccessFlag) BadBytecode(javassist.bytecode.BadBytecode) MethodUsage(com.github.javaparser.resolution.MethodUsage) AccessSpecifier(com.github.javaparser.ast.AccessSpecifier) AbstractClassDeclaration(com.github.javaparser.symbolsolver.logic.AbstractClassDeclaration) CtClass(javassist.CtClass) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) Context(com.github.javaparser.symbolsolver.core.resolution.Context) NotFoundException(javassist.NotFoundException) Node(com.github.javaparser.ast.Node) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Predicate(java.util.function.Predicate) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) SignatureAttribute(javassist.bytecode.SignatureAttribute) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) com.github.javaparser.resolution.declarations(com.github.javaparser.resolution.declarations) Collectors(java.util.stream.Collectors) LambdaArgumentTypePlaceholder(com.github.javaparser.symbolsolver.javaparsermodel.LambdaArgumentTypePlaceholder) CtField(javassist.CtField) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Modifier(java.lang.reflect.Modifier) SyntheticAttribute(javassist.bytecode.SyntheticAttribute) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) NotFoundException(javassist.NotFoundException) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Example 74 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class SourceFileInfoExtractor method solve.

private void solve(Node node) {
    if (node instanceof ClassOrInterfaceDeclaration) {
        solveTypeDecl((ClassOrInterfaceDeclaration) node);
    } else if (node instanceof Expression) {
        if ((requireParentNode(node) instanceof ImportDeclaration) || (requireParentNode(node) instanceof Expression) || (requireParentNode(node) instanceof MethodDeclaration) || (requireParentNode(node) instanceof PackageDeclaration)) {
        // skip
        } else if ((requireParentNode(node) instanceof Statement) || (requireParentNode(node) instanceof VariableDeclarator)) {
            try {
                ResolvedType ref = JavaParserFacade.get(typeSolver).getType(node);
                out.println("  Line " + node.getRange().get().begin.line + ") " + node + " ==> " + ref.describe());
                ok++;
            } catch (UnsupportedOperationException upe) {
                unsupported++;
                err.println(upe.getMessage());
                throw upe;
            } catch (RuntimeException re) {
                ko++;
                err.println(re.getMessage());
                throw re;
            }
        }
    }
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Statement(com.github.javaparser.ast.stmt.Statement) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 75 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class JavaParserSymbolDeclaration method getType.

@Override
public ResolvedType getType() {
    if (wrappedNode instanceof Parameter) {
        Parameter parameter = (Parameter) wrappedNode;
        if (requireParentNode(wrappedNode) instanceof LambdaExpr) {
            int pos = getParamPos(parameter);
            ResolvedType lambdaType = JavaParserFacade.get(typeSolver).getType(requireParentNode(wrappedNode));
            // MethodDeclaration methodCalled = JavaParserFacade.get(typeSolver).solve()
            throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
        } else {
            final ResolvedType rawType;
            if (parameter.getType() instanceof com.github.javaparser.ast.type.PrimitiveType) {
                rawType = ResolvedPrimitiveType.byName(((com.github.javaparser.ast.type.PrimitiveType) parameter.getType()).getType().name());
            } else {
                rawType = JavaParserFacade.get(typeSolver).convertToUsage(parameter.getType(), wrappedNode);
            }
            if (parameter.isVarArgs()) {
                return new ResolvedArrayType(rawType);
            }
            return rawType;
        }
    } else if (wrappedNode instanceof VariableDeclarator) {
        VariableDeclarator variableDeclarator = (VariableDeclarator) wrappedNode;
        if (requireParentNode(wrappedNode) instanceof VariableDeclarationExpr) {
            return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
        } else if (requireParentNode(wrappedNode) instanceof FieldDeclaration) {
            return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), JavaParserFactory.getContext(wrappedNode, typeSolver));
        }
    }
    throw new UnsupportedOperationException(wrappedNode.getClass().getCanonicalName());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) Parameter(com.github.javaparser.ast.body.Parameter) ResolvedPrimitiveType(com.github.javaparser.resolution.types.ResolvedPrimitiveType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Aggregations

ResolvedType (com.github.javaparser.resolution.types.ResolvedType)119 Test (org.junit.Test)78 CompilationUnit (com.github.javaparser.ast.CompilationUnit)68 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)58 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)41 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)39 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)32 Expression (com.github.javaparser.ast.expr.Expression)27 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)22 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)21 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)20 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)18 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)18 MethodUsage (com.github.javaparser.resolution.MethodUsage)17 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)15 Context (com.github.javaparser.symbolsolver.core.resolution.Context)15 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)14 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)10 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)10 Collectors (java.util.stream.Collectors)10