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 PrintWriterWhere 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 booleanvoidclose()Flushes all generated code.private static intcomparePrecedence(String operator, Java.Atom operand) Returns a value < 0 iff theoperatorhas lower precedence than theoperand==; 0 iff theoperatorhas equal precedence than theoperand> 0 iff theoperatorhas higher precedence than theoperandvoidflush()Flushes all generated code.private static intgetOperatorPrecedence(String operator) private static booleanisLeftAssociate(String operator) Returnstrueiff operator is left associative e.g.private static booleanisRightAssociate(String operator) Returnstrueiff operator is right associative e.g.static voidTesting of parsing/unparsing.static voidUnparses the givenJava.AbstractCompilationUnitto the givenWriter.private voidvoidprivate voidunparseAnnotations(Java.Annotation[] annotations) private voidprivate voidvoidvoidGenerates Java code from aJava.Block.voidprivate voidvoidGenerates Java code from aJava.AbstractClassDeclaration.private voidprivate voidprivate voidprivate voidprivate voidunparseFormalParameter(Java.FunctionDeclarator.FormalParameter fp, boolean hasEllipsis) private voidprivate voidprivate voidprivate voidunparseFunctionInvocationArguments(Java.Rvalue[] arguments) voidprivate voidprivate voidvoidvoidprivate voidunparseLhs(Java.Atom lhs, String binaryOperator) Iff thelhsis unnatural for thebinaryOperator, encloses thelhsin parentheses.voidprivate voidprivate voidunparseModifiers(Java.Modifier[] modifiers) private voidprivate voidprivate voidunparseResources(Java.TryStatement.Resource[] resources) private voidunparseRhs(Java.Rvalue rhs, String binaryOperator) Iff therhsis unnatural for thebinaryOperator, enclose therhsin parentheses.voidvoidunparseStatements(List<? extends Java.BlockStatement> statements) Generates Java code from a sequence ofJava.BlockStatements.voidvoidprivate voidprivate voidunparseTypeParameter(Java.TypeParameter typeParameter) private voidunparseTypeParameters(Java.TypeParameter[] typeParameters) private voidunparseUnaryOperation(Java.Rvalue operand, String unaryOperator) Iff theoperandis unnatural for theunaryOperator, encloses theoperandin parentheses.private voidprivate voidunparseVariableDeclarators(Java.VariableDeclarator[] variableDeclarators)
-
Field Details
-
compilationUnitUnparser
private final Visitor.AbstractCompilationUnitVisitor<Void, RuntimeException> compilationUnitUnparser -
moduleDirectiveUnparser
-
importUnparser
-
typeDeclarationUnparser
-
typeBodyDeclarationUnparser
private final Visitor.TypeBodyDeclarationVisitor<Void, RuntimeException> 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 thisPrintWriterdoes not print to the output directly, but through anAutoIndentWriter. -
LEFT_ASSOCIATIVE_OPERATORS
-
RIGHT_ASSOCIATIVE_OPERATORS
-
UNARY_OPERATORS
-
OPERATOR_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.AbstractCompilationUnitto the givenWriter. -
flush
public void flush()Flushes all generated code. -
close
-
unparseAbstractCompilationUnit
- Parameters:
cu- The compilation unit to unparse
-
unparseImportDeclaration
-
unparseConstructorDeclarator
-
unparseMethodDeclarator
-
unparseStatements
Generates Java code from a sequence ofJava.BlockStatements. -
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 theoperandis unnatural for theunaryOperator, encloses theoperandin parentheses. Example: "a+b" is an unnatural operand for unary "!x".- Parameters:
unaryOperator- ++x --x +x -x ~x !x x++ x--
-
unparseLhs
Iff thelhsis unnatural for thebinaryOperator, encloses thelhsin 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 therhsis unnatural for thebinaryOperator, enclose therhsin parentheses. Example: "a+b" is an unnatural rhs for operator "*". -
unparse
-
isRightAssociate
Returnstrueiff operator is right associative e.g.a = b = cevaluates asa = (b = c).- Returns:
- Return true iff operator is right associative
-
isLeftAssociate
Returnstrueiff operator is left associative e.g.a - b - cevaluates as(a - b) - c. -
comparePrecedence
-
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
-