Search in sources :

Example 91 with CompilationUnit

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

the class VisitorGenerator method generate.

public final void generate() throws Exception {
    Log.info("Running %s", getClass().getSimpleName());
    final CompilationUnit compilationUnit = sourceRoot.tryToParse(pkg, visitorClassName + ".java").getResult().get();
    Optional<ClassOrInterfaceDeclaration> visitorClassOptional = compilationUnit.getClassByName(visitorClassName);
    if (!visitorClassOptional.isPresent()) {
        visitorClassOptional = compilationUnit.getInterfaceByName(visitorClassName);
    }
    final ClassOrInterfaceDeclaration visitorClass = visitorClassOptional.get();
    JavaParserMetaModel.getNodeMetaModels().stream().filter((baseNodeMetaModel) -> !baseNodeMetaModel.isAbstract()).forEach(node -> generateVisitMethodForNode(node, visitorClass, compilationUnit));
    after();
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Log(com.github.javaparser.utils.Log) SourceRoot(com.github.javaparser.utils.SourceRoot) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) Name(com.github.javaparser.ast.expr.Name) Optional(java.util.Optional) CompilationUnit(com.github.javaparser.ast.CompilationUnit) PUBLIC(com.github.javaparser.ast.Modifier.PUBLIC) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BaseNodeMetaModel(com.github.javaparser.metamodel.BaseNodeMetaModel) JavaParserMetaModel(com.github.javaparser.metamodel.JavaParserMetaModel) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)

Example 92 with CompilationUnit

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

the class NoCommentHashCodeVisitorTest method testEquals.

@Test
public void testEquals() {
    CompilationUnit p1 = JavaParser.parse("class X { }");
    CompilationUnit p2 = JavaParser.parse("class X { }");
    assertEquals(p1.hashCode(), p2.hashCode());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Test(org.junit.Test)

Example 93 with CompilationUnit

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

the class NoCommentEqualsVisitorTest method testEquals.

@Test
public void testEquals() {
    CompilationUnit p1 = JavaParser.parse("class X { }");
    CompilationUnit p2 = JavaParser.parse("class X { }");
    assertTrue(NoCommentEqualsVisitor.equals(p1, p2));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Test(org.junit.Test)

Example 94 with CompilationUnit

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

the class ClassOrInterfaceDeclarationTest method staticNestedClass.

@Test
public void staticNestedClass() {
    CompilationUnit cu = JavaParser.parse("class X{static class Y{}}");
    ClassOrInterfaceDeclaration y = cu.getClassByName("X").get().getMembers().get(0).asClassOrInterfaceDeclaration();
    assertFalse(y.isInnerClass());
    assertTrue(y.isNestedType());
    assertFalse(y.isLocalClassDeclaration());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Test(org.junit.Test)

Example 95 with CompilationUnit

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

the class ContextTest method resolveReferenceUsingQualifiedName.

@Test
public void resolveReferenceUsingQualifiedName() {
    CompilationUnit cu = parseSample("Navigator2");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator");
    MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
    Parameter param = method.getParameters().get(0);
    ResolvedClassDeclaration compilationUnitDecl = mock(ResolvedClassDeclaration.class);
    when(compilationUnitDecl.getName()).thenReturn("CompilationUnit");
    when(compilationUnitDecl.getQualifiedName()).thenReturn("com.github.javaparser.ast.CompilationUnit");
    TypeSolver typeSolver = mock(TypeSolver.class);
    // when(typeSolver.tryToSolveType("java.lang.com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.unsolved(ClassDeclaration.class));
    when(typeSolver.getRoot()).thenReturn(typeSolver);
    when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
    when(typeSolver.tryToSolveType("com.github.javaparser.ast.CompilationUnit")).thenReturn(SymbolReference.solved(compilationUnitDecl));
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("com.github.javaparser.ast.CompilationUnit", param);
    assertEquals(true, ref.isSolved());
    assertEquals("CompilationUnit", ref.getCorrespondingDeclaration().getName());
    assertEquals("com.github.javaparser.ast.CompilationUnit", ref.getCorrespondingDeclaration().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Parameter(com.github.javaparser.ast.body.Parameter) ResolvedClassDeclaration(com.github.javaparser.resolution.declarations.ResolvedClassDeclaration) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) 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