Class LexicalPreservingPrinter
- java.lang.Object
-
- com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter
-
public class LexicalPreservingPrinter extends java.lang.ObjectA 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 Class Summary
Nested Classes Modifier and Type Class Description private static classLexicalPreservingPrinter.Observer
-
Field Summary
Fields Modifier and Type Field Description private static LexicalDifferenceCalculatorLEXICAL_DIFFERENCE_CALCULATORstatic DataKey<NodeText>NODE_TEXT_DATAThe nodetext for a node is stored in the node's data field.private static AstObserverobserver
-
Constructor Summary
Constructors Constructor Description LexicalPreservingPrinter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static AstObservercreateObserver()(package private) static java.util.List<TokenTextElement>findIndentation(Node node)private static NodefindNodeForToken(Node node, Range tokenRange)private static ObservablePropertyfindNodeListName(NodeList nodeList)static java.util.Optional<Comment>getComment(Node node)Returns the comment or an Optional.empty if there is no comment on this method call.(package private) static NodeTextgetOrCreateNodeText(Node node)private static intgetTokenKind(Comment comment)private static NodeTextinterpret(Node node, CsmElement csm, NodeText nodeText)TODO: Process CsmIndent and CsmUnindent before reaching this pointprivate static booleanisReturningOptionalNodeList(java.lang.reflect.Method m)private static voidprettyPrintingTextNode(Node node, NodeText nodeText)static java.lang.Stringprint(Node node)Print a Node into a String, preserving the lexical information.static voidprint(Node node, java.io.Writer writer)Print a Node into a Writer, preserving the lexical information.static <N extends Node>
Nsetup(N node)Prepares the node so it can be used in the print methods.private static voidstoreInitialText(Node root)private static voidstoreInitialTextForOneNode(Node node, java.util.List<JavaToken> nodeTokens)private static java.util.Iterator<TokenTextElement>tokensPreceeding(Node node)
-
-
-
Field Detail
-
observer
private static AstObserver observer
-
NODE_TEXT_DATA
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.
-
LEXICAL_DIFFERENCE_CALCULATOR
private static final LexicalDifferenceCalculator LEXICAL_DIFFERENCE_CALCULATOR
-
-
Method Detail
-
setup
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:- Parse some code
- Call this setup method on the result
- Make changes to the AST as desired
- Use one of the print methods on this class to print out the original source code with your changes added
- Returns:
- the node passed as a parameter for your convenience.
-
createObserver
private static AstObserver createObserver()
-
storeInitialText
private static void storeInitialText(Node root)
-
storeInitialTextForOneNode
private static void storeInitialTextForOneNode(Node node, java.util.List<JavaToken> nodeTokens)
-
tokensPreceeding
private static java.util.Iterator<TokenTextElement> tokensPreceeding(Node node)
-
print
public static java.lang.String print(Node node)
Print a Node into a String, preserving the lexical information.
-
print
public static void print(Node node, java.io.Writer writer) throws java.io.IOException
Print a Node into a Writer, preserving the lexical information.- Throws:
java.io.IOException
-
interpret
private static NodeText interpret(Node node, CsmElement csm, NodeText nodeText)
TODO: Process CsmIndent and CsmUnindent before reaching this point
-
getComment
public static java.util.Optional<Comment> getComment(Node node)
Returns the comment or an Optional.empty if there is no comment on this method call. In case of MethodCallExpr, Comment are setted in the parent ExpressionStmt comment attribute.
-
getTokenKind
private static int getTokenKind(Comment comment)
-
findIndentation
static java.util.List<TokenTextElement> findIndentation(Node node)
-
isReturningOptionalNodeList
private static boolean isReturningOptionalNodeList(java.lang.reflect.Method m)
-
findNodeListName
private static ObservableProperty findNodeListName(NodeList nodeList)
-
-