本文整理汇总了Java中org.codehaus.janino.Java.CompilationUnit类的典型用法代码示例。如果您正苦于以下问题:Java CompilationUnit类的具体用法?Java CompilationUnit怎么用?Java CompilationUnit使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CompilationUnit类属于org.codehaus.janino.Java包,在下文中一共展示了CompilationUnit类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: get
import org.codehaus.janino.Java.CompilationUnit; //导入依赖的package包/类
private CompilationUnit get(Class<?> c) throws IOException {
URL u = getSourceURL(c);
try (Reader reader = Resources.asCharSource(u, UTF_8).openStream()) {
String body = CharStreams.toString(reader);
// TODO: Hack to remove annotations so Janino doesn't choke. Need to reconsider this problem...
body = body.replaceAll("@\\w+(?:\\([^\\\\]*?\\))?", "");
for(Replacement r : REPLACERS){
body = r.apply(body);
}
// System.out.println("original");
// System.out.println(body);;
// System.out.println("decompiled");
// System.out.println(decompile(c));
try {
return new Parser(new Scanner(null, new StringReader(body))).parseCompilationUnit();
} catch (CompileException e) {
logger.warn("Failure while parsing function class:\n{}", body, e);
return null;
}
}
}
示例2: get
import org.codehaus.janino.Java.CompilationUnit; //导入依赖的package包/类
private CompilationUnit get(Class<?> c) throws IOException{
String path = c.getName();
path = path.replaceFirst("\\$.*", "");
path = path.replace(".", FileUtils.separator);
path = "/" + path + ".java";
CompilationUnit cu = functionUnits.get(path);
if(cu != null) {
return cu;
}
URL u = Resources.getResource(c, path);
InputSupplier<InputStream> supplier = Resources.newInputStreamSupplier(u);
try (InputStream is = supplier.getInput()) {
if (is == null) {
throw new IOException(String.format("Failure trying to located source code for Class %s, tried to read on classpath location %s", c.getName(), path));
}
String body = IO.toString(is);
//TODO: Hack to remove annotations so Janino doesn't choke. Need to reconsider this problem...
body = body.replaceAll("@\\w+(?:\\([^\\\\]*?\\))?", "");
try{
cu = new Parser(new Scanner(null, new StringReader(body))).parseCompilationUnit();
functionUnits.put(path, cu);
return cu;
} catch (CompileException e) {
logger.warn("Failure while parsing function class:\n{}", body, e);
return null;
}
}
}
示例3: checkInit
import org.codehaus.janino.Java.CompilationUnit; //导入依赖的package包/类
private void checkInit() {
count.incrementAndGet();
if (ready) {
return;
}
synchronized (this) {
if (ready) {
return;
}
// get function body.
try {
final Class<?> clazz = Class.forName(className);
final CompilationUnit cu = get(clazz);
if (cu == null) {
throw new IOException(String.format("Failure while loading class %s.", clazz.getName()));
}
methods = ImmutableMap.copyOf(MethodGrabbingVisitor.getMethods(cu, clazz));
if(methods.isEmpty()){
throw UserException.functionError()
.message("Failure reading Function class. No methods were found.")
.addContext("Function Class", className)
.build(logger);
} else {
// logger.warn("{} Methods for class {} loaded. Methods available: {}", methods.size(), className, methods.entrySet());
}
ready = true;
} catch (IOException | ClassNotFoundException e) {
throw UserException.functionError(e)
.message("Failure reading Function class.")
.addContext("Function Class", className)
.build(logger);
}
}
}
示例4: loadFunctionBody
import org.codehaus.janino.Java.CompilationUnit; //导入依赖的package包/类
/**
* Loads function body: methods (for instance, eval, setup, reset) and imports.
* Loading is done once per class instance upon first function invocation.
* Double-checked locking is used to avoid concurrency issues
* when two threads are trying to load the function body at the same time.
*/
private void loadFunctionBody() {
if (isLoaded) {
return;
}
synchronized (this) {
if (isLoaded) {
return;
}
logger.trace("Getting function body for the {}", className);
try {
final Class<?> clazz = Class.forName(className, true, classLoader);
final CompilationUnit cu = convertToCompilationUnit(clazz);
methods = MethodGrabbingVisitor.getMethods(cu, clazz);
imports = ImportGrabber.getImports(cu);
isLoaded = true;
} catch (IOException | ClassNotFoundException e) {
throw UserException.functionError(e)
.message("Failure reading Function class.")
.addContext("Function Class", className)
.build(logger);
}
}
}
示例5: convertToCompilationUnit
import org.codehaus.janino.Java.CompilationUnit; //导入依赖的package包/类
/**
* Using class name generates path to class source code (*.java),
* reads its content as string and parses it into {@link org.codehaus.janino.Java.CompilationUnit}.
*
* @param clazz function class
* @return compilation unit
* @throws IOException if did not find class or could not load it
*/
private CompilationUnit convertToCompilationUnit(Class<?> clazz) throws IOException {
String path = clazz.getName();
path = path.replaceFirst("\\$.*", "");
path = path.replace(".", DrillFileUtils.SEPARATOR);
path = "/" + path + ".java";
logger.trace("Loading function code from the {}", path);
try (InputStream is = clazz.getResourceAsStream(path)) {
if (is == null) {
throw new IOException(String.format(
"Failure trying to locate source code for class %s, tried to read on classpath location %s", clazz.getName(),
path));
}
String body = IO.toString(is);
// TODO: Hack to remove annotations so Janino doesn't choke. Need to reconsider this problem...
body = body.replaceAll("@\\w+(?:\\([^\\\\]*?\\))?", "");
try {
return new Parser(new Scanner(null, new StringReader(body))).parseCompilationUnit();
} catch (CompileException e) {
throw new IOException(String.format("Failure while loading class %s.", clazz.getName()), e);
}
}
}