Package org.codehaus.janino
Class UnitCompiler
- java.lang.Object
-
- org.codehaus.janino.UnitCompiler
-
public class UnitCompiler extends java.lang.ObjectThis 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 interfaceUnitCompiler.ErrorHandlerInterface type forsetCompileErrorHandler(org.codehaus.janino.UnitCompiler.ErrorHandler).static classUnitCompiler.SimpleIFieldShort-hand implementation ofIClass.IFieldthat implements a non-constant, non-static, package-accessible field.
-
Field Summary
Fields Modifier and Type Field Description Java.CompilationUnitcompilationUnitstatic java.lang.ObjectNOT_CONSTANTSpecial return value for thegetConstantValue(Java.Rvalue)method family indicating that the givenJava.Rvaluedoes 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 voidbuildLocalVariableMap(Java.CatchClause cc, java.util.Map localVars)voidcompile2(Java.AnonymousClassDeclaration acd)voidcompile2(Java.ClassDeclaration cd)voidcompile2(org.codehaus.janino.Java.InnerClassDeclaration icd)voidcompile2(Java.InterfaceDeclaration id)voidcompile2(Java.LocalClassDeclaration lcd)voidcompile2(Java.MemberClassDeclaration mcd)voidcompile2(Java.PackageMemberTypeDeclaration pmtd)ClassFile[]compileUnit(boolean debugSource, boolean debugLines, boolean debugVars)Generates an array ofClassFileobjects which represent the classes and interfaces declared in the compilation unit.IClassfindClass(java.lang.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 locatable, IClass.IInvocable[] iInvocables, IClass[] argumentTypes, boolean boxingPermitted, Java.Scope contextScope)Determine the applicable invocables and choose the most specific invocable.booleangeneratesCode2(Java.AssertStatement as)booleangeneratesCode2(Java.Block b)booleangeneratesCode2(Java.BlockStatement bs)booleangeneratesCode2(Java.EmptyStatement es)booleangeneratesCode2(Java.FieldDeclaration fd)booleangeneratesCode2(Java.Initializer i)booleangeneratesCode2(Java.LocalClassDeclarationStatement lcds)booleangeneratesCode2ListStatements(java.util.List l)java.lang.ObjectgetConstantValue(Java.Rvalue rv)Attempts to evaluate as a constant expression.voidgetIMethods(IClass type, java.lang.String methodName, java.util.List v)Add all methods with the givenmethodNamethat are declared by thetype, its superclasses and all their superinterfaces to the result listv.Java.LocalVariablegetLocalVariable(Java.FunctionDeclarator.FormalParameter fp)Java.LocalVariablegetLocalVariable(Java.LocalVariableDeclarationStatement lvds, Java.VariableDeclarator vd)java.lang.String[]getSingleTypeImport(java.lang.String name, Location location)Check if the given simple name was imported through a single type import.CodeContext.OffsetgetWhereToBreak(Java.BreakableStatement bs)IClassimportTypeOnDemand(java.lang.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)UnitCompiler.ErrorHandler.voidsetWarningHandler(WarningHandler optionalWarningHandler)By default, warnings are discarded, but an application my install a customWarningHandler.IClass.IMethodtoIMethod(Java.MethodDeclarator md)Java.LvaluetoLvalueOrCompileException(Java.Atom a)
-
-
-
Field Detail
-
NOT_CONSTANT
public static final java.lang.Object NOT_CONSTANT
Special return value for thegetConstantValue(Java.Rvalue)method family indicating that the givenJava.Rvaluedoes not evaluate to a constant value.
-
compilationUnit
public final Java.CompilationUnit compilationUnit
-
-
Constructor Detail
-
UnitCompiler
public UnitCompiler(Java.CompilationUnit compilationUnit, IClassLoader iClassLoader)
-
-
Method Detail
-
compileUnit
public ClassFile[] compileUnit(boolean debugSource, boolean debugLines, boolean debugVars) throws CompileException
Generates an array ofClassFileobjects which represent the classes and interfaces declared in the compilation unit.- Throws:
CompileException
-
compile2
public void compile2(Java.PackageMemberTypeDeclaration pmtd) throws CompileException
- Throws:
CompileException
-
compile2
public void compile2(Java.ClassDeclaration cd) throws CompileException
- Throws:
CompileException
-
compile2
public void compile2(Java.AnonymousClassDeclaration acd) throws CompileException
- Throws:
CompileException
-
compile2
public void compile2(Java.LocalClassDeclaration lcd) throws CompileException
- Throws:
CompileException
-
compile2
public void compile2(org.codehaus.janino.Java.InnerClassDeclaration icd) throws CompileException- Throws:
CompileException
-
compile2
public void compile2(Java.MemberClassDeclaration mcd) throws CompileException
- Throws:
CompileException
-
compile2
public void compile2(Java.InterfaceDeclaration id) throws CompileException
- Throws:
CompileException
-
getLocalVariable
public Java.LocalVariable getLocalVariable(Java.LocalVariableDeclarationStatement lvds, Java.VariableDeclarator vd) throws CompileException
- Throws:
CompileException
-
buildLocalVariableMap
protected void buildLocalVariableMap(Java.CatchClause cc, java.util.Map localVars) throws CompileException
- Throws:
CompileException
-
getLocalVariable
public Java.LocalVariable getLocalVariable(Java.FunctionDeclarator.FormalParameter fp) throws CompileException
- Throws:
CompileException
-
getConstantValue
public final java.lang.Object getConstantValue(Java.Rvalue rv) throws CompileException
Attempts to evaluate as a constant expression.- Returns:
NOT_CONSTANTiff the rvalue is not a constant value- Throws:
CompileException
-
generatesCode2
public boolean generatesCode2(Java.BlockStatement bs)
-
generatesCode2
public boolean generatesCode2(Java.AssertStatement as)
-
generatesCode2
public boolean generatesCode2(Java.EmptyStatement es)
-
generatesCode2
public boolean generatesCode2(Java.LocalClassDeclarationStatement lcds)
-
generatesCode2
public boolean generatesCode2(Java.Initializer i) throws CompileException
- Throws:
CompileException
-
generatesCode2ListStatements
public boolean generatesCode2ListStatements(java.util.List l) throws CompileException- Throws:
CompileException
-
generatesCode2
public boolean generatesCode2(Java.Block b) throws CompileException
- Throws:
CompileException
-
generatesCode2
public boolean generatesCode2(Java.FieldDeclaration fd) throws CompileException
- Throws:
CompileException
-
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)
-
toLvalueOrCompileException
public final Java.Lvalue toLvalueOrCompileException(Java.Atom a) throws CompileException
- 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.IMethodmay be declared in an enclosing type.- Returns:
- The selected
IClass.IMethodornull - Throws:
CompileException
-
getIMethods
public void getIMethods(IClass type, java.lang.String methodName, java.util.List v) throws CompileException
Add all methods with the givenmethodNamethat are declared by thetype, its superclasses and all their superinterfaces to the result listv.- Throws:
CompileException
-
findIMethod
public IClass.IMethod findIMethod(Java.SuperclassMethodInvocation scmi) throws CompileException
- 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.IInvocableornullif noIClass.IInvocableis applicable - Throws:
CompileException
-
toIMethod
public IClass.IMethod toIMethod(Java.MethodDeclarator md)
-
getSingleTypeImport
public java.lang.String[] getSingleTypeImport(java.lang.String name, Location location) throws CompileExceptionCheck 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(java.lang.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:
nullif the givensimpleTypeNamecannot be resolved through any of the import-on-demand directives- Throws:
CompileException
-
findClass
public IClass findClass(java.lang.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:
nullif a class or an interface with that name is not declared in this compilation unit
-
setCompileErrorHandler
public void setCompileErrorHandler(UnitCompiler.ErrorHandler optionalCompileErrorHandler)
By default,CompileExceptions are thrown on compile errors, but an application my install its own (thread-local)UnitCompiler.ErrorHandler.Be aware that a single problem during compilation often causes a bunch of compile errors, so a good
UnitCompiler.ErrorHandlercounts errors and throws aCompileExceptionwhen a limit is reached.If the given
UnitCompiler.ErrorHandlerdoes not throwCompileExceptions, thencompileUnit(boolean, boolean, boolean)will throw one when the compilation of the unit is finished, and errors had occurred. In other words: TheUnitCompiler.ErrorHandlermay throw aCompileExceptionor not, butcompileUnit(boolean, boolean, boolean)will definitely throw aCompileExceptionif one or more compile errors have occurred.- Parameters:
optionalCompileErrorHandler-nullto 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-nullto indicate that no warnings be issued
-
getWhereToBreak
public CodeContext.Offset getWhereToBreak(Java.BreakableStatement bs)
-
-