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源文件
[code]org/seasar/framework/aop/javassist/AbstractGenerator.java[/code]
的173行
[code]public Class toClass(final ClassLoader classLoader, final CtClass ctClass) [/code]
中体添加以下代码

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()
{
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值