Search in sources :

Example 51 with MethodUsage

use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.

the class MethodsResolutionLogicTest method compatibilityShouldConsiderAlsoTypeVariablesRaw.

@Test
public void compatibilityShouldConsiderAlsoTypeVariablesRaw() {
    JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolver.solveType("com.github.javaparser.ast.body.ConstructorDeclaration");
    ResolvedReferenceType rawClassType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(Class.class, typeSolver);
    MethodUsage mu = constructorDeclaration.getAllMethods().stream().filter(m -> m.getDeclaration().getSignature().equals("isThrows(java.lang.Class<? extends java.lang.Throwable>)")).findFirst().get();
    assertEquals(true, MethodResolutionLogic.isApplicable(mu, "isThrows", ImmutableList.of(rawClassType), typeSolver));
}
Also used : JavaParserClassDeclaration(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) MethodUsage(com.github.javaparser.resolution.MethodUsage) Test(org.junit.Test)

Example 52 with MethodUsage

use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.

the class MethodsResolutionTest method solveMethodInInterfaceParent.

@Test
public void solveMethodInInterfaceParent() {
    CompilationUnit cu = parseSample("MethodCalls");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MethodCalls");
    MethodDeclaration method = Navigator.demandMethod(clazz, "inheritedInterfaceMethod");
    MethodCallExpr expression = Navigator.findMethodCall(method, "toString").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodUsage call1 = javaParserFacade.solveMethodAsUsage(expression);
    assertEquals("java.lang.Object.toString()", call1.getQualifiedSignature());
}
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) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 53 with MethodUsage

use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.

the class MethodsResolutionTest method solveMethodWithClassExpressionAsParameter.

@Test
public void solveMethodWithClassExpressionAsParameter() {
    CompilationUnit cu = parseSample("ClassExpression");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ClassExpression");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo");
    MethodCallExpr expression = Navigator.findMethodCall(method, "noneOf").get();
    MethodUsage methodUsage = JavaParserFacade.get(new ReflectionTypeSolver()).solveMethodAsUsage(expression);
    assertEquals("noneOf", methodUsage.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 54 with MethodUsage

use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.

the class StatementContextResolutionTest method resolveLocalAndSeveralAnnidatedLevels.

@Test
public void resolveLocalAndSeveralAnnidatedLevels() {
    CompilationUnit cu = parseSample("LocalVariableInParent");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
    MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo4");
    MethodCallExpr call = Navigator.findMethodCall(method, "add").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(typeSolver).solve(call.getScope().get());
    assertTrue(ref.isSolved());
    assertEquals("java.util.List<Comment>", ref.getCorrespondingDeclaration().getType().describe());
    MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
    assertEquals("add", methodUsage.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 55 with MethodUsage

use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.

the class ContextTest method resolveReferenceToOverloadMethodWithNullParam.

@Test
public void resolveReferenceToOverloadMethodWithNullParam() {
    CompilationUnit cu = parseSample("OverloadedMethods");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "OverloadedMethods");
    MethodDeclaration method = Navigator.demandMethod(clazz, "m1");
    MethodCallExpr call = Navigator.findMethodCall(method, "overloaded").get();
    ReflectionTypeSolver typeSolver = new ReflectionTypeSolver();
    MethodUsage ref = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
    assertEquals("overloaded", ref.getName());
    assertEquals(1, ref.getNoParams());
    assertEquals("java.lang.String", ref.getParamTypes().get(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodUsage(com.github.javaparser.resolution.MethodUsage) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Aggregations

MethodUsage (com.github.javaparser.resolution.MethodUsage)59 Test (org.junit.Test)39 CompilationUnit (com.github.javaparser.ast.CompilationUnit)36 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)33 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)27 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)24 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)20 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)19 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)14 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)12 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)12 Context (com.github.javaparser.symbolsolver.core.resolution.Context)12 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)11 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)7 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)6 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)6 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)6 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)6 InferenceContext (com.github.javaparser.symbolsolver.logic.InferenceContext)6 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)5