<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to You 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.apache.webbeans.intercept.webbeans;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.NonBinding;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InterceptionType;
import javax.enterprise.inject.spi.Interceptor;
import javax.interceptor.InvocationContext;

import org.apache.webbeans.component.AbstractBean;
import org.apache.webbeans.component.ManagedBean;
import org.apache.webbeans.component.WebBeansType;
import org.apache.webbeans.exception.WebBeansConfigurationException;
import org.apache.webbeans.exception.WebBeansException;
import org.apache.webbeans.inject.InjectableField;
import org.apache.webbeans.inject.InjectableMethods;
import org.apache.webbeans.intercept.InterceptorUtil;
import org.apache.webbeans.intercept.WebBeansInterceptorConfig;
import org.apache.webbeans.logger.WebBeansLogger;
import org.apache.webbeans.proxy.JavassistProxyFactory;
import org.apache.webbeans.util.AnnotationUtil;
import org.apache.webbeans.util.WebBeansUtil;
import org.apache.webbeans.xml.XMLAnnotationTypeManager;

/**
 * Defines the webbeans specific interceptors.
 * <p>
 * WebBeans interceotor classes has at least one {@link javax.interceptor.InterceptorBinding}
 * annotation. It can be defined on the class or method level at the component.
 * WebBeans interceptors are called after the EJB related interceptors are
 * called in the chain. Semantics of the interceptors are specified by the EJB
 * specificatin.
 * </p>
 * 
 * @version $Rev$ $Date$
 */
public class WebBeansInterceptor<T> extends AbstractBean<T> implements Interceptor<T>
{
	private static final WebBeansLogger logger = WebBeansLogger.getLogger(WebBeansInterceptor.class);
	
    /** InterceptorBindingTypes exist on the interceptor class */
    private Map<Class<? extends Annotation>, Annotation> interceptorBindingSet = new HashMap<Class<? extends Annotation>, Annotation>();

    /** Interceptor class */
    private Class<?> clazz;

    /** Simple Web Beans component */
    private AbstractBean<T> delegateComponent;
    
    private CreationalContext<T> creationalContext;

    public WebBeansInterceptor(AbstractBean<T> delegateComponent)
    {
        super(WebBeansType.INTERCEPTOR,delegateComponent.getReturnType());
        
        this.delegateComponent = delegateComponent;
        this.clazz = getDelegate().getReturnType();

    }

    public AbstractBean<T> getDelegate()
    {
        return this.delegateComponent;
    }

    /**
     * Add new binding type to the interceptor.
     * 
     * @param binding interceptor binding annotation. class
     * @param annot binding type annotation
     */
    public void addInterceptorBinding(Class<? extends Annotation> binding, Annotation annot)
    {
        Method[] methods = binding.getDeclaredMethods();

        for (Method method : methods)
        {
            Class<?> clazz = method.getReturnType();
            if (clazz.isArray() || clazz.isAnnotation())
            {
                if (!AnnotationUtil.hasAnnotation(method.getAnnotations(), NonBinding.class))
                {
                    throw new WebBeansConfigurationException("Interceptor definition class : " + getClazz().getName() + " @InterceptorBinding : " + binding.getName() + " must have @NonBinding valued members for its array-valued and annotation valued members");
                }
            }
        }

        interceptorBindingSet.put(binding, annot);
    }

