Search in sources :

Example 11 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class SubtypeOfBoundTest method recognizeProperUpperBound2.

@Test
public void recognizeProperUpperBound2() {
    ResolvedTypeParameterDeclaration typeParameterDeclaration1 = mock(ResolvedTypeParameterDeclaration.class);
    ResolvedTypeParameterDeclaration typeParameterDeclaration2 = mock(ResolvedTypeParameterDeclaration.class);
    // { α <: Iterable<?>, β <: Object, α <: List<β> } describes a proper upper bound for each of α and β, along with a dependency between them.
    InferenceVariable alpha = new InferenceVariable("α", typeParameterDeclaration1);
    InferenceVariable beta = new InferenceVariable("β", typeParameterDeclaration2);
    ResolvedType iterableOfWildcard = new ReferenceTypeImpl(iterableType.getTypeDeclaration(), Arrays.asList(ResolvedWildcard.UNBOUNDED), typeSolver);
    ResolvedType listOfBeta = new ReferenceTypeImpl(listType.getTypeDeclaration(), Arrays.asList(beta), typeSolver);
    Bound bound1 = new SubtypeOfBound(alpha, iterableOfWildcard);
    Bound bound2 = new SubtypeOfBound(beta, objectType);
    Bound bound3 = new SubtypeOfBound(alpha, listOfBeta);
    assertEquals(false, isProperType(listOfBeta));
    assertEquals(Optional.of(new ProperUpperBound(alpha, iterableOfWildcard)), bound1.isProperUpperBound());
    assertEquals(Optional.of(new ProperUpperBound(beta, objectType)), bound2.isProperUpperBound());
    assertEquals(true, bound3.isADependency());
}
Also used : ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 12 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class LambdaExprContext method solveSymbolAsValue.

@Override
public Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) {
    for (Parameter parameter : wrappedNode.getParameters()) {
        SymbolDeclarator sb = JavaParserFactory.getSymbolDeclarator(parameter, typeSolver);
        int index = 0;
        for (ResolvedValueDeclaration decl : sb.getSymbolDeclarations()) {
            if (decl.getName().equals(name)) {
                if (requireParentNode(wrappedNode) instanceof MethodCallExpr) {
                    MethodCallExpr methodCallExpr = (MethodCallExpr) requireParentNode(wrappedNode);
                    MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(methodCallExpr);
                    int i = pos(methodCallExpr, wrappedNode);
                    ResolvedType lambdaType = methodUsage.getParamTypes().get(i);
                    // Get the functional method in order for us to resolve it's type arguments properly
                    Optional<MethodUsage> functionalMethodOpt = FunctionalInterfaceLogic.getFunctionalMethod(lambdaType);
                    if (functionalMethodOpt.isPresent()) {
                        MethodUsage functionalMethod = functionalMethodOpt.get();
                        InferenceContext inferenceContext = new InferenceContext(MyObjectProvider.INSTANCE);
                        // Resolve each type variable of the lambda, and use this later to infer the type of each
                        // implicit parameter
                        inferenceContext.addPair(lambdaType, new ReferenceTypeImpl(lambdaType.asReferenceType().getTypeDeclaration(), typeSolver));
                        // Find the position of this lambda argument
                        boolean found = false;
                        int lambdaParamIndex;
                        for (lambdaParamIndex = 0; lambdaParamIndex < wrappedNode.getParameters().size(); lambdaParamIndex++) {
                            if (wrappedNode.getParameter(lambdaParamIndex).getName().getIdentifier().equals(name)) {
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            return Optional.empty();
                        }
                        // Now resolve the argument type using the inference context
                        ResolvedType argType = inferenceContext.resolve(inferenceContext.addSingle(functionalMethod.getParamType(lambdaParamIndex)));
                        ResolvedLambdaConstraintType conType;
                        if (argType.isWildcard()) {
                            conType = ResolvedLambdaConstraintType.bound(argType.asWildcard().getBoundedType());
                        } else {
                            conType = ResolvedLambdaConstraintType.bound(argType);
                        }
                        Value value = new Value(conType, name);
                        return Optional.of(value);
                    } else {
                        return Optional.empty();
                    }
                } else if (requireParentNode(wrappedNode) instanceof VariableDeclarator) {
                    VariableDeclarator variableDeclarator = (VariableDeclarator) requireParentNode(wrappedNode);
                    ResolvedType t = JavaParserFacade.get(typeSolver).convertToUsageVariableType(variableDeclarator);
                    Optional<MethodUsage> functionalMethod = FunctionalInterfaceLogic.getFunctionalMethod(t);
                    if (functionalMethod.isPresent()) {
                        ResolvedType lambdaType = functionalMethod.get().getParamType(index);
                        // Replace parameter from declarator
                        Map<ResolvedTypeParameterDeclaration, ResolvedType> inferredTypes = new HashMap<>();
                        if (lambdaType.isReferenceType()) {
                            for (com.github.javaparser.utils.Pair<ResolvedTypeParameterDeclaration, ResolvedType> entry : lambdaType.asReferenceType().getTypeParametersMap()) {
                                if (entry.b.isTypeVariable() && entry.b.asTypeParameter().declaredOnType()) {
                                    ResolvedType ot = t.asReferenceType().typeParametersMap().getValue(entry.a);
                                    lambdaType = lambdaType.replaceTypeVariables(entry.a, ot, inferredTypes);
                                }
                            }
                        } else if (lambdaType.isTypeVariable() && lambdaType.asTypeParameter().declaredOnType()) {
                            lambdaType = t.asReferenceType().typeParametersMap().getValue(lambdaType.asTypeParameter());
                        }
                        Value value = new Value(lambdaType, name);
                        return Optional.of(value);
                    } else {
                        throw new UnsupportedOperationException();
                    }
                } else {
                    throw new UnsupportedOperationException();
                }
            }
            index++;
        }
    }
    // if nothing is found we should ask the parent context
    return getParent().solveSymbolAsValue(name, typeSolver);
}
Also used : SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) InferenceContext(com.github.javaparser.symbolsolver.logic.InferenceContext) ResolvedLambdaConstraintType(com.github.javaparser.resolution.types.ResolvedLambdaConstraintType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Value(com.github.javaparser.symbolsolver.model.resolution.Value) Parameter(com.github.javaparser.ast.body.Parameter) MethodUsage(com.github.javaparser.resolution.MethodUsage) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 13 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class SwitchEntryContext method solveSymbol.

@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
    SwitchStmt switchStmt = (SwitchStmt) requireParentNode(wrappedNode);
    ResolvedType type = JavaParserFacade.get(typeSolver).getType(switchStmt.getSelector());
    if (type.isReferenceType() && type.asReferenceType().getTypeDeclaration().isEnum()) {
        if (type instanceof ReferenceTypeImpl) {
            ReferenceTypeImpl typeUsageOfTypeDeclaration = (ReferenceTypeImpl) type;
            if (typeUsageOfTypeDeclaration.getTypeDeclaration().hasField(name)) {
                return SymbolReference.solved(typeUsageOfTypeDeclaration.getTypeDeclaration().getField(name));
            }
        } else {
            throw new UnsupportedOperationException();
        }
    }
    // look for declaration in other switch statements
    for (SwitchEntryStmt seStmt : switchStmt.getEntries()) {
        if (!seStmt.equals(wrappedNode)) {
            for (Statement stmt : seStmt.getStatements()) {
                SymbolDeclarator symbolDeclarator = JavaParserFactory.getSymbolDeclarator(stmt, typeSolver);
                SymbolReference<? extends ResolvedValueDeclaration> symbolReference = solveWith(symbolDeclarator, name);
                if (symbolReference.isSolved()) {
                    return symbolReference;
                }
            }
        }
    }
    return getParent().solveSymbol(name, typeSolver);
}
Also used : SwitchStmt(com.github.javaparser.ast.stmt.SwitchStmt) SymbolDeclarator(com.github.javaparser.symbolsolver.resolution.SymbolDeclarator) Statement(com.github.javaparser.ast.stmt.Statement) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 14 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class JavaParserEnumDeclaration method getAncestors.

