/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.as.weld;

import org.jboss.modules.Module;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.jboss.weld.resources.spi.ResourceLoadingException;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * A {@link ResourceLoader} that can load classes from a {@link Module}
 * <p>
 * Thread Safety: This class is thread safe, and does not require a happens before even between construction and usage
 *
 * @author Stuart Douglas
 *
 */
public class WeldModuleResourceLoader implements ResourceLoader {

    private final Module module;

    /**
     * Additional classes that have been added to the bean archive by the container or by a portable extension
     */
    private final Map<String, Class<?>> classes;

    public WeldModuleResourceLoader(Module module) {
        this.module = module;
        this.classes = new ConcurrentHashMap<String, Class<?>>();
    }

    /**
     * If the class name is found in addtionalClasses then return it.
     *
     * Otherwise the class will be loaded from the module ClassLoader
     */
    @Override
    public Class<?> classForName(String name) {
        try {
            if (classes.containsKey(name)) {
                return classes.get(name);
            }
            final Class<?> clazz = module.getClassLoader().loadClass(name);
            //TODO: this is a temporary workaround for an issue with weld
            //It should be removed when WELD-880 is resolved
            //as weld does not handle linkage errors correctly, we have
            //to initialize the class fully here, so the exception can be wrapped

            Class<?> c = clazz;
            while(c != null && c != Object.class) {
                for(Method method : c.getDeclaredMethods()) {
                    method.getAnnotations();
                }
                for(Field field : c.getDeclaredFields()) {
                    field.getAnnotations();
                }
                c.getDeclaredAnnotations();
                c = c.getSuperclass();
            }
            classes.put(name, clazz);
            return clazz;
        } catch (NoClassDefFoundError e) {
            throw new ResourceLoadingException(e);
        } catch (ClassNotFoundException e) {
            throw new ResourceLoadingException(e);
        } catch (LinkageError e) {
            throw new ResourceLoadingException(e);
        }
    }

    public void addAdditionalClass(Class<?> clazz) {
        this.classes.put(clazz.getName(), clazz);
    }

    /**
     * Loads a resource from the module class loader
     */
    @Override
    public URL getResource(String name) {
        try {
            return module.getClassLoader().getResource(name);
        } catch (Exception e) {
            throw new ResourceLoadingException(e);
        }
    }

    /**
     * Loads resources from the module class loader
     */
    @Override
    public Collection<URL> getResources(String name) {
        try {
            final HashSet<URL> resources = new HashSet<URL>();
            Enumeration<URL> urls = module.getClassLoader().getResources(name);
            while (urls.hasMoreElements()) {
                resources.add(urls.nextElement());
            }
            return resources;
        } catch (Exception e) {
            throw new ResourceLoadingException(e);
        }

    }

    @Override
    public void cleanup() {
        // nop
    }

}

@@ -1189,73 +1189,8 @@
 n;%0A%0A
-import java.lang.reflect.Field;%0Aimport java.lang.reflect.Method;%0A
 impo
@@ -2453,725 +2453,8 @@
 e);%0A
-            //TODO: this is a temporary workaround for an issue with weld%0A            //It should be removed when WELD-880 is resolved%0A            //as weld does not handle linkage errors correctly, we have%0A            //to initialize the class fully here, so the exception can be wrapped%0A%0A            Class%3C?%3E c = clazz;%0A            while(c != null && c != Object.class) %7B%0A                for(Method method : c.getDeclaredMethods()) %7B%0A                    method.getAnnotations();%0A                %7D%0A                for(Field field : c.getDeclaredFields()) %7B%0A                    field.getAnnotations();%0A                %7D%0A                c.getDeclaredAnnotations();%0A                c = c.getSuperclass();%0A            %7D%0A
     

Remove uneeded resource loader hack