Search in sources :

Example 1 with ResolvedReferenceTypeDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration in project javaparser by javaparser.

the class Issue1364 method setup.

@Before
public void setup() {
    ClassOrInterfaceDeclaration fakeObject = new ClassOrInterfaceDeclaration();
    fakeObject.setName(new SimpleName("java.lang.Object"));
    TypeSolver typeSolver = new TypeSolver() {

        @Override
        public TypeSolver getParent() {
            return null;
        }

        @Override
        public void setParent(TypeSolver parent) {
        }

        @Override
        public SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveType(String name) {
            if ("java.lang.Object".equals(name)) {
                // custom handling
                return SymbolReference.solved(new JavaParserClassDeclaration(fakeObject, this));
            }
            return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class);
        }
    };
    ParserConfiguration config = new ParserConfiguration();
    config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
    javaParser = new JavaParser(config);
}
Also used : JavaParser(com.github.javaparser.JavaParser) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) JavaParserClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) SimpleName(com.github.javaparser.ast.expr.SimpleName) ParserConfiguration(com.github.javaparser.ParserConfiguration) Before(org.junit.Before)

Example 2 with ResolvedReferenceTypeDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration 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 3 with ResolvedReferenceTypeDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration in project javaparser by javaparser.

the class SymbolSolver method solveTypeUsage.

public ResolvedType solveTypeUsage(String name, Context context) {
    Optional<ResolvedType> genericType = context.solveGenericType(name, typeSolver);
    if (genericType.isPresent()) {
        return genericType.get();
    }
    ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(name);
    return new ReferenceTypeImpl(typeDeclaration, typeSolver);
}
Also used : ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 4 with ResolvedReferenceTypeDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration in project javaparser by javaparser.

the class ReferenceTypeImpl method getAllAncestors.

public List<ResolvedReferenceType> getAllAncestors() {
    // We need to go through the inheritance line and propagate the type parametes
    List<ResolvedReferenceType> ancestors = typeDeclaration.getAllAncestors();
    ancestors = ancestors.stream().map(a -> typeParametersMap().replaceAll(a).asReferenceType()).collect(Collectors.toList());
    // Avoid repetitions of Object
    ancestors.removeIf(a -> a.getQualifiedName().equals(Object.class.getCanonicalName()));
    ResolvedReferenceTypeDeclaration objectType = typeSolver.solveType(Object.class.getCanonicalName());
    ResolvedReferenceType objectRef = create(objectType);
    ancestors.add(objectRef);
    return ancestors;
}
Also used : ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType)

Example 5 with ResolvedReferenceTypeDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration in project javaparser by javaparser.

the class AbstractJavaParserContext method findTypeDeclarations.

protected Collection<ResolvedReferenceTypeDeclaration> findTypeDeclarations(Optional<Expression> optScope, TypeSolver typeSolver) {
    if (optScope.isPresent()) {
        Expression scope = optScope.get();
        // consider static methods
        if (scope instanceof NameExpr) {
            NameExpr scopeAsName = (NameExpr) scope;
            SymbolReference<ResolvedTypeDeclaration> symbolReference = this.solveType(scopeAsName.getName().getId(), typeSolver);
            if (symbolReference.isSolved() && symbolReference.getCorrespondingDeclaration().isType()) {
                return singletonList(symbolReference.getCorrespondingDeclaration().asReferenceType());
            }
        }
        ResolvedType typeOfScope;
        try {
            typeOfScope = JavaParserFacade.get(typeSolver).getType(scope);
        } catch (Exception e) {
            throw new RuntimeException("Issue calculating the type of the scope of " + this, e);
        }
        if (typeOfScope.isWildcard()) {
            if (typeOfScope.asWildcard().isExtends() || typeOfScope.asWildcard().isSuper()) {
                return singletonList(typeOfScope.asWildcard().getBoundedType().asReferenceType().getTypeDeclaration());
            } else {
                return singletonList(new ReflectionClassDeclaration(Object.class, typeSolver).asReferenceType());
            }
        } else if (typeOfScope.isArray()) {
            // method call on array are Object methods
            return singletonList(new ReflectionClassDeclaration(Object.class, typeSolver).asReferenceType());
        } else if (typeOfScope.isTypeVariable()) {
            Collection<ResolvedReferenceTypeDeclaration> result = new ArrayList<>();
            for (ResolvedTypeParameterDeclaration.Bound bound : typeOfScope.asTypeParameter().getBounds()) {
                result.add(bound.getType().asReferenceType().getTypeDeclaration());
            }
            return result;
        } else if (typeOfScope.isConstraint()) {
            return singletonList(typeOfScope.asConstraintType().getBound().asReferenceType().getTypeDeclaration());
        }
        return singletonList(typeOfScope.asReferenceType().getTypeDeclaration());
    }
    ResolvedType typeOfScope = JavaParserFacade.get(typeSolver).getTypeOfThisIn(wrappedNode);
    return singletonList(typeOfScope.asReferenceType().getTypeDeclaration());
}
Also used : ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) NameExpr(com.github.javaparser.ast.expr.NameExpr) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) Expression(com.github.javaparser.ast.expr.Expression) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Aggregations

ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)14 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)5 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)5 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)5 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)5 Test (org.junit.Test)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)4 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)4 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)3 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)3 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)3 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 MethodUsage (com.github.javaparser.resolution.MethodUsage)2 ResolvedDeclaration (com.github.javaparser.resolution.declarations.ResolvedDeclaration)2 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)2 ResolvedTypeVariable (com.github.javaparser.resolution.types.ResolvedTypeVariable)2 Context (com.github.javaparser.symbolsolver.core.resolution.Context)2 ArrayList (java.util.ArrayList)2 JavaParser (com.github.javaparser.JavaParser)1