Search in sources :

Example 1 with MethodDeclaration

use of com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration in project javaparser by javaparser.

the class ReflectionMethodResolutionLogic method solveMethod.

static SymbolReference<MethodDeclaration> solveMethod(String name, List<Type> parameterTypes, boolean staticOnly, TypeSolver typeSolver, ReferenceTypeDeclaration scopeType, Class clazz) {
    List<MethodDeclaration> methods = new ArrayList<>();
    Predicate<Method> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
    for (Method method : clazz.getMethods()) {
        if (method.isBridge() || method.isSynthetic() || !method.getName().equals(name) || !staticOnlyCheck.test(method))
            continue;
        MethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
        methods.add(methodDeclaration);
    }
    for (ReferenceType ancestor : scopeType.getAncestors()) {
        SymbolReference<MethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(ancestor.getTypeDeclaration(), name, parameterTypes, staticOnly, typeSolver);
        if (ref.isSolved()) {
            methods.add(ref.getCorrespondingDeclaration());
        }
    }
    if (scopeType.getAncestors().isEmpty()) {
        ReferenceTypeImpl objectClass = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
        SymbolReference<MethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(objectClass.getTypeDeclaration(), name, parameterTypes, staticOnly, typeSolver);
        if (ref.isSolved()) {
            methods.add(ref.getCorrespondingDeclaration());
        }
    }
    return MethodResolutionLogic.findMostApplicable(methods, name, parameterTypes, typeSolver);
}
Also used : ReferenceType(com.github.javaparser.symbolsolver.model.typesystem.ReferenceType) TypeParameterDeclaration(com.github.javaparser.symbolsolver.model.declarations.TypeParameterDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Predicate(java.util.function.Predicate) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) MethodUsage(com.github.javaparser.symbolsolver.model.methods.MethodUsage) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Type(com.github.javaparser.symbolsolver.model.typesystem.Type) List(java.util.List) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) Context(com.github.javaparser.symbolsolver.core.resolution.Context) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Modifier(java.lang.reflect.Modifier) MethodDeclaration(com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration) ReferenceTypeDeclaration(com.github.javaparser.symbolsolver.model.declarations.ReferenceTypeDeclaration) Optional(java.util.Optional) TypeVariable(com.github.javaparser.symbolsolver.model.typesystem.TypeVariable) Method(java.lang.reflect.Method) MethodDeclaration(com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ReferenceType(com.github.javaparser.symbolsolver.model.typesystem.ReferenceType)

Example 2 with MethodDeclaration

use of com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration in project javaparser by javaparser.

the class ReflectionMethodResolutionLogic method solveMethodAsUsage.

static Optional<MethodUsage> solveMethodAsUsage(String name, List<Type> argumentsTypes, TypeSolver typeSolver, Context invokationContext, List<Type> typeParameterValues, ReferenceTypeDeclaration scopeType, Class clazz) {
    if (typeParameterValues.size() != scopeType.getTypeParameters().size()) {
        // if it is zero we are going to ignore them
        if (!scopeType.getTypeParameters().isEmpty()) {
            // Parameters not specified, so default to Object
            typeParameterValues = new ArrayList<>();
            for (int i = 0; i < scopeType.getTypeParameters().size(); i++) {
                typeParameterValues.add(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver));
            }
        }
    }
    List<MethodUsage> methods = new ArrayList<>();
    for (Method method : clazz.getMethods()) {
        if (method.getName().equals(name) && !method.isBridge() && !method.isSynthetic()) {
            MethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
            MethodUsage methodUsage = replaceParams(typeParameterValues, scopeType, methodDeclaration);
            methods.add(methodUsage);
        }
    }
    for (ReferenceType ancestor : scopeType.getAncestors()) {
        SymbolReference<MethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(ancestor.getTypeDeclaration(), name, argumentsTypes, typeSolver);
        if (ref.isSolved()) {
            MethodDeclaration correspondingDeclaration = ref.getCorrespondingDeclaration();
            MethodUsage methodUsage = replaceParams(typeParameterValues, ancestor.getTypeDeclaration(), correspondingDeclaration);
            methods.add(methodUsage);
        }
    }
    if (scopeType.getAncestors().isEmpty()) {
        ReferenceTypeImpl objectClass = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
        SymbolReference<MethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(objectClass.getTypeDeclaration(), name, argumentsTypes, typeSolver);
        if (ref.isSolved()) {
            MethodUsage usage = replaceParams(typeParameterValues, objectClass.getTypeDeclaration(), ref.getCorrespondingDeclaration());
            methods.add(usage);
        }
    }
    final List<Type> finalTypeParameterValues = typeParameterValues;
    argumentsTypes = argumentsTypes.stream().map((pt) -> {
        int i = 0;
        for (TypeParameterDeclaration tp : scopeType.getTypeParameters()) {
            pt = pt.replaceTypeVariables(tp, finalTypeParameterValues.get(i));
            i++;
        }
        return pt;
    }).collect(Collectors.toList());
    return MethodResolutionLogic.findMostApplicableUsage(methods, name, argumentsTypes, typeSolver);
}
Also used : MethodDeclaration(com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ReferenceType(com.github.javaparser.symbolsolver.model.typesystem.ReferenceType) ReferenceType(com.github.javaparser.symbolsolver.model.typesystem.ReferenceType) Type(com.github.javaparser.symbolsolver.model.typesystem.Type) TypeParameterDeclaration(com.github.javaparser.symbolsolver.model.declarations.TypeParameterDeclaration) MethodUsage(com.github.javaparser.symbolsolver.model.methods.MethodUsage)

Example 3 with MethodDeclaration

use of com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration in project javaparser by javaparser.

the class CompilationUnitContext method solveMethod.

@Override
public SymbolReference<MethodDeclaration> solveMethod(String name, List<Type> argumentsTypes, boolean staticOnly, TypeSolver typeSolver) {
    for (ImportDeclaration importDecl : wrappedNode.getImports()) {
        if (importDecl.isStatic()) {
            if (importDecl.isAsterisk()) {
                String importString = importDecl.getNameAsString();
                if (this.wrappedNode.getPackageDeclaration().isPresent() && this.wrappedNode.getPackageDeclaration().get().getName().getIdentifier().equals(packageName(importString)) && this.wrappedNode.getTypes().stream().anyMatch(it -> it.getName().getIdentifier().equals(toSimpleName(importString)))) {
                    // a lower level so this will fail
                    return SymbolReference.unsolved(MethodDeclaration.class);
                }
                com.github.javaparser.symbolsolver.model.declarations.TypeDeclaration ref = typeSolver.solveType(importString);
                SymbolReference<MethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, true, typeSolver);
                if (method.isSolved()) {
                    return method;
                }
            } else {
                String qName = importDecl.getNameAsString();
                if (qName.equals(name) || qName.endsWith("." + name)) {
                    String typeName = getType(qName);
                    com.github.javaparser.symbolsolver.model.declarations.TypeDeclaration ref = typeSolver.solveType(typeName);
                    SymbolReference<MethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, true, typeSolver);
                    if (method.isSolved()) {
                        return method;
                    }
                }
            }
        }
    }
    return SymbolReference.unsolved(MethodDeclaration.class);
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) JavaParserAnnotationDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserAnnotationDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Name(com.github.javaparser.ast.expr.Name) ValueDeclaration(com.github.javaparser.symbolsolver.model.declarations.ValueDeclaration) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) Type(com.github.javaparser.symbolsolver.model.typesystem.Type) List(java.util.List) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) MethodDeclaration(com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration) CompilationUnit(com.github.javaparser.ast.CompilationUnit) AnnotationDeclaration(com.github.javaparser.ast.body.AnnotationDeclaration) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration)

