org.codehaus.janino

Class UnitCompiler

public class UnitCompiler extends Object

This class actually implements the JavaTM compiler. It is associated with exactly one compilation unit which it compiles.
Nested Class Summary
interfaceUnitCompiler.ErrorHandler
Interface type for UnitCompiler.
static classUnitCompiler.SimpleIField
Short-hand implementation of IField that implements a non-constant, non-static, package-accessible field.
Field Summary
Java.CompilationUnitcompilationUnit
Constructor Summary
UnitCompiler(Java.CompilationUnit compilationUnit, IClassLoader iClassLoader)
Method Summary
protected voidbuildLocalVariableMap(Java.CatchClause cc, Map localVars)
voidcompile2(Java.PackageMemberTypeDeclaration pmtd)
voidcompile2(Java.ClassDeclaration cd)
voidcompile2(Java.AnonymousClassDeclaration acd)
voidcompile2(Java.LocalClassDeclaration lcd)
voidcompile2(Java.InnerClassDeclaration icd)
voidcompile2(Java.MemberClassDeclaration mcd)
voidcompile2(Java.InterfaceDeclaration id)
ClassFile[]compileUnit(EnumeratorSet debuggingInformation)
Generates an array of ClassFile objects which represent the classes and interfaces defined in the compilation unit.
IClassfindClass(String className)
Find one class or interface declared in this compilation unit by name.
IClass.IMethodfindIMethod(Java.MethodInvocation mi)
Find named methods of "targetType", examine the argument types and choose the most specific method.
IClass.IMethodfindIMethod(Java.SuperclassMethodInvocation scmi)
IClass.IInvocablefindMostSpecificIInvocable(Java.Locatable l, IClass.IInvocable[] iInvocables, IClass[] argumentTypes, boolean boxingPermitted)
Determine the applicable invocables and choose the most specific invocable.
booleangeneratesCode2(Java.BlockStatement bs)
booleangeneratesCode2(Java.EmptyStatement es)
booleangeneratesCode2(Java.LocalClassDeclarationStatement lcds)
booleangeneratesCode2(Java.Initializer i)
booleangeneratesCode2(Java.Block b)
booleangeneratesCode2(Java.FieldDeclaration fd)
booleangeneratesCode2ListStatements(List l)
ObjectgetConstantValue(Java.Rvalue rv)
Attempts to evaluate as a constant expression.
voidgetIMethods(IClass type, String methodName, List v)
Add all methods with the given methodName that are declared by the type, its superclasses and all their superinterfaces to the result list v.
Java.LocalVariablegetLocalVariable(Java.LocalVariableDeclarationStatement lvds, Java.VariableDeclarator vd)
Java.LocalVariablegetLocalVariable(Java.FunctionDeclarator.FormalParameter fp)
String[]getSingleTypeImport(String name)
Check if the given name was imported through a "single type import", e.g.
     import java.util.Map
