Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.proxy » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.proxy;
   15   
   16   import java.io.Serializable;
   17   import java.lang.annotation.Annotation;
   18   import java.lang.reflect.Type;
   19   import java.util.ArrayList;
   20   import java.util.List;
   21   import java.util.Set;
   22   
   23   import javax.enterprise.inject.spi.Bean;
   24   
   25   import org.apache.webbeans.annotation.WebBeansAnnotation;
   26   import org.apache.webbeans.component.AbstractBean;
   27   import org.apache.webbeans.decorator.WebBeansDecorator;
   28   import org.apache.webbeans.exception.WebBeansException;
   29   import org.apache.webbeans.intercept.InterceptorHandler;
   30   import org.apache.webbeans.intercept.webbeans.WebBeansInterceptor;
   31   import org.apache.webbeans.util.ClassUtil;
   32   
   33   import javassist.util.proxy.ProxyFactory;
   34   
   35   public final class JavassistProxyFactory
   36   {
   37       private JavassistProxyFactory()
   38       {
   39   
   40       }
   41   
   42       public static <T> Object createNewProxyInstance(Bean<T> bean)
   43       {
   44           Object result = null;
   45           try
   46           {
   47               Set<Type> types = bean.getTypes();
   48               List<Class<?>> interfaceList = new ArrayList<Class<?>>();
   49               Class<?> superClass = null;
   50               for (Type generic : types)
   51               {
   52                   Class<?> type = (Class<?>)ClassUtil.getClazz(generic);
   53                   
   54                   if (type.isInterface())
   55                   {
   56                       interfaceList.add(type);
   57                   }
   58                   
   59                   else if ((superClass == null) || (superClass.isAssignableFrom(type) && type != Object.class))
   60                   {
   61                       superClass = type;
   62                   }
   63   
   64               }
   65               if (!interfaceList.contains(Serializable.class))
   66               {
   67                   interfaceList.add(Serializable.class);
   68               }
   69   
   70               Class<?>[] interfaceArray = new Class<?>[interfaceList.size()];
   71               interfaceArray = interfaceList.toArray(interfaceArray);
   72   
   73               ProxyFactory fact = new ProxyFactory();
   74               fact.setInterfaces(interfaceArray);
   75               fact.setSuperclass(superClass);
   76   
   77               if (!(bean instanceof WebBeansDecorator) && !(bean instanceof WebBeansInterceptor))
   78               {
   79                   fact.setHandler(new InterceptorHandler((AbstractBean<?>) bean));
   80               }
   81   
   82               result = fact.createClass().newInstance();
   83   
   84           }
   85           catch (Exception e)
   86           {
   87               throw new WebBeansException(e);
   88           }
   89   
   90           return result;
   91       }
   92   
   93       public static WebBeansAnnotation createNewAnnotationProxy(Class<? extends Annotation> annotationType)
   94       {
   95           WebBeansAnnotation result = null;
   96   
   97           try
   98           {
   99               ProxyFactory pf = new ProxyFactory();
  100               pf.setInterfaces(new Class<?>[] { annotationType, Annotation.class });
  101               pf.setSuperclass(WebBeansAnnotation.class);
  102               pf.setHandler(new WebBeansAnnotation(annotationType));
  103   
  104               result = (WebBeansAnnotation) pf.create(new Class[] { Class.class }, new Object[] { annotationType });
  105   
  106           }
  107           catch (Exception e)
  108           {
  109               throw new WebBeansException(e);
  110           }
  111   
  112           return result;
  113       }
  114   
  115   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.proxy » [javadoc | source]