Example 4 with MethodDeclaration

use of com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration in project javaparser by javaparser.

the class MethodCallExprContext method solveMethodAsUsage.

@Override
public Optional<MethodUsage> solveMethodAsUsage(String name, List<Type> 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<TypeDeclaration> ref = solveType(className, typeSolver);
            if (ref.isSolved()) {
                SymbolReference<MethodDeclaration> 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);
                }
            }
        }
        Type typeOfScope = JavaParserFacade.get(typeSolver).getType(scope);
        // we can replace the parameter types from the scope into the typeParametersValues
        Map<TypeParameterDeclaration, Type> 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
            Type originalArgumentType = argumentsTypes.get(i);
            Type updatedArgumentType = usingParameterTypesFromScope(typeOfScope, originalArgumentType, inferredTypes);
            argumentsTypes.set(i, updatedArgumentType);
        }
        for (int i = 0; i < argumentsTypes.size(); i++) {
            Type 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);
    }
}
Also used : UnsolvedSymbolException(com.github.javaparser.symbolsolver.model.resolution.UnsolvedSymbolException) Context(com.github.javaparser.symbolsolver.core.resolution.Context) HashMap(java.util.HashMap) MethodDeclaration(com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration) NameExpr(com.github.javaparser.ast.expr.NameExpr) ArrayType(com.github.javaparser.symbolsolver.model.typesystem.ArrayType) ReferenceType(com.github.javaparser.symbolsolver.model.typesystem.ReferenceType) Type(com.github.javaparser.symbolsolver.model.typesystem.Type) Expression(com.github.javaparser.ast.expr.Expression) TypeParameterDeclaration(com.github.javaparser.symbolsolver.model.declarations.TypeParameterDeclaration) MethodUsage(com.github.javaparser.symbolsolver.model.methods.MethodUsage) TypeDeclaration(com.github.javaparser.symbolsolver.model.declarations.TypeDeclaration)

