Search in sources :

Example 86 with ResolvedType

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

the class Issue116 method arrayTypeIsNotPartOfTheTree.

@Test
public void arrayTypeIsNotPartOfTheTree() {
    CompilationUnit cu = parseSample("Issue116");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
    MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "foo");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    com.github.javaparser.ast.type.Type typeNode = methodDeclaration.getParameters().get(0).getType();
    ResolvedType type = javaParserFacade.convert(typeNode, typeNode);
    assertEquals("java.lang.String[]", type.describe());
    ExpressionStmt expressionStmt = (ExpressionStmt) methodDeclaration.getBody().get().getStatements().get(0);
    Expression argRef = expressionStmt.getExpression();
    assertEquals("java.lang.String[]", javaParserFacade.getType(argRef).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) Expression(com.github.javaparser.ast.expr.Expression) 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) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 87 with ResolvedType

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

the class Issue18 method typeDeclarationSuperClassImplicitlyIncludeObject.

@Test
public void typeDeclarationSuperClassImplicitlyIncludeObject() {
    CompilationUnit cu = parseSample("Issue18");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
    MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "bar");
    ExpressionStmt expr = (ExpressionStmt) methodDeclaration.getBody().get().getStatements().get(1);
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ResolvedType type = javaParserFacade.getType(expr.getExpression());
    assertEquals("java.lang.Object", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) 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) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 88 with ResolvedType

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

the class TypeInference method testForApplicabilityByVariableArityInvocation.

private Optional<ConstraintFormulaSet> testForApplicabilityByVariableArityInvocation(List<ResolvedType> Fs, List<Expression> es, Substitution theta) {
    int k = es.size();
    // Let F'1, ..., F'k be the first k variable arity parameter types of m (§15.12.2.4). C includes,
    // for all i (1 ≤ i ≤ k) where ei is pertinent to applicability, ‹ei → F'i θ›.
    List<ResolvedType> FsFirst = new LinkedList<>();
    for (int i = 0; i < k; i++) {
        ResolvedType FFirstI = i < Fs.size() ? Fs.get(i) : Fs.get(Fs.size() - 1);
        FsFirst.add(FFirstI);
    }
    return Optional.of(constraintSetFromArgumentsSubstitution(FsFirst, es, theta, k));
}
Also used : ResolvedType(com.github.javaparser.resolution.types.ResolvedType) LinkedList(java.util.LinkedList)

Example 89 with ResolvedType

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

the class TypeInference method boundSetup.

// /
// / Private instance methods
// /
/**
 * When inference begins, a bound set is typically generated from a list of type parameter declarations P1, ..., Pp
 * and associated inference variables α1, ..., αp
 *
 * @param typeParameterDeclarations
 * @param inferenceVariables
 * @return
 */
private BoundSet boundSetup(List<ResolvedTypeParameterDeclaration> typeParameterDeclarations, List<InferenceVariable> inferenceVariables) {
    if (typeParameterDeclarations.size() != inferenceVariables.size()) {
        throw new IllegalArgumentException();
    }
    // When inference begins, a bound set is typically generated from a list of
    // type parameter declarations P1, ..., Pp and associated inference variables α1, ..., αp.
    // Such a bound set is constructed as follows. For each l (1 ≤ l ≤ p):
    BoundSet boundSet = BoundSet.empty();
    for (int l = 0; l < typeParameterDeclarations.size(); l++) {
        ResolvedTypeParameterDeclaration Pl = typeParameterDeclarations.get(l);
        InferenceVariable alphaL = inferenceVariables.get(l);
        if (Pl.getBounds().isEmpty()) {
            boundSet = boundSet.withBound(new SubtypeOfBound(alphaL, object));
        } else {
            for (ResolvedTypeParameterDeclaration.Bound bound : Pl.getBounds()) {
                ResolvedType T = bound.getType();
                Substitution substitution = Substitution.empty();
                for (int j = 0; j < typeParameterDeclarations.size(); j++) {
                    substitution = substitution.withPair(typeParameterDeclarations.get(j), inferenceVariables.get(j));
                }
                ResolvedType TWithSubstitutions = substitution.apply(T);
                boundSet = boundSet.withBound(new SubtypeOfBound(alphaL, TWithSubstitutions));
                if (boundSet.getProperUpperBoundsFor(alphaL).isEmpty()) {
                    boundSet = boundSet.withBound(new SubtypeOfBound(alphaL, object));
                }
            }
        }
    }
    return boundSet;
}
Also used : SubtypeOfBound(com.github.javaparser.symbolsolver.resolution.typeinference.bounds.SubtypeOfBound) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 90 with ResolvedType

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

the class TypeInference method constraintSetFromArgumentsSubstitution.

private ConstraintFormulaSet constraintSetFromArgumentsSubstitution(List<ResolvedType> Fs, List<Expression> es, Substitution theta, int k) {
    ConstraintFormulaSet constraintFormulaSet = ConstraintFormulaSet.empty();
    for (int i = 0; i < k; i++) {
        Expression ei = es.get(i);
        ResolvedType fi = Fs.get(i);
        ResolvedType fiTheta = typeWithSubstitution(fi, theta);
        constraintFormulaSet = constraintFormulaSet.withConstraint(new ExpressionCompatibleWithType(typeSolver, ei, fiTheta));
    }
    return constraintFormulaSet;
}
Also used : ExpressionCompatibleWithType(com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.ExpressionCompatibleWithType) ExpressionHelper.isStandaloneExpression(com.github.javaparser.symbolsolver.resolution.typeinference.ExpressionHelper.isStandaloneExpression) 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