    /**
     * Checks whether this interceptor has given binding types with
     * {@link NonBinding} member values.
     * 
     * @param bindingTypes binding types
     * @param annots binding types annots.
     * @return true if binding types exist ow false
     */
    public boolean hasBinding(List<Class<? extends Annotation>> bindingTypes, List<Annotation> annots)
    {
        boolean result = false;

        if (bindingTypes != null && annots != null && (bindingTypes.size() == annots.size()))
        {
            int i = 0;
            for (Class<? extends Annotation> bindingType : bindingTypes)
            {
                if (this.interceptorBindingSet.containsKey(bindingType))
                {
                    Annotation target = this.interceptorBindingSet.get(bindingType);
                    if (AnnotationUtil.hasAnnotationMember(bindingType, annots.get(i), target))
                    {
                        result = true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }

                i++;
            }
        }

        return result;
    }

    /**
     * Gets the interceptor class.
     * 
     * @return interceptor class
     */
    public Class<?> getClazz()
    {
        return clazz;
    }

    public Set<Interceptor<?>> getMetaInceptors()
    {
        Set<Interceptor<?>> set = new HashSet<Interceptor<?>>();

        Set<Class<? extends Annotation>> keys = interceptorBindingSet.keySet();
        Iterator<Class<? extends Annotation>> it = keys.iterator();

        while (it.hasNext())
        {
            Class<? extends Annotation> clazzAnnot = it.next();
            Set<Annotation> declared = null;
            Annotation[] anns = null;

            if (XMLAnnotationTypeManager.getInstance().hasInterceptorBindingType(clazzAnnot))
            {
                declared = XMLAnnotationTypeManager.getInstance().getInterceptorBindingTypeInherites(clazzAnnot);
                anns = new Annotation[declared.size()];
                anns = declared.toArray(anns);
            }<commit_msg>Refactored as discussed in the list.  No logic change.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to You 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.apache.webbeans.intercept.webbeans;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.NonBinding;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.InterceptionType;
import javax.enterprise.inject.spi.Interceptor;
import javax.interceptor.InvocationContext;

import org.apache.webbeans.component.AbstractBean;
import org.apache.webbeans.component.ManagedBean;
import org.apache.webbeans.component.WebBeansType;
import org.apache.webbeans.exception.WebBeansConfigurationException;
import org.apache.webbeans.exception.WebBeansException;
import org.apache.webbeans.inject.InjectableField;
import org.apache.webbeans.inject.InjectableMethods;
import org.apache.webbeans.intercept.InterceptorUtil;
import org.apache.webbeans.intercept.WebBeansInterceptorConfig;
import org.apache.webbeans.logger.WebBeansLogger;
import org.apache.webbeans.proxy.JavassistProxyFactory;
import org.apache.webbeans.util.AnnotationUtil;
import org.apache.webbeans.util.WebBeansUtil;
import org.apache.webbeans.xml.XMLAnnotationTypeManager;

/**
 * Defines the webbeans specific interceptors.
 * <p>
 * WebBeans interceotor classes has at least one {@link javax.interceptor.InterceptorBinding}
 * annotation. It can be defined on the class or method level at the component.
 * WebBeans interceptors are called after the EJB related interceptors are
 * called in the chain. Semantics of the interceptors are specified by the EJB
 * specificatin.
 * </p>
 * 
 * @version $Rev$ $Date$
 */
public class WebBeansInterceptor<T> extends AbstractBean<T> implements Interceptor<T>
{
	private static final WebBeansLogger logger = WebBeansLogger.getLogger(WebBeansInterceptor.class);
	
    /** InterceptorBindingTypes exist on the interceptor class */
    private Map<Class<? extends Annotation>, Annotation> interceptorBindingSet = new HashMap<Class<? extends Annotation>, Annotation>();

    /** Interceptor class */
    private Class<?> clazz;

    /** Simple Web Beans component */
    private AbstractBean<T> delegateComponent;
    
    private CreationalContext<T> creationalContext;

    public WebBeansInterceptor(AbstractBean<T> delegateComponent)
    {
        super(WebBeansType.INTERCEPTOR,delegateComponent.getReturnType());
        
        this.delegateComponent = delegateComponent;
        this.clazz = getDelegate().getReturnType();

    }

    public AbstractBean<T> getDelegate()
    {
        return this.delegateComponent;
    }

    /**
     * Add new binding type to the interceptor.
     * 
     * @param binding interceptor binding annotation. class
     * @param annot binding type annotation
     */
    public void addInterceptorBinding(Class<? extends Annotation> binding, Annotation annot)
    {
        Method[] methods = binding.getDeclaredMethods();

        for (Method method : methods)
        {
            Class<?> clazz = method.getReturnType();
            if (clazz.isArray() || clazz.isAnnotation())
            {
                if (!AnnotationUtil.hasAnnotation(method.getAnnotations(), NonBinding.class))
                {
                    throw new WebBeansConfigurationException("Interceptor definition class : " + getClazz().getName() + " @InterceptorBinding : " + binding.getName() + " must have @NonBinding valued members for its array-valued and annotation valued members");
                }
            }
        }

        interceptorBindingSet.put(binding, annot);
    }

    /**
     * Checks whether this interceptor has given binding types with
     * {@link NonBinding} member values.
     * 
     * @param bindingTypes binding types
     * @param annots binding types annots.
     * @return true if binding types exist ow false
     */
    public boolean hasBinding(List<Class<? extends Annotation>> bindingTypes, List<Annotation> annots)
    {
        if (bindingTypes == null || annots == null)
        {
            return false;
        }
        if (bindingTypes.size() != annots.size())
        {
            return false;
        }
        if (bindingTypes.size() == 0)
        {
            return false;
        }

        int i = 0;
        for (Class<? extends Annotation> bindingType : bindingTypes)
        {
            Annotation target = this.interceptorBindingSet.get(bindingType);

            if (target == null)
            {
                return false;
            }

            if (!AnnotationUtil.hasAnnotationMember(bindingType, annots.get(i), target))
            {
                return false;
            }

            i++;
        }

        return true;
    }

    /**
     * Gets the interceptor class.
     * 
     * @return interceptor class
     */
    public Class<?> getClazz()
    {
        return clazz;
    }

    public Set<Interceptor<?>> getMetaInceptors()
    {
        Set<Interceptor<?>> set = new HashSet<Interceptor<?>>();

        Set<Class<? extends Annotation>> keys = interceptorBindingSet.keySet();
        Iterator<Class<? extends Annotation>> it = keys.iterator();

        while (it.hasNext())
        {
            Class<? extends Annotation> clazzAnnot = it.next();
            Set<Annotation> declared = null;
            Annotation[] anns = null;

            if (XMLAnnotationTypeManager.getInstance().hasInterceptorBindingType(clazzAnnot))
            {
                declared = XMLAnnotationTypeManager.getInstance().getInterceptorBindingTypeInherites(clazzAnnot);
                anns = new Annotation[declared.size()];
                anns = declared.toArray(anns);
            }
{'fraction_non_alphanumeric': 0.09161605688046215, 'fraction_numerical': 0.00081469411938972, 'mean_word_length': 2.8712729357798166, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '4049531', 'n_tokens_mistral': 3567, 'n_tokens_neox': 3439, 'n_words': 976}