Search in sources :

Example 81 with ResolvedType

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

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

the class Issue84 method variadicIssue.

@Test
public void variadicIssue() {
    CompilationUnit cu = parseSample("Issue84");
    final MethodCallExpr methodCall = Navigator.findMethodCall(cu, "variadicMethod").get();
    final JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    final ResolvedType type = javaParserFacade.getType(methodCall);
    assertEquals(String.class.getCanonicalName(), type.asReferenceType().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 83 with ResolvedType

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

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

the class ReferenceTypeImpl method transformTypeParameters.

/**
 * Execute a transformation on all the type parameters of this element.
 */
@Override
public ResolvedType transformTypeParameters(ResolvedTypeTransformer transformer) {
    ResolvedType result = this;
    int i = 0;
    for (ResolvedType tp : this.typeParametersValues()) {
        ResolvedType transformedTp = transformer.transform(tp);
        // Identity comparison on purpose
        if (transformedTp != tp) {
            List<ResolvedType> typeParametersCorrected = result.asReferenceType().typeParametersValues();
            typeParametersCorrected.set(i, transformedTp);
            result = create(typeDeclaration, typeParametersCorrected);
        }
        i++;
    }
    return result;
}
Also used : ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 85 with ResolvedType

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

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