Replies: 2 comments
-
I do not think that this is doing what you intend. With |
Beta Was this translation helpful? Give feedback.
0 replies
-
Wow Your email format is really nice
… 2023年11月21日 05:41,Laura Sarmiento Cárdenes ***@***.***> 写道:
Hello,
I want to create a dynamic class that implements an interface. This interface has four void methods. I try to do it with byte buddy but I get an error in my way of implimenting it. I have tried to find a solution, but I can't find anything that works for my problem, so below I pass the code and the exact error it gives me.
I have this Interface:
import javafx.scene.canvas.GraphicsContext;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
public interface Figura {
public void setXYTamano(int x, int y, int tamano);
public void dibujar(GraphicsContext gc);
public void mover();
public void agrandar();
}
Then, I have this code for the dynamic class:
package javafxapplication1.acciones;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.matcher.ElementMatchers;
import static net.bytebuddy.matcher.ElementMatchers.named;
public class CreateClass{
Class<? extends Figura> claseDinamica = null;
public void crearClaseRectangulo () throws InstantiationException, IllegalAccessException {
try {
claseDinamica = new ByteBuddy()
.subclass(Figura.class)
.name("Rectangulo")
.defineField("x", int.class, Visibility.PRIVATE)
.defineField("y", int.class, Visibility.PRIVATE)
.defineField("tamano", int.class, Visibility.PRIVATE)
.method(ElementMatchers.is(named("setXYTamano")))
.intercept(MethodDelegation.to(Interceptor.class))
.method(ElementMatchers.is(named("dibujar")))
.intercept(MethodDelegation.to(Interceptor.class))
.method(ElementMatchers.is(named("mover")))
.intercept(MethodDelegation.to(Interceptor.class))
.method(ElementMatchers.is(named("agrandar")))
.intercept(MethodDelegation.to(Interceptor.class))
.make()
.load(CreateClass.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
}catch (Exception e) {
// Manejo de excepciones
e.printStackTrace();
}
}
public static class Interceptor{
@RuntimeType
public void ***@***.*** Object [] arguments) {
//implementation
}
@RuntimeType
public void ***@***.*** Object [] arguments){
//implementation
}
@RuntimeType
public void ***@***.*** Object [] arguments){
//implementation
}
@RuntimeType
public void ***@***.*** Object [] arguments){
//implementation
}
}
public Figura getInstance() throws InstantiationException, IllegalAccessException {
if (claseDinamica != null){
return claseDinamica.newInstance();
}
return null;
}
}
When I try to create "Rectangulo" instance, I get this error:
GRAVE: null
java.lang.NullPointerException
at javafxapplication1.PantallaController.lambda$initialize$5(PantallaController.java:142)
I have checked what I am creating in a file, and it creates a class without its methods.
I am using Java 8 and Byte Buddy 1.10.22.
Why might this be happening? Can anyone help me? Thanks.
@raphw <https://github.com/raphw>
—
Reply to this email directly, view it on GitHub <#1563>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ADCC5KLUDMBARZUFRK6IXF3YFPFBPAVCNFSM6AAAAAA7TR4VY2VHI2DSMVQWIX3LMV43ERDJONRXK43TNFXW4OZVHA3TGNZTG4>.
You are receiving this because you are subscribed to this thread.
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I want to create a dynamic class that implements an interface. This interface has four void methods. I try to do it with byte buddy but I get an error in my way of implimenting it. I have tried to find a solution, but I can't find anything that works for my problem, so below I pass the code and the exact error it gives me.
I have this Interface:
Then, I have this code for the dynamic class:
When I try to create "Rectangulo" instance, I get this error:
I have checked what I am creating in a file, and it creates a class without its methods.
I am using Java 8 and Byte Buddy 1.10.22.
Why might this be happening? Can anyone help me? Thanks.
@raphw
Beta Was this translation helpful? Give feedback.
All reactions