Top Description Inners Fields Constructors Methods
com.github.javaparser.printer.lexicalpreservation

public Class LexicalPreservingPrinter

extends Object
Class Inheritance
Imports
com.github.javaparser.*, com.github.javaparser.ast.DataKey, .Modifier, .Node, .NodeList, com.github.javaparser.ast.body.VariableDeclarator, com.github.javaparser.ast.comments.BlockComment, .Comment, .JavadocComment, .LineComment, com.github.javaparser.ast.nodeTypes.NodeWithVariables, com.github.javaparser.ast.observer.AstObserver, .ObservableProperty, .PropagatingAstObserver, com.github.javaparser.ast.type.PrimitiveType, com.github.javaparser.ast.visitor.TreeVisitor, com.github.javaparser.printer.ConcreteSyntaxModel, com.github.javaparser.printer.concretesyntaxmodel.*, com.github.javaparser.utils.Pair, .Utils, java.io.IOException, .StringWriter, .Writer, java.lang.reflect.InvocationTargetException, .Method, .ParameterizedType, java.util.*, java.util.stream.Collectors

A Lexical Preserving Printer is used to capture all the lexical information while parsing, update them when operating on the AST and then used them to reproduce the source code in its original formatting including the AST changes.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final LexicalDifferenceCalculator
public static final DataKey<NodeText>
NODE_TEXT_DATA

The nodetext for a node is stored in the node's data field.

private static AstObserver

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static AstObserver
pack-priv static List<TokenTextElement>
private static Node
findNodeForToken(Node node, Range tokenRange)

private static ObservableProperty
findNodeListName(NodeList<N extends Node> nodeList)

pack-priv static NodeText
private static NodeText
interpret(Node node, CsmElement csm, NodeText nodeText)

Todo

Process CsmIndent and CsmUnindent before reaching this point

private static boolean
private static void
public static <N extends Node> N

Returns:

the node passed as a parameter for your convenience.
setup
(N node)

Prepares the node so it can be used in the print methods.

private static void
private static void
private static Iterator<TokenTextElement>
tokensPreceeding(final Node node)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

LEXICAL_DIFFERENCE_CALCULATORback to summary
private static final LexicalDifferenceCalculator LEXICAL_DIFFERENCE_CALCULATOR
NODE_TEXT_DATAback to summary
public static final DataKey<NodeText> NODE_TEXT_DATA

The nodetext for a node is stored in the node's data field. This is the key to set and retrieve it.

observerback to summary
private static AstObserver observer

Constructor Detail

LexicalPreservingPrinterback to summary
public LexicalPreservingPrinter()

Method Detail

createObserverback to summary
private static AstObserver createObserver()
findIndentationback to summary
pack-priv static List<TokenTextElement> findIndentation(Node node)
findNodeForTokenback to summary
private static Node findNodeForToken(Node node, Range tokenRange)
findNodeListNameback to summary
private static ObservableProperty findNodeListName(NodeList<N extends Node> nodeList)
getOrCreateNodeTextback to summary
pack-priv static NodeText getOrCreateNodeText(Node node)
interpretback to summary
private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText)

Todo

Process CsmIndent and CsmUnindent before reaching this point

isReturningOptionalNodeListback to summary
private static boolean isReturningOptionalNodeList(Method m)
prettyPrintingTextNodeback to summary
private static void prettyPrintingTextNode(Node node, NodeText nodeText)
setupback to summary
public static <N extends Node> N setup(N node)

Prepares the node so it can be used in the print methods. The correct order is:

  1. Parse some code
  2. Call this setup method on the result
  3. Make changes to the AST as desired
  4. Use one of the print methods on this class to print out the original source code with your changes added
Returns:N

the node passed as a parameter for your convenience.

storeInitialTextback to summary
private static void storeInitialText(Node root)
storeInitialTextForOneNodeback to summary
private static void storeInitialTextForOneNode(Node node, List<JavaToken> nodeTokens)
tokensPreceedingback to summary
private static Iterator<TokenTextElement> tokensPreceeding(final Node node)
com.github.javaparser.printer.lexicalpreservation back to summary

private Class LexicalPreservingPrinter.Observer

extends PropagatingAstObserver
Class Inheritance

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
public void
public void
private List<ChildTextElement>
private List<TokenTextElement>
private int
getIndexOfComment(Comment oldValue, NodeText nodeText)

private boolean
Inherited from com.github.javaparser.ast.observer.PropagatingAstObserver:
listChangelistReplacementparentChangepropertyChangetransformInPropagatingObserver

Constructor Detail

Observerback to summary
private Observer()

Method Detail

concreteListChangeback to summary
public void concreteListChange(NodeList<N extends Node> changedList, AstObserver.ListChangeType type, int index, Node nodeAddedOrRemoved)

Overrides com.github.javaparser.ast.observer.PropagatingAstObserver.concreteListChange.

Annotations
@Override
concreteListReplacementback to summary
public void concreteListReplacement(NodeList<N extends Node> changedList, int index, Node oldValue, Node newValue)

Overrides com.github.javaparser.ast.observer.PropagatingAstObserver.concreteListReplacement.

Annotations
@Override
concretePropertyChangeback to summary
public void concretePropertyChange(Node observedNode, ObservableProperty property, Object oldValue, Object newValue)

Overrides com.github.javaparser.ast.observer.PropagatingAstObserver.concretePropertyChange.

Annotations
@Override
findChildTextElementForCommentback to summary
private List<ChildTextElement> findChildTextElementForComment(Comment oldValue, NodeText nodeText)
findTokenTextElementForCommentback to summary
private List<TokenTextElement> findTokenTextElementForComment(Comment oldValue, NodeText nodeText)
getIndexOfCommentback to summary
private int getIndexOfComment(Comment oldValue, NodeText nodeText)
isEqualRangeback to summary
private boolean isEqualRange(Optional<Range> range1, Optional<Range> range2)