Save This Page
Home » Groovy-1.7.0 » org.codehaus » groovy » reflection » [javadoc | source]
    1   package org.codehaus.groovy.reflection;
    2   
    3   import org.codehaus.groovy.runtime.callsite.GroovySunClassLoader;
    4   import org.codehaus.groovy.runtime.callsite.CallSite;
    5   
    6   import java.lang.ref.SoftReference;
    7   import java.lang.reflect.Constructor;
    8   import java.lang.reflect.Method;
    9   import java.util.Set;
   10   import java.util.HashSet;
   11   import java.security.AccessController;
   12   import java.security.PrivilegedAction;
   13   
   14   import groovy.lang.MetaClassImpl;
   15   import groovy.lang.MetaMethod;
   16   
   17   public class ClassLoaderForClassArtifacts extends ClassLoader {
   18       public final SoftReference<Class> klazz;
   19       private final Set<String> allocatedNames = new HashSet<String>();
   20   
   21       public ClassLoaderForClassArtifacts(Class klazz) {
   22           super(klazz.getClassLoader());
   23           this.klazz = new SoftReference<Class> (klazz);
   24       }
   25   
   26       public Class define (String name, byte [] bytes) {
   27           Class cls = defineClass(name, bytes, 0, bytes.length, klazz.get().getProtectionDomain());
   28           resolveClass(cls);
   29           return cls;
   30       }
   31   
   32       public Class loadClass(String name) throws ClassNotFoundException {
   33           Class cls = findLoadedClass(name);
   34           if (cls != null)
   35             return cls;
   36   
   37           if (GroovySunClassLoader.sunVM != null) {
   38               cls = GroovySunClassLoader.sunVM.doesKnow(name);
   39               if (cls != null)
   40                 return cls;
   41           }
   42   
   43           return super.loadClass(name);
   44       }
   45   
   46       public synchronized String createClassName(Method method) {
   47           final String name;
   48           final String clsName = klazz.get().getName();
   49           if (clsName.startsWith("java."))
   50             name = clsName.replace('.','_') + "$" + method.getName();
   51           else
   52             name = clsName + "$" + method.getName();
   53   
   54           if (!allocatedNames.contains(name)) {
   55             allocatedNames.add(name);
   56             return name;
   57           }
   58   
   59           for (int i = 0; ; i++) {
   60               String newName = name + "$" + i;
   61               if (!allocatedNames.contains(newName)) {
   62                 allocatedNames.add(newName);
   63                 return newName;
   64               }
   65           }
   66       }
   67   
   68       public Constructor defineClassAndGetConstructor(final String name, final byte[] bytes) {
   69           final Class cls = AccessController.doPrivileged( new PrivilegedAction<Class>(){
   70               public Class run() {
   71                   return define(name, bytes);
   72               }
   73           });
   74   
   75           if (cls != null) {
   76               try {
   77                   return cls.getConstructor(CallSite.class, MetaClassImpl.class, MetaMethod.class, Class[].class);
   78               } catch (NoSuchMethodException e) { //
   79               }
   80           }
   81           return null;
   82       }
   83   }

Save This Page
Home » Groovy-1.7.0 » org.codehaus » groovy » reflection » [javadoc | source]