Search in sources :

Example 1 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class LexicalPreservingPrinter method interpret.

private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) {
    LexicalDifferenceCalculator.CalculatedSyntaxModel calculatedSyntaxModel = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(csm, node);
    List<TokenTextElement> indentation = findIndentation(node);
    boolean pendingIndentation = false;
    for (CsmElement element : calculatedSyntaxModel.elements) {
        if (pendingIndentation && !(element instanceof CsmToken && ((CsmToken) element).isNewLine())) {
            indentation.forEach(nodeText::addElement);
        }
        pendingIndentation = false;
        if (element instanceof LexicalDifferenceCalculator.CsmChild) {
            nodeText.addChild(((LexicalDifferenceCalculator.CsmChild) element).getChild());
        } else if (element instanceof CsmToken) {
            CsmToken csmToken = (CsmToken) element;
            nodeText.addToken(csmToken.getTokenType(), csmToken.getContent(node));
            if (csmToken.isNewLine()) {
                pendingIndentation = true;
            }
        } else if (element instanceof CsmMix) {
            CsmMix csmMix = (CsmMix) element;
            csmMix.getElements().forEach(e -> interpret(node, e, nodeText));
        } else {
            throw new UnsupportedOperationException(element.getClass().getSimpleName());
        }
    }
    // so they have to be handled in a special way
    if (node instanceof VariableDeclarator) {
        VariableDeclarator variableDeclarator = (VariableDeclarator) node;
        variableDeclarator.getParentNode().ifPresent(parent -> ((NodeWithVariables<?>) parent).getMaximumCommonType().ifPresent(mct -> {
            int extraArrayLevels = variableDeclarator.getType().getArrayLevel() - mct.getArrayLevel();
            for (int i = 0; i < extraArrayLevels; i++) {
                nodeText.addElement(new TokenTextElement(LBRACKET));
                nodeText.addElement(new TokenTextElement(RBRACKET));
            }
        }));
    }
    return nodeText;
}
Also used : java.util(java.util) AstObserver(com.github.javaparser.ast.observer.AstObserver) CsmMix(com.github.javaparser.printer.concretesyntaxmodel.CsmMix) TreeVisitor(com.github.javaparser.ast.visitor.TreeVisitor) Pair(com.github.javaparser.utils.Pair) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) DataKey(com.github.javaparser.ast.DataKey) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) Utils.assertNotNull(com.github.javaparser.utils.Utils.assertNotNull) Utils.decapitalize(com.github.javaparser.utils.Utils.decapitalize) PropagatingAstObserver(com.github.javaparser.ast.observer.PropagatingAstObserver) Method(java.lang.reflect.Method) Node(com.github.javaparser.ast.Node) NodeList(com.github.javaparser.ast.NodeList) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) TokenTypes.eolTokenKind(com.github.javaparser.TokenTypes.eolTokenKind) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) InvocationTargetException(java.lang.reflect.InvocationTargetException) GeneratedJavaParserConstants(com.github.javaparser.GeneratedJavaParserConstants) com.github.javaparser(com.github.javaparser) ParameterizedType(java.lang.reflect.ParameterizedType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) NodeWithVariables(com.github.javaparser.ast.nodeTypes.NodeWithVariables) ConcreteSyntaxModel(com.github.javaparser.printer.ConcreteSyntaxModel) Utils(com.github.javaparser.utils.Utils) Writer(java.io.Writer) Comment(com.github.javaparser.ast.comments.Comment) Comparator(java.util.Comparator) CsmMix(com.github.javaparser.printer.concretesyntaxmodel.CsmMix) CsmElement(com.github.javaparser.printer.concretesyntaxmodel.CsmElement) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CsmToken(com.github.javaparser.printer.concretesyntaxmodel.CsmToken) NodeWithVariables(com.github.javaparser.ast.nodeTypes.NodeWithVariables)

Example 2 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class DumpVisitor method visit.

@Override
public void visit(final FieldDeclaration n, final Object arg) {
    printOrphanCommentsBeforeThisChildNode(n);
    printJavaComment(n.getComment(), arg);
    printJavadoc(n.getJavaDoc(), arg);
    printMemberAnnotations(n.getAnnotations(), arg);
    printModifiers(n.getModifiers());
    n.getType().accept(this, arg);
    printer.print(" ");
    for (final Iterator<VariableDeclarator> i = n.getVariables().iterator(); i.hasNext(); ) {
        final VariableDeclarator var = i.next();
        var.accept(this, arg);
        if (i.hasNext()) {
            printer.print(", ");
        }
    }
    printer.print(";");
}
Also used : VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 3 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class DumpVisitor method visit.

@Override
public void visit(final VariableDeclarationExpr n, final Object arg) {
    printJavaComment(n.getComment(), arg);
    printAnnotations(n.getAnnotations(), arg);
    printModifiers(n.getModifiers());
    n.getType().accept(this, arg);
    printer.print(" ");
    for (final Iterator<VariableDeclarator> i = n.getVars().iterator(); i.hasNext(); ) {
        final VariableDeclarator v = i.next();
        v.accept(this, arg);
        if (i.hasNext()) {
            printer.print(", ");
        }
    }
}
Also used : VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 4 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class VoidVisitorAdapter method visit.

@Override
public void visit(final VariableDeclarationExpr n, final A arg) {
    visitComment(n.getComment(), arg);
    if (n.getAnnotations() != null) {
        for (final AnnotationExpr a : n.getAnnotations()) {
            a.accept(this, arg);
        }
    }
    n.getType().accept(this, arg);
    for (final VariableDeclarator v : n.getVars()) {
        v.accept(this, arg);
    }
}
Also used : VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 5 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.

the class ModifierVisitorAdapter method visit.

@Override
public Node visit(final VariableDeclarationExpr n, final A arg) {
    visitComment(n, arg);
    final List<AnnotationExpr> annotations = n.getAnnotations();
    if (annotations != null) {
        for (int i = 0; i < annotations.size(); i++) {
            annotations.set(i, (AnnotationExpr) annotations.get(i).accept(this, arg));
        }
        removeNulls(annotations);
    }
    final Type type = (Type) n.getElementType().accept(this, arg);
    if (type == null) {
        return null;
    }
    n.setElementType(type);
    final List<VariableDeclarator> vars = n.getVariables();
    for (int i = 0; i < vars.size(); ) {
        final VariableDeclarator decl = (VariableDeclarator) vars.get(i).accept(this, arg);
        if (decl == null) {
            vars.remove(i);
        } else {
            vars.set(i++, decl);
        }
    }
    if (vars.isEmpty()) {
        return null;
    }
    return n;
}
Also used : AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Aggregations

VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)110 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)50 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)44 Expression (com.github.javaparser.ast.expr.Expression)43 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)41 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)39 NameExpr (com.github.javaparser.ast.expr.NameExpr)33 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)32 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 Test (org.junit.Test)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)18 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)17 CompilationUnit (com.github.javaparser.ast.CompilationUnit)16 NodeList (com.github.javaparser.ast.NodeList)16 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)14 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)13 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)13 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)11 Parameter (com.github.javaparser.ast.body.Parameter)9