How can I define a method and then decorate it (multiple times) with ByteBuddy ?
This is my example
Builder<Object> builder = new ByteBuddy().subclass(Object.class).name("Dynamic");
builder = builder.defineMethod("method", void.class, Visibility.PUBLIC)
.intercept(MethodDelegation.to(new Object(){
#RuntimeType
public void intercept(#This Object o) {
System.out.println("Executing code...");
}
}));
builder = builder.method(ElementMatchers.named("method")).
intercept(MethodDelegation.to(new Object(){
#RuntimeType
public void intercept(#This Object o) {
System.out.println("Executing other code...");
}
}));
try {
Class cls = builder.make()
.load(StructClassBuilder.class.getClassLoader())
.getLoaded();
Object obj = cls.newInstance();
cls.getDeclaredMethod("method").invoke(obj, args);
} catch (Exception e1) {
e1.printStackTrace();
}
The output is
Executing other code...
I would like that the output is
Executing code...
Executing other code...
Thanks
One option is to chain Your interceptors using MethodDelegation.to(...).addThen(...) methods.
public class ByteBuddyTest {
public static void main(String[] args) throws Exception {
DynamicType.Builder<Object> builder = new ByteBuddy().subclass(Object.class).name("Dynamic");
builder = builder
.defineMethod("method", void.class, Visibility.PUBLIC)
.intercept(MethodDelegation.to(Interceptor1.class).andThen(MethodDelegation.to(Interceptor2.class)));
try {
Class<?> clazz = builder.make().include().load(ByteBuddyTest.class.getClassLoader()).getLoaded();
Object obj = clazz.newInstance();
clazz.getDeclaredMethod("method").invoke(obj, args);
} catch (Exception e1) {
e1.printStackTrace();
}
}
public static class Interceptor1 {
public static void intercept() {
System.out.println("Executing code...");
}
}
public static class Interceptor2 {
public static void intercept() {
System.out.println("Executing other code...");
}
}
}
I'll use decorator pattern to decorate the Interceptor, now it works as expected.
I share my solution:
private static interface Interceptor{
public void intercept(#This Object o);
}
private abstract static class InterceptorDecorator implements Interceptor{
protected Interceptor interceptor;
public InterceptorDecorator(Interceptor interceptor){
this.interceptor = interceptor;
}
public void intercept(#This Object o) {
if(interceptor!=null){
interceptor.intercept(o);
}
}
}
private static class Interceptor1 extends InterceptorDecorator{
public Interceptor1(Interceptor interceptor) {
super(interceptor);
}
public void intercept(#This Object o) {
super.intercept(o);
System.out.println("Executing code...");
}
}
private static class Interceptor2 extends InterceptorDecorator{
public Interceptor2(Interceptor interceptor) {
super(interceptor);
}
public void intercept(#This Object o) {
super.intercept(o);
System.out.println("Executing other code...");
}
}
public static void main(String[] args) {
Interceptor interceptor = new Interceptor1(null);
interceptor = new Interceptor2(interceptor);
Builder<Object> builder = new ByteBuddy().subclass(Object.class).name("Dynamic");
builder = builder.defineMethod("method", void.class, Visibility.PUBLIC)
.intercept(MethodDelegation.to(interceptor));
try {
Class cls = builder.make()
.load(StructClassBuilder.class.getClassLoader())
.getLoaded();
Object obj = cls.newInstance();
cls.getDeclaredMethod("method").invoke(obj, args);
} catch (Exception e1) {
e1.printStackTrace();
}
}
Related
I am using Mockito/PowerMockito APIs to mock some objects for junit cases.
In the example given below, I want to create a mock object of class C (returned by Utils.getC()). Also I want to use same mock object of C in B.execute(), and not a new object. Is there a way I can achieve this? Please help. [Update - Thanks Lino for answering this. I have edited the code given below.]
However, this works for static methods only. I am not able to mock instance method D.displayMessage() (invoked from A.execute() and B.execute()).
#PrepareForTest(mock.Utils.class)
#RunWith(PowerMockRunner.class)
public class TestMock {
private static C c;
private static D d;
#BeforeClass
public static void runOnceBeforeClass() {
try {
System.out.println("#BeforeClass - runOnceBeforeClass");
PowerMockito.mockStatic(Utils.class);
c = Mockito.mock(C.class);
System.out.println("c = " + c);
PowerMockito.doReturn("Hello!!").when(c).displayMessage();
Answer<Void> answer = new Answer() {
public Void answer(InvocationOnMock invocation) {
System.out.println("I can perform!");
return null;
}
};
PowerMockito.doAnswer(answer).when(c).perform();
PowerMockito.when(Utils.getC()).thenReturn(c);
Answer<Void> answer1 = new Answer() {
public Void answer(InvocationOnMock invocation) {
System.out.println("I can utilize!");
return null;
}
};
PowerMockito.doAnswer(answer1).when(Utils.class);
Utils.utilize();
Answer<Void> answer2 = new Answer() {
public String answer(InvocationOnMock invocation) {
System.out.println("I can run with params!");
return null;
}
};
PowerMockito.doAnswer(answer2).when(Utils.class);
Utils.runWithParams(Mockito.anyString(), Mockito.anyInt(), Mockito.any());
d = PowerMockito.mock(D.class);
PowerMockito.when(d.displayMessage()).thenReturn("D: I can display!");
PowerMockito.whenNew(D.class).withNoArguments().thenReturn(d);
} catch (Exception ex) {
ex.printStackTrace();
}
}
#AfterClass
public static void runOnceAfterClass() {
System.out.println("#AfterClass - runOnceAfterClass");
}
#Before
public void runBeforeTestMethod() {
System.out.println("#After - runBeforeTestMethod");
}
#After
public void runAfterTestMethod() {
System.out.println("#After - runAfterTestMethod");
}
#Test
public void testExecution() {
System.out.println(Utils.getC().displayMessage());
A a = new A();
a.execute();
}
}
class A {
public void execute() {
System.out.println("executing A");
B b = new B();
b.execute();
System.out.println(new D().displayMessage());
}
}
class B {
public void execute() {
System.out.println("executing B");
C c1 = Utils.getC();
System.out.println("c = " + c1.hashCode());
c1.perform();
Utils.utilize();
Utils.runWithParams("", 3, "2");
System.out.println(new D().displayMessage());
}
}
class C {
public String displayMessage() {
return "C: I can't display.";
}
public void perform() {
System.out.println("I can't perform.");
}
}
class D {
public String displayMessage() {
return "D: I can't display.";
}
}
class Utils {
public static C getC() {
return null;
}
public static void utilize() {
System.out.println("I can't unitilize.");
}
public static String runWithParams(String s, Integer i, Object o) {
System.out.println("I can't run with params.");
return "abc";
}
}
If you are trying to reuse the mocked object of C, for the static method call inside execute() method of B , the same mocked object can be reused.
I have a working annotation processor that gathers information of the annotated classes. Everything is there during compilation. But I would like to have access to those results during runtime.
#SupportedSourceVersion(SourceVersion.RELEASE_8)
#AutoService(Processor.class)
public class TestProcessor extends AbstractProcessor {
private final static List<TestInfo> tests = new ArrayList<>();
#Override
public Set getSupportedAnnotationTypes() {
return new LinkedHashSet() {
{
add(Annotation.class.getCanonicalName());
}
};
}
#Override
public boolean process(final Set<? extends TypeElement> annotations,
final RoundEnvironment env) {
System.out.println("Processing!");
if (!env.processingOver()) {
Set<? extends Element> rootE = env.getRootElements();
for (Element e : rootE) {
if (e.getKind() == ElementKind.CLASS) {
TestInfo t = new TestInfo(e.asType().toString());
for (Element se : e.getEnclosedElements()) {
if (se.getKind() == ElementKind.METHOD) {
t.addMethod(se.getSimpleName().toString());
}
}
getTests().add(t);
}
}
getTests().forEach(ti -> {
System.out.println(ti);
});
}
return false;
}
public static TypeElement findEnclosingTypeElement(Element e) {
while (e != null && !(e instanceof TypeElement)) {
e = e.getEnclosingElement();
}
return TypeElement.class.cast(e);
}
/**
* #return the tests
*/
public static List<TestInfo> getTests() {
return tests;
}
}
Is there a way to retrieve the results at runtime? TestProcessor.getTests returns an empty list.
Here's the TestInfo class fyi:
public class TestInfo {
private final String name;
private final List<String> methods = new ArrayList<>();
public TestInfo(String name) {
this.name = name;
}
public void addMethod(String m) {
getMethods().add(m);
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #return the methods
*/
public List<String> getMethods() {
return methods;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(name).append(methods.toString());
return sb.toString();
}
}
Update: The annotation is marked with retention runtime.
The annotation-processing is in compile time. So you can't get the information in the runtime.
A direct way is to write the information as a resource file in compile time and read it at runtime.
Here is my example:
The annotation:
#Retention(SOURCE)
#Target(TYPE)
public #interface Anno {
}
The processor:
#Override
public boolean processActual(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
return false;
}
try {
write(roundEnv);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
private void write(RoundEnvironment roundEnv) throws IOException, UnsupportedEncodingException {
Filer filer = processingEnv.getFiler();
FileObject resource = filer.createResource(StandardLocation.CLASS_OUTPUT, "", "TestInfo");
OutputStream output = resource.openOutputStream();
PrintStream writer = new PrintStream(output, false, "UTF-8");
roundEnv.getElementsAnnotatedWith(Anno.class)
.stream()
.filter(e -> e.getKind() == ElementKind.CLASS)
.map(e -> e.asType().toString())
.forEach(writer::println);
writer.flush();
}
And the user code:
#Anno
public class Q48177784 {
public static final List<Class<?>> CLASSES;
static {
try {
URL resource = Q48177784.class.getClassLoader().getResource("TestInfo");
CLASSES = Files.readAllLines(Paths.get(resource.toURI()))
.stream()
.map(s -> {
try {
return Class.forName(s);
} catch (ClassNotFoundException e) {
throw new Error(e);
}
})
.collect(Collectors.toList());
} catch (Exception e) {
throw new Error(e);
}
}
public static void main(String[] args) {
System.out.println(CLASSES);
}
}
After build with processor, run the main method:
[class xdean.stackoverflow.Q48177784]
For your case, the only thing you should do is serialize/deserialize your TestInfo
Check out #RetentionPolicy. I think you want to set it to RUNTIME.
I want to route certain chars to methods, so that when the char is typed in the command-line the method is then executed.
Based on the answer How to call a method stored in a HashMap, I'm mapping these chars to methods by using the "Command" design-pattern.
However I want to generically implement this, so it seems that I need to implement reflection in order to use the Method class as a parameter. My attempt is getting a NullPointerException on the field private Method method in my anonymous class...
Here is my code:
import java.lang.reflect.Method;
public interface InvokesMethod {
public void invokeMethod() throws Exception;
public void setMethod(Method method);
} // end of interface
import java.util.HashMap;
import java.lang.reflect.Method;
public class Terminal {
public HashMap<Character, InvokesMethod> commands;
public Terminal() {
this.commands = new HashMap<Character, InvokesMethod>();
try {
this.setCommand('p',
this.getClass().getDeclaredMethod("printHelloWorld"));
} catch (Exception e) {
e.printStackTrace();
}
}
private void printHelloWorld() {
System.out.println("Hello World!");
}
private void setCommand(char letter, Method method) {
this.commands.put(letter, new InvokesMethod() {
// NullPointerException starts here in the stack-trace:
private Method method;
#Override
public void invokeMethod() throws Exception {
method.invoke(null);
}
#Override
public void setMethod(Method method) {
this.method = method;
}
}).setMethod(method);
}
public void executeCommand(char letter) throws Exception {
this.commands.get(letter).invokeMethod();
}
} // end of class
public class Main() {
public static void main(String[] args) {
Terminal commandLine = new Terminal();
try {
commandLine.executeCommand('p');
} catch (Exception e) {
e.printStackTrace();
}
}
} // end of class
Regards to your code you didn't initiate method. Bear in mind that execute with null you must call public static method:
Your other issue , you didn't initiated interface properly. Here is working example:
InvokesMethodItf
public interface InvokesMethodItf {
public void invokeMethod() throws Exception;
public void setMethod(Method method);
}
InvokesMethod
public class InvokesMethod implements InvokesMethodItf{
private Method method;
#Override
public void invokeMethod() throws Exception {
method.invoke(null);
}
#Override
public void setMethod(Method method) {
this.method = method;
}
}
Terminal
public class Terminal {
public HashMap<Character, InvokesMethodItf> commands;
public Terminal() {
this.commands = new HashMap<Character, InvokesMethodItf>();
try {
this.setCommand('p',
this.getClass().getDeclaredMethod("printHelloWorld"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void printHelloWorld() {// method.invoke(null) looking for "static" method
System.out.println("Hello World!");
}
private void setCommand(char letter, Method method) {
InvokesMethodItf inv = new InvokesMethod();
inv.setMethod(method);
this.commands.put(letter, inv);
}
public void executeCommand(char letter) throws Exception {
this.commands.get(letter).invokeMethod();
}
}
Main
public class Main {
public static void main(String[] args) {
Terminal commandLine = new Terminal();
try {
commandLine.executeCommand('p');
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Hello World!
Thanks to #Maxim's original suggestion here, I have an alternate solution by setting the methods as Strings in the HashMap instead --
import java.util.HashMap;
import java.lang.reflect.Method;
public class Terminal {
private HashMap<Character, String> commands;
public Terminal() {
this.commands = new HashMap<Character, String>();
this.commands.put('p', "printHelloWorld");
}
private void printHelloWorld() {
System.out.println("Hello World!");
}
public void executeCommand(char letter) throws Exception {
Method method = getClass().getDeclaredMethod(this.commands.get(letter));
method.invoke(this);
}
public class Main {
public static void main(String[] args) {
Terminal commandLine = new Terminal();
try {
commandLine.executeCommand('p');
} catch (Exception e) {
e.printStackTrace();
}
}
} // end of class
Output:
Hello World!
Now to figure out how to pass parameters to the reflected methods...
I want to do a factory pattern in java with generics. My code is:
The interface:
public abstract class Factory<T> {
public abstract T create();
}
FactoryA class:
public class FactoryA extends Factory<FactoryA> {
public FactoryA() {
}
public FactoryA create() {
return new FactoryA();
}
}
FactoryB class:
public class FactoryB extends Factory<FactoryB> {
public FactoryB() {
}
public FactoryB create() {
return new FactoryB();
}
}
The main class:
public class FactoryCreator {
public static <T> T createFactory() {
Factory<T> t = ?; // is that right way?
return t.create();
}
public static void main(String[] args) {
FactoryA factoryA = FactoryCreator.createFactory();
FactoryB factoryB = FactoryCreator.createFactory();
}
}
The question, what Factory t = need to be equal, or is there any other way?
Not really sure what you're trying to achieve, but this might help;
public interface Factory<T>
{
public T create(String type);
public T create(String type, Object arg);
public T create(String type, Object[] args);
}
And then have a class implement that factory interface, like this;
public class TemplateFactory<T> implements Factory {
#Override
public T create(String type) throws IllegalArgumentException
{
return create(type, null);
}
#Override
public T create(String type, Object arg) throws IllegalArgumentException
{
// Convert to array of 1 element
Object[] arguments = new Object[1];
arguments[0] = arg;
return create(type, arguments);
}
#Override
public T create(String type, Object[] args) throws IllegalArgumentException
{
// Create array for all the parameters
Class<?> params[] = (args != null) ? new Class<?>[args.length] : new Class<?>[0];
if(args != null)
{
// Adding the types of the arguments
for(int i = 0; i < args.length; ++i)
params[i] = (args[i] != null) ? args[i].getClass() : null;
}
try
{
// Create a class variable
Class classLoader = Class.forName(type);
// Find the right constructor
Constructor co;
if(params.length > 0)
co = classLoader.getConstructor(params);
else
co = classLoader.getConstructor();
// Instantiate the class with the given arguments
T newObject = (T)co.newInstance(args);
return newObject;
}
catch(Exception e)
{
throw new IllegalArgumentException(e.toString());
}
}
}
And then use it like this (using some imaginary strategy-classes as an example):
TemplateFactory<StrategyInterface> factory;
factory = new TemplateFactory<>();
factory.create("packageName.StrategyA");
factory.create("packageName.StrategyB");
factory.create("packageName.StrategyC");
The strategy classes (A, B and C) would implement the StrategyInterface-class in this example.
Something like this might work:
public static <T extends Factory> T createFactory(Class<T> clazz) {
try {
t = clazz.newInstance();
return t.create();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
...
FactoryA factoryA = FactoryCreator.createFactory(FactoryA.class);
alternatively, without parameters. But then you need two methods.
public static FactoryA createFactoryA() {
return new FactoryA().create();
}
...
FactoryA factoryA = FactoryCreator.createFactoryA();
Since the Generic types are erased at runtime you have to supply the Class parameter so that the runtime knows what class you are talking about.
I've got something along the lines of the following:
public class A {
public void theMethod(Object arg1) {
// do some stuff with a single argument
}
}
public class B {
public void reflectingMethod(Object arg) {
Method method = A.class.getMethod("theMethod", Object.class);
method.invoke(new A(), arg);
}
}
How do I modify that so that I can do the following instead?
public class A {
public void theMethod(Object... args) {
// do some stuff with a list of arguments
}
}
public class B {
public void reflectingMethod(Object... args) {
Method method = A.class.getMethod("theMethod", /* what goes here ? */);
method.invoke(new A(), args);
}
}
A.class.getMethod("theMethod", Object[].class);
Darthenius's suggestion in the comments for the original question worked, once I wrapped my head around how to do it.
public class A {
public void theMethod(ArrayList<Object> args) { // do stuff
}
}
public class B {
public void reflectingMethod(ArrayList<Object> args) {
Method method;
try {
method = A.class.getMethod("theMethod", args.getClass());
method.invoke(new A(), args);
} catch (Exception e) {}
}
}