日期:2014-05-17  浏览次数:20969 次

AspectWeaver ——javassist具体应用
/*
 * Copyright 2004-2010 the Seasar Foundation and the Others.
 *
 * 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.seasar.framework.aop.javassist;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javassist.ClassPool;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.seasar.framework.aop.InterType;
import org.seasar.framework.exception.NoSuchFieldRuntimeException;
import org.seasar.framework.util.ClassLoaderUtil;
import org.seasar.framework.util.ClassPoolUtil;
import org.seasar.framework.util.FieldUtil;
import org.seasar.framework.util.MethodUtil;

/**
 * アスペクトを織り込むクラスです。
 * 
 * @author koichik
 */
public class AspectWeaver {
    /**
     * エンハンスされるクラスにつけるプレフィックス。
     */
    public static final String PREFIX_ENHANCED_CLASS = "$$";

    /**
     * エンハンスされるクラスにつけるサフィックス。
     */
    public static final String SUFFIX_ENHANCED_CLASS = "$$EnhancedByS2AOP$$";

    /**
     * エンハンスされる{@link MethodInvocation}につけるサフィックス。
     */
    public static final String SUFFIX_METHOD_INVOCATION_CLASS = "$$MethodInvocation$$";

    /**
     * super(親クラス)のメソッドを呼び出すときのサフィックス。
     */
    public static final String SUFFIX_INVOKE_SUPER_METHOD = "$$invokeSuperMethod$$";

    /**
     * エンハンスされるクラス名の {@link Set}
     */
    protected static final Set enhancedClassNames = Collections
            .synchronizedSet(new HashSet());

    /**
	 * ターゲットクラス
	 * @uml.property  name="targetClass"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.util.Collection"
	 */
    protected final Class targetClass;

    /**
	 * パラメータ
	 * @uml.property  name="parameters"
	 */
    protected final Map parameters;

    /**
	 * エンハンスされるクラス名
	 * @uml.property  name="enhancedClassName"
	 */
    protected final String enhancedClassName;

    /**
	 * エンハンスされるクラスジェネレータ
	 * @uml.property  name="enhancedClassGenerator"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
    protected final EnhancedClassGenerator enhancedClassGenerator;

    /**
	 * メソッド呼び出しクラスの   {@link List} 
	 * @uml.property  name="methodInvocationClassList"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.lang.Class"
	 */
    protected final List methodInvocationClassList = new ArrayList();

    /**
	 * エンハンスされるクラス
	 * @uml.property  name="enhancedClass"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.util.Collection"
	 */
    protected Class enhancedClass;

    /**
	 * クラスプール
	 * @uml.property  name="classPool"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
    protected ClassPool classPool;

    /**
     * {@link AspectWeaver}を作成します。
     * 
     * @param targetClass
     * @param parameters
     */
    public AspectWeaver(final Class targetClass, final Map parameters) {
        this.targetClass = targetClass;
        this.parameters = parameters;

        classPool = ClassPoolUtil.getClassPool(targetClass);
        enhancedClassName = getEnhancedClassName();
        //根据targetClass,产生enhancedClass的CtClass形式(由javassist负责)
        enhancedClassGenerator = new EnhancedClassGenerator(classPool,
                targetClass, enhancedClassName);
    }

    /**
     * {@link MethodInterceptor}を設定します。
     * 
     * @param method
     * @param intercepto