Search in sources :

Example 1 with ClassLoaderResourceLoader

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;
}
Also used : ClassLoaderResourceLoader(org.jboss.weld.resources.ClassLoaderResourceLoader)

Example 2 with ClassLoaderResourceLoader

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;
            }
        }
    }
}
Also used : ClassLoaderResourceLoader(org.jboss.weld.resources.ClassLoaderResourceLoader) DefaultResourceLoader(org.jboss.weld.resources.DefaultResourceLoader) ResourceLoader(org.jboss.weld.resources.spi.ResourceLoader) WeldClassLoaderResourceLoader(org.jboss.weld.resources.WeldClassLoaderResourceLoader) Constructor(java.lang.reflect.Constructor) AnnotatedConstructor(javax.enterprise.inject.spi.AnnotatedConstructor) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) URL(java.net.URL) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) IOException(java.io.IOException) ClassLoaderResourceLoader(org.jboss.weld.resources.ClassLoaderResourceLoader) WeldClassLoaderResourceLoader(org.jboss.weld.resources.WeldClassLoaderResourceLoader)

Example 3 with ClassLoaderResourceLoader

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());
    }
}
Also used : BeanArchive(org.jboss.shrinkwrap.api.BeanArchive) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZipExporter(org.jboss.shrinkwrap.api.exporter.ZipExporter) URLClassLoader(java.net.URLClassLoader) WeldContainer(org.jboss.weld.environment.se.WeldContainer) URLClassLoader(java.net.URLClassLoader) ClassLoaderResourceLoader(org.jboss.weld.resources.ClassLoaderResourceLoader) File(java.io.File) URL(java.net.URL) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 4 with ClassLoaderResourceLoader

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());
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) WeldContainer(org.jboss.weld.environment.se.WeldContainer) URLClassLoader(java.net.URLClassLoader) ClassLoaderResourceLoader(org.jboss.weld.resources.ClassLoaderResourceLoader) URL(java.net.URL) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Aggregations

ClassLoaderResourceLoader (org.jboss.weld.resources.ClassLoaderResourceLoader)4 URL (java.net.URL)3 URLClassLoader (java.net.URLClassLoader)2 Weld (org.jboss.weld.environment.se.Weld)2 WeldContainer (org.jboss.weld.environment.se.WeldContainer)2 Test (org.junit.Test)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AnnotatedConstructor (javax.enterprise.inject.spi.AnnotatedConstructor)1 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)1 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 BeanArchive (org.jboss.shrinkwrap.api.BeanArchive)1 ZipExporter (org.jboss.shrinkwrap.api.exporter.ZipExporter)1 DefaultResourceLoader (org.jboss.weld.resources.DefaultResourceLoader)1 WeldClassLoaderResourceLoader (org.jboss.weld.resources.WeldClassLoaderResourceLoader)1 ResourceLoader (org.jboss.weld.resources.spi.ResourceLoader)1