<commit_before>/*
 * Copyright 2010 - 2011 <NAME>
 *
 * 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 net.sf.jpasecurity.proxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.jpasecurity.SecureEntity;

/**
 * An implementation of {@link SecureEntityProxyFactory} that uses CGLib
 * to create proxies
 * @author <NAME>
 */
public class CgLibSecureEntityProxyFactory implements SecureEntityProxyFactory {

    /**
     * {@inheritDoc}
     */
    public SecureEntity createSecureEntityProxy(final Class<?> entityType,
                                                final MethodInterceptor interceptor,
                                                final Decorator<SecureEntity> decorator) {
        SecureEntity entity = (SecureEntity)Enhancer.create(entityType,
                                                            new Class[] {SecureEntity.class},
                                                            new CgLibMethodInterceptor(interceptor, decorator));
        decorator.setDelegate(entity);
        return entity;
    }

    /**
     * {@inheritDoc}
     */
    public MethodInterceptor getInterceptor(SecureEntity entity) {
        return getCgLibMethodInterceptor(entity).interceptor;
    }

    /**
     * {@inheritDoc}
     */
    public Decorator<SecureEntity> getDecorator(SecureEntity entity) {
        return getCgLibMethodInterceptor(entity).decorator;
    }

    private CgLibMethodInterceptor getCgLibMethodInterceptor(SecureEntity entity) {
        try {
            for (Callback callback: getCallbacks(entity)) {
                if (callback instanceof CgLibMethodInterceptor) {
                    return (CgLibMethodInterceptor)callback;
                }
            }
            throw new IllegalArgumentException("The specified object was not created by this factory");
        } catch (SecurityException e) {
            if (e.getCause() instanceof NoSuchMethodException) {
                throw new IllegalArgumentException("The specified object was not created by this factory");
            } else {
                throw e;
            }
        }
    }

   private Callback[] getCallbacks(SecureEntity entity) {
      if (entity instanceof Factory) {
         return ((Factory)entity).getCallbacks();
      }
      throw new IllegalArgumentException("The specified object was not created by this factory");
   }

   private class CgLibMethodInterceptor implements net.sf.cglib.proxy.MethodInterceptor {
<commit_msg>Fixed indentation<commit_after>/*
 * Copyright 2010 - 2011 <NAME>
 *
 * 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 net.sf.jpasecurity.proxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.jpasecurity.SecureEntity;

/**
 * An implementation of {@link SecureEntityProxyFactory} that uses CGLib
 * to create proxies
 * @author <NAME>
 */
public class CgLibSecureEntityProxyFactory implements SecureEntityProxyFactory {

    /**
     * {@inheritDoc}
     */
    public SecureEntity createSecureEntityProxy(final Class<?> entityType,
                                                final MethodInterceptor interceptor,
                                                final Decorator<SecureEntity> decorator) {
        SecureEntity entity = (SecureEntity)Enhancer.create(entityType,
                                                            new Class[] {SecureEntity.class},
                                                            new CgLibMethodInterceptor(interceptor, decorator));
        decorator.setDelegate(entity);
        return entity;
    }

    /**
     * {@inheritDoc}
     */
    public MethodInterceptor getInterceptor(SecureEntity entity) {
        return getCgLibMethodInterceptor(entity).interceptor;
    }

    /**
     * {@inheritDoc}
     */
    public Decorator<SecureEntity> getDecorator(SecureEntity entity) {
        return getCgLibMethodInterceptor(entity).decorator;
    }

    private CgLibMethodInterceptor getCgLibMethodInterceptor(SecureEntity entity) {
        try {
            for (Callback callback: getCallbacks(entity)) {
                if (callback instanceof CgLibMethodInterceptor) {
                    return (CgLibMethodInterceptor)callback;
                }
            }
            throw new IllegalArgumentException("The specified object was not created by this factory");
        } catch (SecurityException e) {
            if (e.getCause() instanceof NoSuchMethodException) {
                throw new IllegalArgumentException("The specified object was not created by this factory");
            } else {
                throw e;
            }
        }
    }

    private Callback[] getCallbacks(SecureEntity entity) {
        if (entity instanceof Factory) {
            return ((Factory)entity).getCallbacks();
        }
        throw new IllegalArgumentException("The specified object was not created by this factory");
    }

    private class CgLibMethodInterceptor implements net.sf.cglib.proxy.MethodInterceptor {

{'fraction_non_alphanumeric': 0.0778405524168236, 'fraction_numerical': 0.003766478342749529, 'mean_word_length': 2.4263440860215053, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7664906', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1550, 'n_words': 457}