CodeContext.OffsetgetWhereToBreak(Java.BreakableStatement bs)
IClassimportTypeOnDemand(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.
voidleave2(Java.BlockStatement bs, IClass optionalStackValueType)
voidleave2(Java.SynchronizedStatement ss, IClass optionalStackValueType)
voidleave2(Java.TryStatement ts, IClass optionalStackValueType)
voidsetCompileErrorHandler(UnitCompiler.ErrorHandler optionalCompileErrorHandler)
By default, CompileExceptions are thrown on compile errors, but an application my install its own (thread-local) ErrorHandler.
voidsetWarningHandler(WarningHandler optionalWarningHandler)
By default, warnings are discarded, but an application my install a custom WarningHandler.
IClass.IMethodtoIMethod(Java.MethodDeclarator md)
Java.LvaluetoLvalueOrCE(Java.Atom a)

Field Detail

compilationUnit

public final Java.CompilationUnit compilationUnit

Constructor Detail

UnitCompiler

public UnitCompiler(Java.CompilationUnit compilationUnit, IClassLoader iClassLoader)

Method Detail

buildLocalVariableMap

protected void buildLocalVariableMap(Java.CatchClause cc, Map localVars)

compile2

public void compile2(Java.PackageMemberTypeDeclaration pmtd)

compile2

public void compile2(Java.ClassDeclaration cd)

compile2

public void compile2(Java.AnonymousClassDeclaration acd)

compile2

public void compile2(Java.LocalClassDeclaration lcd)

compile2

public void compile2(Java.InnerClassDeclaration icd)

compile2

public void compile2(Java.MemberClassDeclaration mcd)

compile2

public void compile2(Java.InterfaceDeclaration id)

compileUnit

public ClassFile[] compileUnit(EnumeratorSet debuggingInformation)
Generates an array of ClassFile objects which represent the classes and interfaces defined in the compilation unit.

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 with that name is not declared in this compilation unit

findIMethod

public IClass.IMethod findIMethod(Java.MethodInvocation mi)
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 IMethod may be declared in an enclosing type.

Returns: The selected IMethod or null

findIMethod

public IClass.IMethod findIMethod(Java.SuperclassMethodInvocation scmi)

findMostSpecificIInvocable

public IClass.IInvocable findMostSpecificIInvocable(Java.Locatable l, IClass.IInvocable[] iInvocables, IClass[] argumentTypes, boolean boxingPermitted)
Determine the applicable invocables and choose the most specific invocable.

Returns: the maximally specific IInvocable or null if no IInvocable is applicable

Throws: CompileException

generatesCode2

public boolean generatesCode2(Java.BlockStatement bs)

generatesCode2

public boolean generatesCode2(Java.EmptyStatement es)

generatesCode2

public boolean generatesCode2(Java.LocalClassDeclarationStatement lcds)

generatesCode2

public boolean generatesCode2(Java.Initializer i)

generatesCode2

public boolean generatesCode2(Java.Block b)

generatesCode2

public boolean generatesCode2(Java.FieldDeclaration fd)

generatesCode2ListStatements

public boolean generatesCode2ListStatements(List l)

getConstantValue

public final Object getConstantValue(Java.Rvalue rv)
Attempts to evaluate as a constant expression.

Expression typeReturn value type
StringString
byteByte
shortChort
intInteger
booleanBoolean
charCharacter
floatFloat
longLong
doubleDouble
nullCONSTANT_VALUE_NULL

Returns: null iff the rvalue is not a constant value

getIMethods

public void getIMethods(IClass type, String methodName, List v)
Add all methods with the given methodName that are declared by the type, its superclasses and all their superinterfaces to the result list v.

Parameters: type methodName v

Throws: CompileException

getLocalVariable

public Java.LocalVariable getLocalVariable(Java.LocalVariableDeclarationStatement lvds, Java.VariableDeclarator vd)

getLocalVariable

public Java.LocalVariable getLocalVariable(Java.FunctionDeclarator.FormalParameter fp)

getSingleTypeImport

public String[] getSingleTypeImport(String name)
Check if the given name was imported through a "single type import", e.g.
     import java.util.Map

Returns: the fully qualified name or null

getWhereToBreak

public CodeContext.Offset getWhereToBreak(Java.BreakableStatement bs)

importTypeOnDemand

public 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.

Returns: null if the given simpleTypeName cannot be resolved through any of the import-on-demand directives

leave2

public void leave2(Java.BlockStatement bs, IClass optionalStackValueType)

leave2

public void leave2(Java.SynchronizedStatement ss, IClass optionalStackValueType)

leave2

public void leave2(Java.TryStatement ts, IClass optionalStackValueType)

setCompileErrorHandler

public void setCompileErrorHandler(UnitCompiler.ErrorHandler optionalCompileErrorHandler)
By default, CompileExceptions 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 a CompileException when a limit is reached.

If the given ErrorHandler does not throw CompileExceptions, then compileUnit will throw one when the compilation of the unit is finished, and errors had occurred. In other words: The ErrorHandler may throw a CompileException or not, but compileUnit will definitely throw a CompileException if one or more compile errors have occurred.

Parameters: optionalCompileErrorHandler null to restore the default behavior (throwing a CompileException

setWarningHandler

public void setWarningHandler(WarningHandler optionalWarningHandler)
By default, warnings are discarded, but an application my install a custom WarningHandler.

Parameters: optionalWarningHandler null to indicate that no warnings be issued

toIMethod

public IClass.IMethod toIMethod(Java.MethodDeclarator md)

toLvalueOrCE

public final Java.Lvalue toLvalueOrCE(Java.Atom a)