From 48789abfd3738d660abb6f8cd758a40c57379597 Mon Sep 17 00:00:00 2001 From: Danny van Bruggen Date: Sun, 28 Jan 2018 15:37:27 +0100 Subject: [PATCH] Some formatting --- .../LexicalPreservingPrinter.java | 71 ++++++++++--------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/LexicalPreservingPrinter.java b/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/LexicalPreservingPrinter.java index a435dc4ea4..ef647fbf03 100644 --- a/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/LexicalPreservingPrinter.java +++ b/javaparser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/LexicalPreservingPrinter.java @@ -54,6 +54,7 @@ import static com.github.javaparser.TokenTypes.eolTokenKind; import static com.github.javaparser.utils.Utils.assertNotNull; import static com.github.javaparser.utils.Utils.decapitalize; +import static java.util.Comparator.*; /** * A Lexical Preserving Printer is used to capture all the lexical information while parsing, update them when @@ -65,7 +66,8 @@ public class LexicalPreservingPrinter { /** * The nodetext for a node is stored in the node's data field. This is the key to set and retrieve it. */ - public static final DataKey NODE_TEXT_DATA = new DataKey() { }; + public static final DataKey NODE_TEXT_DATA = new DataKey() { + }; // // Factory methods @@ -73,6 +75,7 @@ public class LexicalPreservingPrinter { /** * Parse the code and setup the LexicalPreservingPrinter. + * * @deprecated use setup(Node) and the static methods on this class. */ public static Pair, LexicalPreservingPrinter> setup(ParseStart parseStart, @@ -90,11 +93,12 @@ public static Pair, LexicalPreservingPrinter> se * Prepares the node so it can be used in the print methods. * The correct order is: *
    - *
  1. Parse some code
  2. - *
  3. Call this setup method on the result
  4. - *
  5. Make changes to the AST as desired
  6. - *
  7. Use one of the print methods on this class to print out the original source code with your changes added
  8. + *
  9. Parse some code
  10. + *
  11. Call this setup method on the result
  12. + *
  13. Make changes to the AST as desired
  14. + *
  15. Use one of the print methods on this class to print out the original source code with your changes added
  16. *
