Search in sources :

Example 31 with TypeSolver

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

the class JavaParserFacade method solveMethodAsUsage.

public MethodUsage solveMethodAsUsage(MethodCallExpr call) {
    List<ResolvedType> params = new ArrayList<>();
    if (call.getArguments() != null) {
        for (Expression param : call.getArguments()) {
            // getTypeConcrete(Node node, boolean solveLambdas)
            try {
                params.add(getType(param, false));
            } catch (Exception e) {
                throw new RuntimeException(String.format("Error calculating the type of parameter %s of method call %s", param, call), e);
            }
        // params.add(getTypeConcrete(param, false));
        }
    }
    Context context = JavaParserFactory.getContext(call, typeSolver);
    Optional<MethodUsage> methodUsage = context.solveMethodAsUsage(call.getName().getId(), params, typeSolver);
    if (!methodUsage.isPresent()) {
        throw new RuntimeException("Method '" + call.getName() + "' cannot be resolved in context " + call + " (line: " + call.getRange().map(r -> "" + r.begin.line).orElse("??") + ") " + context + ". Parameter types: " + params);
    }
    return methodUsage.get();
}
Also used : FieldAccessContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.FieldAccessContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) FieldAccessContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.FieldAccessContext) java.util(java.util) Navigator.requireParentNode(com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode) MethodUsage(com.github.javaparser.resolution.MethodUsage) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) Level(java.util.logging.Level) com.github.javaparser.ast.body(com.github.javaparser.ast.body) Context(com.github.javaparser.symbolsolver.core.resolution.Context) ConstructorResolutionLogic(com.github.javaparser.symbolsolver.resolution.ConstructorResolutionLogic) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) com.github.javaparser.ast.type(com.github.javaparser.ast.type) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) Logger(java.util.logging.Logger) com.github.javaparser.resolution.declarations(com.github.javaparser.resolution.declarations) Collectors(java.util.stream.Collectors) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) com.github.javaparser.symbolsolver.javaparsermodel.declarations(com.github.javaparser.symbolsolver.javaparsermodel.declarations) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) com.github.javaparser.resolution.types(com.github.javaparser.resolution.types) ConsoleHandler(java.util.logging.ConsoleHandler) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver) com.github.javaparser.ast.expr(com.github.javaparser.ast.expr) MethodUsage(com.github.javaparser.resolution.MethodUsage) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException)

Example 32 with TypeSolver

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

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver 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)

Example 34 with TypeSolver

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

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

the class ContextTest method resolveReferenceUsingQualifiedName.

@Test
public void resolveReferenceUsingQualifiedName() {
    CompilationUnit cu = parseSample("Navigator2");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator");
    MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
    Parameter param = method.getParameters().get(0);
    ResolvedClassDeclaration compilationUnitDecl = mock(ResolvedClassDeclaration.class);
    when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
    when(compilationUnitDecl.getQualifiedName()).thenReturn("com.github.javaparser.ast.CompilationUnit");
    TypeSolver typeSolver = mock(TypeSolver.class);
    // when(typeSolver.tryToSolveType("java.lang.com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.unsolved(ClassDeclaration.class));
    when(typeSolver.getRoot()).thenReturn(typeSolver);
    when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
    when(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("com.github.javaparser.ast.CompilationUnit", param);
    assertEquals(true, ref.isSolved());
    assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName());
    assertEquals("com.github.javaparser.ast.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Parameter(com.github.javaparser.ast.body.Parameter) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Aggregations

TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)114 Test (org.junit.Test)89 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)76 CompilationUnit (com.github.javaparser.ast.CompilationUnit)59 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)46 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)40 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)33 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)32 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)30 MethodUsage (com.github.javaparser.resolution.MethodUsage)27 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)25 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)21 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)20 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)18 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)18 Collectors (java.util.stream.Collectors)17 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)16 ResolvedClassDeclaration (com.github.javaparser.resolution.declarations.ResolvedClassDeclaration)13 java.util (java.util)13 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)12