Search in sources :

Example 51 with Node

use of com.github.javaparser.ast.Node in project Gargoyle by callakrsos.

the class VOEditorParser2 method main.

public static void main(String[] args) throws ParseException, IOException {
    String fileName = "C:\\Users\\KYJ\\JAVA_FX\\gagoyleWorkspace\\VisualFxVoEditor\\src\\main\\java\\com\\kyj\\fx\\voeditor\\visual\\main\\model\\vo\\ClassPathEntry.java";
    FileInputStream in = new FileInputStream(fileName);
    CompilationUnit cu;
    try {
        // parse the file
        cu = JavaParser.parse(in);
    } finally {
        in.close();
    }
    PackageDeclaration packageDeclaration = cu.getPackage();
    // System.out.println(packageDeclaration.getName().toString());
    // System.out.println();
    // System.out.println(String.format("package name : %s",
    // packageDeclaration.getName().getName()));
    ClassMeta classMeta = new ClassMeta("");
    classMeta.setPackageName(packageDeclaration.getName().toString());
    ArrayList<FieldMeta> fields = new ArrayList<FieldMeta>();
    VoEditor voEditor = new VoEditor(classMeta, fields);
    List<Node> childrenNodes = cu.getChildrenNodes();
    for (Node n : childrenNodes) {
    }
    new MethodVisitor().visit(cu, null);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassMeta(com.kyj.fx.voeditor.core.model.meta.ClassMeta) FieldMeta(com.kyj.fx.voeditor.core.model.meta.FieldMeta) Node(com.github.javaparser.ast.Node) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration) VoEditor(com.kyj.fx.voeditor.core.VoEditor)

Example 52 with Node

use of com.github.javaparser.ast.Node in project controller by opendaylight.

the class MbeASTVisitor method visit.

@Override
public void visit(final NormalAnnotationExpr expr, final Void arg) {
    super.visit(expr, arg);
    final String fqcn = expr.getName().toString();
    if (fqcn.equals(RequireInterface.class.getCanonicalName()) && expr.getParentNode() instanceof MethodDeclaration) {
        final Node parent = expr.getParentNode();
        if (parent instanceof MethodDeclaration) {
            // remember only top level description annotation
            String reqVal = expr.getPairs().get(0).toString();
            requireIfc.put(((MethodDeclaration) parent).getName(), reqVal);
        }
    }
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Node(com.github.javaparser.ast.Node)

Example 53 with Node

use of com.github.javaparser.ast.Node in project activityinfo by bedatadriven.

the class DefaultUpdatingVisitor method updateAnnotationValue.

private void updateAnnotationValue(AnnotationExpr annotation, String literalValue) {
    List<Node> children = annotation.getChildrenNodes();
    if (children.size() == 2 && children.get(0) instanceof NameExpr && children.get(1) instanceof StringLiteralExpr) {
        StringLiteralExpr literal = (StringLiteralExpr) children.get(1);
        if (!literal.getValue().equals(literalValue)) {
            literal.setValue(literalValue);
            dirty = true;
        }
    } else if (annotation instanceof SingleMemberAnnotationExpr) {
        // Annotations can contain more complex ASTs, for example "part of a string" + "part of another string"
        // In this case, replace wholesale
        SingleMemberAnnotationExpr smae = (SingleMemberAnnotationExpr) annotation;
        smae.setMemberValue(new StringLiteralExpr(literalValue));
    } else {
        throw new RuntimeException("Expected @" + defaultAnnotationName + " to be of type " + SingleMemberAnnotationExpr.class.getName() + ", found:  " + annotation.getClass().getName());
    }
}
Also used : SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) Node(com.github.javaparser.ast.Node) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr)

Example 54 with Node

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

the class Difference method calculate.

/**
 * Calculate the Difference between two CalculatedSyntaxModel elements, determining which elements were kept,
 * which were added and which were removed.
 */
