ByteBuddy invoking a method from super class which has been overwritten in child - byte-buddy

I am using ByteBuddy to dynamically create a like B in this example:
class A{
public void greet(String name){
System.out.println("Hello from class A "+ name + "!");
}
}
class B extends A{
public void greet(String name){
System.out.println("Hola from class B "+ name + "!");
}
public void superGreet(String name){
super.greet(name);
}
}
And this is my code:
Class<?> dynamicType = new ByteBuddy()
.subclass(A.class)
.name("B")
.defineMethod("superGreet", void.class, Modifier.PUBLIC)
.withParameters(String.class)
.intercept(
MethodCall.invoke(A.class.getMethod("greet", String.class))
.withAllArguments()
)
.defineMethod("greet", void.class, Modifier.PUBLIC)
.withParameters(String.class)
.intercept(MethodDelegation.to(new MyInterceptor()))
.make()
.load(Test.class.getClassLoader(), ClassLoadingStrategy.Default.INJECTION)
.getLoaded();
Object obj = dynamicType.newInstance();
dynamicType.getMethod("superGreet", String.class).invoke(obj, "name");
when I execute this code superGreet invokes method in Class B not Class A.
How can I make ByteBuddy invoke the greet method in A ?

You have to specify the MethodCall::onSuper invocation target after invoke.

Related

JUnit 5 Parameterized test #ArgumentsSource parameters not loading

I have created below JUnit5 parameterized test with ArgumentsSource for loading arguments for the test:
public class DemoModelValidationTest {
public ParamsProvider paramsProvider;
public DemoModelValidationTest () {
try {
paramsProvider = new ParamsProvider();
}
catch (Exception iaex) {
}
}
#ParameterizedTest
#ArgumentsSource(ParamsProvider.class)
void testAllConfigurations(int configIndex, String a) throws Exception {
paramsProvider.executeSimulation(configIndex);
}
}
and the ParamsProvider class looks like below:
public class ParamsProvider implements ArgumentsProvider {
public static final String modelPath = System.getProperty("user.dir") + File.separator + "demoModels";
YAMLDeserializer deserializedYAML;
MetaModelToValidationModel converter;
ValidationRunner runner;
List<Configuration> configurationList;
List<Arguments> listOfArguments;
public ParamsProvider() throws Exception {
configurationList = new ArrayList<>();
listOfArguments = new LinkedList<>();
deserializedYAML = new YAMLDeserializer(modelPath);
deserializedYAML.load();
converter = new MetaModelToValidationModel(deserializedYAML);
runner = converter.convert();
configurationList = runner.getConfigurations();
for (int i = 0; i < configurationList.size(); i++) {
listOfArguments.add(Arguments.of(i, configurationList.get(i).getName()));
}
}
public void executeSimulation(int configListIndex) throws Exception {
final Configuration config = runner.getConfigurations().get(configListIndex);
runner.run(config);
runner.getReporter().consolePrintReport();
}
#Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return listOfArguments.stream().map(Arguments::of);
// return Stream.of(Arguments.of(0, "Actuator Power"), Arguments.of(1, "Error Logging"));
}}
In the provideArguments() method, the commented out code is working fine, but the first line of code
listOfArguments.stream().map(Arguments::of)
is returning the following error:
org.junit.platform.commons.PreconditionViolationException: Configuration error: You must configure at least one set of arguments for this #ParameterizedTest
I am not sure whether I am having a casting problem for the stream in provideArguments() method, but I guess it somehow cannot map the elements of listOfArguments to the stream, which can finally take the form like below:
Stream.of(Arguments.of(0, "Actuator Power"), Arguments.of(1, "Error Logging"))
Am I missing a proper stream mapping of listOfArguments?
provideArguments(…) is called before your test is invoked.
Your ParamsProvider class is instantiated by JUnit. Whatever you’re doing in desiralizeAndCreateValidationRunnerInstance should be done in the ParamsProvider constructor.
Also you’re already wrapping the values fro deserialised configurations to Arguments and you’re double wrapping them in providesArguments.
Do this:
#Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return listOfArguments.stream();
}}

