Search in sources :

Example 16 with CombinedTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver in project javaparser by javaparser.

the class SymbolSolverWithJavassistInterfaceTest method setup.

@Before
public void setup() throws IOException {
    final String pathToMainJar = adaptPath("src/test/resources/javassist_symbols/main_jar/main_jar.jar");
    final String pathToIncludedJar = adaptPath("src/test/resources/javassist_symbols/included_jar/included_jar.jar");
    typeSolver = new CombinedTypeSolver(new JarTypeSolver(pathToIncludedJar), new JarTypeSolver(pathToMainJar), new ReflectionTypeSolver());
    symbolSolver = new SymbolSolver(typeSolver);
    interfaceDeclarationStandalone = (JavassistInterfaceDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.StandaloneInterface");
    interfaceDeclarationSubInterfaceOwnJar = (JavassistInterfaceDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.SubInterfaceOwnJar");
    interfaceDeclarationSubInterfaceIncludedJar = (JavassistInterfaceDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.SubInterfaceIncludedJar");
    interfaceDeclarationSubInterfaceExcludedJar = (JavassistInterfaceDeclaration) typeSolver.solveType("com.github.javaparser.javasymbolsolver.javassist_symbols.main_jar.SubInterfaceExcludedJar");
}
Also used : JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) Before(org.junit.Before)

Example 17 with CombinedTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver in project javaparser by javaparser.

the class LambdaExprContextResolutionTest method solveParameterOfLambdaInFieldDecl.

@Test
public void solveParameterOfLambdaInFieldDecl() {
    CompilationUnit cu = parseSample("Lambda");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    VariableDeclarator field = Navigator.demandField(clazz, "functional");
    LambdaExpr lambdaExpr = (LambdaExpr) field.getInitializer().get();
    File src = new File("src/test/resources");
    CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new ReflectionTypeSolver());
    combinedTypeSolver.add(new JavaParserTypeSolver(adaptPath(src)));
    Context context = new LambdaExprContext(lambdaExpr, combinedTypeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
    assertTrue(ref.isPresent());
    assertEquals("java.lang.String", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 18 with CombinedTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver in project javaparser by javaparser.

the class LambdaExprContextResolutionTest method solveParameterOfLambdaInVarDecl.

@Test
public void solveParameterOfLambdaInVarDecl() {
    CompilationUnit cu = parseSample("Lambda");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    MethodDeclaration method = Navigator.demandMethod(clazz, "testFunctionalVar");
    VariableDeclarator varDecl = Navigator.demandVariableDeclaration(method, "a").get();
    LambdaExpr lambdaExpr = (LambdaExpr) varDecl.getInitializer().get();
    File src = adaptPath(new File("src/test/resources"));
    CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new ReflectionTypeSolver());
    combinedTypeSolver.add(new JavaParserTypeSolver(src));
    Context context = new LambdaExprContext(lambdaExpr, combinedTypeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
    assertTrue(ref.isPresent());
    assertEquals("java.lang.String", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 19 with CombinedTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver in project javaparser by javaparser.

the class MethodCallExprContextResolutionTest method assertCanSolveGenericMethodCallMustUseProvidedTypeArgs.

private void assertCanSolveGenericMethodCallMustUseProvidedTypeArgs(String callMethodName) {
    MethodCallExpr methodCallExpr = getMethodCallExpr("genericMethodTest", callMethodName);
    CombinedTypeSolver typeSolver = createTypeSolver();
    MethodCallExprContext context = new MethodCallExprContext(methodCallExpr, typeSolver);
    Optional<MethodUsage> ref = context.solveMethodAsUsage(callMethodName, Collections.emptyList(), typeSolver);
    assertTrue(ref.isPresent());
    assertEquals("MethodCalls", ref.get().declaringType().getQualifiedName());
    assertEquals(Collections.singletonList("java.lang.Integer"), ref.get().typeParametersMap().getTypes().stream().map(ty -> ty.asReferenceType().describe()).collect(Collectors.toList()));
}
Also used : MethodCallExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext) MethodUsage(com.github.javaparser.resolution.MethodUsage) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 20 with CombinedTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver in project javaparser by javaparser.

the class Widget method main.

public static void main(String[] args) throws IOException, ParseException {
    File src = new File(JAVA_ROOT);
    CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new ReflectionTypeSolver(true));
    combinedTypeSolver.add(new JavaParserTypeSolver(src));
    CompilationUnit compilationUnit = JavaParser.parse(new File(CLASS));
    JavaParserFacade parserFacade = JavaParserFacade.get(combinedTypeSolver);
    MethodDeclaration methodDeclaration = compilationUnit.getNodesByType(MethodDeclaration.class).stream().filter(node -> node.getName().equals("doSomething")).findAny().orElse(null);
    methodDeclaration.getNodesByType(MethodCallExpr.class).forEach(parserFacade::solve);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)49 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)46 CompilationUnit (com.github.javaparser.ast.CompilationUnit)23 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)22 Test (org.junit.Test)21 File (java.io.File)20 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)19 JarTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver)15 Before (org.junit.Before)15 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)11 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)9 MethodUsage (com.github.javaparser.resolution.MethodUsage)9 Context (com.github.javaparser.symbolsolver.core.resolution.Context)9 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)6 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)6 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)6 Value (com.github.javaparser.symbolsolver.model.resolution.Value)4 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)3 SourceFileInfoExtractor (com.github.javaparser.symbolsolver.SourceFileInfoExtractor)3