static Difference calculate(LexicalDifferenceCalculator.CalculatedSyntaxModel original, LexicalDifferenceCalculator.CalculatedSyntaxModel after) {
    // For performance reasons we use the positions of matching children
    // to guide the calculation of the difference
    // 
    // Suppose we have:
    // qwerty[A]uiop
    // qwer[A]uiop
    // 
    // with [A] being a child and lowercase letters being tokens
    // 
    // We would calculate the Difference between "qwerty" and "qwer" then we know the A is kept, and then we
    // would calculate the difference between "uiop" and "uiop"
    Map<Node, Integer> childrenInOriginal = findChildrenPositions(original);
    Map<Node, Integer> childrenInAfter = findChildrenPositions(after);
    List<Node> commonChildren = new LinkedList<>(childrenInOriginal.keySet());
    commonChildren.retainAll(childrenInAfter.keySet());
    commonChildren.sort(Comparator.comparingInt(childrenInOriginal::get));
    List<DifferenceElement> elements = new LinkedList<>();
    int originalIndex = 0;
    int afterIndex = 0;
    int commonChildrenIndex = 0;
    while (commonChildrenIndex < commonChildren.size()) {
        Node child = commonChildren.get(commonChildrenIndex++);
        int posOfNextChildInOriginal = childrenInOriginal.get(child);
        int posOfNextChildInAfter = childrenInAfter.get(child);
        if (originalIndex < posOfNextChildInOriginal || afterIndex < posOfNextChildInAfter) {
            elements.addAll(calculateImpl(original.sub(originalIndex, posOfNextChildInOriginal), after.sub(afterIndex, posOfNextChildInAfter)).elements);
        }
        elements.add(new Kept(new CsmChild(child)));
        originalIndex = posOfNextChildInOriginal + 1;
        afterIndex = posOfNextChildInAfter + 1;
    }
    if (originalIndex < original.elements.size() || afterIndex < after.elements.size()) {
        elements.addAll(calculateImpl(original.sub(originalIndex, original.elements.size()), after.sub(afterIndex, after.elements.size())).elements);
    }
    return new Difference(elements);
}
Also used : CsmChild(com.github.javaparser.printer.lexicalpreservation.LexicalDifferenceCalculator.CsmChild) Node(com.github.javaparser.ast.Node)

Example 55 with Node

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

the class LexicalPreservingPrinter method storeInitialTextForOneNode.

private static void storeInitialTextForOneNode(Node node, List<JavaToken> nodeTokens) {
    if (nodeTokens == null) {
        nodeTokens = Collections.emptyList();
    }
    List<Pair<Range, TextElement>> elements = new LinkedList<>();
    for (Node child : node.getChildNodes()) {
        if (!PhantomNodeLogic.isPhantomNode(child)) {
            if (!child.getRange().isPresent()) {
                throw new RuntimeException("Range not present on node " + child);
            }
            elements.add(new Pair<>(child.getRange().get(), new ChildTextElement(child)));
        }
    }
    for (JavaToken token : nodeTokens) {
        elements.add(new Pair<>(token.getRange().get(), new TokenTextElement(token)));
    }
    elements.sort(comparing(e -> e.a.begin));
    node.setData(NODE_TEXT_DATA, new NodeText(elements.stream().map(p -> p.b).collect(Collectors.toList())));
}
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) Node(com.github.javaparser.ast.Node) Pair(com.github.javaparser.utils.Pair)

Aggregations

Node (com.github.javaparser.ast.Node)95 Test (org.junit.Test)24 Expression (com.github.javaparser.ast.expr.Expression)22 NodeList (com.github.javaparser.ast.NodeList)18 Comment (com.github.javaparser.ast.comments.Comment)13 CompilationUnit (com.github.javaparser.ast.CompilationUnit)12 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)12 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 ArrayList (java.util.ArrayList)11 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)8 Collectors (java.util.stream.Collectors)8 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)7 LineComment (com.github.javaparser.ast.comments.LineComment)7 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)7 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)7 SimpleName (com.github.javaparser.ast.expr.SimpleName)7 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)7 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)6 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)6