Interface ICookable
- All Known Subinterfaces:
IClassBodyEvaluator, IExpressionEvaluator, IScriptEvaluator, ISimpleCompiler
- All Known Implementing Classes:
ClassBodyEvaluator, ClassBodyEvaluator, Cookable, ExpressionEvaluator, ExpressionEvaluator, MultiCookable, ScriptEvaluator, ScriptEvaluator, SimpleCompiler, SimpleCompiler
IClassBodyEvaluator, then the tokens are interpreted as a Java class body and compiled into a Class which
is accessible through IClassBodyEvaluator.getClazz().-
Method Summary
Modifier and TypeMethodDescriptionvoidcook(InputStream is) Reads, scans, parses and compiles Java tokens from the givenInputStream, encoded in the "platform default encoding".voidcook(InputStream is, String encoding) Reads, scans, parses and compiles Java tokens from the givenInputStreamwith the givenencoding.voidReads, scans, parses and compiles Java tokens from the givenReader.voidReads, scans, parses and compiles Java tokens from the givenString.voidcook(String fileName, InputStream is) Reads, scans, parses and compiles Java tokens from the givenInputStream, encoded in the "platform default encoding".voidcook(String fileName, InputStream is, String encoding) Reads, scans, parses and compiles Java tokens from the givenInputStreamwith the givenencoding.voidReads, scans, parses and compiles Java tokens from the givenReader.voidReads, scans, parses and compiles Java tokens from the givenString.voidReads, scans, parses and compiles Java tokens from the givenFile, encoded in the "platform default encoding".voidReads, scans, parses and compiles Java tokens from the givenFilewith the givenencoding.voidReads, scans, parses and compiles Java tokens from the named file, encoded in the "platform default encoding".voidReads, scans, parses and compiles Java tokens from the named file with the given encoding.default voidsetProtectionDomain(ProtectionDomain protectionDomain) Sets theProtectionDomainwith which the generated classes are defined.default voidsetSandboxPolicy(SandboxPolicy policy) Restricts the APIs that the cooked code may use.voidsetSourceVersion(int version) Specifies the version of source code accepted, in analogy with JAVAC's-sourcecommand line option.voidsetTargetVersion(int version) Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand line option.
-
Method Details
-
cook
Reads, scans, parses and compiles Java tokens from the givenReader.- Parameters:
fileName- Used when reporting errors and warnings- Throws:
CompileExceptionIOException
-
cook
Reads, scans, parses and compiles Java tokens from the givenReader.- Throws:
CompileExceptionIOException
-
cook
Reads, scans, parses and compiles Java tokens from the givenInputStream, encoded in the "platform default encoding".- Throws:
CompileExceptionIOException
-
cook
Reads, scans, parses and compiles Java tokens from the givenInputStream, encoded in the "platform default encoding".- Parameters:
fileName- Used when reporting errors and warnings- Throws:
CompileExceptionIOException
-
cook
Reads, scans, parses and compiles Java tokens from the givenInputStreamwith the givenencoding.- Throws:
CompileExceptionIOException
-
cook
void cook(@Nullable String fileName, InputStream is, @Nullable String encoding) throws CompileException, IOException Reads, scans, parses and compiles Java tokens from the givenInputStreamwith the givenencoding.- Parameters:
fileName- Used when reporting errors and warnings- Throws:
CompileExceptionIOException
-
cook
Reads, scans, parses and compiles Java tokens from the givenString.- Throws:
CompileException
-
cook
Reads, scans, parses and compiles Java tokens from the givenString.- Parameters:
fileName- Used when reporting errors and warnings- Throws:
CompileException
-
cookFile
Reads, scans, parses and compiles Java tokens from the givenFile, encoded in the "platform default encoding".- Throws:
CompileExceptionIOException
-
cookFile
Reads, scans, parses and compiles Java tokens from the givenFilewith the givenencoding.- Throws:
CompileExceptionIOException
-
cookFile
Reads, scans, parses and compiles Java tokens from the named file, encoded in the "platform default encoding".- Throws:
CompileExceptionIOException
-
cookFile
Reads, scans, parses and compiles Java tokens from the named file with the given encoding.- Throws:
CompileExceptionIOException
-
setSourceVersion
void setSourceVersion(int version) Specifies the version of source code accepted, in analogy with JAVAC's-sourcecommand line option. May be ignored by an implementation (e.g. thejaninoimplementation always accepts the language features as described on the home page). Allowed values, and the default value, depend on the implementation.-1means to use a default version. -
setTargetVersion
void setTargetVersion(int version) Generates class files that target a specified release of the virtual machine, in analogy with JAVAC's-targetcommand line option. Allowed values depend on the implementation. The default value also depends on the implementation. The only invariant is that the generated class files are suitable for the currently executing JVM.-1means to use a default version. -
getBytecodes
- Returns:
- The generated Java bytecode; maps class name to bytes
- Throws:
IllegalStateException- This IClassBodyEvaluator is not yet cooked
-
setSandboxPolicy
Restricts the APIs that the cooked code may use. If set, then all generated classes are verified against the policy before they are loaded, and cooking fails with aCompileExceptionthat lists all violations if the code uses any field, method or constructor that the policy does not allow.Must be called before cooking.
null(the default) means that the generated code is not restricted.If a policy is set, then the generated classes always contain debugging information about the source file and the line numbers, so that the violations can be reported with their locations in the cooked document (see
SandboxViolation.getLocation()).- Throws:
UnsupportedOperationException- The implementation does not support sandbox policies- See Also:
-
setProtectionDomain
Sets theProtectionDomainwith which the generated classes are defined.Must be called before cooking.
null(the default) means that the generated classes are defined with the protection domain of the compiler itself, i.e. that they have the same permissions as the compiler.Use this when the cooked code is executed in a
Sandbox: Without it, the cooked code could callAccessController.doPrivileged()and would then run with the permissions of the compiler, not with the permissions of the sandbox. To prevent that, define the generated classes with the same permissions as the sandbox, through a protection domain with static permissions (i.e. one that is created with the two-argument constructorProtectionDomain(java.security.CodeSource, java.security.PermissionCollection), which does not consult the globalPolicy):Sandbox sandbox = new Sandbox(permissions); scriptEvaluator.setProtectionDomain(new ProtectionDomain(null, permissions)); scriptEvaluator.cook(script); sandbox.confine(...);On JVMs without a security manager, protection domains have no effect.
- Throws:
UnsupportedOperationException- The implementation does not support setting the protection domain- See Also:
-