Unable to add mutator for an existing field of a class

I'm trying to add a mutator for an existing private final field. I can transform the field modifiers to remove the final specification and add an accessor method:
// accessor interface
public interface UniqueIdAccessor {
Serializable getUniqueId();
}
// mutator interface
public interface UniqueIdMutator {
void setUniqueId(Serializable uniqueId);
}
...
// fragment of Java agent implementation
return new AgentBuilder.Default()
.type(hasSuperType(named("org.junit.runner.Description")))
.transform(new Transformer() {
#Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription,
ClassLoader classLoader, JavaModule module) {
return builder.field(named("fUniqueId")).transform(ForField.withModifiers(FieldManifestation.PLAIN))
.implement(UniqueIdAccessor.class).intercept(FieldAccessor.ofField("fUniqueId"))
// .implement(UniqueIdMutator.class).intercept(FieldAccessor.ofField("fUniqueId"))
.implement(Hooked.class);
}
})
.installOn(instrumentation);
...
Here's a method that uses reflection to check the modifiers of the target field and calls the accessor to get the value of the field.
private static void injectProxy(Description description) {
try {
Field bar = Description.class.getDeclaredField("fUniqueId");
System.out.println("isFinal: " + ((bar.getModifiers() & Modifier.FINAL) != 0));
} catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Serializable uniqueId = ((UniqueIdAccessor) description).getUniqueId();
System.out.println("uniqueId: " + uniqueId);
}
// isFinal: false
// uniqueId: <description-unique-id>
... but if I uncomment the second "implement" expression to add the mutator, the transform blows up:
// isFinal: true
// java.lang.ClassCastException:
// class org.junit.runner.Description cannot be cast to class com.nordstrom.automation.junit.UniqueIdAccessor
// (org.junit.runner.Description and com.nordstrom.automation.junit.UniqueIdAccessor
// are in unnamed module of loader 'app')
I could set the field value with reflection, but that defeats the purpose of using Byte Buddy in the first place!
The problem with this approach is that the field accessor considers the input type prior to the modification. Byte Buddy prohibits this as it does not consider the mutation to be legal, not knowing about the removed modifier. As a result, the transformation fails in its entirety and you get the error you are seeing. (Register a listener to see this error.)
To avoid this, you can implement a custom Implementation using FieldAccess (without or). You can have a look at the more convenient FieldAccessor to see how this is implemented, only that you need to drop the validity checks.
Thanks for pointing me in the right direction! I assemble the StackManipulation object that defines the mutator method with this:
final TypeDescription description = TypePool.Default.ofSystemLoader().describe("org.junit.runner.Description").resolve();
final Generic _void_ = TypeDescription.VOID.asGenericType();
final Generic serializable = TypePool.Default.ofSystemLoader().describe("java.io.Serializable").resolve().asGenericType();
final MethodDescription.Token setUniqueIdToken = new MethodDescription.Token("setUniqueId", Modifier.PUBLIC, _void_, Arrays.asList(serializable));
final MethodDescription setUniqueId = new MethodDescription.Latent(description, setUniqueIdToken);
final Token fUniqueIdToken = new FieldDescription.Token("fUniqueId", Modifier.PRIVATE, serializable);
final FieldDescription fUniqueId = new FieldDescription.Latent(description, fUniqueIdToken);
final StackManipulation setUniqueIdImpl = new StackManipulation.Compound(
MethodVariableAccess.loadThis(),
MethodVariableAccess.load(setUniqueId.getParameters().get(0)),
Assigner.DEFAULT.assign(serializable, serializable, Typing.STATIC),
FieldAccess.forField(fUniqueId).write(),
MethodReturn.VOID
);
... and I transform the target class with this:
return new AgentBuilder.Default()
.type(hasSuperType(named("org.junit.runner.Description")))
.transform(new Transformer() {
#Override
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription,
ClassLoader classLoader, JavaModule module) {
return builder.field(named("fUniqueId")).transform(ForField.withModifiers(FieldManifestation.PLAIN))
.implement(AnnotationsAccessor.class).intercept(FieldAccessor.ofField("fAnnotations"))
.implement(UniqueIdAccessor.class).intercept(FieldAccessor.ofField("fUniqueId"))
.implement(UniqueIdMutator.class).intercept(new Implementation.Simple(setUniqueIdImpl));
}
})
.installOn(instrumentation);
Here are the definitions of the three interfaces used in the transform:
// annotations accessor interface
public interface AnnotationsAccessor {
Annotation[] annotations();
}
// unique ID accessor interface
public interface UniqueIdAccessor {
Serializable getUniqueId();
}
// unique ID mutator interface
public interface UniqueIdMutator {
void setUniqueId(Serializable uniqueId);
}

