<commit_before>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc. and/or its affiliates, 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.bean.proxy;

import static org.jboss.weld.logging.Category.BEAN;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BeanMessage.PROXY_INSTANTIATION_BEAN_ACCESS_FAILED;
import static org.jboss.weld.logging.messages.BeanMessage.PROXY_INSTANTIATION_FAILED;

import java.io.Serializable;
import java.lang.reflect.Modifier;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Arrays;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewConstructor;
import javassist.CtNewMethod;
import javassist.NotFoundException;

import org.jboss.interceptor.proxy.LifecycleMixin;
import org.jboss.interceptor.util.proxy.TargetInstanceProxy;
import org.jboss.weld.bean.proxy.util.ClassloaderClassPath;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.WeldException;
import org.slf4j.cal10n.LocLogger;

/**
 * Main factory to produce proxy classes and instances for Weld beans. This
 * implementation creates proxies which forward non-static method invocations to
 * a {@link BeanInstance}. All proxies implement the {@link Proxy} interface.
 * 
 * @author <NAME>
 */
public class ProxyFactory<T>
{
   // The log provider
   protected static final LocLogger    log                  = loggerFactory().getLogger(BEAN);
   // Default proxy class name suffix
   private static final String         PROXY_SUFFIX         = "Proxy";

   protected final Class<?>            beanType;
   protected final ArrayList<Class<?>> additionalInterfaces = new ArrayList<Class<?>>();
   protected final ClassLoader               classLoader;
   protected final ProtectionDomain          protectionDomain;
   protected final ClassPool                 classPool;

   /**
    * Creates a new proxy factory from any type of BeanInstance. This bean
    * instance is only used for initialization information and is not associated
    * with this factory once created.
    * 
    * @param instance a bean instance that will be used with the proxy
    */
   public ProxyFactory(BeanInstance beanInstance)
   {
      this(beanInstance.getInstanceType());
   }

   /**
    * Creates a new proxy factory with only the type of proxy specified.
    * 
    * @param proxiedBeanType the super-class for this proxy class
    */
   public ProxyFactory(Class<?> proxiedBeanType)
   {
      this.beanType = proxiedBeanType;
      if (beanType.getName().startsWith("java."))
      {
         this.classLoader = this.getClass().getClassLoader();
         this.protectionDomain = this.getClass().getProtectionDomain();
      }
      else
      {
         this.classLoader = beanType.getClassLoader();
         this.protectionDomain = beanType.getProtectionDomain();
      }
      this.classPool = new ClassPool();
      this.classPool.appendClassPath(new ClassloaderClassPath(classLoader));
      addDefaultAdditionalInterfaces();
   }

   /**
    * Adds an additional interface that the proxy should implement. The default
    * implementation will be to forward invocations to the bean instance.
    * 
    * @param newInterface an interface
    */
   public void addInterface(Class<?> newInterface)
   {
      if (!newInterface.isInterface())
      {
         throw new IllegalArgumentException(newInterface + " is not an interface");
      }
      additionalInterfaces.add(newInterface);
   }

   /**
    * Method to create a new proxy that wraps the bean instance.
    * 
    * @return a new proxy object
    */
   public T create(BeanInstance beanInstance)
   {
      T proxy = null;
      Class<T> proxyClass = getProxyClass();
      try
      {
         proxy = proxyClass.newInstance();
      }
      catch (InstantiationException e)
      {
         throw new DefinitionException(PROXY_INSTANTIATION_FAILED, e, this);
      }
      catch (IllegalAccessException e)
      {
         throw new DefinitionException(PROXY_INSTANTIATION_BEAN_ACCESS_FAILED, e, this);
      }
      ((Proxy) proxy).proxy_setInstance(beanInstance);
      return proxy;
   }

