Is it possible to intercept method of unnamed instances of interfaces?

23 views
Skip to first unread message

Scott Babcock

unread,
Mar 28, 2024, 3:25:57 AM3/28/24
to Byte Buddy
I'm trying to intercept a method defined in an interface. Instances of this interface are created via 'new' with a closure that defines the interface methods:

public interface Foo {
    void bar();
}

public static void main(String[] args) {
    Foo myFoo = new Foo() {
        @Override
        public void bar() {
            System.out.println("FOOBAR");
        }
    };
}

Is it possible to define a transformation to intercept 'bar()' in the myFoo instance?

Rafael Winterhalter

unread,
Mar 28, 2024, 4:18:24 AM3/28/24
to Scott Babcock, Byte Buddy
Yes, this will be conpiled to an anonymous class named Outer$1 which behaves like a regular class. TypeDescription also lets you check if a class is anonymous.

--
You received this message because you are subscribed to the Google Groups "Byte Buddy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to byte-buddy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/byte-buddy/494721fc-2e97-4176-9ff5-d5ef4a8e66aen%40googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages