use of groovy.lang.GroovyClassLoader in project grails-core by grails.
the class LoggingTransformer method performInjectionOnAnnotatedClass.
@Override
public void performInjectionOnAnnotatedClass(SourceUnit source, ClassNode classNode) {
if (classNode.getNodeMetaData(Slf4j.class) != null)
return;
String packageName = Slf4j.class.getPackage().getName();
// if already annotated skip
for (AnnotationNode annotationNode : classNode.getAnnotations()) {
if (annotationNode.getClassNode().getPackageName().equals(packageName)) {
return;
}
}
FieldNode logField = classNode.getField("log");
if (logField != null) {
if (!Modifier.isPrivate(logField.getModifiers())) {
return;
}
}
if (classNode.getSuperClass().getName().equals("grails.boot.config.GrailsAutoConfiguration")) {
return;
}
AnnotationNode annotationNode = new AnnotationNode(ClassHelper.make(Slf4j.class));
LogASTTransformation logASTTransformation = new LogASTTransformation();
logASTTransformation.setCompilationUnit(new CompilationUnit(new GroovyClassLoader(getClass().getClassLoader())));
logASTTransformation.visit(new ASTNode[] { annotationNode, classNode }, source);
classNode.putNodeMetaData(Slf4j.class, annotationNode);
}
use of groovy.lang.GroovyClassLoader in project qi4j-sdk by Qi4j.
the class GroovyMixin method invokeAsObject.
private Object invokeAsObject(Method method, Object[] args, URL groovySource) throws Throwable {
try {
Class declaringClass = method.getDeclaringClass();
GroovyObject groovyObject = groovyObjects.get(declaringClass);
if (groovyObject == null) {
InputStream is = null;
final Class groovyClass;
try {
is = groovySource.openStream();
StringBuilder sourceBuilder = new StringBuilder();
Inputs.text(groovySource).transferTo(Outputs.text(sourceBuilder));
GroovyClassLoader groovyClassLoader = new GroovyClassLoader(declaringClass.getClassLoader());
groovyClass = groovyClassLoader.parseClass(sourceBuilder.toString());
} finally {
if (is != null) {
is.close();
}
}
groovyObject = (GroovyObject) groovyClass.newInstance();
if (hasProperty(groovyObject, "This")) {
groovyObject.setProperty("This", me);
}
groovyObjects.put(declaringClass, groovyObject);
}
return groovyObject.invokeMethod(method.getName(), args);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
use of groovy.lang.GroovyClassLoader in project fess by codelibs.
the class DocBoostMatcherTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
ScriptEngineFactory scriptEngineFactory = new ScriptEngineFactory();
ComponentUtil.register(scriptEngineFactory, "scriptEngineFactory");
new AbstractScriptEngine() {
@Override
public Object evaluate(String template, Map<String, Object> paramMap) {
final Map<String, Object> bindingMap = new HashMap<>(paramMap);
bindingMap.put("container", SingletonLaContainerFactory.getContainer());
final GroovyShell groovyShell = new GroovyShell(new Binding(bindingMap));
try {
return groovyShell.evaluate(template);
} catch (final JobProcessingException e) {
throw e;
} catch (final Exception e) {
return null;
} finally {
final GroovyClassLoader loader = groovyShell.getClassLoader();
loader.clearCache();
}
}
@Override
protected String getName() {
return Constants.DEFAULT_SCRIPT;
}
}.register();
}
use of groovy.lang.GroovyClassLoader in project groovy-core by groovy.
the class JavacJavaCompiler method makeParameters.
private String[] makeParameters(List<String> files, GroovyClassLoader parentClassLoader) {
Map options = config.getJointCompilationOptions();
LinkedList<String> paras = new LinkedList<String>();
File target = config.getTargetDirectory();
if (target == null)
target = new File(".");
// defaults
paras.add("-d");
paras.add(target.getAbsolutePath());
paras.add("-sourcepath");
paras.add(((File) options.get("stubDir")).getAbsolutePath());
// add flags
String[] flags = (String[]) options.get("flags");
if (flags != null) {
for (String flag : flags) {
paras.add('-' + flag);
}
}
boolean hadClasspath = false;
// add namedValues
String[] namedValues = (String[]) options.get("namedValues");
if (namedValues != null) {
for (int i = 0; i < namedValues.length; i += 2) {
String name = namedValues[i];
if (name.equals("classpath"))
hadClasspath = true;
paras.add('-' + name);
paras.add(namedValues[i + 1]);
}
}
// append classpath if not already defined
if (!hadClasspath) {
// add all classpaths that compilation unit sees
StringBuilder resultPath = new StringBuilder(DefaultGroovyMethods.join((Iterable) config.getClasspath(), File.pathSeparator));
ClassLoader cl = parentClassLoader;
while (cl != null) {
if (cl instanceof URLClassLoader) {
for (URL u : ((URLClassLoader) cl).getURLs()) {
try {
resultPath.append(File.pathSeparator);
resultPath.append(new File(u.toURI()).getPath());
} catch (URISyntaxException e) {
// ignore it
}
}
}
cl = cl.getParent();
}
paras.add("-classpath");
paras.add(resultPath.toString());
}
// files to compile
paras.addAll(files);
return paras.toArray(new String[paras.size()]);
}
use of groovy.lang.GroovyClassLoader in project groovy-core by groovy.
the class ClassNodeResolver method tryAsLoaderClassOrScript.
/**
* This method is used to realize the lookup of a class using the compilation
* unit class loader. Should no class be found we fall back to a script lookup.
* If a class is found we check if there is also a script and maybe use that
* one in case it is newer.<p/>
*
* Two class search strategies are possible: by ASM decompilation or by usual Java classloading.
* The latter is slower but is unavoidable for scripts executed in dynamic environments where
* the referenced classes might only be available in the classloader, not on disk.
*/
private LookupResult tryAsLoaderClassOrScript(String name, CompilationUnit compilationUnit) {
GroovyClassLoader loader = compilationUnit.getClassLoader();
Map<String, Boolean> options = compilationUnit.configuration.getOptimizationOptions();
boolean useAsm = !Boolean.FALSE.equals(options.get("asmResolving"));
boolean useClassLoader = !Boolean.FALSE.equals(options.get("classLoaderResolving"));
LookupResult result = useAsm ? findDecompiled(name, compilationUnit, loader) : null;
if (result != null) {
return result;
}
if (!useClassLoader) {
return tryAsScript(name, compilationUnit, null);
}
return findByClassLoading(name, compilationUnit, loader);
}
Aggregations