@Override
public List<ResolvedReferenceType> getAncestors() {
    List<ResolvedReferenceType> ancestors = new ArrayList<>();
    ResolvedReferenceType enumClass = ReflectionFactory.typeUsageFor(Enum.class, typeSolver).asReferenceType();
    ResolvedTypeParameterDeclaration eTypeParameter = enumClass.getTypeDeclaration().getTypeParameters().get(0);
    enumClass = enumClass.deriveTypeParameters(new ResolvedTypeParametersMap.Builder().setValue(eTypeParameter, new ReferenceTypeImpl(this, typeSolver)).build());
    ancestors.add(enumClass);
    if (wrappedNode.getImplementedTypes() != null) {
        for (ClassOrInterfaceType implementedType : wrappedNode.getImplementedTypes()) {
            SymbolReference<ResolvedTypeDeclaration> implementedDeclRef = new SymbolSolver(typeSolver).solveTypeInType(this, implementedType.getName().getId());
            if (!implementedDeclRef.isSolved()) {
                throw new UnsolvedSymbolException(implementedType.getName().getId());
            }
            ancestors.add(new ReferenceTypeImpl((ResolvedReferenceTypeDeclaration) implementedDeclRef.getCorrespondingDeclaration(), typeSolver));
        }
    }
    return ancestors;
}
Also used : UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) SymbolSolver(com.github.javaparser.symbolsolver.resolution.SymbolSolver)

Example 15 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class JavaParserTypeAdapter method isAssignableBy.

public boolean isAssignableBy(ResolvedReferenceTypeDeclaration other) {
    List<ResolvedReferenceType> ancestorsOfOther = other.getAllAncestors();
    ancestorsOfOther.add(new ReferenceTypeImpl(other, typeSolver));
    for (ResolvedReferenceType ancestorOfOther : ancestorsOfOther) {
        if (ancestorOfOther.getQualifiedName().equals(this.getQualifiedName())) {
            return true;
        }
    }
    return false;
}
Also used : ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)

Aggregations

ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)27 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)16 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)13 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)12 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)12 Test (org.junit.Test)9 MethodUsage (com.github.javaparser.resolution.MethodUsage)5 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)4 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)4 ResolvedTypeVariable (com.github.javaparser.resolution.types.ResolvedTypeVariable)4 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)4 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)3 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)3 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)3 Context (com.github.javaparser.symbolsolver.core.resolution.Context)3 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)3 File (java.io.File)3 Collectors (java.util.stream.Collectors)3