Search in sources :

Example 66 with TypeSolver

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

the class ConstructorResolutionLogic method findMostApplicable.

public static SymbolReference<ResolvedConstructorDeclaration> findMostApplicable(List<ResolvedConstructorDeclaration> constructors, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, boolean wildcardTolerance) {
    List<ResolvedConstructorDeclaration> applicableConstructors = constructors.stream().filter((m) -> isApplicable(m, argumentsTypes, typeSolver, wildcardTolerance)).collect(Collectors.toList());
    if (applicableConstructors.isEmpty()) {
        return SymbolReference.unsolved(ResolvedConstructorDeclaration.class);
    }
    if (applicableConstructors.size() == 1) {
        return SymbolReference.solved(applicableConstructors.get(0));
    } else {
        ResolvedConstructorDeclaration winningCandidate = applicableConstructors.get(0);
        ResolvedConstructorDeclaration other = null;
        boolean possibleAmbiguity = false;
        for (int i = 1; i < applicableConstructors.size(); i++) {
            other = applicableConstructors.get(i);
            if (isMoreSpecific(winningCandidate, other, typeSolver)) {
                possibleAmbiguity = false;
            } else if (isMoreSpecific(other, winningCandidate, typeSolver)) {
                possibleAmbiguity = false;
                winningCandidate = other;
            } else {
                if (winningCandidate.declaringType().getQualifiedName().equals(other.declaringType().getQualifiedName())) {
                    possibleAmbiguity = true;
                } else {
                // we expect the methods to be ordered such that inherited methods are later in the list
                }
            }
        }
        if (possibleAmbiguity) {
            // pick the first exact match if it exists
            if (!MethodResolutionLogic.isExactMatch(winningCandidate, argumentsTypes)) {
                if (MethodResolutionLogic.isExactMatch(other, argumentsTypes)) {
                    winningCandidate = other;
                } else {
                    throw new MethodAmbiguityException("Ambiguous constructor call: cannot find a most applicable constructor: " + winningCandidate + ", " + other);
                }
            }
        }
        return SymbolReference.solved(winningCandidate);
    }
}
Also used : ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) List(java.util.List) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodAmbiguityException(com.github.javaparser.resolution.MethodAmbiguityException) ResolvedConstructorDeclaration(com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration) Map(java.util.Map) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) MethodAmbiguityException(com.github.javaparser.resolution.MethodAmbiguityException) ResolvedConstructorDeclaration(com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration)

Example 67 with TypeSolver

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

the class Issue232 method issue232.

@Test
public void issue232() {
    CompilationUnit cu = parseSample("Issue232");
    ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "OfDouble");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    Context context = JavaParserFactory.getContext(cls, typeSolver);
    SymbolReference<ResolvedTypeDeclaration> reference = context.solveType("OfPrimitive<Double, DoubleConsumer, OfDouble>", typeSolver);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 68 with TypeSolver

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

the class Issue251 method testSolveStaticallyImportedMemberType.

@Test
public void testSolveStaticallyImportedMemberType() {
    CompilationUnit cu = parseSample("Issue251");
    ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Main");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodDeclaration m = Navigator.demandMethod(cls, "bar");
    ExpressionStmt stmt = (ExpressionStmt) m.getBody().get().getStatements().get(1);
    MethodCallExpr expression = (MethodCallExpr) stmt.getExpression();
    Assert.assertNotNull(javaParserFacade.solve(expression));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 69 with TypeSolver

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

the class JavassistInterfaceDeclaration method solveMethod.

@Deprecated
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);
    }
    try {
        for (CtClass interfaze : ctClass.getInterfaces()) {
            SymbolReference<ResolvedMethodDeclaration> ref = new JavassistInterfaceDeclaration(interfaze, 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) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) 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 70 with TypeSolver

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

the class ReflectionClassAdapter method getSuperClass.

public ReferenceTypeImpl getSuperClass() {
    if (clazz.getGenericSuperclass() == null) {
        return null;
    }
    java.lang.reflect.Type superType = clazz.getGenericSuperclass();
    if (superType instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) superType;
        List<ResolvedType> typeParameters = Arrays.stream(parameterizedType.getActualTypeArguments()).map((t) -> ReflectionFactory.typeUsageFor(t, typeSolver)).collect(Collectors.toList());
        return new ReferenceTypeImpl(new ReflectionClassDeclaration(clazz.getSuperclass(), typeSolver), typeParameters, typeSolver);
    }
    return new ReferenceTypeImpl(new ReflectionClassDeclaration(clazz.getSuperclass(), typeSolver), typeSolver);
}
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) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

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