use of com.github.javaparser.ast.Modifier.Keyword.PUBLIC in project javaparser by javaparser.
the class LexicalPreservingPrinterTest method moveOverrideAnnotations.
// See issue #866
@Test
void moveOverrideAnnotations() {
String code = "public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + " protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + " protected @Override void initializePage() {}" + SYSTEM_EOL + "}";
CompilationUnit cu = parse(code);
LexicalPreservingPrinter.setup(cu);
cu.getTypes().forEach(type -> type.getMembers().forEach(member -> member.ifMethodDeclaration(methodDeclaration -> {
if (methodDeclaration.getAnnotationByName("Override").isPresent()) {
while (methodDeclaration.getAnnotations().isNonEmpty()) {
AnnotationExpr annotationExpr = methodDeclaration.getAnnotations().get(0);
annotationExpr.remove();
}
methodDeclaration.addMarkerAnnotation("Override");
}
})));
assertEquals("public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + " protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + " @Override" + SYSTEM_EOL + " protected void initializePage() {}" + SYSTEM_EOL + "}", LexicalPreservingPrinter.print(cu));
}
use of com.github.javaparser.ast.Modifier.Keyword.PUBLIC in project javaparser by javaparser.
the class LexicalPreservingPrinterTest method handleAddingMarkerAnnotation.
// See issue #865
@Test
void handleAddingMarkerAnnotation() {
String code = "public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + " protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + " @Override" + SYSTEM_EOL + " protected void initializePage() {}" + SYSTEM_EOL + "}";
CompilationUnit cu = parse(code);
LexicalPreservingPrinter.setup(cu);
cu.getTypes().forEach(type -> type.getMembers().forEach(member -> {
if (member instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) member;
if (!methodDeclaration.getAnnotationByName("Override").isPresent()) {
methodDeclaration.addMarkerAnnotation("Override");
}
}
}));
assertEquals("public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + " @Override" + SYSTEM_EOL + " protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + " @Override" + SYSTEM_EOL + " protected void initializePage() {}" + SYSTEM_EOL + "}", LexicalPreservingPrinter.print(cu));
}
use of com.github.javaparser.ast.Modifier.Keyword.PUBLIC in project javaparser by javaparser.
the class LexicalPreservingPrinterTest method handleOverrideAnnotationAlternative.
// See issue #865
@Test
void handleOverrideAnnotationAlternative() {
String code = "public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + " protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + " protected void initializePage() {}" + SYSTEM_EOL + "}";
CompilationUnit cu = parse(code);
LexicalPreservingPrinter.setup(cu);
cu.getTypes().forEach(type -> type.getMembers().forEach(member -> member.ifMethodDeclaration(methodDeclaration -> methodDeclaration.addAnnotation("Override"))));
assertEquals("public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + " @Override()" + SYSTEM_EOL + " protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + " @Override()" + SYSTEM_EOL + " protected void initializePage() {}" + SYSTEM_EOL + "}", LexicalPreservingPrinter.print(cu));
}
use of com.github.javaparser.ast.Modifier.Keyword.PUBLIC in project javaparser by javaparser.
the class LexicalPreservingPrinterTest method moveOrAddOverrideAnnotations.
// See issue #866
@Test
void moveOrAddOverrideAnnotations() {
String code = "public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + " protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + " protected @Override void initializePage() {}" + SYSTEM_EOL + "}";
CompilationUnit cu = parse(code);
LexicalPreservingPrinter.setup(cu);
cu.getTypes().forEach(type -> type.getMembers().forEach(member -> {
if (member instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) member;
if (methodDeclaration.getAnnotationByName("Override").isPresent()) {
while (methodDeclaration.getAnnotations().isNonEmpty()) {
AnnotationExpr annotationExpr = methodDeclaration.getAnnotations().get(0);
annotationExpr.remove();
}
}
methodDeclaration.addMarkerAnnotation("Override");
}
}));
assertEquals("public class TestPage extends Page {" + SYSTEM_EOL + SYSTEM_EOL + " @Override" + SYSTEM_EOL + " protected void test() {}" + SYSTEM_EOL + SYSTEM_EOL + " @Override" + SYSTEM_EOL + " protected void initializePage() {}" + SYSTEM_EOL + "}", LexicalPreservingPrinter.print(cu));
}
use of com.github.javaparser.ast.Modifier.Keyword.PUBLIC 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();
}
Aggregations