S2Aop浅析

java类

public class MyCls {
    public void add(int a, int b){
        System.out.println(a+b);
    }
}

拦截器
import org.aopalliance.intercept.MethodInvocation;
import org.seasar.framework.aop.interceptors.AbstractInterceptor;

public class MyInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID = 8348047103038121708L;

    public Object invoke(MethodInvocation invocation) throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("BEGIN");
        Object ret = invocation.proceed();
        System.out.println("END");
        return ret;
    }

}

配置
<?xml version="1.0" encoding="Shift_JIS"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container//EN"
"http://www.seasar.org/dtd/components.dtd">
<components>
    <include path="dao.dicon"/>
    <component class="jp.co.test.MyCls">
        <aspect pointcut="add">
            <component class="jp.co.test.interceptor.MyInterceptor"/>
        </aspect>
    </component>
</components>

调用
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        S2Container container = S2ContainerFactory.create("testapp.dicon");
        container.init();
        MyCls myCls = (MyCls) container.getComponent(MyCls.class);
        myCls.add(1, 2);
    }


java源文件
org/seasar/framework/aop/javassist/AbstractGenerator.java

的173行
public Class toClass(final ClassLoader classLoader, final CtClass ctClass) 

中体添加以下代码
            File file = new File("D:\\gencls\\cls\\"+ctClass.getSimpleName()+".class");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(bytecode);
            fos.flush();
            fos.close();

把动态生成的类保存起来。

把类反编译一下
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MyCls$$EnhancedByS2AOP$$9f671b.java

package jp.co.test;

import java.lang.reflect.UndeclaredThrowableException;

// Referenced classes of package jp.co.test:
//            MyCls

public class MyCls$$EnhancedByS2AOP$$9f671b extends MyCls
{

    public void add$$invokeSuperMethod$$(int i, int j)
    {
        super.add(i, j);
        Object _tmp = null;
    }

    public void add(int i, int j)
    {
        try
        {
            class .MethodInvocation..add0
                implements S2MethodInvocation
            {

                public Class getTargetClass()
                {
                    return targetClass;
                }

                public Method getMethod()
                {
                    return method;
                }

                public AccessibleObject getStaticPart()
                {
                    return method;
                }

                public Object getParameter(String name)
                {
                    if(parameters == null)
                        return null;
                    else
                        return parameters.get(name);
                }

                public Object getThis()
                {
                    return target;
                }

                public Object[] getArguments()
                {
                    return arguments;
                }

                public Object proceed()
                    throws Throwable
                {
                    if(interceptorsIndex < interceptors.length)
                    {
                        return interceptors[interceptorsIndex++].invoke(this);
                    } else
                    {
                        ((MyCls..EnhancedByS2AOP.._cls9f671b)target).add$$invokeSuperMethod$$(((Number)arguments[0]).intValue(), ((Number)arguments[1]).intValue());
                        return null;
                    }
                }

                private static Class targetClass;
                private static Method method;
                private static MethodInterceptor interceptors[];
                private static Map parameters;
                private Object target;
                private Object arguments[];
                private int interceptorsIndex;

            public .MethodInvocation..add0(Object target, Object arguments[])
            {
                this.target = target;
                this.arguments = arguments;
            }
            }

            Object obj = (new .MethodInvocation..add0(this, new Object[] {
                new Integer(i), new Integer(j)
            })).proceed();
            return;
        }
        catch(RuntimeException runtimeexception)
        {
            throw runtimeexception;
        }
        catch(Error error)
        {
            throw error;
        }
        catch(Throwable throwable)
        {
            throw new UndeclaredThrowableException(throwable);
        }
    }

    public MyCls$$EnhancedByS2AOP$$9f671b()
    {
    }
}

你可能感兴趣的:(java,AOP,html,xml,J#)