<commit_before>package org.jboss.weld.bean.proxy;

import static org.jboss.weld.bean.proxy.InterceptionDecorationContext.endInterceptorContext;
import static org.jboss.weld.bean.proxy.InterceptionDecorationContext.startInterceptorContext;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.util.Set;

import org.jboss.weld.security.SetAccessibleAction;
import org.jboss.weld.util.reflection.Reflections;

/**
 * A method handler that wraps the invocation of interceptors and decorators.
 *
 * @author <NAME>
 */
public class CombinedInterceptorAndDecoratorStackMethodHandler implements MethodHandler, Serializable {

    private MethodHandler interceptorMethodHandler;

    private Object outerDecorator;


    public void setInterceptorMethodHandler(MethodHandler interceptorMethodHandler) {
        this.interceptorMethodHandler = interceptorMethodHandler;
    }

    public void setOuterDecorator(Object outerDecorator) {
        this.outerDecorator = outerDecorator;
    }

    private Set<CombinedInterceptorAndDecoratorStackMethodHandler> getDisabledHandlers() {

        return InterceptionDecorationContext.peek();
    }

    public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
        boolean externalContext = false;

        try {
            if (InterceptionDecorationContext.empty()) {
                externalContext = true;
                startInterceptorContext();
            }
            if (!getDisabledHandlers().contains(this)) {
                try {

                    getDisabledHandlers().add(this);
                    if (interceptorMethodHandler != null) {
                        if (proceed != null) {
                            return this.interceptorMethodHandler.invoke(outerDecorator != null ? outerDecorator : self, thisMethod, thisMethod, args);
                        } else {
                            return this.interceptorMethodHandler.invoke(self, thisMethod, null, args);
                        }
                    } else {
                        if (outerDecorator != null) {
                            if (!thisMethod.isAccessible()) {
                                AccessController.doPrivileged(SetAccessibleAction.of(thisMethod));
                            }
                            return Reflections.invokeAndUnwrap(outerDecorator, thisMethod, args);
                        }
                    }
                } finally {
                    this.getDisabledHandlers().remove(this);
                }
            }
            if (!proceed.isAccessible()) {
                AccessController.doPrivileged(SetAccessibleAction.of(proceed));
            }
            return Reflections.invokeAndUnwrap(self, proceed, args);
        } finally {
            if (externalContext) {
                endInterceptorContext();
            }
        }
    }

    public boolean isDisabledHandler() {
        if (InterceptionDecorationContext.empty()) {
            return false;
        }
        return getDisabledHandlers().contains(this);
    }
<commit_msg>WELD-1595 Minor improvements of CombinedInterceptorAndDecoratorStackMethodHandler<commit_after>package org.jboss.weld.bean.proxy;

import static org.jboss.weld.bean.proxy.InterceptionDecorationContext.endInterceptorContext;
import static org.jboss.weld.bean.proxy.InterceptionDecorationContext.startInterceptorContext;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Set;

import org.jboss.weld.security.SetAccessibleAction;
import org.jboss.weld.util.reflection.Reflections;

/**
 * A method handler that wraps the invocation of interceptors and decorators.
 *
 * @author <NAME>
 */
public class CombinedInterceptorAndDecoratorStackMethodHandler implements MethodHandler, Serializable {

    private MethodHandler interceptorMethodHandler;

    private Object outerDecorator;


    public void setInterceptorMethodHandler(MethodHandler interceptorMethodHandler) {
        this.interceptorMethodHandler = interceptorMethodHandler;
    }

    public void setOuterDecorator(Object outerDecorator) {
        this.outerDecorator = outerDecorator;
    }

    private Set<CombinedInterceptorAndDecoratorStackMethodHandler> getDisabledHandlers() {
        return InterceptionDecorationContext.peek();
    }

    public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
        boolean externalContext = false;

        try {
            if (InterceptionDecorationContext.empty()) {
                externalContext = true;
                startInterceptorContext();
            }
            Set<CombinedInterceptorAndDecoratorStackMethodHandler> disabledHandlers = getDisabledHandlers();
            if (!disabledHandlers.contains(this)) {
                try {

                    disabledHandlers.add(this);
                    if (interceptorMethodHandler != null) {
                        if (proceed != null) {
                            return this.interceptorMethodHandler.invoke(outerDecorator != null ? outerDecorator : self, thisMethod, thisMethod, args);
                        } else {
                            return this.interceptorMethodHandler.invoke(self, thisMethod, null, args);
                        }
                    } else {
                        if (outerDecorator != null) {
                            SetAccessibleAction.ensureAccessible(thisMethod);
                            return Reflections.invokeAndUnwrap(outerDecorator, thisMethod, args);
                        }
                    }
                } finally {
                    disabledHandlers.remove(this);
                }
            }
            SetAccessibleAction.ensureAccessible(proceed);
            return Reflections.invokeAndUnwrap(self, proceed, args);
        } finally {
            if (externalContext) {
                endInterceptorContext();
            }
        }
    }

    public boolean isDisabledHandler() {
        if (InterceptionDecorationContext.empty()) {
            return false;
        }
        return getDisabledHandlers().contains(this);
    }

{'fraction_non_alphanumeric': 0.07626572073524669, 'fraction_numerical': 0.0006449532408900354, 'mean_word_length': 2.151930894308943, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7665692', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1464, 'n_words': 281}