public class PrinterAdapter extends Object
| Modifier and Type | Field and Description |
|---|---|
protected JSweetContext |
context |
protected org.apache.log4j.Logger |
logger |
Set<TypeParameterElement> |
typeVariablesToErase
A list of type variables to be erased (mapped to any).
|
| Constructor and Description |
|---|
PrinterAdapter(JSweetContext context)
Creates a root adapter (with no parent).
|
PrinterAdapter(PrinterAdapter parentAdapter)
Creates a new adapter that will try delegate to the given parent adapter when
not implementing its own behavior.
|
| Modifier and Type | Method and Description |
|---|---|
String |
adaptDocComment(Element element,
String commentText)
Adapts the JavaDoc comment for a given element.
|
void |
addAnnotation(Class<? extends Annotation> annotationType,
String... filters)
Adds an annotation on the AST through global filters.
|
void |
addAnnotation(String annotationDescriptor,
String... filters)
Adds an annotation on the AST through global filters.
|
void |
addAnnotationManager(AnnotationManager annotationManager)
Adds an annotation manager that will tune (add or remove) annotations on the
AST.
|
void |
addAnnotationWithValue(Class<? extends Annotation> annotationType,
Object value,
String... filters)
Adds an annotation on the AST through global filters.
|
void |
addHeader(String key,
String header)
This method sets a header to the currently printed file.
|
void |
addTypeMapping(BiFunction<ExtendedElement,String,Object> mappingFunction)
Adds a type mapping so that this adapter substitutes the source type tree
with a target type during the transpilation process.
|
protected void |
addTypeMapping(String sourceTypeName,
String targetTypeName)
Adds a type mapping so that this adapter substitutes the source type with the
target type during the transpilation process.
|
protected void |
addTypeMappings(Map<String,String> nameMappings)
Adds a set of name-based type mappings.
|
void |
afterType(TypeElement type)
This method is called after a type was printed.
|
PrinterAdapter |
endIndent()
Decrements the current indentation value.
|
boolean |
eraseSuperClass(TypeElement type,
TypeElement superClass)
Tells if a super class has to be erased in the generated source.
|
boolean |
eraseSuperInterface(TypeElement type,
TypeElement superInterface)
Tells if a super interface has to be erased in the generated source.
|
ExecutableElement |
findExecutableDeclarationInType(TypeElement type,
MethodInvocationElement invocation)
Looks-up the executable that is invoked by the given invocation.
|
<T> T |
getAnnotationValue(Element element,
String annotationType,
Class<T> propertyClass,
T defaultValue)
Gets the first value of the 'value' property for the given annotation type if
found on the given element.
|
<T> T |
getAnnotationValue(Element element,
String annotationType,
String propertyName,
Class<T> propertyClass,
T defaultValue)
Gets the first value of the given property for the given annotation type if
found on the given element.
|
JSweetContext |
getContext()
Gets the transpiler's context.
|
Set<String> |
getErasedTypes() |
protected List<BiFunction<ExtendedElement,String,Object>> |
getFunctionalTypeMappings()
Gets the functional type mappings.
|
String |
getHeader(String key)
Gets the header associated to the given key (null if non-existing key).
|
String |
getMappedType(TypeMirror type)
Gets the string that corresponds to the given type, taking into account all
type mappings.
|
ModuleImportDescriptor |
getModuleImportDescriptor(CompilationUnitElement currentCompilationUnit,
String importedName,
TypeElement importedClass)
This method implements the default behavior to generate module imports.
|
PrinterAdapter |
getParentAdapter()
Gets the parent adapter.
|
ExtendedElement |
getParentElement()
Gets the parent element in the printer's scanning stack.
|
<T extends Element> |
getParentElement(Class<T> type)
Gets the parent element in the printer's scanning stack.
|
AbstractTreePrinter |
getPrinter()
Gets the printer on which rely the adapter (this is not recommended).
|
String |
getRootRelativeName(Element element)
Gets the qualified name of an element, relatively to a possible
@Root annotation. |
JSweetOptions |
getTranspilerOptions()
Gets the transpiler options.
|
protected String |
getTypeMappingTarget(String sourceTypeName)
Returns the type the given type name is mapped through the
addTypeMapping(String, String) or
addTypeMapping(String, String) function. |
boolean |
hasAnnotationType(Element element,
String... annotationTypes)
Returns true if the given element is annotated with one of the given
annotation types.
|
boolean |
isAmbientDeclaration(Element element)
Tells if the given element is ambient (part of a def.* package or within an
@Ambient-annotated scope). |
protected boolean |
isMappedType(String sourceTypeName)
Returns true if the given type name is mapped through the
addTypeMapping(String, String) or
addTypeMapping(String, String) function. |
boolean |
isSubstituteSuperTypes()
Tells if this adapter substitutes types in extends or implements clauses.
|
String |
needsImport(ImportElement importElement,
String qualifiedName)
Returns the import qualified id if the given import requires an import
statement to be printed.
|
PrinterAdapter |
print(ExtendedElement element)
Prints a generic element by delegating to the printer.
|
PrinterAdapter |
print(Name name)
Prints a name by delegating to the printer.
|
PrinterAdapter |
print(String string)
Prints a string by delegating to the printer.
|
void |
print(String exprStr,
ExtendedElement expr)
Print either a string, or a tree if the string is null.
|
PrinterAdapter |
printArgList(List<? extends ExtendedElement> args)
Prints an argument list by delegating to the printer.
|
PrinterAdapter |
printIdentifierList(String prefix,
int count)
Prints a comma-separated, zero-indexed, generated identifier list.
|
PrinterAdapter |
printIndent()
Prints an indentation for the current indentation value.
|
PrinterAdapter |
println()
Prints a new line by delegating to the printer.
|
protected void |
printMacroName(String macroName)
Print the macro name in the code.
|
PrinterAdapter |
removeLastChar()
Removes the last output character.
|
boolean |
removeLastChar(char expectedChar)
removes last character if expectedChar
|
PrinterAdapter |
removeLastChars(int count)
Removes the last output characters.
|
PrinterAdapter |
removeLastIndent()
Removes the last printed indentation.
|
protected void |
report(Element element,
JSweetProblem problem,
Object... params)
Reports a problem during the printing phase.
|
protected void |
report(Element element,
Name name,
JSweetProblem problem,
Object... params)
Reports a problem during the printing phase.
|
protected void |
report(ExtendedElement element,
JSweetProblem problem,
Object... params)
Reports a problem during the printing phase.
|
protected void |
report(ExtendedElement element,
Name name,
JSweetProblem problem,
Object... params)
Reports a problem during the printing phase.
|
void |
setParentAdapter(PrinterAdapter parentAdapter)
Sets the parent adapter.
|
void |
setPrinter(AbstractTreePrinter printer)
Sets the printer attached to this adapter.
|
PrinterAdapter |
space()
Adds a space to the output.
|
PrinterAdapter |
startIndent()
Increments the current indentation value.
|
boolean |
substitute(ExtendedElement extendedElement)
Upcalled by the transpiler to forward to the right subtitution method
depending on the actual extended element type.
|
boolean |
substituteArrayAccess(ArrayAccessElement arrayAccess)
Substitutes the value of an array access expression.
|
boolean |
substituteAssignment(AssignmentElement assignment)
Substitutes the value of a field assignment expression.
|
boolean |
substituteAssignmentWithOperator(AssignmentWithOperatorElement assignment)
Substitutes the value of an assignment with operator (a += b) expression.
|
boolean |
substituteBinaryOperator(BinaryOperatorElement binaryOperator)
Substitutes the value of a binary operator.
|
boolean |
substituteCaseStatementPattern(CaseElement caseStatement,
ExtendedElement pattern)
Substitutes if necessary the pattern of a case statement.
|
boolean |
substituteForEachLoop(ForeachLoopElement foreachLoop,
boolean targetHasLength,
String indexVarName)
Substitutes if necessary the given foreach loop.
|
boolean |
substituteIdentifier(IdentifierElement identifier)
Substitutes the value of an identifier.
|
boolean |
substituteInstanceof(String exprStr,
ExtendedElement expr,
TypeMirror type)
Substitutes if necessary an instanceof expression.
|
boolean |
substituteMethodInvocation(MethodInvocationElement invocation)
Substitutes the value of a method invocation expression.
|
boolean |
substituteNewClass(NewClassElement newClass)
To override to tune the printing of a new class expression.
|
boolean |
substituteUnaryOperator(UnaryOperatorElement unaryOperator)
Substitutes the value of a unary operator.
|
boolean |
substituteVariableAccess(VariableAccessElement variableAccess)
Substitutes the given variable access.
|
Types |
types()
Gets the types API, which provides a set of utilities on TypeMirror.
|
Util |
util()
Gets the util API, which provides a set of utilities.
|
protected org.apache.log4j.Logger logger
protected JSweetContext context
public Set<TypeParameterElement> typeVariablesToErase
public PrinterAdapter(JSweetContext context)
context - the transpilation contextpublic PrinterAdapter(PrinterAdapter parentAdapter)
parentAdapter - cannot be null: if no parent you must use the
#AbstractPrinterAdapter(JSweetContext) constructorpublic JSweetContext getContext()
protected final void addTypeMapping(String sourceTypeName, String targetTypeName)
sourceTypeName - the fully qualified name of the type to be substitutedtargetTypeName - the fully Qualified name of the type the source type is mapped toprotected final void addTypeMappings(Map<String,String> nameMappings)
addTypeMapping(String, String) for each entry of the given map.protected final boolean isMappedType(String sourceTypeName)
addTypeMapping(String, String) or
addTypeMapping(String, String) function.protected final String getTypeMappingTarget(String sourceTypeName)
addTypeMapping(String, String) or
addTypeMapping(String, String) function.protected final List<BiFunction<ExtendedElement,String,Object>> getFunctionalTypeMappings()
public void addTypeMapping(BiFunction<ExtendedElement,String,Object> mappingFunction)
mappingFunction - a function that takes the type tree, the type name, and returns a
substitution (either under the form of a string, or of a string,
or of another type tree).public final String getMappedType(TypeMirror type)
Some type mappings are set by default, some are added in the context by adapters.
public final void addAnnotation(Class<? extends Annotation> annotationType, String... filters)
Example:
context.addAnnotation(FunctionalInterface.class, "**.MyInterface");
Filters are simplified regular expressions matching on the Java AST. Special characters are the following:
For example, to match:
annotationType - the annotation typefilters - the annotation is activated if one of the filters match and no
negative filter matchespublic final void addAnnotationWithValue(Class<? extends Annotation> annotationType, Object value, String... filters)
addAnnotation(String, String...)public final void addAnnotationManager(AnnotationManager annotationManager)
public final boolean hasAnnotationType(Element element, String... annotationTypes)
addAnnotation(Class, String...) or
addAnnotationManager(AnnotationManager).public final <T> T getAnnotationValue(Element element, String annotationType, Class<T> propertyClass, T defaultValue)
addAnnotation(Class, String...) or
addAnnotationManager(AnnotationManager).element - the element holding the annotationannotationType - the fully qualified name of the value property typepropertyClass - the expected class of the property (String.class,
TypeMirror.class, Number.class, and arrays such as
String[].class...)defaultValue - the default value if the property is not foundpublic final <T> T getAnnotationValue(Element element, String annotationType, String propertyName, Class<T> propertyClass, T defaultValue)
element - the element holding the annotationannotationType - the fully qualified name of the value property typepropertyName - the name of the property in the annotation (null will
look up the value property)propertyClass - the expected class of the property (String.class,
TypeMirror.class, Number.class, and arrays such as
String[].class...)defaultValue - the default value if the property is not foundpublic final void addAnnotation(String annotationDescriptor, String... filters)
Example:
context.addAnnotation("@Erased", "*.writeObject(*)");
context.addAnnotation("@Name('newName')", "*.MyDeclarationToBeRenamed");
Filters are simplified regular expressions matching on the Java AST. Special characters are the following:
annotationDescriptor - the annotation type name, optionally preceded with a @, and
optionally defining a value (fully qualified name is not necessary
for JSweet annotations)filters - the annotation is activated if one of the filters match and no
negative filter matchespublic PrinterAdapter print(ExtendedElement element)
public PrinterAdapter print(String string)
public PrinterAdapter print(Name name)
public PrinterAdapter println()
public PrinterAdapter printArgList(List<? extends ExtendedElement> args)
public PrinterAdapter printIdentifierList(String prefix, int count)
For instance printIdentifierList("x", 4) will print:
x0, x1, x2, x3.
prefix - the prefix of the identifierscount - the number of identifiers in the listpublic void print(String exprStr, ExtendedElement expr)
public PrinterAdapter printIndent()
public final PrinterAdapter startIndent()
public final PrinterAdapter endIndent()
public final PrinterAdapter space()
public final boolean removeLastChar(char expectedChar)
public final PrinterAdapter removeLastChar()
public final PrinterAdapter removeLastChars(int count)
public final PrinterAdapter removeLastIndent()
public final ExtendedElement getParentElement()
public final <T extends Element> T getParentElement(Class<T> type)
public final ExecutableElement findExecutableDeclarationInType(TypeElement type, MethodInvocationElement invocation)
public final String getRootRelativeName(Element element)
@Root annotation.protected void report(ExtendedElement element, JSweetProblem problem, Object... params)
element - the code where the problem occurredproblem - the reported problemparams - the parameters if anyprotected void report(ExtendedElement element, Name name, JSweetProblem problem, Object... params)
element - the code where the problem occurredname - the name of the element if anyproblem - the reported problemparams - the parameters if anyprotected void report(Element element, JSweetProblem problem, Object... params)
element - the code where the problem occurredproblem - the reported problemparams - the parameters if anyprotected void report(Element element, Name name, JSweetProblem problem, Object... params)
element - the code where the problem occurredname - the name of the element if anyproblem - the reported problemparams - the parameters if anypublic boolean substituteArrayAccess(ArrayAccessElement arrayAccess)
arrayAccess - the array access being printedpublic boolean substituteBinaryOperator(BinaryOperatorElement binaryOperator)
binaryOperator - the binary operator being printedpublic boolean substituteUnaryOperator(UnaryOperatorElement unaryOperator)
unaryOperator - the unary operator being printedpublic boolean substituteIdentifier(IdentifierElement identifier)
identifier - the identifier being printedpublic boolean substituteNewClass(NewClassElement newClass)
newClass - the new class expressionpublic final boolean substitute(ExtendedElement extendedElement)
public boolean substituteVariableAccess(VariableAccessElement variableAccess)
variableAccess - the variable access being printedpublic String needsImport(ImportElement importElement, String qualifiedName)
importElement - the given import declarationqualifiedName - the qualified import idpublic ModuleImportDescriptor getModuleImportDescriptor(CompilationUnitElement currentCompilationUnit, String importedName, TypeElement importedClass)
currentCompilationUnit - the currently transpiled compilation unitimportedName - the name to be importedimportedClass - the class being importedModuleImportDescriptor instance that will be used to
generate the TypeScript import statementpublic boolean substituteMethodInvocation(MethodInvocationElement invocation)
invocation - the invocation being printedpublic boolean substituteAssignment(AssignmentElement assignment)
assignment - the field assignment being printedpublic boolean substituteAssignmentWithOperator(AssignmentWithOperatorElement assignment)
assignment - the assignment being printedpublic AbstractTreePrinter getPrinter()
Accessing the printer with this method allows the user to access the internal
javac API directly (com.sun.tools.javac), which is non-standard and
may get deprecated in future Java versions. As a consequence, to write
sustainable adapters, it is not recommended to use the printer API.
Instead, use the adapter's API directly, which relies on an abstraction of
the AST: javax.lang.model and org.jsweet.transpiler.model. If
some feature seems to be missing, please contact JSweet.org to help improving
this API.
public void setPrinter(AbstractTreePrinter printer)
public boolean substituteForEachLoop(ForeachLoopElement foreachLoop, boolean targetHasLength, String indexVarName)
foreachLoop - the foreach loop to printtargetHasLength - true if the iterable defines a public length fieldindexVarName - a possible (fresh) variable name that can used to iteratepublic boolean eraseSuperClass(TypeElement type, TypeElement superClass)
public boolean eraseSuperInterface(TypeElement type, TypeElement superInterface)
public boolean isSubstituteSuperTypes()
public boolean substituteInstanceof(String exprStr, ExtendedElement expr, TypeMirror type)
exprStr - the expression being tested as a string (null if provided as a
tree)expr - the expression being tested as a tree (null if provided as a
string)type - the type of the instanceof expressionpublic boolean substituteCaseStatementPattern(CaseElement caseStatement, ExtendedElement pattern)
public void afterType(TypeElement type)
public String adaptDocComment(Element element, String commentText)
element - the documented elementcommentText - the comment text if any (null when no comment)public PrinterAdapter getParentAdapter()
public void setParentAdapter(PrinterAdapter parentAdapter)
public Types types()
TypeMirror,
Element.asType(),
ExtendedElement.getType()public Util util()
protected final void printMacroName(String macroName)
public final boolean isAmbientDeclaration(Element element)
@Ambient-annotated scope).public final JSweetOptions getTranspilerOptions()
public final void addHeader(String key, String header)
Several headers can be added to the same file. Note that a new line will be automatically added at the end of the last header (if any), but not between each header. Headers will be printer in the order they have been added to the file. Headers are reset for each new file.
key - a key to identify the header (see getHeader(String))header - any string that will be printed at the beginning of the file (only
when not in bundle mode)getHeader(String)public final String getHeader(String key)
key - the header's key as set by addHeader(String, String)addHeader(String, String)