| 1 | /* |
|---|---|
| 2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
| 3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
| 4 | * |
| 5 | * This file is part of JavaParser. |
| 6 | * |
| 7 | * JavaParser can be used either under the terms of |
| 8 | * a) the GNU Lesser General Public License as published by |
| 9 | * the Free Software Foundation, either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * b) the terms of the Apache License |
| 12 | * |
| 13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
| 14 | * LICENCE.APACHE. Please refer to those files for details. |
| 15 | * |
| 16 | * JavaParser is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | */ |
| 21 | |
| 22 | package com.github.javaparser.printer.lexicalpreservation; |
| 23 | |
| 24 | import com.github.javaparser.JavaToken; |
| 25 | import com.github.javaparser.Range; |
| 26 | import com.github.javaparser.ast.DataKey; |
| 27 | import com.github.javaparser.ast.Modifier; |
| 28 | import com.github.javaparser.ast.Node; |
| 29 | import com.github.javaparser.ast.NodeList; |
| 30 | import com.github.javaparser.ast.body.VariableDeclarator; |
| 31 | import com.github.javaparser.ast.comments.BlockComment; |
| 32 | import com.github.javaparser.ast.comments.Comment; |
| 33 | import com.github.javaparser.ast.comments.JavadocComment; |
| 34 | import com.github.javaparser.ast.comments.LineComment; |
| 35 | import com.github.javaparser.ast.nodeTypes.NodeWithVariables; |
| 36 | import com.github.javaparser.ast.observer.AstObserver; |
| 37 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 38 | import com.github.javaparser.ast.observer.PropagatingAstObserver; |
| 39 | import com.github.javaparser.ast.type.PrimitiveType; |
| 40 | import com.github.javaparser.ast.visitor.TreeVisitor; |
| 41 | import com.github.javaparser.printer.ConcreteSyntaxModel; |
| 42 | import com.github.javaparser.printer.concretesyntaxmodel.CsmElement; |
| 43 | import com.github.javaparser.printer.concretesyntaxmodel.CsmIndent; |
| 44 | import com.github.javaparser.printer.concretesyntaxmodel.CsmMix; |
| 45 | import com.github.javaparser.printer.concretesyntaxmodel.CsmToken; |
| 46 | import com.github.javaparser.printer.concretesyntaxmodel.CsmUnindent; |
| 47 | import com.github.javaparser.utils.LineSeparator; |
| 48 | import com.github.javaparser.utils.Pair; |
| 49 | |
| 50 | import java.io.IOException; |
| 51 | import java.io.StringWriter; |
| 52 | import java.io.Writer; |
| 53 | import java.lang.reflect.InvocationTargetException; |
| 54 | import java.lang.reflect.Method; |
| 55 | import java.lang.reflect.ParameterizedType; |
| 56 | import java.util.Collections; |
| 57 | import java.util.IdentityHashMap; |
| 58 | import java.util.Iterator; |
| 59 | import java.util.LinkedList; |
| 60 | import java.util.List; |
| 61 | import java.util.Map; |
| 62 | import java.util.Optional; |
| 63 | |
| 64 | import static com.github.javaparser.GeneratedJavaParserConstants.*; |
| 65 | import static com.github.javaparser.TokenTypes.eolTokenKind; |
| 66 | import static com.github.javaparser.utils.Utils.assertNotNull; |
| 67 | import static com.github.javaparser.utils.Utils.decapitalize; |
| 68 | import static java.util.Comparator.comparing; |
| 69 | import static java.util.stream.Collectors.toList; |
| 70 | |
| 71 | /** |
| 72 | * A Lexical Preserving Printer is used to capture all the lexical information while parsing, update them when |
| 73 | * operating on the AST and then used them to reproduce the source code |
| 74 | * in its original formatting including the AST changes. |
| 75 | */ |
| 76 | public class LexicalPreservingPrinter { |
| 77 | |
| 78 | private static AstObserver observer; |
| 79 | |
| 80 | /** |
| 81 | * The nodetext for a node is stored in the node's data field. This is the key to set and retrieve it. |
| 82 | */ |
| 83 | public static final DataKey<NodeText> NODE_TEXT_DATA = new DataKey<NodeText>() { |
| 84 | }; |
| 85 | |
| 86 | private static final LexicalDifferenceCalculator LEXICAL_DIFFERENCE_CALCULATOR = new LexicalDifferenceCalculator(); |
| 87 | |
| 88 | // |
| 89 | // Factory methods |
| 90 | // |
| 91 | |
| 92 | /** |
| 93 | * Prepares the node so it can be used in the print methods. |
| 94 | * The correct order is: |
| 95 | * <ol> |
| 96 | * <li>Parse some code</li> |
| 97 | * <li>Call this setup method on the result</li> |
| 98 | * <li>Make changes to the AST as desired</li> |
| 99 | * <li>Use one of the print methods on this class to print out the original source code with your changes added</li> |
| 100 | * </ol> |
| 101 | * |
| 102 | * @return the node passed as a parameter for your convenience. |
| 103 | */ |
| 104 | public static <N extends Node> N setup(N node) { |
| 105 | assertNotNull(node); |
| 106 | |
| 107 | if (observer == null) { |
| 108 | observer = createObserver(); |
| 109 | } |
| 110 | |
| 111 | node.getTokenRange().ifPresent(r -> { |
| 112 | storeInitialText(node); |
| 113 | // Setup observer |
| 114 | if (!node.isRegistered(observer)) { |
| 115 | node.registerForSubtree(observer); |
| 116 | } |
| 117 | }); |
| 118 | return node; |
| 119 | } |
| 120 | |
| 121 | // |
| 122 | // Constructor and setup |
| 123 | // |
| 124 | |
| 125 | private static AstObserver createObserver() { |
| 126 | return new LexicalPreservingPrinter.Observer(); |
| 127 | } |
| 128 | |
| 129 | private static class Observer extends PropagatingAstObserver { |
| 130 | |
| 131 | @Override |
| 132 | public void concretePropertyChange(Node observedNode, ObservableProperty property, Object oldValue, Object newValue) { |
| 133 | if (oldValue == newValue) { |
| 134 | // Not really a change, ignore |
| 135 | return; |
| 136 | } |
| 137 | if (property == ObservableProperty.RANGE || property == ObservableProperty.COMMENTED_NODE) { |
| 138 | return; |
| 139 | } |
| 140 | if (property == ObservableProperty.COMMENT) { |
| 141 | Optional<Node> parentNode = observedNode.getParentNode(); |
| 142 | NodeText nodeText = parentNode |
| 143 | .map(parent -> getOrCreateNodeText(parentNode.get())) |
| 144 | // We're at the root node. |
| 145 | .orElse(getOrCreateNodeText(observedNode)); |
| 146 | |
| 147 | if (oldValue == null) { |
| 148 | int index = parentNode.isPresent() ? |
| 149 | // Find the position of the comment node and put in front of it the [...] |
| 150 | nodeText.findChild(observedNode) : |
| 151 | // |
| 152 | 0; |
| 153 | // Add the same indent depth of the comment to the following node |
| 154 | fixIndentOfMovedNode(nodeText, index); |
| 155 | |
| 156 | LineSeparator lineSeparator = observedNode.getLineEndingStyleOrDefault(LineSeparator.SYSTEM); |
| 157 | nodeText.addElement(index, makeCommentToken((Comment) newValue)); |
| 158 | nodeText.addToken(index + 1, eolTokenKind(lineSeparator), lineSeparator.asRawString()); |
| 159 | } else if (newValue == null) { |
| 160 | if (oldValue instanceof Comment) { |
| 161 | if (((Comment) oldValue).isOrphan()) { |
| 162 | nodeText = getOrCreateNodeText(observedNode); |
| 163 | } |
| 164 | int index = getIndexOfComment((Comment) oldValue, nodeText); |
| 165 | nodeText.removeElement(index); |
| 166 | if (nodeText.getElements().get(index).isNewline()) { |
| 167 | nodeText.removeElement(index); |
| 168 | } |
| 169 | } else { |
| 170 | throw new UnsupportedOperationException(); |
| 171 | } |
| 172 | } else { |
| 173 | List<TokenTextElement> matchingTokens = findTokenTextElementForComment((Comment) oldValue, nodeText); |
| 174 | |
| 175 | if (matchingTokens.size() != 1) { |
| 176 | throw new IllegalStateException("The matching comment to be replaced could not be found"); |
| 177 | } |
| 178 | |
| 179 | Comment newComment = (Comment) newValue; |
| 180 | TokenTextElement matchingElement = matchingTokens.get(0); |
| 181 | nodeText.replace(matchingElement.and(matchingElement.matchByRange()), makeCommentToken(newComment)); |
| 182 | } |
| 183 | } |
| 184 | NodeText nodeText = getOrCreateNodeText(observedNode); |
| 185 | |
| 186 | if (nodeText == null) { |
| 187 | throw new NullPointerException(observedNode.getClass().getSimpleName()); |
| 188 | } |
| 189 | |
| 190 | LEXICAL_DIFFERENCE_CALCULATOR.calculatePropertyChange(nodeText, observedNode, property, oldValue, newValue); |
| 191 | } |
| 192 | |
| 193 | private TokenTextElement makeCommentToken(Comment newComment) { |
| 194 | if (newComment.isJavadocComment()) { |
| 195 | return new TokenTextElement(JAVADOC_COMMENT, "/**" + newComment.getContent() + "*/"); |
| 196 | } |
| 197 | if (newComment.isLineComment()) { |
| 198 | return new TokenTextElement(SINGLE_LINE_COMMENT, "//" + newComment.getContent()); |
| 199 | } |
| 200 | if (newComment.isBlockComment()) { |
| 201 | return new TokenTextElement(MULTI_LINE_COMMENT, "/*" + newComment.getContent() + "*/"); |
| 202 | } |
| 203 | throw new UnsupportedOperationException("Unknown type of comment: " + newComment.getClass().getSimpleName()); |
| 204 | |
| 205 | } |
| 206 | |
| 207 | private int getIndexOfComment(Comment oldValue, NodeText nodeText) { |
| 208 | List<TokenTextElement> matchingTokens = findTokenTextElementForComment(oldValue, nodeText); |
| 209 | |
| 210 | if (!matchingTokens.isEmpty()) { |
| 211 | TextElement matchingElement = matchingTokens.get(0); |
| 212 | return nodeText.findElement(matchingElement.and(matchingElement.matchByRange())); |
| 213 | } |
| 214 | // If no matching TokenTextElements were found, we try searching through ChildTextElements as well |
| 215 | List<ChildTextElement> matchingChilds = findChildTextElementForComment(oldValue, nodeText); |
| 216 | ChildTextElement matchingChild = matchingChilds.get(0); |
| 217 | return nodeText.findElement(matchingChild.and(matchingChild.matchByRange())); |
| 218 | } |
| 219 | |
| 220 | private List<ChildTextElement> findChildTextElementForComment(Comment oldValue, NodeText nodeText) { |
| 221 | List<ChildTextElement> matchingChildElements; |
| 222 | |
| 223 | matchingChildElements = nodeText.getElements().stream() |
| 224 | .filter(e -> e.isChild()) |
| 225 | .map(c -> (ChildTextElement) c) |
| 226 | .filter(c -> c.isComment()) |
| 227 | .filter(c -> ((Comment) c.getChild()).getContent().equals(oldValue.getContent())) |
| 228 | .collect(toList()); |
| 229 | |
| 230 | if (matchingChildElements.size() > 1) { |
| 231 | // Duplicate child nodes found, refine the result |
| 232 | matchingChildElements = matchingChildElements.stream() |
| 233 | .filter(t -> isEqualRange(t.getChild().getRange(), oldValue.getRange())) |
| 234 | .collect(toList()); |
| 235 | } |
| 236 | |
| 237 | if (matchingChildElements.size() != 1) { |
| 238 | throw new IllegalStateException("The matching child text element for the comment to be removed could not be found."); |
| 239 | } |
| 240 | |
| 241 | return matchingChildElements; |
| 242 | } |
| 243 | |
| 244 | private List<TokenTextElement> findTokenTextElementForComment(Comment oldValue, NodeText nodeText) { |
| 245 | List<TokenTextElement> matchingTokens; |
| 246 | |
| 247 | if (oldValue instanceof JavadocComment) { |
| 248 | matchingTokens = nodeText.getElements().stream() |
| 249 | .filter(e -> e.isToken(JAVADOC_COMMENT)) |
| 250 | .map(e -> (TokenTextElement) e) |
| 251 | .filter(t -> t.getText().equals("/**" + oldValue.getContent() + "*/")) |
| 252 | .collect(toList()); |
| 253 | } else if (oldValue instanceof BlockComment) { |
| 254 | matchingTokens = nodeText.getElements().stream() |
| 255 | .filter(e -> e.isToken(MULTI_LINE_COMMENT)) |
| 256 | .map(e -> (TokenTextElement) e) |
| 257 | .filter(t -> t.getText().equals("/*" + oldValue.getContent() + "*/")) |
| 258 | .collect(toList()); |
| 259 | } else { |
| 260 | matchingTokens = nodeText.getElements().stream() |
| 261 | .filter(e -> e.isToken(SINGLE_LINE_COMMENT)) |
| 262 | .map(e -> (TokenTextElement) e) |
| 263 | .filter(t -> t.getText().trim().equals(("//" + oldValue.getContent()).trim())) |
| 264 | .collect(toList()); |
| 265 | } |
| 266 | |
| 267 | if (matchingTokens.size() > 1) { |
| 268 | // Duplicate comments found, refine the result |
| 269 | matchingTokens = matchingTokens.stream() |
| 270 | .filter(t -> isEqualRange(t.getToken().getRange(), oldValue.getRange())) |
| 271 | .collect(toList()); |
| 272 | } |
| 273 | |
| 274 | return matchingTokens; |
| 275 | } |
| 276 | |
| 277 | private boolean isEqualRange(Optional<Range> range1, Optional<Range> range2) { |
| 278 | if (range1.isPresent() && range2.isPresent()) { |
| 279 | return range1.get().equals(range2.get()); |
| 280 | } |
| 281 | |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * This method inserts new space tokens at the given {@code index}. If a new comment is added to the node |
| 287 | * at the position of {@code index}, the new comment and the node will have the same indent. |
| 288 | * |
| 289 | * @param nodeText The text of the node |
| 290 | * @param index The position where a new comment will be added to |
| 291 | */ |
| 292 | private void fixIndentOfMovedNode(NodeText nodeText, int index) { |
| 293 | if (index <= 0) { |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | for (int i = index - 1; i >= 0; i--) { |
| 298 | TextElement spaceCandidate = nodeText.getTextElement(i); |
| 299 | if (!spaceCandidate.isSpaceOrTab()) { |
| 300 | if (spaceCandidate.isNewline() && i != index - 1) { |
| 301 | for (int j = 0; j < (index - 1) - i; j++) { |
| 302 | nodeText.addElement(index, new TokenTextElement(JavaToken.Kind.SPACE.getKind())); |
| 303 | } |
| 304 | } |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public void concreteListChange(NodeList<?> changedList, ListChangeType type, int index, Node nodeAddedOrRemoved) { |
| 312 | NodeText nodeText = getOrCreateNodeText(changedList.getParentNodeForChildren()); |
| 313 | final List<DifferenceElement> differenceElements; |
| 314 | if (type == AstObserver.ListChangeType.REMOVAL) { |
| 315 | differenceElements = LEXICAL_DIFFERENCE_CALCULATOR.calculateListRemovalDifference(findNodeListName(changedList), changedList, index); |
| 316 | } else if (type == AstObserver.ListChangeType.ADDITION) { |
| 317 | differenceElements = LEXICAL_DIFFERENCE_CALCULATOR.calculateListAdditionDifference(findNodeListName(changedList), changedList, index, nodeAddedOrRemoved); |
| 318 | } else { |
| 319 | throw new UnsupportedOperationException(); |
| 320 | } |
| 321 | |
| 322 | Difference difference = new Difference(differenceElements, nodeText, changedList.getParentNodeForChildren()); |
| 323 | difference.apply(); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void concreteListReplacement(NodeList<?> changedList, int index, Node oldValue, Node newValue) { |
| 328 | NodeText nodeText = getOrCreateNodeText(changedList.getParentNodeForChildren()); |
| 329 | List<DifferenceElement> differenceElements = LEXICAL_DIFFERENCE_CALCULATOR.calculateListReplacementDifference(findNodeListName(changedList), changedList, index, newValue); |
| 330 | |
| 331 | Difference difference = new Difference(differenceElements, nodeText, changedList.getParentNodeForChildren()); |
| 332 | difference.apply(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | private static void storeInitialText(Node root) { |
| 337 | Map<Node, List<JavaToken>> tokensByNode = new IdentityHashMap<>(); |
| 338 | |
| 339 | // We go over tokens and find to which nodes they belong. Note that we do not traverse the tokens as they were |
| 340 | // on a list but as they were organized in a tree. At each time we select only the branch corresponding to the |
| 341 | // range of interest and ignore all other branches |
| 342 | root.getTokenRange().ifPresent(rootTokenRange -> { |
| 343 | for (JavaToken token : rootTokenRange) { |
| 344 | Range tokenRange = token.getRange().orElseThrow(() -> new RuntimeException("Token without range: " + token)); |
| 345 | Node owner = findNodeForToken(root, tokenRange).orElseThrow(() -> new RuntimeException("Token without node owning it: " + token)); |
| 346 | if (!tokensByNode.containsKey(owner)) { |
| 347 | tokensByNode.put(owner, new LinkedList<>()); |
| 348 | } |
| 349 | tokensByNode.get(owner).add(token); |
| 350 | } |
| 351 | |
| 352 | // Now that we know the tokens we use them to create the initial NodeText for each node |
| 353 | new TreeVisitor() { |
| 354 | @Override |
| 355 | public void process(Node node) { |
| 356 | if (!PhantomNodeLogic.isPhantomNode(node)) { |
| 357 | LexicalPreservingPrinter.storeInitialTextForOneNode(node, tokensByNode.get(node)); |
| 358 | } |
| 359 | } |
| 360 | }.visitBreadthFirst(root); |
| 361 | }); |
| 362 | } |
| 363 | |
| 364 | private static Optional<Node> findNodeForToken(Node node, Range tokenRange) { |
| 365 | if (PhantomNodeLogic.isPhantomNode(node)) { |
| 366 | return Optional.empty(); |
| 367 | } |
| 368 | if(!node.getRange().isPresent()) { |
| 369 | return Optional.empty(); |
| 370 | } |
| 371 | if (!node.getRange().get().contains(tokenRange)) { |
| 372 | return Optional.empty(); |
| 373 | } |
| 374 | |
| 375 | for (Node child : node.getChildNodes()) { |
| 376 | Optional<Node> found = findNodeForToken(child, tokenRange); |
| 377 | if (found.isPresent()) { |
| 378 | return found; |
| 379 | } |
| 380 | } |
| 381 | return Optional.of(node); |
| 382 | } |
| 383 | |
| 384 | private static void storeInitialTextForOneNode(Node node, List<JavaToken> nodeTokens) { |
| 385 | if (nodeTokens == null) { |
| 386 | nodeTokens = Collections.emptyList(); |
| 387 | } |
| 388 | List<Pair<Range, TextElement>> elements = new LinkedList<>(); |
| 389 | for (Node child : node.getChildNodes()) { |
| 390 | if (!PhantomNodeLogic.isPhantomNode(child)) { |
| 391 | if (!child.getRange().isPresent()) { |
| 392 | throw new RuntimeException("Range not present on node " + child); |
| 393 | } |
| 394 | elements.add(new Pair<>(child.getRange().get(), new ChildTextElement(child))); |
| 395 | } |
| 396 | } |
| 397 | for (JavaToken token : nodeTokens) { |
| 398 | elements.add(new Pair<>(token.getRange().get(), new TokenTextElement(token))); |
| 399 | } |
| 400 | elements.sort(comparing(e -> e.a.begin)); |
| 401 | node.setData(NODE_TEXT_DATA, new NodeText(elements.stream().map(p -> p.b).collect(toList()))); |
| 402 | } |
| 403 | |
| 404 | // |
| 405 | // Iterators |
| 406 | // |
| 407 | |
| 408 | private static Iterator<TokenTextElement> tokensPreceeding(final Node node) { |
| 409 | if (!node.getParentNode().isPresent()) { |
| 410 | return new TextElementIteratorsFactory.EmptyIterator<>(); |
| 411 | } |
| 412 | // There is the awfully painful case of the fake types involved in variable declarators and |
| 413 | // fields or variable declaration that are, of course, an exception... |
| 414 | |
| 415 | NodeText parentNodeText = getOrCreateNodeText(node.getParentNode().get()); |
| 416 | int index = parentNodeText.tryToFindChild(node); |
| 417 | if (index == NodeText.NOT_FOUND) { |
| 418 | if (node.getParentNode().get() instanceof VariableDeclarator) { |
| 419 | return tokensPreceeding(node.getParentNode().get()); |
| 420 | } else { |
| 421 | throw new IllegalArgumentException( |
| 422 | String.format("I could not find child '%s' in parent '%s'. parentNodeText: %s", |
| 423 | node, node.getParentNode().get(), parentNodeText)); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return new TextElementIteratorsFactory.CascadingIterator<>( |
| 428 | TextElementIteratorsFactory.partialReverseIterator(parentNodeText, index - 1), |
| 429 | () -> tokensPreceeding(node.getParentNode().get())); |
| 430 | } |
| 431 | |
| 432 | // |
| 433 | // Printing methods |
| 434 | // |
| 435 | |
| 436 | /** |
| 437 | * Print a Node into a String, preserving the lexical information. |
| 438 | */ |
| 439 | public static String print(Node node) { |
| 440 | StringWriter writer = new StringWriter(); |
| 441 | try { |
| 442 | print(node, writer); |
| 443 | } catch (IOException e) { |
| 444 | throw new RuntimeException("Unexpected IOException on a StringWriter", e); |
| 445 | } |
| 446 | return writer.toString(); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Print a Node into a Writer, preserving the lexical information. |
| 451 | */ |
| 452 | public static void print(Node node, Writer writer) throws IOException { |
| 453 | if (!node.containsData(NODE_TEXT_DATA)) { |
| 454 | getOrCreateNodeText(node); |
| 455 | } |
| 456 | final NodeText text = node.getData(NODE_TEXT_DATA); |
| 457 | writer.append(text.expand()); |
| 458 | } |
| 459 | |
| 460 | // |
| 461 | // Methods to handle transformations |
| 462 | // |
| 463 | |
| 464 | private static void prettyPrintingTextNode(Node node, NodeText nodeText) { |
| 465 | if (node instanceof PrimitiveType) { |
| 466 | PrimitiveType primitiveType = (PrimitiveType) node; |
| 467 | switch (primitiveType.getType()) { |
| 468 | case BOOLEAN: |
| 469 | nodeText.addToken(BOOLEAN, node.toString()); |
| 470 | break; |
| 471 | case CHAR: |
| 472 | nodeText.addToken(CHAR, node.toString()); |
| 473 | break; |
| 474 | case BYTE: |
| 475 | nodeText.addToken(BYTE, node.toString()); |
| 476 | break; |
| 477 | case SHORT: |
| 478 | nodeText.addToken(SHORT, node.toString()); |
| 479 | break; |
| 480 | case INT: |
| 481 | nodeText.addToken(INT, node.toString()); |
| 482 | break; |
| 483 | case LONG: |
| 484 | nodeText.addToken(LONG, node.toString()); |
| 485 | break; |
| 486 | case FLOAT: |
| 487 | nodeText.addToken(FLOAT, node.toString()); |
| 488 | break; |
| 489 | case DOUBLE: |
| 490 | nodeText.addToken(DOUBLE, node.toString()); |
| 491 | break; |
| 492 | default: |
| 493 | throw new IllegalArgumentException(); |
| 494 | } |
| 495 | return; |
| 496 | } |
| 497 | if (node instanceof JavadocComment) { |
| 498 | nodeText.addToken(JAVADOC_COMMENT, "/**" + ((JavadocComment) node).getContent() + "*/"); |
| 499 | return; |
| 500 | } |
| 501 | if (node instanceof BlockComment) { |
| 502 | nodeText.addToken(MULTI_LINE_COMMENT, "/*" + ((BlockComment) node).getContent() + "*/"); |
| 503 | return; |
| 504 | } |
| 505 | if (node instanceof LineComment) { |
| 506 | nodeText.addToken(SINGLE_LINE_COMMENT, "//" + ((LineComment) node).getContent()); |
| 507 | return; |
| 508 | } |
| 509 | if (node instanceof Modifier) { |
| 510 | Modifier modifier = (Modifier) node; |
| 511 | nodeText.addToken(LexicalDifferenceCalculator.toToken(modifier), modifier.getKeyword().asString()); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | interpret(node, ConcreteSyntaxModel.forClass(node.getClass()), nodeText); |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * TODO: Process CsmIndent and CsmUnindent before reaching this point |
| 520 | */ |
| 521 | private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText) { |
| 522 | LexicalDifferenceCalculator.CalculatedSyntaxModel calculatedSyntaxModel = new LexicalDifferenceCalculator().calculatedSyntaxModelForNode(csm, node); |
| 523 | |
| 524 | List<TokenTextElement> indentation = findIndentation(node); |
| 525 | |
| 526 | boolean pendingIndentation = false; |
| 527 | for (CsmElement element : calculatedSyntaxModel.elements) { |
| 528 | if (element instanceof CsmIndent) { |
| 529 | int indexCurrentElement = calculatedSyntaxModel.elements.indexOf(element); |
| 530 | if (calculatedSyntaxModel.elements.size() > indexCurrentElement && |
| 531 | !(calculatedSyntaxModel.elements.get(indexCurrentElement + 1) instanceof CsmUnindent)) { |
| 532 | for (int i = 0; i < Difference.STANDARD_INDENTATION_SIZE; i++) { |
| 533 | indentation.add(new TokenTextElement(SPACE, " ")); |
| 534 | } |
| 535 | } |
| 536 | } else if (element instanceof CsmUnindent) { |
| 537 | for (int i = 0; i < Difference.STANDARD_INDENTATION_SIZE && indentation.size() > 0; i++) { |
| 538 | indentation.remove(indentation.size() - 1); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (pendingIndentation && !(element instanceof CsmToken && ((CsmToken) element).isNewLine())) { |
| 543 | indentation.forEach(nodeText::addElement); |
| 544 | } |
| 545 | |
| 546 | pendingIndentation = false; |
| 547 | if (element instanceof LexicalDifferenceCalculator.CsmChild) { |
| 548 | nodeText.addChild(((LexicalDifferenceCalculator.CsmChild) element).getChild()); |
| 549 | } else if (element instanceof CsmToken) { |
| 550 | CsmToken csmToken = (CsmToken) element; |
| 551 | nodeText.addToken(csmToken.getTokenType(), csmToken.getContent(node)); |
| 552 | if (csmToken.isNewLine()) { |
| 553 | pendingIndentation = true; |
| 554 | } |
| 555 | } else if (element instanceof CsmMix) { |
| 556 | CsmMix csmMix = (CsmMix) element; |
| 557 | csmMix.getElements().forEach(e -> interpret(node, e, nodeText)); |
| 558 | } else { |
| 559 | // Indentation should probably be dealt with before because an indentation has effects also on the |
| 560 | // following lines |
| 561 | if (!(element instanceof CsmIndent) && !(element instanceof CsmUnindent)) { |
| 562 | throw new UnsupportedOperationException(element.getClass().getSimpleName()); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | // Array brackets are a pain... we do not have a way to represent them explicitly in the AST |
| 567 | // so they have to be handled in a special way |
| 568 | if (node instanceof VariableDeclarator) { |
| 569 | VariableDeclarator variableDeclarator = (VariableDeclarator) node; |
| 570 | variableDeclarator.getParentNode().ifPresent(parent -> |
| 571 | ((NodeWithVariables<?>) parent).getMaximumCommonType().ifPresent(mct -> { |
| 572 | int extraArrayLevels = variableDeclarator.getType().getArrayLevel() - mct.getArrayLevel(); |
| 573 | for (int i = 0; i < extraArrayLevels; i++) { |
| 574 | nodeText.addElement(new TokenTextElement(LBRACKET)); |
| 575 | nodeText.addElement(new TokenTextElement(RBRACKET)); |
| 576 | } |
| 577 | }) |
| 578 | ); |
| 579 | } |
| 580 | return nodeText; |
| 581 | } |
| 582 | |
| 583 | // Visible for testing |
| 584 | static NodeText getOrCreateNodeText(Node node) { |
| 585 | if (!node.containsData(NODE_TEXT_DATA)) { |
| 586 | NodeText nodeText = new NodeText(); |
| 587 | node.setData(NODE_TEXT_DATA, nodeText); |
| 588 | prettyPrintingTextNode(node, nodeText); |
| 589 | } |
| 590 | return node.getData(NODE_TEXT_DATA); |
| 591 | } |
| 592 | |
| 593 | // Visible for testing |
| 594 | static List<TokenTextElement> findIndentation(Node node) { |
| 595 | List<TokenTextElement> followingNewlines = new LinkedList<>(); |
| 596 | Iterator<TokenTextElement> it = tokensPreceeding(node); |
| 597 | while (it.hasNext()) { |
| 598 | TokenTextElement tte = it.next(); |
| 599 | if (tte.getTokenKind() == SINGLE_LINE_COMMENT |
| 600 | || tte.isNewline()) { |
| 601 | break; |
| 602 | } else { |
| 603 | followingNewlines.add(tte); |
| 604 | } |
| 605 | } |
| 606 | Collections.reverse(followingNewlines); |
| 607 | for (int i = 0; i < followingNewlines.size(); i++) { |
| 608 | if (!followingNewlines.get(i).isSpaceOrTab()) { |
| 609 | return followingNewlines.subList(0, i); |
| 610 | } |
| 611 | } |
| 612 | return followingNewlines; |
| 613 | } |
| 614 | |
| 615 | // |
| 616 | // Helper methods |
| 617 | // |
| 618 | |
| 619 | private static boolean isReturningOptionalNodeList(Method m) { |
| 620 | if (!m.getReturnType().getCanonicalName().equals(Optional.class.getCanonicalName())) { |
| 621 | return false; |
| 622 | } |
| 623 | if (!(m.getGenericReturnType() instanceof ParameterizedType)) { |
| 624 | return false; |
| 625 | } |
| 626 | ParameterizedType parameterizedType = (ParameterizedType) m.getGenericReturnType(); |
| 627 | java.lang.reflect.Type optionalArgument = parameterizedType.getActualTypeArguments()[0]; |
| 628 | return (optionalArgument.getTypeName().startsWith(NodeList.class.getCanonicalName())); |
| 629 | } |
| 630 | |
| 631 | private static ObservableProperty findNodeListName(NodeList<?> nodeList) { |
| 632 | Node parent = nodeList.getParentNodeForChildren(); |
| 633 | for (Method m : parent.getClass().getMethods()) { |
| 634 | if (m.getParameterCount() == 0 && m.getReturnType().getCanonicalName().equals(NodeList.class.getCanonicalName())) { |
| 635 | try { |
| 636 | Object raw = m.invoke(parent); |
| 637 | if (!(raw instanceof NodeList)) { |
| 638 | throw new IllegalStateException("Expected NodeList, found " + raw.getClass().getCanonicalName()); |
| 639 | } |
| 640 | NodeList<?> result = (NodeList<?>) raw; |
| 641 | if (result == nodeList) { |
| 642 | String name = m.getName(); |
| 643 | if (name.startsWith("get")) { |
| 644 | name = name.substring("get".length()); |
| 645 | } |
| 646 | return ObservableProperty.fromCamelCaseName(decapitalize(name)); |
| 647 | } |
| 648 | } catch (IllegalAccessException | InvocationTargetException e) { |
| 649 | throw new RuntimeException(e); |
| 650 | } |
| 651 | } else if (m.getParameterCount() == 0 && isReturningOptionalNodeList(m)) { |
| 652 | try { |
| 653 | Optional<NodeList<?>> raw = (Optional<NodeList<?>>) m.invoke(parent); |
| 654 | if (raw.isPresent() && raw.get() == nodeList) { |
| 655 | String name = m.getName(); |
| 656 | if (name.startsWith("get")) { |
| 657 | name = name.substring("get".length()); |
| 658 | } |
| 659 | return ObservableProperty.fromCamelCaseName(decapitalize(name)); |
| 660 | } |
| 661 | } catch (IllegalAccessException | InvocationTargetException e) { |
| 662 | throw new RuntimeException(e); |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | throw new IllegalArgumentException("Cannot find list name of NodeList of size " + nodeList.size()); |
| 667 | } |
| 668 | } |
| 669 |
Members