Search in sources :

Example 26 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveMethodStaticallyImportedWithAsterisk.

@Test
public void solveMethodStaticallyImportedWithAsterisk() throws ParseException, IOException {
    CompilationUnit cu = parseSample("CompilationUnitWithImports");
    Context context = new CompilationUnitContext(cu, typeSolver);
    CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    typeSolver.add(new ReflectionTypeSolver());
    SymbolReference<ResolvedMethodDeclaration> ref = context.solveMethod("assertFalse", ImmutableList.of(ResolvedPrimitiveType.BOOLEAN), false, typeSolver);
    assertEquals(true, ref.isSolved());
    assertEquals("assertFalse", ref.getCorrespondingDeclaration().getName());
    assertEquals(1, ref.getCorrespondingDeclaration().getNumberOfParams());
    assertEquals("boolean", ref.getCorrespondingDeclaration().getParam(0).getType().describe());
    assertEquals(true, ref.getCorrespondingDeclaration().getParam(0).getType().isPrimitive());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 27 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.

the class CompilationUnitContextResolutionTest method solveMethodStaticallyImportedWithoutAsterisk.

@Test
public void solveMethodStaticallyImportedWithoutAsterisk() throws ParseException, IOException {
    CompilationUnit cu = parseSample("CompilationUnitSymbols");
    Context context = new CompilationUnitContext(cu, typeSolver);
    CombinedTypeSolver typeSolver = new CombinedTypeSolver();
    typeSolver.add(new JarTypeSolver(adaptPath("src/test/resources/junit-4.8.1.jar")));
    typeSolver.add(new ReflectionTypeSolver());
    SymbolReference<ResolvedMethodDeclaration> ref = context.solveMethod("assertEquals", ImmutableList.of(NullType.INSTANCE, NullType.INSTANCE), false, typeSolver);
    assertEquals(true, ref.isSolved());
    assertEquals("assertEquals", ref.getCorrespondingDeclaration().getName());
    assertEquals(2, ref.getCorrespondingDeclaration().getNumberOfParams());
    assertEquals("java.lang.Object", ref.getCorrespondingDeclaration().getParam(0).getType().asReferenceType().getQualifiedName());
    assertEquals("java.lang.Object", ref.getCorrespondingDeclaration().getParam(1).getType().asReferenceType().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 28 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.

the class JavaSymbolSolverTest method resolveMethodDeclaration.

@Test
public void resolveMethodDeclaration() {
    TypeSolver typeSolver = new ReflectionTypeSolver();
    CompilationUnit cu = parseSample("SymbolResolverExample");
    new JavaSymbolSolver(typeSolver).inject(cu);
    MethodDeclaration methodDeclaration = cu.getClassByName("A").get().getMethods().get(0);
    ResolvedMethodDeclaration resolvedMethodDeclaration = methodDeclaration.resolve();
    assertEquals("foo", resolvedMethodDeclaration.getName());
    assertEquals("A[]", resolvedMethodDeclaration.getReturnType().describe());
    assertEquals("java.lang.String[]", resolvedMethodDeclaration.getParam(0).getType().describe());
    assertEquals("int[]", resolvedMethodDeclaration.getParam(1).getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 29 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.

the class MethodsResolutionTest method solveMethodWithTypePromotionsToByteWithExtraParam.

@Test
public void solveMethodWithTypePromotionsToByteWithExtraParam() {
    CompilationUnit cu = parseSample("Issue338");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "TypePromotionsWithExtraParam");
    MethodDeclaration method = Navigator.demandMethod(clazz, "callingByte");
    {
        MethodCallExpr expression = method.getBody().get().getStatements().get(0).asExpressionStmt().getExpression().asMethodCallExpr();
        SymbolReference<ResolvedMethodDeclaration> reference = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
        assertEquals(true, reference.isSolved());
        assertEquals("byteParam", reference.getCorrespondingDeclaration().getName());
    }
    {
        MethodCallExpr expression = method.getBody().get().getStatements().get(1).asExpressionStmt().getExpression().asMethodCallExpr();
        SymbolReference<ResolvedMethodDeclaration> reference = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
        assertEquals(false, reference.isSolved());
    }
    {
        MethodCallExpr expression = method.getBody().get().getStatements().get(2).asExpressionStmt().getExpression().asMethodCallExpr();
        SymbolReference<ResolvedMethodDeclaration> reference = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
        assertEquals(false, reference.isSolved());
    }
    {
        MethodCallExpr expression = method.getBody().get().getStatements().get(3).asExpressionStmt().getExpression().asMethodCallExpr();
        SymbolReference<ResolvedMethodDeclaration> reference = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
        assertEquals(false, reference.isSolved());
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 30 with ResolvedMethodDeclaration

use of com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration in project javaparser by javaparser.

the class ReflectionMethodDeclarationTest method testGetGenericParameters.

@Test
public void testGetGenericParameters() {
    TypeSolver typeResolver = new ReflectionTypeSolver();
    ResolvedInterfaceDeclaration map = new ReflectionInterfaceDeclaration(Map.class, typeResolver);
    ResolvedMethodDeclaration put = map.getAllMethods().stream().filter(m -> m.getName().equals("put")).findFirst().get().getDeclaration();
    assertEquals(true, put.getParam(0).getType().isTypeVariable());
    assertEquals(true, put.getParam(0).getType().asTypeParameter().declaredOnType());
    assertEquals("java.util.Map.K", put.getParam(0).getType().asTypeParameter().getQualifiedName());
    assertEquals(true, put.getParam(1).getType().isTypeVariable());
    assertEquals(true, put.getParam(1).getType().asTypeParameter().declaredOnType());
    assertEquals("java.util.Map.V", put.getParam(1).getType().asTypeParameter().getQualifiedName());
}
Also used : List(java.util.List) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) Map(java.util.Map) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Test(org.junit.Test) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) Assert.assertEquals(org.junit.Assert.assertEquals) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedInterfaceDeclaration(com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) Test(org.junit.Test)

Aggregations

ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)35 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)27 Test (org.junit.Test)27 CompilationUnit (com.github.javaparser.ast.CompilationUnit)23 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)19 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)12 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)12 Context (com.github.javaparser.symbolsolver.core.resolution.Context)11 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)11 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)11 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)8 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)7 List (java.util.List)7 MethodUsage (com.github.javaparser.resolution.MethodUsage)6 ResolvedInterfaceDeclaration (com.github.javaparser.resolution.declarations.ResolvedInterfaceDeclaration)6 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)5 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)5 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)5 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)5