Syntax error when adding Runnable in static initializer with javassist

I want to use javassist (version 3.19-GA) to generate bytecode of a static initializer of a class that starts a thread. For some reason I cannot understand javassist expects a ";" somewhere even though I believe the code I provide is syntactically correct. Does someone see more than I do? Here is the code. What is the problem?
ClassPool pool = ClassPool.getDefault();
final CtClass clazz = pool.get(somename);
clazz.makeClassInitializer().insertAfter(
"try{Runnable r=new Runnable () {public void run () { System.out.println (\"hello!!!!\"); }}; " +
"new Thread(r).start(); } catch(Exception e){}");
I'm getting the following exception:
javassist.CannotCompileException: [source error] ; is missing
at javassist.CtBehavior.insertAfter(CtBehavior.java:877)
at javassist.CtBehavior.insertAfter(CtBehavior.java:792)
at my.code(myclass.java:111)
Thanks for any hint.
Most probably javassist compiler does not support anonymous inner classes like new Runnable () {...}
You have to create new class, inherit it from Runnable, implement method run and create object of this class in your constructor.
package hello;
import javassist.*;
class Test{
}
class Main {
public static void main(String[] args) throws Exception {
ClassPool pool = ClassPool.getDefault();
final CtClass clazz = pool.get(Test.class.getCanonicalName());
CtClass runnable = pool.makeClass("my.custom.RunnableImpl");
runnable.addInterface(pool.get("java.lang.Runnable"));
CtMethod method = CtNewMethod.make("public void run() { System.out.println(\"hello!!!!\"); }", runnable);
runnable.addMethod(method);
// load class
runnable.toClass();
clazz.setName("newTest");
CtConstructor ctConstructor = clazz.makeClassInitializer();
ctConstructor.insertAfter("try{ new Thread( new my.custom.RunnableImpl() ).start(); } catch(Exception e){}");
Class aClass = clazz.toClass();
// call initializer
Class.forName(aClass.getCanonicalName());
Thread.sleep(1000);
}
}
From the official documentation of void insertAfter(String src):
Parameters:
src - the source code representing the inserted bytecode. It must be a
single statement or block.
In your src String parameter, you don't provide a single statement or a block.
A block is "{}".
Try insertBefore(String src) method with global enclosing "{}":
ClassPool pool = ClassPool.getDefault();
final CtClass clazz = pool.get(somename);
clazz.makeClassInitializer().insertBefore(
"{try{Runnable r = new Runnable () {public void run () { System.out.println (\"hello!!!!\"); }}; " +
"new Thread(r).start(); } catch(Exception e){}}");

How to define point cuts for a sequence of method(s)?

