use of org.jboss.weld.resources.ClassLoaderResourceLoader in project core by weld.
the class Weld method setClassLoader.
/**
* Set a {@link ClassLoader}. The given {@link ClassLoader} will be scanned automatically for bean archives if scanning is enabled.
*
* @param classLoader
* @return self
*/
public Weld setClassLoader(ClassLoader classLoader) {
Preconditions.checkNotNull(classLoader);
resourceLoader = new ClassLoaderResourceLoader(classLoader);
return this;
}
use of org.jboss.weld.resources.ClassLoaderResourceLoader in project core by weld.
the class Formats method getLineNumber.
/**
* Try to get the line number associated with the given member.
*
* The reflection API does not expose such an info and so we need to analyse the bytecode. Unfortunately, it seems there is no way to get this kind of
* information for fields. Moreover, the <code>LineNumberTable</code> attribute is just optional, i.e. the compiler is not required to store this
* information at all. See also <a href="http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.1">Java Virtual Machine Specification</a>
*
* Implementation note: it wouldn't be appropriate to add a bytecode scanning dependency just for this functionality, therefore Apache BCEL included in
* Oracle JDK 1.5+ and OpenJDK 1.6+ is used. Other JVMs should not crash as we only use it if it's on the classpath and by means of reflection calls.
*
* @param member
* @param resourceLoader
* @return the line number or 0 if it's not possible to find it
*/
public static int getLineNumber(Member member) {
if (!(member instanceof Method || member instanceof Constructor)) {
// We are not able to get this info for fields
return 0;
}
ResourceLoader bcelResourceLoader = WeldClassLoaderResourceLoader.INSTANCE;
if (!Reflections.isClassLoadable(BCEL_JAVA_CLASS_FQCN, bcelResourceLoader)) {
// Try TCCL as a fallback
bcelResourceLoader = DefaultResourceLoader.INSTANCE;
if (!Reflections.isClassLoadable(BCEL_JAVA_CLASS_FQCN, bcelResourceLoader)) {
// Apache BCEL classes not found on the classpath
return 0;
}
}
String classFile = member.getDeclaringClass().getName().replace('.', '/');
ClassLoaderResourceLoader classFileResourceLoader = new ClassLoaderResourceLoader(member.getDeclaringClass().getClassLoader());
InputStream in = null;
try {
URL classFileUrl = classFileResourceLoader.getResource(classFile + ".class");
if (classFileUrl == null) {
// The class file is not available
return 0;
}
in = classFileUrl.openStream();
Class<?> classParserClass = Reflections.loadClass(BCEL_CLASS_PARSER_FQCN, bcelResourceLoader);
Class<?> javaClassClass = Reflections.loadClass(BCEL_JAVA_CLASS_FQCN, bcelResourceLoader);
Class<?> methodClass = Reflections.loadClass(BCEL_METHOD_FQCN, bcelResourceLoader);
Class<?> lntClass = Reflections.loadClass(BCEL_LINE_NUMBER_TABLE_FQCN, bcelResourceLoader);
Object parser = classParserClass.getConstructor(InputStream.class, String.class).newInstance(in, classFile);
Object javaClass = classParserClass.getMethod(BCEL_M_PARSE).invoke(parser);
// First get all declared methods and constructors
// Note that in bytecode constructor is translated into a method
Object[] methods = (Object[]) javaClassClass.getMethod(BCEL_M_GET_METHODS).invoke(javaClass);
Object match = null;
String signature;
String name;
if (member instanceof Method) {
signature = DescriptorUtils.methodDescriptor((Method) member);
name = member.getName();
} else if (member instanceof Constructor) {
signature = DescriptorUtils.makeDescriptor((Constructor<?>) member);
name = INIT_METHOD_NAME;
} else {
return 0;
}
for (Object method : methods) {
// Matching method must have the same name, modifiers and signature
if (methodClass.getMethod(BCEL_M_GET_NAME).invoke(method).equals(name) && methodClass.getMethod(BCEL_M_GET_MODIFIERS).invoke(method).equals(member.getModifiers()) && methodClass.getMethod(BCEL_M_GET_SIGNATURE).invoke(method).equals(signature)) {
match = method;
}
}
if (match != null) {
// If a method is found, try to obtain the optional LineNumberTable attribute
Object lineNumberTable = methodClass.getMethod(BCEL_M_GET_LINE_NUMBER_TABLE).invoke(match);
if (lineNumberTable != null) {
int line = (int) lntClass.getMethod(BCEL_M_GET_SOURCE_LINE, int.class).invoke(lineNumberTable, 0);
return line == -1 ? 0 : line;
}
}
// No suitable method found
return 0;
} catch (Throwable t) {
return 0;
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
return 0;
}
}
}
}
use of org.jboss.weld.resources.ClassLoaderResourceLoader in project core by weld.
the class ExplicitResourceLoaderScanningTest method testScanning.
@Test
public void testScanning() throws IOException {
// this is the application
final Archive<?> archive = create(BeanArchive.class).addClass(EmbeddedApplication.class);
final File jar = File.createTempFile("weld-se-resource-loader-test", ".jar");
jar.deleteOnExit();
archive.as(ZipExporter.class).exportTo(jar, true);
/*
* Special classloader that hides BDAs in parent classloaders. This would not be needed normally. We need this here because
* , since this testsuite defines a top-level beans.xml file, each file in this testsuite is already part of this single giant BDA.
* Since we are adding the EmbeddedApplication class to the special BDA we test, we do not want the class to be found twice. We cannot just leave
* out the parent classloader as we need CDI classes to be loadable from the application.
*/
ClassLoader classLoader = new URLClassLoader(new URL[] { jar.toURI().toURL() }) {
@Override
public Enumeration<URL> getResources(String name) throws IOException {
if (AbstractWeldDeployment.BEANS_XML.equals(name)) {
return findResources(name);
}
return super.getResources(name);
}
};
try (WeldContainer weld = new Weld().setResourceLoader(new ClassLoaderResourceLoader(classLoader)).initialize()) {
AtomicInteger payload = new AtomicInteger();
weld.event().fire(payload);
Assert.assertEquals(10, payload.intValue());
}
}
use of org.jboss.weld.resources.ClassLoaderResourceLoader in project core by weld.
the class ExplicitResourceLoaderExtensionScanningTest method testScanning.
@Test
public void testScanning() {
ClassLoader classLoader = new URLClassLoader(new URL[] {}, Alpha.class.getClassLoader()) {
@Override
public Enumeration<URL> getResources(String name) throws IOException {
if ("META-INF/services/javax.enterprise.inject.spi.Extension".equals(name)) {
// Load only AlphaExtension
return super.getResources("META-INF/services/" + MyExtension.class.getName());
}
return super.getResources(name);
}
};
try (WeldContainer container = new Weld().setResourceLoader(new ClassLoaderResourceLoader(classLoader)).initialize()) {
container.select(Alpha.class).get().ping();
assertTrue(container.select(Bravo.class).isUnsatisfied());
}
}
Aggregations