【问题标题】:Transform Method Input With Aspects使用方面转换方法输入
【发布时间】:2012-05-04 16:36:57
【问题描述】:

我有一个将字符串作为输入的方法,我希望有一个方面拦截方法执行,并在某些条件下修改字符串并让连接点接收新输入,所以它不知道它已经被改造了。

这可能吗,还是出于安全考虑不能这样做?

【问题讨论】:

    标签: java aop transform


    【解决方案1】:

    使用 AspectJ,您可以像这样声明环绕建议:

     public aspect TransformationAspect {
       pointcut transform(String s) : call(* yourMethodName(..)) && args(s);
    
       Object around(String s): transform(s) {
         // ... Transform String here
         return proceed(s);
       }
     }
    

    您的代码必须在编译时或在运行时使用 AspectJ Java 代理进行处理。

    【讨论】:

      【解决方案2】:

      这应该是可能的,但取决于您使用的 AOP 技术。

      对于 EJB 3.x 或 CDI,它可能类似于以下伪代码:

      Object intercept( InvocationContext context ) {
      
        Object[] params = context .getParameters();
      
        //modify the string and update the array
        //the value of i depends on the method signature (the parameter position)
        //and getting it is left for you as an excersise
        params[i] =  modify( (String)params[i] );  
      
        context.setParameters( params );
        return context.proceed();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多