Package org.codehaus.janino
Class Unparser
java.lang.Object
org.codehaus.janino.Unparser
- All Implemented Interfaces:
AutoCloseable
Unparses (un-compiles) an AST to a
Writer
. See main(String[])
for a usage example.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Visitor.AnnotationVisitor
<Void, RuntimeException> private final Visitor.ArrayInitializerOrRvalueVisitor
<Void, RuntimeException> private final Visitor.AtomVisitor
<Void, RuntimeException> private final Visitor.BlockStatementVisitor
<Void, RuntimeException> private final Visitor.AbstractCompilationUnitVisitor
<Void, RuntimeException> private final Visitor.ElementValueVisitor
<Void, RuntimeException> private final Visitor.FunctionDeclaratorVisitor
<Void, RuntimeException> private final Visitor.ImportVisitor
<Void, RuntimeException> private final Visitor.LambdaBodyVisitor
<Void, RuntimeException> private final Visitor.LambdaParametersVisitor
<Void, RuntimeException> private final Visitor.LvalueVisitor
<Void, RuntimeException> private final Visitor.ModifierVisitor
<Void, RuntimeException> private final Visitor.ModuleDirectiveVisitor
<Void, RuntimeException> Maps (pseudo-)operators like"?:"
and"x++"
to precedences (higher value means higher precedence).protected final PrintWriter
Where thevisit...()
methods print their text.private final Visitor.TryStatementResourceVisitor
<Void, RuntimeException> private final Visitor.RvalueVisitor
<Void, RuntimeException> private final Visitor.TypeBodyDeclarationVisitor
<Void, RuntimeException> private final Visitor.TypeDeclarationVisitor
<Void, RuntimeException> private final Visitor.TypeVisitor
<Void, RuntimeException> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static boolean
void
close()
Flushes all generated code.private static int
comparePrecedence
(String operator, Java.Atom operand) Returns a value < 0 iff theoperator
has lower precedence than theoperand
==; 0 iff theoperator
has equal precedence than theoperand
> 0 iff theoperator
has higher precedence than theoperand
void
flush()
Flushes all generated code.private static int
getOperatorPrecedence
(String operator) private static boolean
isLeftAssociate
(String operator) Returnstrue
iff operator is left associative e.g.private static boolean
isRightAssociate
(String operator) Returnstrue
iff operator is right associative e.g.static void
Testing of parsing/unparsing.static void
Unparses the givenJava.AbstractCompilationUnit
to the givenWriter
.private void
void
private void
unparseAnnotations
(Java.Annotation[] annotations) private void
private void
void
void
Generates Java code from aJava.Block
.void
private void
void
Generates Java code from aJava.AbstractClassDeclaration
.private void
private void
private void
private void
private void
unparseFormalParameter
(Java.FunctionDeclarator.FormalParameter fp, boolean hasEllipsis) private void
private void
private void
private void
unparseFunctionInvocationArguments
(Java.Rvalue[] arguments) void
private void
private void
void
void
private void
unparseLhs
(Java.Atom lhs, String binaryOperator) Iff thelhs
is unnatural for thebinaryOperator
, encloses thelhs
in parentheses.void
private void
private void
unparseModifiers
(Java.Modifier[] modifiers) private void
private void
private void
unparseResources
(Java.TryStatement.Resource[] resources) private void
unparseRhs
(Java.Rvalue rhs, String binaryOperator) Iff therhs
is unnatural for thebinaryOperator
, enclose therhs
in parentheses.void
void
unparseStatements
(List<? extends Java.BlockStatement> statements) Generates Java code from a sequence ofJava.BlockStatement
s.void
void
private void
private void
unparseTypeParameter
(Java.TypeParameter typeParameter) private void
unparseTypeParameters
(Java.TypeParameter[] typeParameters) private void
unparseUnaryOperation
(Java.Rvalue operand, String unaryOperator) Iff theoperand
is unnatural for theunaryOperator
, encloses theoperand
in parentheses.private void
private void
unparseVariableDeclarators
(Java.VariableDeclarator[] variableDeclarators)
-
Field Details
-
compilationUnitUnparser
-
moduleDirectiveUnparser
-
importUnparser
-
typeDeclarationUnparser
-
typeBodyDeclarationUnparser
-
blockStatementUnparser
-
atomUnparser
-
typeUnparser
-
arrayInitializerOrRvalueUnparser
private final Visitor.ArrayInitializerOrRvalueVisitor<Void,RuntimeException> arrayInitializerOrRvalueUnparser -
rvalueUnparser
-
lvalueUnparser
-
elementValueUnparser
-
annotationUnparser
-
modifierUnparser
-
lambdaParametersUnparser
-
lambdaBodyUnparser
-
functionDeclaratorUnparser
-
resourceUnparser
-
pw
Where thevisit...()
methods print their text. Notice that thisPrintWriter
does not print to the output directly, but through anAutoIndentWriter
. -
LEFT_ASSOCIATIVE_OPERATORS
-
RIGHT_ASSOCIATIVE_OPERATORS
-
UNARY_OPERATORS
-
OPERATOR_PRECEDENCE
Maps (pseudo-)operators like"?:"
and"x++"
to precedences (higher value means higher precedence).
-
-
Constructor Details
-
Unparser
-
-
Method Details
-
unparseInitializer
-
unparseFieldDeclaration
-
unparseResources
-
main
Testing of parsing/unparsing.Reads compilation units from the files named on the command line and unparses them to
System.out
.- Throws:
Exception
-
unparse
Unparses the givenJava.AbstractCompilationUnit
to the givenWriter
. -
flush
public void flush()Flushes all generated code. -
close
public void close()Flushes all generated code.- Specified by:
close
in interfaceAutoCloseable
-
unparseAbstractCompilationUnit
- Parameters:
cu
- The compilation unit to unparse
-
unparseImportDeclaration
-
unparseConstructorDeclarator
-
unparseMethodDeclarator
-
unparseStatements
Generates Java code from a sequence ofJava.BlockStatement
s. -
unparseVariableDeclarator
-
unparseFormalParameter
private void unparseFormalParameter(Java.FunctionDeclarator.FormalParameter fp, boolean hasEllipsis) -
unparseCatchParameter
-
unparseLambdaParameters
-
unparseLambdaBody
-
unparseBlock
Generates Java code from aJava.Block
. -
unparseBlockStatement
-
unparseTypeDeclaration
-
unparseType
-
unparseAtom
-
unparseArrayInitializerOrRvalue
-
unparseRvalue
-
unparseLvalue
-
unparseUnaryOperation
Iff theoperand
is unnatural for theunaryOperator
, encloses theoperand
in parentheses. Example: "a+b" is an unnatural operand for unary "!x".- Parameters:
unaryOperator
- ++x --x +x -x ~x !x x++ x--
-
unparseLhs
Iff thelhs
is unnatural for thebinaryOperator
, encloses thelhs
in parentheses. Example: "a+b" is an unnatural lhs for operator "*".- Parameters:
binaryOperator
- = +=... ?: || invalid input: '&'invalid input: '&' | ^ invalid input: '&' == != invalid input: '<' > invalid input: '<'= >= instanceof invalid input: '<'invalid input: '<' >> >>> + - * / % cast
-
unparseRhs
Iff therhs
is unnatural for thebinaryOperator
, enclose therhs
in parentheses. Example: "a+b" is an unnatural rhs for operator "*". -
unparse
-
isRightAssociate
Returnstrue
iff operator is right associative e.g.a = b = c
evaluates asa = (b = c)
.- Returns:
- Return true iff operator is right associative
-
isLeftAssociate
Returnstrue
iff operator is left associative e.g.a - b - c
evaluates as(a - b) - c
. -
comparePrecedence
Returns a value- < 0 iff the
operator
has lower precedence than theoperand
- ==; 0 iff the
operator
has equal precedence than theoperand
- > 0 iff the
operator
has higher precedence than theoperand
- < 0 iff the
-
getOperatorPrecedence
-
unparseNamedClassDeclaration
-
unparseClassDeclarationBody
Generates Java code from aJava.AbstractClassDeclaration
. -
classDeclarationBodyIsEmpty
- Returns:
- Whether
unparseClassDeclarationBody(Java.AbstractClassDeclaration)
will produce no output
-
unparseInterfaceDeclaration
-
unparseTypeDeclarationBody
-
unparseFunctionDeclaratorRest
-
unparseFormalParameters
-
unparseDocComment
-
unparseAnnotations
-
unparseModifiers
-
unparseTypeParameters
-
unparseTypeParameter
-
unparseFunctionInvocationArguments
-
unparseEnumDeclaration
-
unparseAnnotationTypeDeclaration
-
unparseFunctionDeclarator
-
unparseResource
-
unparseVariableDeclarators
-