Search in sources :

Example 1 with SymbolReference

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

the class ReflectionMethodResolutionLogic method solveMethod.

static SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> parameterTypes, boolean staticOnly, TypeSolver typeSolver, ResolvedReferenceTypeDeclaration scopeType, Class clazz) {
    List<ResolvedMethodDeclaration> 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;
        ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
        methods.add(methodDeclaration);
    }
    for (ResolvedReferenceType ancestor : scopeType.getAncestors()) {
        SymbolReference<ResolvedMethodDeclaration> 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<ResolvedMethodDeclaration> 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 : ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) 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) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) List(java.util.List) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) 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) Optional(java.util.Optional) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) Method(java.lang.reflect.Method) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)

Example 2 with SymbolReference

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

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

the class CompilationUnitContext method solveMethod.

@Override
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> 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(ResolvedMethodDeclaration.class);
                }
                ResolvedTypeDeclaration ref = typeSolver.solveType(importString);
                SymbolReference<ResolvedMethodDeclaration> 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);
                    ResolvedTypeDeclaration ref = typeSolver.solveType(typeName);
                    SymbolReference<ResolvedMethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, true, typeSolver);
                    if (method.isSolved()) {
                        return method;
                    }
                }
            }
        }
    }
    return SymbolReference.unsolved(ResolvedMethodDeclaration.class);
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) 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) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) List(java.util.List) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) 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) JavaParserEnumDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)

Example 4 with SymbolReference

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

the class JavassistEnumDeclaration method solveMethod.

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);
    }
    return MethodResolutionLogic.findMostApplicable(candidates, name, argumentsTypes, typeSolver);
}
Also used : CtMethod(javassist.CtMethod) java.util(java.util) AccessFlag(javassist.bytecode.AccessFlag) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Predicate(java.util.function.Predicate) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) MethodUsage(com.github.javaparser.resolution.MethodUsage) AccessSpecifier(com.github.javaparser.ast.AccessSpecifier) CtClass(javassist.CtClass) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) com.github.javaparser.resolution.declarations(com.github.javaparser.resolution.declarations) Collectors(java.util.stream.Collectors) CtField(javassist.CtField) AbstractTypeDeclaration(com.github.javaparser.symbolsolver.logic.AbstractTypeDeclaration) 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) SyntheticAttribute(javassist.bytecode.SyntheticAttribute) NotFoundException(javassist.NotFoundException) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) NotFoundException(javassist.NotFoundException) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Example 5 with SymbolReference

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

the class ContextTest method resolveParameterReference.

@Test
public void resolveParameterReference() {
    CompilationUnit cu = parseSample("ReferencesToParameter");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "ReferenceToParameter");
    MethodDeclaration method1 = Navigator.demandMethod(referencesToField, "aMethod");
    NameExpr foo = Navigator.findNameExpression(method1, "foo").get();
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    SymbolReference symbolReference = symbolSolver.solveSymbol("foo", foo);
    assertEquals(true, symbolReference.isSolved());
    assertEquals("foo", symbolReference.getCorrespondingDeclaration().getName());
    assertEquals(true, symbolReference.getCorrespondingDeclaration().isParameter());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) NameExpr(com.github.javaparser.ast.expr.NameExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Aggregations

SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)23 CompilationUnit (com.github.javaparser.ast.CompilationUnit)16 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)12 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)12 Test (org.junit.Test)11 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)10 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)10 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)10 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)9 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)9 Collectors (java.util.stream.Collectors)8 MethodUsage (com.github.javaparser.resolution.MethodUsage)7 Context (com.github.javaparser.symbolsolver.core.resolution.Context)7 MethodResolutionLogic (com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic)7 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)6 SymbolSolver (com.github.javaparser.symbolsolver.resolution.SymbolSolver)6 com.github.javaparser.resolution.declarations (com.github.javaparser.resolution.declarations)5 Modifier (java.lang.reflect.Modifier)5 java.util (java.util)5 List (java.util.List)5