Search in sources :

Example 31 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class MultiCatchMethodCallExprTest method issue1482.

@Test
public void issue1482() {
    CompilationUnit cu = parseSample("Issue1482");
    cu.accept(new Visitor(), null);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 32 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class JavaParserAnonymousClassDeclarationTest method callingSuperClassInnerClassMethod.

@Test
public void callingSuperClassInnerClassMethod() {
    CompilationUnit cu = parseSample("AnonymousClassDeclarations");
    ClassOrInterfaceDeclaration aClass = Navigator.demandClass(cu, "AnonymousClassDeclarations");
    MethodDeclaration method = Navigator.demandMethod(aClass, "fooBar2");
    MethodCallExpr methodCall = Navigator.findMethodCall(method, "innerClassMethod").get();
    CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new ReflectionTypeSolver());
    MethodUsage methodUsage = JavaParserFacade.get(combinedTypeSolver).solveMethodAsUsage(methodCall);
    assertThat(methodUsage.getQualifiedSignature(), is("AnonymousClassDeclarations.DoFn.ProcessContext.innerClassMethod()"));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 33 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class JavaParserClassDeclarationTest method testHasAnnotation.

// hasAnnotation
@Test
public void testHasAnnotation() throws IOException, ParseException {
    TypeSolver typeSolver = new ReflectionTypeSolver();
    CompilationUnit cu = JavaParser.parse(adaptPath(new File("src/test/resources/Annotations.java.txt")));
    JavaParserClassDeclaration ca = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CA"), typeSolver);
    assertEquals(true, ca.hasAnnotation("foo.bar.MyAnnotation"));
    assertEquals(false, ca.hasAnnotation("foo.bar.MyAnnotation2"));
    assertEquals(false, ca.hasAnnotation("MyAnnotation"));
    assertEquals(false, ca.hasAnnotation("foo.bar.MyUnexistingAnnotation"));
    JavaParserClassDeclaration cb = new JavaParserClassDeclaration(Navigator.demandClass(cu, "CB"), typeSolver);
    assertEquals(true, cb.hasAnnotation("foo.bar.MyAnnotation"));
    assertEquals(true, cb.hasAnnotation("foo.bar.MyAnnotation2"));
    assertEquals(false, cb.hasAnnotation("MyAnnotation"));
    assertEquals(false, cb.hasAnnotation("foo.bar.MyUnexistingAnnotation"));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 34 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class Issue257 method issue257.

@Test
public void issue257() throws FileNotFoundException {
    String pathToSourceFile = adaptPath("src/test/resources/issue257/A.java.txt");
    CompilationUnit cu = JavaParser.parse(new File(pathToSourceFile));
    Statement statement = cu.getClassByName("A").get().getMethodsByName("run").get(0).getBody().get().getStatement(0);
    ExpressionStmt expressionStmt = (ExpressionStmt) statement;
    Expression expression = expressionStmt.getExpression();
    JavaParserFacade.get(typeSolver).getType(expression);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) Statement(com.github.javaparser.ast.stmt.Statement) File(java.io.File) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 35 with CompilationUnit

use of com.github.javaparser.ast.CompilationUnit in project javaparser by javaparser.

the class PrettyPrintVisitorTest method printLambdaIntersectionTypeReturn.

@Test
public void printLambdaIntersectionTypeReturn() {
    String code = "class A {" + EOL + "  Object f() {" + EOL + "    return (Comparator<Map.Entry<K, V>> & Serializable)(c1, c2) -> c1.getKey().compareTo(c2.getKey()); " + EOL + "}}";
    CompilationUnit cu = JavaParser.parse(code);
    MethodDeclaration methodDeclaration = (MethodDeclaration) cu.getType(0).getMember(0);
    assertEquals("return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> c1.getKey().compareTo(c2.getKey());", print(methodDeclaration.getBody().get().getStatements().get(0)));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Test(org.junit.Test)

Aggregations

CompilationUnit (com.github.javaparser.ast.CompilationUnit)489 Test (org.junit.Test)304 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)160 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)140 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)128 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)101 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)70 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)66 Context (com.github.javaparser.symbolsolver.core.resolution.Context)62 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)55 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)51 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)45 File (java.io.File)39 Expression (com.github.javaparser.ast.expr.Expression)38 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)38 MethodUsage (com.github.javaparser.resolution.MethodUsage)34 MemoryTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver)33 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)29 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)29 ArrayList (java.util.ArrayList)29