Example 5 with MethodDeclaration

use of com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration in project javaparser by javaparser.

the class AbstractTypeDeclaration method getAllMethods.

@Override
public final Set<MethodUsage> getAllMethods() {
    Set<MethodUsage> methods = new HashSet<>();
    Set<String> methodsSignatures = new HashSet<>();
    for (MethodDeclaration methodDeclaration : getDeclaredMethods()) {
        methods.add(new MethodUsage(methodDeclaration));
        methodsSignatures.add(methodDeclaration.getSignature());
    }
    for (ReferenceType ancestor : getAllAncestors()) {
        for (MethodUsage mu : ancestor.getDeclaredMethods()) {
            String signature = mu.getDeclaration().getSignature();
            if (!methodsSignatures.contains(signature)) {
                methodsSignatures.add(signature);
                methods.add(mu);
            }
        }
    }
    return methods;
}
Also used : MethodDeclaration(com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration) MethodUsage(com.github.javaparser.symbolsolver.model.methods.MethodUsage) ReferenceType(com.github.javaparser.symbolsolver.model.typesystem.ReferenceType) HashSet(java.util.HashSet)

Aggregations

MethodDeclaration (com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration)7 MethodUsage (com.github.javaparser.symbolsolver.model.methods.MethodUsage)5 ReferenceType (com.github.javaparser.symbolsolver.model.typesystem.ReferenceType)5 Type (com.github.javaparser.symbolsolver.model.typesystem.Type)5 TypeParameterDeclaration (com.github.javaparser.symbolsolver.model.declarations.TypeParameterDeclaration)4 Expression (com.github.javaparser.ast.expr.Expression)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 Context (com.github.javaparser.symbolsolver.core.resolution.Context)2 TypeDeclaration (com.github.javaparser.symbolsolver.model.declarations.TypeDeclaration)2 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)2 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)2 UnsolvedSymbolException (com.github.javaparser.symbolsolver.model.resolution.UnsolvedSymbolException)2 ArrayType (com.github.javaparser.symbolsolver.model.typesystem.ArrayType)2 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)2 MethodResolutionLogic (com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1