Main issue: 似乎如果我修改了Java类的metaClass,那么只有在我的Groovy代码调用该Java类时才会遵守这些更改 . 但是,如果另一个Java类正在调用我修改的Java类,则忽略metaClass更改 .


我想这种行为似乎是正确的(编译的Java代码不会检查Groovy元类注册表)但我希望有人可以给我一些关于如何实现我的目标的其他想法 . 这是一些示例代码:

A.java

public class A {
   A() {
      System.out.println("A constructor");
      B b = new B();
      b.foo();
   }
}

B.java

public class B {
   B() { System.out.println("B constructor"); }
   public void foo() { System.out.println("B.foo() invoked"); }
}

Compilation

javac *.java
jar cvfe java.jar Main *.class

foo.groovy

println 'Groovy now invoking Java...'
new A()

println 'Modifying B.metaClass'
B.metaClass.foo = { -> println 'modified foo() method!' }

println 'Groovy now invoking Java...'
new A()   // <<===== I would like to see the "modified foo()" here but I don't :(

println 'Groovy creating B directly'
new B().foo()   // "modified foo()" appears here

Run

> groovy -cp .:java.jar foo.groovy

我有一个Grails应用程序,它使用一些现有的Java JAR(字节码,无法访问其源代码) . 我想拦截对这些Java类中某些方法的一些调用,但这些方法是由同一个JAR中的其他Java类调用的 .

Spring AOP是不可能的,因为有问题的对象不是由Spring管理的 .

Bigger picture: 我实际上是在尝试解决我所说的问题:

我的想法是编写如下代码:

import org.codehaus.groovy.grails.orm.hibernate.validation.UniqueConstraint

class HibernateEnhancerGrailsPlugin {
    def loadBefore = ['hibernate']

    def doWithSpring = {
        UniqueConstraint.metaClass.processValidate = { final Object target, final Object propertyValue, Errors errors ->
          // no-op...
        }
    }
}

思考?