Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6538480
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Java

2011-05-13 21:39:38

java类

Java代码 复制代码 收藏代码
  1. public class MyCls {   
  2.     public void add(int a, int b){   
  3.         System.out.println(a+b);   
  4.     }   
  5. }  
拦截器
Java代码 复制代码 收藏代码
  1. import org.aopalliance.intercept.MethodInvocation;   
  2. import org.seasar.framework.aop.interceptors.AbstractInterceptor;   
  3.   
  4. public class MyInterceptor extends AbstractInterceptor {   
  5.     private static final long serialVersionUID = 8348047103038121708L;   
  6.   
  7.     public Object invoke(MethodInvocation invocation) throws Throwable {   
  8.         // TODO Auto-generated method stub   
  9.         System.out.println("BEGIN");   
  10.         Object ret = invocation.proceed();   
  11.         System.out.println("END");   
  12.         return ret;   
  13.     }   
  14.   
  15. }  
配置
Xml代码 复制代码 收藏代码
  1. xml version="1.0" encoding="Shift_JIS"?>  
  2. "">  
  3. <components>  
  4.     <include path="dao.dicon"/>  
  5.     <component class="jp.co.test.MyCls">  
  6.         <aspect pointcut="add">  
  7.             <component class="jp.co.test.interceptor.MyInterceptor"/>  
  8.         aspect>  
  9.     component>  
  10. components>  
调用
Java代码 复制代码 收藏代码
  1. public static void main(String[] args) {   
  2.     // TODO Auto-generated method stub   
  3.     S2Container container = S2ContainerFactory.create("testapp.dicon");   
  4.     container.init();   
  5.     MyCls myCls = (MyCls) container.getComponent(MyCls.class);   
  6.     myCls.add(12);   
  7. }  

java源文件
Java代码 复制代码 收藏代码
  1. org/seasar/framework/aop/javassist/AbstractGenerator.java  
的173行
Java代码 复制代码 收藏代码
  1. public Class toClass(final ClassLoader classLoader, final CtClass ctClass)   
中体添加以下代码
Java代码 复制代码 收藏代码
  1. File file = new File("D:\\gencls\\cls\\"+ctClass.getSimpleName()+".class");   
  2. FileOutputStream fos = new FileOutputStream(file);   
  3. fos.write(bytecode);   
  4. fos.flush();   
  5. fos.close();  
把动态生成的类保存起来。

把类反编译一下
Java代码 复制代码 收藏代码
  1. // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.   
  2. // Jad home page:    
  3. // Decompiler options: packimports(3)    
  4. // Source File Name:   MyCls$$EnhancedByS2AOP$$9f671b.java   
  5.   
  6. package jp.co.test;   
  7.   
  8. import java.lang.reflect.UndeclaredThrowableException;   
  9.   
  10. // Referenced classes of package jp.co.test:   
  11. //            MyCls   
  12.   
  13. public class MyCls$$EnhancedByS2AOP$$9f671b extends MyCls   
  14. {   
  15.   
  16.     public void add$$invokeSuperMethod$$(int i, int j)   
  17.     {   
  18.         super.add(i, j);   
  19.         Object _tmp = null;   
  20.     }   
  21.   
  22.     public void add(int i, int j)   
  23.     {   
  24.         try  
  25.         {   
  26.             class .MethodInvocation..add0   
  27.                 implements S2MethodInvocation   
  28.             {   
  29.   
  30.                 public Class getTargetClass()   
  31.                 {   
  32.                     return targetClass;   
  33.                 }   
  34.   
  35.                 public Method getMethod()   
  36.                 {   
  37.                     return method;   
  38.                 }   
  39.   
  40.                 public AccessibleObject getStaticPart()   
  41.                 {   
  42.                     return method;   
  43.                 }   
  44.   
  45.                 public Object getParameter(String name)   
  46.                 {   
  47.                     if(parameters == null)   
  48.                         return null;   
  49.                     else  
  50.                         return parameters.get(name);   
  51.                 }   
  52.   
  53.                 public Object getThis()   
  54.                 {   
  55.                     return target;   
  56.                 }   
  57.   
  58.                 public Object[] getArguments()   
  59.                 {   
  60.                     return arguments;   
  61.                 }   
  62.   
  63.                 public Object proceed()   
  64.                     throws Throwable   
  65.                 {   
  66.                     if(interceptorsIndex < interceptors.length)   
  67.                     {   
  68.                         return interceptors[interceptorsIndex++].invoke(this);   
  69.                     } else  
  70.                     {   
  71.                         ((MyCls..EnhancedByS2AOP.._cls9f671b)target).add$$invokeSuperMethod$$(((Number)arguments[0]).intValue(), ((Number)arguments[1]).intValue());   
  72.                         return null;   
  73.                     }   
  74.                 }   
  75.   
  76.                 private static Class targetClass;   
  77.                 private static Method method;   
  78.                 private static MethodInterceptor interceptors[];   
  79.                 private static Map parameters;   
  80.                 private Object target;   
  81.                 private Object arguments[];   
  82.                 private int interceptorsIndex;   
  83.   
  84.             public .MethodInvocation..add0(Object target, Object arguments[])   
  85.             {   
  86.                 this.target = target;   
  87.                 this.arguments = arguments;   
  88.             }   
  89.             }   
  90.   
  91.             Object obj = (new .MethodInvocation..add0(thisnew Object[] {   
  92.                 new Integer(i), new Integer(j)   
  93.             })).proceed();   
  94.             return;   
  95.         }   
  96.         catch(RuntimeException runtimeexception)   
  97.         {   
  98.             throw runtimeexception;   
  99.         }   
  100.         catch(Error error)   
  101.         {   
  102.             throw error;   
  103.         }   
  104.         catch(Throwable throwable)   
  105.         {   
  106.             throw new UndeclaredThrowableException(throwable);   
  107.         }   
  108.     }   
  109.   
  110.     public MyCls$$EnhancedByS2AOP$$9f671b()   
  111.     {   
  112.     }   
  113. }  
阅读(790) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~