   /**
    * Produces or returns the existing proxy class.
    * 
    * @return always the class of the proxy
    */
   @SuppressWarnings("unchecked")
   public Class<T> getProxyClass()
   {
      String proxyClassName = beanType.getName() + "_$$_Weld" + getProxyNameSuffix();
      if (proxyClassName.startsWith("java."))
      {<commit_msg>Fixed problem with wrong classloader usage for javax packages<commit_after>/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc. and/or its affiliates, 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.bean.proxy;

import static org.jboss.weld.logging.Category.BEAN;
import static org.jboss.weld.logging.LoggerFactory.loggerFactory;
import static org.jboss.weld.logging.messages.BeanMessage.PROXY_INSTANTIATION_BEAN_ACCESS_FAILED;
import static org.jboss.weld.logging.messages.BeanMessage.PROXY_INSTANTIATION_FAILED;

import java.io.Serializable;
import java.lang.reflect.Modifier;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Arrays;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewConstructor;
import javassist.CtNewMethod;
import javassist.NotFoundException;

import org.jboss.interceptor.proxy.LifecycleMixin;
import org.jboss.interceptor.util.proxy.TargetInstanceProxy;
import org.jboss.weld.bean.proxy.util.ClassloaderClassPath;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.WeldException;
import org.slf4j.cal10n.LocLogger;

/**
 * Main factory to produce proxy classes and instances for Weld beans. This
 * implementation creates proxies which forward non-static method invocations to
 * a {@link BeanInstance}. All proxies implement the {@link Proxy} interface.
 * 
 * @author <NAME>
 */
public class ProxyFactory<T>
{
   // The log provider
   protected static final LocLogger    log                  = loggerFactory().getLogger(BEAN);
   // Default proxy class name suffix
   private static final String         PROXY_SUFFIX         = "Proxy";

   protected final Class<?>            beanType;
   protected final ArrayList<Class<?>> additionalInterfaces = new ArrayList<Class<?>>();
   protected final ClassLoader               classLoader;
   protected final ProtectionDomain          protectionDomain;
   protected final ClassPool                 classPool;

   /**
    * Creates a new proxy factory from any type of BeanInstance. This bean
    * instance is only used for initialization information and is not associated
    * with this factory once created.
    * 
    * @param instance a bean instance that will be used with the proxy
    */
   public ProxyFactory(BeanInstance beanInstance)
   {
      this(beanInstance.getInstanceType());
   }

   /**
    * Creates a new proxy factory with only the type of proxy specified.
    * 
    * @param proxiedBeanType the super-class for this proxy class
    */
   public ProxyFactory(Class<?> proxiedBeanType)
   {
      this.beanType = proxiedBeanType;
      if (beanType.getName().startsWith("java"))
      {
         this.classLoader = this.getClass().getClassLoader();
         this.protectionDomain = this.getClass().getProtectionDomain();
      }
      else
      {
         this.classLoader = beanType.getClassLoader();
         this.protectionDomain = beanType.getProtectionDomain();
      }
      this.classPool = new ClassPool();
      this.classPool.appendClassPath(new ClassloaderClassPath(classLoader));
      addDefaultAdditionalInterfaces();
   }

   /**
    * Adds an additional interface that the proxy should implement. The default
    * implementation will be to forward invocations to the bean instance.
    * 
    * @param newInterface an interface
    */
   public void addInterface(Class<?> newInterface)
   {
      if (!newInterface.isInterface())
      {
         throw new IllegalArgumentException(newInterface + " is not an interface");
      }
      additionalInterfaces.add(newInterface);
   }

   /**
    * Method to create a new proxy that wraps the bean instance.
    * 
    * @return a new proxy object
    */
   public T create(BeanInstance beanInstance)
   {
      T proxy = null;
      Class<T> proxyClass = getProxyClass();
      try
      {
         proxy = proxyClass.newInstance();
      }
      catch (InstantiationException e)
      {
         throw new DefinitionException(PROXY_INSTANTIATION_FAILED, e, this);
      }
      catch (IllegalAccessException e)
      {
         throw new DefinitionException(PROXY_INSTANTIATION_BEAN_ACCESS_FAILED, e, this);
      }
      ((Proxy) proxy).proxy_setInstance(beanInstance);
      return proxy;
   }

   /**
    * Produces or returns the existing proxy class.
    * 
    * @return always the class of the proxy
    */
   @SuppressWarnings("unchecked")
   public Class<T> getProxyClass()
   {
      String proxyClassName = beanType.getName() + "_$$_Weld" + getProxyNameSuffix();
      if (proxyClassName.startsWith("java"))
      {
{'fraction_non_alphanumeric': 0.08403361344537816, 'fraction_numerical': 0.002054154995331466, 'mean_word_length': 3.3033346725592607, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2368555', 'n_tokens_mistral': 2946, 'n_tokens_neox': 2875, 'n_words': 892}