Search in sources :

Example 16 with ResolvedType

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

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

Example 18 with ResolvedType

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

the class ReflectionInterfaceDeclaration method solveMethodAsUsage.

public Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> parameterTypes, TypeSolver typeSolver, Context invokationContext, List<ResolvedType> typeParameterValues) {
    Optional<MethodUsage> res = ReflectionMethodResolutionLogic.solveMethodAsUsage(name, parameterTypes, typeSolver, invokationContext, typeParameterValues, this, clazz);
    if (res.isPresent()) {
        // We have to replace method type typeParametersValues here
        InferenceContext inferenceContext = new InferenceContext(MyObjectProvider.INSTANCE);
        MethodUsage methodUsage = res.get();
        int i = 0;
        List<ResolvedType> parameters = new LinkedList<>();
        for (ResolvedType actualType : parameterTypes) {
            ResolvedType formalType = methodUsage.getParamType(i);
            // We need to replace the class type typeParametersValues (while we derive the method ones)
            parameters.add(inferenceContext.addPair(formalType, actualType));
            i++;
        }
        try {
            ResolvedType returnType = inferenceContext.addSingle(methodUsage.returnType());
            for (int j = 0; j < parameters.size(); j++) {
                methodUsage = methodUsage.replaceParamType(j, inferenceContext.resolve(parameters.get(j)));
            }
            methodUsage = methodUsage.replaceReturnType(inferenceContext.resolve(returnType));
            return Optional.of(methodUsage);
        } catch (ConfilictingGenericTypesException e) {
            return Optional.empty();
        }
    } else {
        return res;
    }
}
Also used : InferenceContext(com.github.javaparser.symbolsolver.logic.InferenceContext) ConfilictingGenericTypesException(com.github.javaparser.symbolsolver.logic.ConfilictingGenericTypesException) MethodUsage(com.github.javaparser.resolution.MethodUsage) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 19 with ResolvedType

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

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

the class ReflectionClassAdapter method getInterfaces.

public List<ResolvedReferenceType> getInterfaces() {
    List<ResolvedReferenceType> interfaces = new ArrayList<>();
    for (java.lang.reflect.Type superInterface : clazz.getGenericInterfaces()) {
        if (superInterface instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) superInterface;
            List<ResolvedType> typeParameters = Arrays.stream(parameterizedType.getActualTypeArguments()).map((t) -> ReflectionFactory.typeUsageFor(t, typeSolver)).collect(Collectors.toList());
            interfaces.add(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration((Class<?>) ((ParameterizedType) superInterface).getRawType(), typeSolver), typeParameters, typeSolver));
        } else {
            interfaces.add(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration((Class<?>) superInterface, typeSolver), typeSolver));
        }
    }
    return interfaces;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) NullType(com.github.javaparser.symbolsolver.model.typesystem.NullType) java.util(java.util) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) TypeVariable(java.lang.reflect.TypeVariable) 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) FunctionalInterfaceLogic(com.github.javaparser.symbolsolver.logic.FunctionalInterfaceLogic) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) ParameterizedType(java.lang.reflect.ParameterizedType) Annotation(java.lang.annotation.Annotation) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) 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