Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * http://www.apache.org/licenses/LICENSE-2.0
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.weld.util.bytecode;
 
 
 
Utility class for loading a ClassFile into a classloader. This borrows heavily from javassist

Author(s):
Stuart Douglas
 
 public class ClassFileUtils {
     private static java.lang.reflect.Method defineClass1defineClass2;
 
     private ClassFileUtils() {
     }
 
     static {
         try {
             AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                 public Object run() throws Exception {
                     Class<?> cl = Class.forName("java.lang.ClassLoader");
                     final String name = "defineClass";
                      = cl.getDeclaredMethod(namenew Class[]{String.classbyte[].classint.classint.class});
                     .setAccessible(true);
 
                      = cl.getDeclaredMethod(namenew Class[]{String.classbyte[].classint.classint.classProtectionDomain.class});
                     .setAccessible(true);
                     return null;
                 }
             });
         } catch (PrivilegedActionException pae) {
             throw new RuntimeException("cannot initialize ClassPool"pae.getException());
         }
     }

    
Converts the class to a java.lang.Class object. Once this method is called, further modifications are not allowed any more.

The class file represented by the given CtClass is loaded by the given class loader to construct a java.lang.Class object. Since a private method on the class loader is invoked through the reflection API, the caller must have permissions to do that.

An easy way to obtain ProtectionDomain object is to call getProtectionDomain() in java.lang.Class. It returns the domain that the class belongs to.

This method is provided for convenience. If you need more complex functionality, you should write your own class loader.

Parameters:
loader the class loader used to load this class. For example, the loader returned by getClassLoader() can be used for this parameter.
domain the protection domain for the class. If it is null, the default domain created by java.lang.ClassLoader is
 
     public static Class<?> toClass(ClassFile ctClassLoader loaderProtectionDomain domain) {
         try {
             byte[] b = ct.toBytecode();
             java.lang.reflect.Method method;
             Object[] args;
             if (domain == null) {
                 method = ;
                 args = new Object[]{ct.getName(), b, 0, b.length};
             } else {
                 method = ;
                 args = new Object[]{ct.getName(), b, 0, b.lengthdomain};
             }
 
             return toClass2(methodloaderargs);
         } catch (RuntimeException e) {
             throw e;
        } catch (java.lang.reflect.InvocationTargetException e) {
            throw new RuntimeException(e.getTargetException());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private static synchronized Class<?> toClass2(Method methodClassLoader loaderObject[] argsthrows Exception {
        Class<?> clazz = Class.class.cast(method.invoke(loaderargs));
        return clazz;
    }
New to GrepCode? Check out our FAQ X