Class UnitCompiler
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
static class
Short-hand implementation ofIClass.IField
that implements a non-constant, non-static, package-accessible field. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final boolean
Special value for the orientation parameter of thecompileBoolean(Java.Rvalue, CodeContext.Offset, boolean)
methods, indicating that the code should be generated such that execution branches if the value on top of the operand stack is FALSE.static final boolean
Special value for the orientation parameter of thecompileBoolean(Java.Rvalue, CodeContext.Offset, boolean)
methods, indicating that the code should be generated such that execution branches if the value on top of the operand stack is TRUE.static final Object
Special return value for thegetConstantValue(Java.Rvalue)
method family indicating that the givenJava.Rvalue
does not evaluate to a constant value. -
Constructor Summary
ConstructorsConstructorDescriptionUnitCompiler
(Java.AbstractCompilationUnit abstractCompilationUnit, IClassLoader iClassLoader) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
buildLocalVariableMap
(Java.CatchClause catchClause, Map<String, Java.LocalVariable> localVars) Adds the given localVars to the 'local variable map' of the given catchClause.void
compileUnit
(boolean debugSource, boolean debugLines, boolean debugVars, Collection<ClassFile> generatedClassFiles) Generates a set ofClassFile
objects which represent the classes and interfaces declared in the compilation unit.void
compileUnit
(boolean debugSource, boolean debugLines, boolean debugVars, UnitCompiler.ClassFileConsumer storesClassFiles) Generates a set ofClassFile
objects which represent the classes and interfaces declared in the compilation unit.Finds one class or interface declaration in this compilation unit and resolves it into anIClass
.Finds methods of the mi.
target
named mi.
methodName
, examines the argument types and chooses the most specific method.findIMethod
(Java.SuperclassMethodInvocation superclassMethodInvocation) findMostSpecificIInvocable
(Java.Locatable locatable, IClass.IInvocable[] iInvocables, IClass[] argumentTypes, boolean boxingPermitted, Java.Scope contextScope) Determines the applicable invocables and choose the most specific invocable.final Object
Attempts to evaluate as a constant expression.final Object
Attempts to evaluate as a constant expression.static int
void
getIMethods
(IClass type, String methodName, List<IClass.IMethod> v) Adds all methods with the given methodName that are declared by the type, its superclasses and all their superinterfaces to the result list v.getLocalVariable
(Java.CatchParameter parameter) getLocalVariable
(Java.FunctionDeclarator.FormalParameter parameter, boolean isVariableArityParameter) String[]
getSingleTypeImport
(String name, Location location) Checks if the given simple name was imported through a single type import.int
importTypeOnDemand
(String simpleTypeName, Location location) 6.5.2.BL1.B1.B5, 6.5.2.BL1.B1.B6 Type-import-on-demand.
6.5.5.1.6 Type-import-on-demand declaration.options()
options
(EnumSet<JaninoOption> options) Sets the options for all future compilations.void
setCompileErrorHandler
(ErrorHandler compileErrorHandler) By default,CompileException
s are thrown on compile errors, but an application my install its own (thread-local)ErrorHandler
.void
setTargetVersion
(int version) Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-target
command line option.void
setWarningHandler
(WarningHandler warningHandler) By default, warnings are discarded, but an application my install a customWarningHandler
.toIMethod
(Java.MethodDeclarator methodDeclarator)
-
Field Details
-
JUMP_IF_TRUE
public static final boolean JUMP_IF_TRUESpecial value for the orientation parameter of thecompileBoolean(Java.Rvalue, CodeContext.Offset, boolean)
methods, indicating that the code should be generated such that execution branches if the value on top of the operand stack is TRUE.- See Also:
-
JUMP_IF_FALSE
public static final boolean JUMP_IF_FALSESpecial value for the orientation parameter of thecompileBoolean(Java.Rvalue, CodeContext.Offset, boolean)
methods, indicating that the code should be generated such that execution branches if the value on top of the operand stack is FALSE.- See Also:
-
NOT_CONSTANT
Special return value for thegetConstantValue(Java.Rvalue)
method family indicating that the givenJava.Rvalue
does not evaluate to a constant value.
-
-
Constructor Details
-
UnitCompiler
public UnitCompiler(Java.AbstractCompilationUnit abstractCompilationUnit, IClassLoader iClassLoader)
-
-
Method Details
-
options
- Returns:
- A reference to the currently effective compilation options; changes to it take effect immediately
-
options
Sets the options for all future compilations. -
setTargetVersion
public void setTargetVersion(int version) Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-target
command line option. By default, Java 6 .class files are generated. -
getAbstractCompilationUnit
- Returns:
- The
Java.AbstractCompilationUnit
that thisUnitCompiler
compiles
-
compileUnit
public void compileUnit(boolean debugSource, boolean debugLines, boolean debugVars, Collection<ClassFile> generatedClassFiles) throws CompileException Generates a set ofClassFile
objects which represent the classes and interfaces declared in the compilation unit.- Parameters:
generatedClassFiles
- Adds generatedClassFile
s to thisCollection
- Throws:
CompileException
-
compileUnit
public void compileUnit(boolean debugSource, boolean debugLines, boolean debugVars, UnitCompiler.ClassFileConsumer storesClassFiles) throws CompileException Generates a set ofClassFile
objects which represent the classes and interfaces declared in the compilation unit.- Parameters:
storesClassFiles
- Consumes each generatedClassFile
- Throws:
CompileException
-
getLocalVariable
public Java.LocalVariable getLocalVariable(Java.LocalVariableDeclarationStatement lvds, Java.VariableDeclarator vd) throws CompileException - Returns:
- The
Java.LocalVariable
corresponding with the local variable declaration/declarator - Throws:
CompileException
-
getTargetVersion
public int getTargetVersion() -
getDefaultTargetVersion
public static int getDefaultTargetVersion()- Returns:
- The target version that applies iff no target version is actively configured with
setTargetVersion(int)
-
buildLocalVariableMap
protected void buildLocalVariableMap(Java.CatchClause catchClause, Map<String, Java.LocalVariable> localVars) throws CompileExceptionAdds the given localVars to the 'local variable map' of the given catchClause.- Throws:
CompileException
-
getLocalVariable
public Java.LocalVariable getLocalVariable(Java.FunctionDeclarator.FormalParameter parameter) throws CompileException - Returns:
- The
Java.LocalVariable
corresponding with the parameter - Throws:
CompileException
-
getLocalVariable
public Java.LocalVariable getLocalVariable(Java.FunctionDeclarator.FormalParameter parameter, boolean isVariableArityParameter) throws CompileException - Parameters:
isVariableArityParameter
- Whether the parameter is the last parameter of a 'variable arity' (a.k.a. 'varargs') method declaration- Returns:
- The
Java.LocalVariable
corresponding with the parameter - Throws:
CompileException
-
getLocalVariable
- Returns:
- The
Java.LocalVariable
corresponding with the parameter - Throws:
CompileException
-
getConstantValue
@Nullable public final Object getConstantValue(Java.ArrayInitializerOrRvalue rv) throws CompileException Attempts to evaluate as a constant expression. The result is one of the following:Boolean
,Byte
,Short
,Integer
,Long
,Float
,Double
,Character
,String
,null
(representing thenull
literal.This method cannot be STATIC, because the constant value may refer to a constant declaration in this compilation unit.
- Returns:
NOT_CONSTANT
iff the rvalue is not a constant value- Throws:
CompileException
-
getConstantValue
Attempts to evaluate as a constant expression. The result is one of the following:Boolean
,Byte
,Short
,Integer
,Long
,Float
,Double
,Character
,String
,null
(representing thenull
literal.This method cannot be STATIC, because the constant value may refer to a constant declaration in this compilation unit.
- Returns:
NOT_CONSTANT
iff the rvalue is not a constant value- Throws:
CompileException
-
findIMethod
Finds methods of the mi.
target
named mi.
methodName
, examines the argument types and chooses the most specific method. Checks that only the allowed exceptions are thrown.Notice that the returned
IClass.IMethod
may be declared in an enclosing type.- Returns:
- The selected
IClass.IMethod
- Throws:
CompileException
-
getIMethods
public void getIMethods(IClass type, String methodName, List<IClass.IMethod> v) throws CompileException Adds all methods with the given methodName that are declared by the type, its superclasses and all their superinterfaces to the result list v.- Throws:
CompileException
-
findIMethod
public IClass.IMethod findIMethod(Java.SuperclassMethodInvocation superclassMethodInvocation) throws CompileException - Returns:
- The
IClass.IMethod
that implements the superclassMethodInvocation - Throws:
CompileException
-
findMostSpecificIInvocable
@Nullable public IClass.IInvocable findMostSpecificIInvocable(Java.Locatable locatable, IClass.IInvocable[] iInvocables, IClass[] argumentTypes, boolean boxingPermitted, Java.Scope contextScope) throws CompileException Determines the applicable invocables and choose the most specific invocable.- Returns:
- The maximally specific
IClass.IInvocable
ornull
if noIClass.IInvocable
is applicable - Throws:
CompileException
-
toIMethod
- Returns:
- The
IClass.IMethod
that implements the methodDeclarator
-
getSingleTypeImport
@Nullable public String[] getSingleTypeImport(String name, Location location) throws CompileException Checks if the given simple name was imported through a single type import.- Parameters:
name
- The simple type name, e.g."Inner"
- Returns:
- The fully qualified name, e.g.
{ "pkg", "Outer", "Inner" }
, ornull
- Throws:
CompileException
-
importTypeOnDemand
@Nullable public IClass importTypeOnDemand(String simpleTypeName, Location location) throws CompileException 6.5.2.BL1.B1.B5, 6.5.2.BL1.B1.B6 Type-import-on-demand.
6.5.5.1.6 Type-import-on-demand declaration.- Returns:
null
if the given simpleTypeName cannot be resolved through any of the type-import-on-demand declarations- Throws:
CompileException
-
findClass
Finds one class or interface declaration in this compilation unit and resolves it into anIClass
.- Parameters:
className
- Fully qualified class name, e.g. "pkg1.pkg2.Outer$Inner"- Returns:
null
if a class or an interface with that name is not declared in this compilation unit
-
setCompileErrorHandler
By default,CompileException
s are thrown on compile errors, but an application my install its own (thread-local)ErrorHandler
.Be aware that a single problem during compilation often causes a bunch of compile errors, so a good
ErrorHandler
counts errors and throws aCompileException
when a limit is reached.If the given
ErrorHandler
does not throwCompileException
s, thencompileUnit(boolean, boolean, boolean, ClassFileConsumer)
will throw one when the compilation of the unit is finished, and errors had occurred. In other words: TheErrorHandler
may throw aCompileException
or not, butcompileUnit(boolean, boolean, boolean, ClassFileConsumer)
will definitely throw aCompileException
if one or more compile errors have occurred.- Parameters:
compileErrorHandler
-null
to restore the default behavior (throwing aCompileException
)
-
setWarningHandler
By default, warnings are discarded, but an application my install a customWarningHandler
.- Parameters:
warningHandler
-null
to indicate that no warnings be issued
-