+ * * @return the node passed as a parameter for your convenience. */ public static N setup(N node) { @@ -143,13 +147,13 @@ public void concretePropertyChange(Node observedNode, ObservableProperty propert if (oldValue == null) { // Find the position of the comment node and put in front of it the comment and a newline int index = nodeText.findChild(observedNode); - nodeText.addChild(index, (Comment)newValue); + nodeText.addChild(index, (Comment) newValue); nodeText.addToken(index + 1, eolTokenKind(), Utils.EOL); } else if (newValue == null) { if (oldValue instanceof JavadocComment) { - JavadocComment javadocComment = (JavadocComment)oldValue; + JavadocComment javadocComment = (JavadocComment) oldValue; List matchingTokens = nodeText.getElements().stream().filter(e -> e.isToken(JAVADOC_COMMENT) - && ((TokenTextElement)e).getText().equals("/**"+javadocComment.getContent()+"*/")).map(e -> (TokenTextElement)e).collect(Collectors.toList()); + && ((TokenTextElement) e).getText().equals("/**" + javadocComment.getContent() + "*/")).map(e -> (TokenTextElement) e).collect(Collectors.toList()); if (matchingTokens.size() != 1) { throw new IllegalStateException(); } @@ -163,13 +167,13 @@ public void concretePropertyChange(Node observedNode, ObservableProperty propert } } else { if (oldValue instanceof JavadocComment) { - JavadocComment oldJavadocComment = (JavadocComment)oldValue; + JavadocComment oldJavadocComment = (JavadocComment) oldValue; List matchingTokens = nodeText.getElements().stream().filter(e -> e.isToken(JAVADOC_COMMENT) - && ((TokenTextElement)e).getText().equals("/**"+oldJavadocComment.getContent()+"*/")).map(e -> (TokenTextElement)e).collect(Collectors.toList()); + && ((TokenTextElement) e).getText().equals("/**" + oldJavadocComment.getContent() + "*/")).map(e -> (TokenTextElement) e).collect(Collectors.toList()); if (matchingTokens.size() != 1) { throw new IllegalStateException(); } - JavadocComment newJavadocComment = (JavadocComment)newValue; + JavadocComment newJavadocComment = (JavadocComment) newValue; nodeText.replace(matchingTokens.get(0), new TokenTextElement(JAVADOC_COMMENT, "/**" + newJavadocComment.getContent() + "*/")); } else { throw new UnsupportedOperationException(); @@ -191,7 +195,7 @@ public void concreteListChange(NodeList changedList, ListChangeType type, int in if (type == ListChangeType.REMOVAL) { new LexicalDifferenceCalculator().calculateListRemovalDifference(findNodeListName(changedList), changedList, index).apply(nodeText, changedList.getParentNodeForChildren()); } else if (type == ListChangeType.ADDITION) { - new LexicalDifferenceCalculator().calculateListAdditionDifference(findNodeListName(changedList),changedList, index, nodeAddedOrRemoved).apply(nodeText, changedList.getParentNodeForChildren()); + new LexicalDifferenceCalculator().calculateListAdditionDifference(findNodeListName(changedList), changedList, index, nodeAddedOrRemoved).apply(nodeText, changedList.getParentNodeForChildren()); } else { throw new UnsupportedOperationException(); } @@ -267,7 +271,7 @@ private static void storeInitialTextForOneNode(Node node, List nodeTo for (JavaToken token : nodeTokens) { elements.add(new Pair<>(token.getRange().get(), new TokenTextElement(token))); } - elements.sort(Comparator.comparing(e -> e.a.begin)); + elements.sort(comparing(e -> e.a.begin)); node.setData(NODE_TEXT_DATA, new NodeText(elements.stream().map(p -> p.b).collect(Collectors.toList()))); } @@ -331,9 +335,9 @@ public static void print(Node node, Writer writer) throws IOException { // Methods to handle transformations // - private static NodeText prettyPrintingTextNode(Node node, NodeText nodeText) { + private static void prettyPrintingTextNode(Node node, NodeText nodeText) { if (node instanceof PrimitiveType) { - PrimitiveType primitiveType = (PrimitiveType)node; + PrimitiveType primitiveType = (PrimitiveType) node; switch (primitiveType.getType()) { case BOOLEAN: nodeText.addToken(BOOLEAN, node.toString()); @@ -362,14 +366,14 @@ private static NodeText prettyPrintingTextNode(Node node, NodeText nodeText) { default: throw new IllegalArgumentException(); } - return nodeText; + return; } if (node instanceof JavadocComment) { - nodeText.addToken(JAVADOC_COMMENT, "/**"+((JavadocComment)node).getContent()+"*/"); - return nodeText; + nodeText.addToken(JAVADOC_COMMENT, "/**" + ((JavadocComment) node).getContent() + "*/"); + return; } - return interpret(node, ConcreteSyntaxModel.forClass(node.getClass()), nodeText); + interpret(node, ConcreteSyntaxModel.forClass(node.getClass()), nodeText); } private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) { @@ -379,7 +383,7 @@ private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) boolean pendingIndentation = false; for (CsmElement element : calculatedSyntaxModel.elements) { - if (pendingIndentation && !(element instanceof CsmToken && ((CsmToken)element).isNewLine())) { + if (pendingIndentation && !(element instanceof CsmToken && ((CsmToken) element).isNewLine())) { indentation.forEach(nodeText::addElement); } pendingIndentation = false; @@ -392,7 +396,7 @@ private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) pendingIndentation = true; } } else if (element instanceof CsmMix) { - CsmMix csmMix = (CsmMix)element; + CsmMix csmMix = (CsmMix) element; csmMix.getElements().forEach(e -> interpret(node, e, nodeText)); } else { throw new UnsupportedOperationException(element.getClass().getSimpleName()); @@ -402,16 +406,15 @@ private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) // so they have to be handled in a special way if (node instanceof VariableDeclarator) { VariableDeclarator variableDeclarator = (VariableDeclarator) node; - variableDeclarator.getParentNode().ifPresent(parent -> { - NodeWithVariables nodeWithVariables = (NodeWithVariables) parent; - nodeWithVariables.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)); - } - }); - }); + 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; } @@ -440,7 +443,7 @@ static List findIndentation(Node node) { } } Collections.reverse(followingNewlines); - for (int i=0;i raw = (Optional)m.invoke(parent); + Optional> raw = (Optional>) m.invoke(parent); if (raw.isPresent() && raw.get() == nodeList) { String name = m.getName(); if (name.startsWith("get")) {