Search in sources :

Example 6 with NodeList

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);
}
Also used : NodeList(com.github.javaparser.ast.NodeList) ArrayCreationLevel(com.github.javaparser.ast.ArrayCreationLevel) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) Name(com.github.javaparser.ast.expr.Name) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Example 7 with NodeList

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);
}
Also used : TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) NodeList(com.github.javaparser.ast.NodeList) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Example 8 with NodeList

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);
}
Also used : NodeList(com.github.javaparser.ast.NodeList) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) AnnotationMemberDeclaration(com.github.javaparser.ast.body.AnnotationMemberDeclaration) Name(com.github.javaparser.ast.expr.Name) Test(org.junit.Test) AbstractLexicalPreservingTest(com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest)

Example 9 with NodeList

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());
}
Also used : NodeList(com.github.javaparser.ast.NodeList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Test(org.junit.Test)

Example 10 with NodeList

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;
}
Also used : JsonObject(com.google.gson.JsonObject) Package(org.structr.javaparser.entity.Package) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UnsolvedSymbolException(com.github.javaparser.symbolsolver.javaparsermodel.UnsolvedSymbolException) LoggerFactory(org.slf4j.LoggerFactory) SecurityContext(org.structr.common.SecurityContext) StringUtils(org.apache.commons.lang3.StringUtils) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) Actions(org.structr.schema.action.Actions) FrameworkException(org.structr.common.error.FrameworkException) App(org.structr.core.app.App) NodeWithSimpleName(com.github.javaparser.ast.nodeTypes.NodeWithSimpleName) Type(com.github.javaparser.ast.type.Type) Document(org.w3c.dom.Document) Map(java.util.Map) CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaInterface(org.structr.javaparser.entity.JavaInterface) NodeList(com.github.javaparser.ast.NodeList) Folder(org.structr.web.entity.Folder) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Set(java.util.Set) GraphObject(org.structr.core.GraphObject) ResolvedValueDeclaration(com.github.javaparser.resolution.declarations.ResolvedValueDeclaration) Functions(org.structr.core.function.Functions) JsonArray(com.google.gson.JsonArray) List(java.util.List) StructrModule(org.structr.module.StructrModule) JarTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver) SAXException(org.xml.sax.SAXException) LicenseManager(org.structr.api.service.LicenseManager) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) QName(javax.xml.namespace.QName) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeWithOptionalBlockStmt(com.github.javaparser.ast.nodeTypes.NodeWithOptionalBlockStmt) StructrApp(org.structr.core.app.StructrApp) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) Method(org.structr.javaparser.entity.Method) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration) XPath(javax.xml.xpath.XPath) XPathConstants(javax.xml.xpath.XPathConstants) Parameter(com.github.javaparser.ast.body.Parameter) HashMap(java.util.HashMap) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ArrayList(java.util.ArrayList) PropertyMap(org.structr.core.property.PropertyMap) File(org.structr.web.entity.File) JavaClass(org.structr.javaparser.entity.JavaClass) JsonPrimitive(com.google.gson.JsonPrimitive) ClassOrInterface(org.structr.javaparser.entity.ClassOrInterface) LinkedHashSet(java.util.LinkedHashSet) InputSource(org.xml.sax.InputSource) Logger(org.slf4j.Logger) NodeWithModifiers(com.github.javaparser.ast.nodeTypes.NodeWithModifiers) Files(java.nio.file.Files) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) CallableDeclaration(com.github.javaparser.ast.body.CallableDeclaration) AddJarsToIndexFunction(org.structr.javaparser.entity.AddJarsToIndexFunction) AtomicLong(java.util.concurrent.atomic.AtomicLong) XPathFactory(javax.xml.xpath.XPathFactory) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) StringReader(java.io.StringReader) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Paths(java.nio.file.Paths) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) Module(org.structr.javaparser.entity.Module) JavaParser(com.github.javaparser.JavaParser) InputStream(java.io.InputStream) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) NodeList(com.github.javaparser.ast.NodeList) ArrayList(java.util.ArrayList) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) CallableDeclaration(com.github.javaparser.ast.body.CallableDeclaration) BodyDeclaration(com.github.javaparser.ast.body.BodyDeclaration) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) ArrayList(java.util.ArrayList) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Aggregations

NodeList (com.github.javaparser.ast.NodeList)83 Expression (com.github.javaparser.ast.expr.Expression)48 NameExpr (com.github.javaparser.ast.expr.NameExpr)40 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)37 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)36 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)29 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 List (java.util.List)22 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)18 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)17 ArrayList (java.util.ArrayList)17 Collectors (java.util.stream.Collectors)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)16 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 Parameter (com.github.javaparser.ast.body.Parameter)15 Test (org.junit.Test)15 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14