Search in sources :

Example 1 with ResourceLoader

use of org.jboss.weld.resources.spi.ResourceLoader in project core by weld.

the class WeldConfiguration method findPropertiesFiles.

@SuppressFBWarnings(value = "DMI_COLLECTION_OF_URLS", justification = "Only local URLs involved")
private Set<URL> findPropertiesFiles(Deployment deployment, String fileName) {
    Set<ResourceLoader> resourceLoaders = new HashSet<ResourceLoader>();
    Set<URL> files = new HashSet<URL>();
    ResourceLoader deploymentResourceLoader = deployment.getServices().get(ResourceLoader.class);
    if (deploymentResourceLoader != null) {
        resourceLoaders.add(deploymentResourceLoader);
    }
    for (BeanDeploymentArchive archive : deployment.getBeanDeploymentArchives()) {
        ResourceLoader resourceLoader = archive.getServices().get(ResourceLoader.class);
        if (resourceLoader == null) {
            ConfigurationLogger.LOG.resourceLoaderNotSpecifiedForArchive(archive);
            continue;
        }
        resourceLoaders.add(resourceLoader);
    }
    for (ResourceLoader resourceLoader : resourceLoaders) {
        URL file = resourceLoader.getResource(fileName);
        if (file != null) {
            files.add(file);
        }
    }
    return files;
}
Also used : ResourceLoader(org.jboss.weld.resources.spi.ResourceLoader) WeldClassLoaderResourceLoader(org.jboss.weld.resources.WeldClassLoaderResourceLoader) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) URL(java.net.URL) HashSet(java.util.HashSet) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with ResourceLoader

use of org.jboss.weld.resources.spi.ResourceLoader 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 ResourceLoader

use of org.jboss.weld.resources.spi.ResourceLoader in project core by weld.

the class AdditionalServiceLoader method getResourceLoaders.

private Set<ResourceLoader> getResourceLoaders() {
    Set<ResourceLoader> resourceLoaders = new HashSet<>();
    for (BeanDeploymentArchive archive : deployment.getBeanDeploymentArchives()) {
        ResourceLoader resourceLoader = archive.getServices().get(ResourceLoader.class);
        if (resourceLoader != null) {
            resourceLoaders.add(resourceLoader);
        }
    }
    resourceLoaders.add(WeldClassLoaderResourceLoader.INSTANCE);
    resourceLoaders.add(DefaultResourceLoader.INSTANCE);
    return resourceLoaders;
}
Also used : ResourceLoader(org.jboss.weld.resources.spi.ResourceLoader) WeldClassLoaderResourceLoader(org.jboss.weld.resources.WeldClassLoaderResourceLoader) DefaultResourceLoader(org.jboss.weld.resources.DefaultResourceLoader) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) HashSet(java.util.HashSet)

Aggregations

WeldClassLoaderResourceLoader (org.jboss.weld.resources.WeldClassLoaderResourceLoader)3 ResourceLoader (org.jboss.weld.resources.spi.ResourceLoader)3 URL (java.net.URL)2 HashSet (java.util.HashSet)2 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)2 DefaultResourceLoader (org.jboss.weld.resources.DefaultResourceLoader)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Constructor (java.lang.reflect.Constructor)1 Method (java.lang.reflect.Method)1 AnnotatedConstructor (javax.enterprise.inject.spi.AnnotatedConstructor)1 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)1 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 ClassLoaderResourceLoader (org.jboss.weld.resources.ClassLoaderResourceLoader)1