For example if I have 3 classes,
class A {
public void doA() {
/* do something */
}
}
class B {
public void doB() {
A a = new A();
a.doA();
}
}
class MyClass {
public static void main(String args[]) {
B b = new B();
b.doB();
}
}
Now I want to define a point cut for flow doB() -> doA(), like if doB() calls doA() grab parameters from class A and class B and do something in aspect method. Could someone help me out.
Let me slightly extend your sample code in order to make you understand what my solution does and what it cannot do:
class A {
public void doA() {}
}
class B {
public void doB() {
new A().doA();
new C().doC();
}
}
class C {
public void doC() {
new A().doA();
}
}
class MyClass {
public static void main(String args[]) {
new A().doA(); // should not be captured
new B().doB(); // should be captured
}
}
As you can see, there is a new class C now and we have three control flows now:
MyClass.main -> A.doA
MyClass.main -> B.doB -> A.doA
MyClass.main -> B.doB -> C.doC -> A.doA
You want to exclude #1 and capture #2, but what about #3? In this case a.doA is called indirectly from B.doB via C.doC. My solution also captures this indirect case. If this is fine for you or it does not happen in your code base, you can use my solution. Otherwise things would get a little more complicated and you would need to inspect the call stack. Tell me if you need to exclude #2, and I will extend my answer, but the solution will not look as simple as this one, I can promise.
Now here is the aspect:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class ControlFlowInterceptor {
#Before("execution(void A.doA()) && target(a) && cflow(execution(void B.doB()) && target(b))")
public void advice(JoinPoint thisJoinPoint, A a, B b) {
System.out.println(thisJoinPoint);
System.out.println(" " + a);
System.out.println(" " + b);
}
}
The console output looks like this:
execution(void A.doA())
A#7b19f779
B#65c66812
execution(void A.doA())
A#4df2868
B#65c66812
Please note that we have the same B object ID in both outputs, but because C.doC creates an new A object, we have two different A object IDs.

declare generic parents statement in aspectj

Is it possible to use generic types with declare parents such that a class defined with generics implements an interface with the same generic types
i.e declare parents: AClass<Generic1,Generic2> implements
AnInterface<Generic1,Generic2>
What I am saying is whether it is possible to pass the generic types of the child to parents
Kind of. Check this out:
Generic class:
package de.scrum_master.app;
public class KeyValuePair<K,V> {
private K key;
private V value;
KeyValuePair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
#Override
public String toString() {
return "KeyValuePair [key=" + key + ", value=" + value + "]";
}
}
Generic interface:
package de.scrum_master.app;
public interface KeyValueComparator<K, V> {
boolean equalsKey(K otherKey);
boolean equalsValue(V otherValue);
}
Aspect with ITD:
The ITD (inter-type definition) makes sure that the class implements the interface and also gets method implementations for the interface at the same time.
package de.scrum_master.aspect;
import de.scrum_master.app.KeyValuePair;
import de.scrum_master.app.KeyValueComparator;
public aspect InterfaceIntroductionAspect {
declare parents : KeyValuePair implements KeyValueComparator;
public boolean KeyValuePair.equalsKey(K otherKey) {
return this.getKey().equals(otherKey);
}
public boolean KeyValuePair.equalsValue(V otherValue) {
return this.getValue().equals(otherValue);
}
}
Driver application:
Create two different types of class objects and try to cast them both to the introduced interface:
package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
KeyValuePair<Integer, String> pair1 = new KeyValuePair<>(11, "eleven");
System.out.println(pair1);
KeyValueComparator<Integer, String> comparator1 = (KeyValueComparator<Integer, String>) pair1;
System.out.println("equalsKey = " + comparator1.equalsKey(12));
System.out.println("equalsValue = " + comparator1.equalsValue("eleven"));
KeyValuePair<String, Integer> pair2 = new KeyValuePair<>("twelve", 12);
System.out.println(pair2);
KeyValueComparator<String, Integer> comparator2 = (KeyValueComparator<String, Integer>) pair2;
System.out.println("equalsKey = " + comparator2.equalsKey("twelve"));
System.out.println("equalsValue = " + comparator2.equalsValue(11));
}
}
Output:
KeyValuePair [key=11, value=eleven]
equalsKey = false
equalsValue = true
KeyValuePair [key=twelve, value=12]
equalsKey = true
equalsValue = false