use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class ArrayCreationLevelTransformationsTest method replacingAnnotation.
@Test
public void replacingAnnotation() throws IOException {
ArrayCreationLevel it = consider("@myAnno []");
it.getAnnotations().set(0, new NormalAnnotationExpr(new Name("myOtherAnno"), new NodeList<>()));
assertTransformedToString("@myOtherAnno() []", it);
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class ClassOrInterfaceDeclarationTransformationsTest method addingTypeParameterAsFirstWhenThereAreSome.
@Test
public void addingTypeParameterAsFirstWhenThereAreSome() throws IOException {
ClassOrInterfaceDeclaration cid = consider("class A<U> {}");
cid.getTypeParameters().addFirst(new TypeParameter("T", new NodeList<>()));
assertTransformedToString("class A<T, U> {}", cid);
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class AnnotationMemberDeclarationTransformationsTest method replacingAnnotation.
@Test
public void replacingAnnotation() {
AnnotationMemberDeclaration it = consider("@myAnno int foo();");
it.getAnnotations().set(0, new NormalAnnotationExpr(new Name("myOtherAnno"), new NodeList<>()));
assertTransformedToString("@myOtherAnno() int foo();", it);
}
use of com.github.javaparser.ast.NodeList in project javaparser by javaparser.
the class ModifierVisitorTest method makeSureParentListsCanBeModified.
@Test
public void makeSureParentListsCanBeModified() {
NodeList<StringLiteralExpr> list = new NodeList<>();
list.add(new StringLiteralExpr("t"));
list.add(new StringLiteralExpr("a"));
list.add(new StringLiteralExpr("b"));
list.add(new StringLiteralExpr("c"));
list.accept(new ModifierVisitor<Void>() {
@Override
public Visitable visit(final StringLiteralExpr n, final Void arg) {
String v = n.getValue();
list.addFirst(new StringLiteralExpr("extra " + v));
list.remove(new StringLiteralExpr("t"));
if (v.equals("a")) {
return new StringLiteralExpr("x");
}
if (v.equals("b")) {
return null;
}
return n;
}
}, null);
assertEquals("extra c", list.get(0).getValue());
assertEquals("extra b", list.get(1).getValue());
assertEquals("extra a", list.get(2).getValue());
assertEquals("extra t", list.get(3).getValue());
assertEquals("x", list.get(4).getValue());
assertEquals("c", list.get(5).getValue());
assertEquals(6, list.size());
}
use of com.github.javaparser.ast.NodeList in project structr by structr.
the class JavaParserModule method toJson.
private JsonResult toJson(final CompilationUnit cu) {
final JsonResult jsonResult = new JsonResult();
final NodeList<TypeDeclaration<?>> types = cu.getTypes();
if (types.isEmpty()) {
return jsonResult;
}
final TypeDeclaration<?> type = types.get(0);
jsonResult.addName(type);
jsonResult.addModifiers(type);
final Optional<PackageDeclaration> pkg = cu.getPackageDeclaration();
if (pkg.isPresent()) {
jsonResult.addPackage(pkg.get());
}
final List<BodyDeclaration<?>> members = type.getMembers();
final List<JsonResult> membersList = new ArrayList<>();
members.forEach((t) -> {
final JsonResult member = new JsonResult();
if (t instanceof FieldDeclaration) {
final FieldDeclaration fd = t.asFieldDeclaration();
member.addName(fd.getVariable(0));
member.addType(fd.getVariable(0).getType());
member.addModifiers(fd);
} else if (t instanceof CallableDeclaration) {
final CallableDeclaration callable = t.asCallableDeclaration();
if (t instanceof ConstructorDeclaration) {
final ConstructorDeclaration cd = t.asConstructorDeclaration();
member.addName(cd);
member.isConstructor();
member.addModifiers(cd);
} else if (t instanceof MethodDeclaration) {
final MethodDeclaration md = t.asMethodDeclaration();
member.addName(md);
member.isMethod();
member.addReturnType(md.getType());
member.addModifiers(md);
member.addBody(md);
}
final NodeList<Parameter> parameters = callable.getParameters();
List<JsonResult> parameterList = new ArrayList<>();
parameters.forEach((p) -> {
JsonResult param = new JsonResult();
param.addName(p);
param.addType(p.getType());
param.addModifiers(p);
parameterList.add(param);
});
member.addParameters(parameterList);
}
membersList.add(member);
});
jsonResult.addMembers(membersList);
return jsonResult;
}
Aggregations