byte-buddy icon indicating copy to clipboard operation
byte-buddy copied to clipboard

Samples: Why some methods are not exists?

Open Bizarrus opened this issue 2 years ago • 6 comments

I've tried to use byte-buddy with their examples. But why some methods are not available?

image

import net.bytebuddy.*;
import net.bytebuddy.dynamic.*;
import net.bytebuddy.dynamic.loading.*;
import net.bytebuddy.implementation.*;
import net.bytebuddy.pool.*;

class Foo {
  public String bar() { return null; }
  public String foo() { return null; }
  public String foo(Object o) { return null; }
}

public class Test {
	public Test() {
		try {
			Foo dynamicFoo = new ByteBuddy()
			  .subclass(Foo.class)
			  .method(isDeclaredBy(Foo.class)).intercept(FixedValue.value("One!"))
			  .method(named("foo")).intercept(FixedValue.value("Two!"))
			  .method(named("foo").and(takesArguments(1))).intercept(FixedValue.value("Three!"))
			  .make()
			  .load(getClass().getClassLoader())
			  .getLoaded()
			  .newInstance();
		} catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		new Test();	
	}
}

Bizarrus avatar Aug 27 '23 15:08 Bizarrus

What do you mean by that? There are no methods invoked in your sample, you only construct the instance.

raphw avatar Aug 27 '23 21:08 raphw

Please use the simple samples on the website. Create a new project in eclipse, add byte-buddy & ASM libs and create the class above in the question.

You will see that some methods like isDeclaredBy or names not exists.

Bizarrus avatar Aug 27 '23 21:08 Bizarrus

They are static imports by ElementMatchers.

raphw avatar Aug 27 '23 21:08 raphw

Please explain yor answer.

On the site only following informations are available:

Byte Buddy is a light-weight library and only depends on the visitor API of the Java byte code parser library ASM which does itself not require any further dependencies.

For that, i have cloned the byte-buddy repo, build it with nvm package and packed the builded .jar and the .jar from ASM in my project folder.

Bizarrus avatar Aug 27 '23 21:08 Bizarrus

I had tried it with IntelliJ (Gradle), here the same Problem:

image

Bizarrus avatar Aug 27 '23 22:08 Bizarrus

In Java, you need to inport symbols. Normally, your IDE gives you suggestions in the context menu.

raphw avatar Aug 27 '23 22:08 raphw