Search in sources :

Example 1 with MethodComparator

use of com.github.javaparser.symbolsolver.reflectionmodel.comparators.MethodComparator in project javaparser by javaparser.

the class ReflectionClassDeclaration method solveMethod.

@Deprecated
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) {
    List<ResolvedMethodDeclaration> methods = new ArrayList<>();
    Predicate<Method> staticFilter = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
    for (Method method : Arrays.stream(clazz.getDeclaredMethods()).filter((m) -> m.getName().equals(name)).filter(staticFilter).sorted(new MethodComparator()).collect(Collectors.toList())) {
        if (method.isBridge() || method.isSynthetic())
            continue;
        ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
        methods.add(methodDeclaration);
    }
    if (getSuperClass() != null) {
        ResolvedClassDeclaration superClass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration();
        SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(superClass, name, argumentsTypes, staticOnly, typeSolver);
        if (ref.isSolved()) {
            methods.add(ref.getCorrespondingDeclaration());
        }
    }
    for (ResolvedReferenceType interfaceDeclaration : getInterfaces()) {
        SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(interfaceDeclaration.getTypeDeclaration(), name, argumentsTypes, staticOnly, typeSolver);
        if (ref.isSolved()) {
            methods.add(ref.getCorrespondingDeclaration());
        }
    }
    return MethodResolutionLogic.findMostApplicable(methods, name, argumentsTypes, typeSolver);
}
Also used : java.util(java.util) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Predicate(java.util.function.Predicate) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) AccessSpecifier(com.github.javaparser.ast.AccessSpecifier) AbstractClassDeclaration(com.github.javaparser.symbolsolver.logic.AbstractClassDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Field(java.lang.reflect.Field) com.github.javaparser.resolution.declarations(com.github.javaparser.resolution.declarations) Collectors(java.util.stream.Collectors) LambdaArgumentTypePlaceholder(com.github.javaparser.symbolsolver.javaparsermodel.LambdaArgumentTypePlaceholder) ContextHelper(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ContextHelper) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) 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) MethodComparator(com.github.javaparser.symbolsolver.reflectionmodel.comparators.MethodComparator) Method(java.lang.reflect.Method) Node(com.github.javaparser.ast.Node) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) MethodComparator(com.github.javaparser.symbolsolver.reflectionmodel.comparators.MethodComparator) Method(java.lang.reflect.Method)

Example 2 with MethodComparator

use of com.github.javaparser.symbolsolver.reflectionmodel.comparators.MethodComparator in project javaparser by javaparser.

the class ReflectionClassDeclaration method solveMethodAsUsage.

public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext, List<ResolvedType> typeParameterValues) {
    List<MethodUsage> methods = new ArrayList<>();
    for (Method method : Arrays.stream(clazz.getDeclaredMethods()).filter((m) -> m.getName().equals(name)).sorted(new MethodComparator()).collect(Collectors.toList())) {
        if (method.isBridge() || method.isSynthetic())
            continue;
        ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
        MethodUsage methodUsage = new MethodUsage(methodDeclaration);
        for (int i = 0; i < getTypeParameters().size() && i < typeParameterValues.size(); i++) {
            ResolvedTypeParameterDeclaration tpToReplace = getTypeParameters().get(i);
            ResolvedType newValue = typeParameterValues.get(i);
            methodUsage = methodUsage.replaceTypeParameter(tpToReplace, newValue);
        }
        methods.add(methodUsage);
    }
    if (getSuperClass() != null) {
        ResolvedClassDeclaration superClass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration();
        Optional<MethodUsage> ref = ContextHelper.solveMethodAsUsage(superClass, name, argumentsTypes, typeSolver, invokationContext, typeParameterValues);
        if (ref.isPresent()) {
            methods.add(ref.get());
        }
    }
    for (ResolvedReferenceType interfaceDeclaration : getInterfaces()) {
        Optional<MethodUsage> ref = ContextHelper.solveMethodAsUsage(interfaceDeclaration.getTypeDeclaration(), name, argumentsTypes, typeSolver, invokationContext, typeParameterValues);
        if (ref.isPresent()) {
            methods.add(ref.get());
        }
    }
    Optional<MethodUsage> ref = MethodResolutionLogic.findMostApplicableUsage(methods, name, argumentsTypes, typeSolver);
    return ref;
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Method(java.lang.reflect.Method) MethodComparator(com.github.javaparser.symbolsolver.reflectionmodel.comparators.MethodComparator) MethodUsage(com.github.javaparser.resolution.MethodUsage) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Aggregations

MethodUsage (com.github.javaparser.resolution.MethodUsage)2 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)2 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)2 MethodComparator (com.github.javaparser.symbolsolver.reflectionmodel.comparators.MethodComparator)2 Method (java.lang.reflect.Method)2 AccessSpecifier (com.github.javaparser.ast.AccessSpecifier)1 Node (com.github.javaparser.ast.Node)1 com.github.javaparser.resolution.declarations (com.github.javaparser.resolution.declarations)1 Context (com.github.javaparser.symbolsolver.core.resolution.Context)1 LambdaArgumentTypePlaceholder (com.github.javaparser.symbolsolver.javaparsermodel.LambdaArgumentTypePlaceholder)1 ContextHelper (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ContextHelper)1 AbstractClassDeclaration (com.github.javaparser.symbolsolver.logic.AbstractClassDeclaration)1 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)1 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)1 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)1 MethodResolutionLogic (com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic)1 Field (java.lang.reflect.Field)1 Modifier (java.lang.reflect.Modifier)1 java.util (java.util)1 Predicate (java.util.function.Predicate)1