Override sayHello() method

Report a typo


Please override sayHello() method from HelloClass.class in dynamicClassSayHelloFromSpain() method. Make sure this method returns "Hola". Select all lines that need to add.

package bytebuddy;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.FixedValue;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class GreetDynamicClass {
    public String dynamicClassSayHelloFromSpain() throws Exception {
        Class<?> dynamicClass = new ByteBuddy()
                .subclass(HelloClass.class)
                // select the appropriate lines

                .intercept(FixedValue.value("Hola!"))
                .make()
                .load(GreetDynamicClass.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
                .getLoaded();

        Object instance = dynamicClass.getDeclaredConstructor().newInstance();

        Method m = dynamicClass.getDeclaredMethod("sayHello");
        return m.invoke(instance).toString();
    }
}

class HelloClass {
    public String sayHello() {
        return "Hello!";
    }
}
Select one or more options from the list
___

Create a free account to access the full topic