janino-compiler/janino

help

UltraSina opened this issue · 3 comments

hi , how i can make it ignore classes for import , for example java.lang is default ignored

You might want to use methods such as

void setDefaultImports(String... defaultImports);
or similar

thanks , but i use SimpleCompiler for compiling full classes with package name inside of them , and Simple Compiler does not have a methode for imports

First of all: Reminding anyone about issues isn't necessary as we generally get an email and a github notification for every message. This is also the reason why internet etiquette generally advises against doing so as you'll be senselessly distracting people unrelated to this issue (this repo has 58 watchers, a good chunk of them will get notifications for issues), too.

I personally believe that SimpleCompiler won't be able to accomplish this without you editing the code of the file you wish to compile manually. SimpleCompiler is primarily meant as - who guessed it - a simple implementation of the compiler and going beyond that will require you to use lower-level components of janino. For reference, this is how ClassBodyEvaluator does it (ClassBodyEvaluator is in turn used by ScriptEvaluator):

Import generation:

/**
* @return The {@link #setDefaultImports(String...) default imports}, concatenated
* with the import declarations that can be parsed from the
* <var>parser</var>
* @see Parser#parseImportDeclarationBody()
*/
final Java.AbstractCompilationUnit.ImportDeclaration[]
makeImportDeclarations(@Nullable Parser parser) throws CompileException, IOException {
List<Java.AbstractCompilationUnit.ImportDeclaration>
l = new ArrayList<>();
// Honor the default imports.
for (String defaultImport : this.defaultImports) {
final Parser p = new Parser(new Scanner(null, new StringReader(defaultImport)));
p.setSourceVersion(this.sourceVersion);
p.setWarningHandler(this.warningHandler);
l.add(p.parseImportDeclarationBody());
p.read(TokenType.END_OF_INPUT);
}
// Parse all available IMPORT declarations.
if (parser != null) {
while (parser.peek("import")) l.add(parser.parseImportDeclaration());
}
return (Java.AbstractCompilationUnit.ImportDeclaration[]) l.toArray(
new AbstractCompilationUnit.ImportDeclaration[l.size()]
);
}

Source file parsing process:
@Override public final void
cook(@Nullable String fileName, Reader r) throws CompileException, IOException {
this.cook(new Scanner(fileName, r));
}
public void
cook(Scanner scanner) throws CompileException, IOException {
try {
this.cook2(scanner);
} catch (StackOverflowError soe) {
throw new CompileException("Script is nested too deeply", null, soe);
}
}
private void
cook2(Scanner scanner) throws CompileException, IOException {
Parser parser = new Parser(scanner);
parser.setSourceVersion(this.sourceVersion);
Java.AbstractCompilationUnit.ImportDeclaration[] importDeclarations = this.makeImportDeclarations(parser);
Java.CompilationUnit compilationUnit = new Java.CompilationUnit(scanner.getFileName(), importDeclarations);
// Add class declaration.
Java.AbstractClassDeclaration
acd = this.addPackageMemberClassDeclaration(scanner.location(), compilationUnit);
// Parse class body declarations (member declarations) until EOF.
while (!parser.peek(TokenType.END_OF_INPUT)) parser.parseClassBodyDeclaration(acd);
// Compile and load it.
this.cook(compilationUnit);
}