Package org.codehaus.janino
Class UnitCompiler
- java.lang.Object
-
- org.codehaus.janino.UnitCompiler
-
public class UnitCompiler extends Object
This class actually implements the Java™ compiler. It is associated with exactly one compilation unit which it compiles.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
UnitCompiler.SimpleIField
Short-hand implementation ofIClass.IField
that implements a non-constant, non-static, package-accessible field.
-
Field Summary
Fields Modifier and Type Field Description static boolean
JUMP_IF_FALSE
Special value for theorientation
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 boolean
JUMP_IF_TRUE
Special value for theorientation
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 Object
NOT_CONSTANT
Special return value for thegetConstantValue(Java.Rvalue)
method family indicating that the givenJava.Rvalue
does not evaluate to a constant value.
-
Constructor Summary
Constructors Constructor Description UnitCompiler(Java.CompilationUnit compilationUnit, IClassLoader iClassLoader)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
buildLocalVariableMap(Java.CatchClause catchClause, Map<String,Java.LocalVariable> localVars)
Adds the givenlocalVars
to the 'local variable map' of the givencatchClause
.ClassFile[]
compileUnit(boolean debugSource, boolean debugLines, boolean debugVars)
Generates an array ofClassFile
objects which represent the classes and interfaces declared in the compilation unit.IClass
findClass(String className)
Find one class or interface declared in this compilation unit by name.IClass.IMethod
findIMethod(Java.MethodInvocation mi)
Find named methods of "targetType", examine the argument types and choose the most specific method.IClass.IMethod
findIMethod(Java.SuperclassMethodInvocation superclassMethodInvocation)
IClass.IInvocable
findMostSpecificIInvocable(Java.Locatable locatable, IClass.IInvocable[] iInvocables, IClass[] argumentTypes, boolean boxingPermitted, Java.Scope contextScope)
Determine the applicable invocables and choose the most specific invocable.Java.CompilationUnit
getCompilationUnit()
Object
getConstantValue(Java.Rvalue rv)
Attempts to evaluate as a constant expression.void
getIMethods(IClass type, String methodName, List<IClass.IMethod> v)
Add all methods with the givenmethodName
that are declared by thetype
, its superclasses and all their superinterfaces to the result listv
.Java.LocalVariable
getLocalVariable(Java.FunctionDeclarator.FormalParameter parameter)
Java.LocalVariable
getLocalVariable(Java.FunctionDeclarator.FormalParameter parameter, boolean isVariableArityParameter)
Java.LocalVariable
getLocalVariable(Java.LocalVariableDeclarationStatement lvds, Java.VariableDeclarator vd)
String[]
getSingleTypeImport(String name, Location location)
Check if the given simple name was imported through a single type import.IClass
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.void
setCompileErrorHandler(ErrorHandler optionalCompileErrorHandler)
By default,CompileException
s are thrown on compile errors, but an application my install its own (thread-local)ErrorHandler
.void
setWarningHandler(WarningHandler optionalWarningHandler)
By default, warnings are discarded, but an application my install a customWarningHandler
.IClass.IMethod
toIMethod(Java.MethodDeclarator methodDeclarator)
-
-
-
Field Detail
-
JUMP_IF_TRUE
public static final boolean JUMP_IF_TRUE
Special value for theorientation
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:
- Constant Field Values
-
JUMP_IF_FALSE
public static final boolean JUMP_IF_FALSE
Special value for theorientation
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:
- Constant Field Values
-
NOT_CONSTANT
public static final Object NOT_CONSTANT
Special return value for thegetConstantValue(Java.Rvalue)
method family indicating that the givenJava.Rvalue
does not evaluate to a constant value.
-
-
Constructor Detail
-
UnitCompiler
public UnitCompiler(Java.CompilationUnit compilationUnit, IClassLoader iClassLoader)
-
-
Method Detail
-
getCompilationUnit
public Java.CompilationUnit getCompilationUnit()
- Returns:
- The
Java.CompilationUnit
that thisUnitCompiler
compiles
-
compileUnit
public ClassFile[] compileUnit(boolean debugSource, boolean debugLines, boolean debugVars) throws CompileException
Generates an array ofClassFile
objects which represent the classes and interfaces declared in the compilation unit.- 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
-
buildLocalVariableMap
protected void buildLocalVariableMap(Java.CatchClause catchClause, Map<String,Java.LocalVariable> localVars) throws CompileException
Adds the givenlocalVars
to the 'local variable map' of the givencatchClause
.- Throws:
CompileException
-
getLocalVariable
public Java.LocalVariable getLocalVariable(Java.FunctionDeclarator.FormalParameter parameter) throws CompileException
- Returns:
- The
Java.LocalVariable
corresponding with theparameter
- Throws:
CompileException
-
getLocalVariable
public Java.LocalVariable getLocalVariable(Java.FunctionDeclarator.FormalParameter parameter, boolean isVariableArityParameter) throws CompileException
- Parameters:
isVariableArityParameter
- Whether theparameter
is the last parameter of a 'variable arity' (a.k.a. 'varargs') method declaration- Returns:
- The
Java.LocalVariable
corresponding with theparameter
- Throws:
CompileException
-
getConstantValue
public final Object getConstantValue(Java.Rvalue rv) throws CompileException
Attempts to evaluate as a constant expression.- Returns:
NOT_CONSTANT
iff the rvalue is not a constant value- Throws:
CompileException
-
findIMethod
public IClass.IMethod findIMethod(Java.MethodInvocation mi) throws CompileException
Find named methods of "targetType", examine the argument types and choose the most specific method. Check 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
ornull
- Throws:
CompileException
-
getIMethods
public void getIMethods(IClass type, String methodName, List<IClass.IMethod> v) throws CompileException
Add all methods with the givenmethodName
that are declared by thetype
, its superclasses and all their superinterfaces to the result listv
.- Throws:
CompileException
-
findIMethod
public IClass.IMethod findIMethod(Java.SuperclassMethodInvocation superclassMethodInvocation) throws CompileException
- Returns:
- The
IClass.IMethod
that implements thesuperclassMethodInvocation
- Throws:
CompileException
-
findMostSpecificIInvocable
public IClass.IInvocable findMostSpecificIInvocable(Java.Locatable locatable, IClass.IInvocable[] iInvocables, IClass[] argumentTypes, boolean boxingPermitted, Java.Scope contextScope) throws CompileException
Determine the applicable invocables and choose the most specific invocable.- Returns:
- The maximally specific
IClass.IInvocable
ornull
if noIClass.IInvocable
is applicable - Throws:
CompileException
-
toIMethod
public IClass.IMethod toIMethod(Java.MethodDeclarator methodDeclarator)
- Returns:
- The
IClass.IMethod
that implements themethodDeclarator
-
getSingleTypeImport
public String[] getSingleTypeImport(String name, Location location) throws CompileException
Check 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
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 givensimpleTypeName
cannot be resolved through any of the import-on-demand directives- Throws:
CompileException
-
findClass
public IClass findClass(String className)
Find one class or interface declared in this compilation unit by name.- 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
public void setCompileErrorHandler(ErrorHandler optionalCompileErrorHandler)
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)
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)
will definitely throw aCompileException
if one or more compile errors have occurred.- Parameters:
optionalCompileErrorHandler
-null
to restore the default behavior (throwing aCompileException
-
setWarningHandler
public void setWarningHandler(WarningHandler optionalWarningHandler)
By default, warnings are discarded, but an application my install a customWarningHandler
.- Parameters:
optionalWarningHandler
-null
to indicate that no warnings be issued
-
-