I am having trouble in using jdk dynamic proxy - java

Before using spring aop and cglib, Now I replaced a simple example.I found that executing method sayHello1 () and sayHello2 () both output "before" and "after" Oh my god, it's very difficult, Do you understand what I am talking about? I am going crazy now. T.T
public interface HelloWorld {
void sayHello1(String say);
void sayHello2(String say);
}
public static class HelloWorldImpl implements HelloWorld {
#Override
public void sayHello1(String say) { System.out.println(say); }
#Override
public void sayHello2(String say) { System.out.println(say); }
}
public static class Invocation implements InvocationHandler {
private final Object target;
public Invocation(Object target) { this.target = target; }
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before..."); // TODO method before
Object object = method.invoke(target, args);
System.out.println("after..."); // TODO method after
return object;
}
}
public static void main(String[] args) {
HelloWorld helloWorld = (HelloWorld) Proxy.newProxyInstance(
ClassLoader.getSystemClassLoader(),
new Class[] { HelloWorld.class },
new Invocation(new HelloWorldImpl())
);
helloWorld.sayHello1("Hello World1 ...");
helloWorld.sayHello2("Hello World2 ...");
}

You mean you want something like this?
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Instrumenting " + method);
Object result;
switch (method.getName()) {
case "sayHello1":
System.out.println("before A");
result = method.invoke(target, args);
System.out.println("after A");
break;
case "sayHello2":
System.out.println("before B");
// Let's change the argument just for fun
args[0] = "changed argument";
result = method.invoke(target, args);
System.out.println("after B");
break;
default:
result = method.invoke(target, args);
}
return result;
}
That would yield the following console log:
Instrumenting public abstract void de.scrum_master.spring.q62001911.HelloWorld.sayHello1(java.lang.String)
before A
Hello World1 ...
after A
Instrumenting public abstract void de.scrum_master.spring.q62001911.HelloWorld.sayHello2(java.lang.String)
before B
changed argument
after B
Of course you could print further information or differentiate overloaded methods with the same names by parameter types. Try things like
method.getParameterTypes();
method.getParameterCount();
method.getReturnType();
Is this tedious? Yeah, it is, but still straightforward. And it being tedious is the reason why AspectJ or Spring AOP with their elegant pointcut + advice model are so much easier to use because they did the work already and hide the inner complexity from you.

Related

In Java Proxy, if i use an Object's method (like toString()),why it's method.invoke(this),not method.invoke(proxy) or other

In some code,like Mybatis,I saw the use of JDK-proxy.
Example:
org.apache.ibatis.logging.jdbc.ConnectionLogger:
public final class ConnectionLogger extends BaseJdbcLogger implements InvocationHandler {
#Override
public Object invoke(Object proxy, Method method, Object[] params)
throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, params);
}
}
...
}
}
Maybe my code like this :
UserMapper user = getProxy(UserMapper.class);
user.hashCode()
If i use the method like hashCode() , it means i want to get the hashCode from the user, not user's Proxy.hashCode(), not invocationHandler.hashCode().And I really try to change the method like this,it happens an error:
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(proxy, params);
}
But why use 'this'(is this means an object from ConnectionLogger ?)
Please tell me ,and sorry for my English ;)
You guessed right. The proxy address refers to the invocationhandler. Here is the test
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (Object.class.equals(method.getDeclaringClass())) {
System.out.println(this);
System.out.println(Integer.toHexString(System.identityHashCode(this)));
System.out.println(Integer.toHexString(System.identityHashCode(proxy)));
return method.invoke(this, args);
}
return "ddd";
}
public static void main(String[] args) {
InsertDataHandler c =new InsertDataHandler();
Object L=c.getProxy();
// L.toString();
System.out.println(L.toString());
}
E:\xzb\jdk\bin\java.exe
com.example.tenant.InsertDataHandler#728938a9
728938a9
21b8d17c
com.example.tenant.InsertDataHandler#728938a9

How to create chain of dynamic proxies?

I create two InvocationHandler, one for logging purpose and the other one for measuring time. Each on works but I do not know how to create a chain of these two, so that both will be executed. I thought it would be enough that for example the LoggingInvocationHandler extends the TimerInvocationHandler
public class DynamicProxyMain {
public static void main(String[] args) {
System.out.println("Starting dynamic proxy sample");
SubjectInterface timerProxy = (SubjectInterface) Proxy.newProxyInstance(SubjectInterface.class.getClassLoader(),
new Class<?>[]{SubjectInterface.class},
new TimerInvocationHandler(new SubjectInterfaceImpl()));
SubjectInterface logginProxy = (SubjectInterface) Proxy.newProxyInstance(SubjectInterface.class.getClassLoader(),
new Class<?>[]{SubjectInterface.class},
new LoggingInvocationHandler(new SubjectInterfaceImpl()));
timerProxy.methodA("a");
timerProxy.methodB("test b");
timerProxy.methodC(1, "test c");
}
}
public class LoggingInvocationHandler implements InvocationHandler {
Object impl;
String CLASSNAME = this.getClass().getCanonicalName();
public LoggingInvocationHandler(Object impl){
this.impl = impl;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object retVal;
System.out.println("LoggingHandler:" + this.getClass().getName() + " has been called");
retVal = method.invoke(impl, args);
System.out.println("LoggingHandler:" + this.getClass().getName() + " has ended");
return retVal;
}
}
public class TimerInvocationHandler extends LoggingInvocationHandler implements InvocationHandler{
private Object impl;
public TimerInvocationHandler(Object impl) {
super(impl);
this.impl = impl;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object retVal = null;
System.out.println("getting duration time for method " + method.getName());
long duration = -System.currentTimeMillis();
retVal = super.invoke(proxy,method,args);
duration += System.currentTimeMillis();
System.out.println("it took " + duration + " milliseconds");
System.out.println("duration time handler has ended");
return retVal;
}
}
Actually I solved it, so that both InvocationHandlers will be called. I edited my post with the currently working code
The idea bears a similarity to Intercepting Filter, I'll give your an implementation of it, which slightly modified in order to work with DynamicProxyHandler, if you're interested and want more details, you should read the link thoroughly.
Participants:
InvocationChain - which is responsible for dispatching invocations.
Invocation - where you should put things like logging and timer.
DynamicProxyHanlder - that simply delegates the request to InvocationChain.
Implementaiton:
DynamicProxyHandler.java
public class DynamicProxyHandler implements InvocationHandler {
private Object proxied;
InvocationChain chain = new InvocationChainImp();
DynamicProxyHandler(Object proxied) {
this.proxied = proxied;
}
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return chain.invoke(proxied, method, args);
}
}
Invocation.java
public interface Invocation {
Object invoke(Object callee, Method method, Object[] args, InvocationChain chain);
}
InvocationChain.java
public interface InvocationChain {
public Object invoke(Object callee, Method method, Object[] args);
}
InvocationChainImp.java
public class InvocationChainImp implements InvocationChain {
List<Invocation> list = new ArrayList<>();
Object result;
Iterator<Invocation> tasks;
InvocationChainImp() {
list.add(new LoggingInvocation());
list.add(new TimerInvocation());
list.add(new FinalInvocation());
tasks = list.iterator();
}
#Override
public Object invoke(Object callee, Method method, Object[] args) {
if (tasks.hasNext()) {
Object result = tasks.next().invoke(callee, method, args, this);
this.result = (this.result == null ? result : this.result);
}
return this.result;
}
Last not least, we want to define some custom classes that must be confined to Invocation interface for logging, timer, etc.
LoggingInvocation.java
public class LoggingInvocation implements Invocation {
#Override
public Object invoke(Object callee, Method method, Object[] args, InvocationChain chain) {
chain.invoke(callee, method, args);
Logger.getLogger(this.getClass().getCanonicalName()).info(method.getName() + "() execution logged!");
return null;
}
}
TimerInvocation.java
public class TimerInvocation implements Invocation {
#Override
public Object invoke(Object callee, Method method, Object[] args, InvocationChain chain) {
long start_time = System.nanoTime();
chain.invoke(callee, method, args);
long end_time = System.nanoTime();
System.out.println("Timer: excution took " + (end_time - start_time) / 1e6 + "ms");
return null;
}
}
FinalInvocation.java where the request is finally invoked on the proxied instance.
public class FinalInvocation implements Invocation {
#Override
public Object invoke(Object callee, Method method, Object[] args, InvocationChain chain) {
try {
return method.invoke(callee, args);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
}
Rest of code is trivial, as it's just used to prove that the implementation works.
You can stop reading now if want to write your own.
SubjectInterface.java
public interface SubjectInterface {
String hello();
}
SubjectInterfaceImp.java
public class SubjectInterfaceImp implements SubjectInterface {
#Override
public String hello() {
System.out.println("in SubjectInterfaceImp: Greeting!");
return "hello";
}
}
Main.java
public class Main {
public static void main(String[] args) throws Exception {
SubjectInterface subject = (SubjectInterface) Proxy.newProxyInstance(
SubjectInterface.class.getClassLoader(),
new Class[] { SubjectInterface.class }, new DynamicProxyHandler(new SubjectInterfaceImp()));
System.out.println("in Main: subject.hello() = " + subject.hello());
}
}
Okay, we have enough of code, it's show time, let's see we got, voila!
in SubjectInterfaceImp: Greeting!
Timer: excution took 0.532198ms
九月 02, 2016 12:37:36 下午 LoggingInvocation invoke
信息: hello() execution logged!
in Main: subject.hello() = hello
This is not the natural way to implement it.TimerInvocationHandler has nothing to do with LoggingInvocationHandler.
define a decorator which is a InvocationHandler and wraps a another InovovationHandler
https://en.wikipedia.org/wiki/Decorator_pattern
EDIT: since one comment ask me to provide a sample implementation,following part is added, but this is the not exact decorator pattern, but i think others can understand the solution. in this case TimeInvocationHandler is not limited to measure the login time
public class TimerInvocationHandler implements InvocationHandler
{
protected InvocationHandler invocationHandler;
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
//do whatever you want
Object result = invocationHandler.invoke(proxy, method, args);
// do what ever you want
return result;
}
}

Use reflection to create classes at runtime

I have to create a list of objects, which are configured according to the name of some classes received as input.
For each object I have to call a method, which add an operation that is created dynamically.
However I don't know exactly ho to resolve the problem.
Please see an example below.
String className; // this is an input parameter
final Class<?> classType = Class.forName(className);
// here I would like to use classType instead of "?" but it gives me an error.
Task<?> task = TaskFactory.createTask((String)classType.getField("_TYPE").get(null)));
tasks.put(task, null);
task.addOperation(new Operation<classType>() { // this gives an error
#Override
public void onNewInput(classType input) { // this gives an error
System.out.println(input)
}
});
As you can see from the comments, the surrounding infrastructure and the intention are not entirely clear. However, you can achieve a certain degree of type-safety with a "helper" method that captures the type of the given Task, and allows you to work with this type internally:
public class RuntimeType
{
public static void main(String[] args) throws Exception
{
String className = "";
final Class<?> classType = Class.forName(className);
Task<?> task = TaskFactory.createTask((String)classType.getField("_TYPE").get(null));
addOperation(task);
}
private static <T> void addOperation(Task<T> task)
{
task.addOperation(new Operation<T>()
{
#Override
public void onNewInput(T input)
{
System.out.println(input);
}
});
}
}
class TaskFactory
{
public static Task<?> createTask(String string)
{
return null;
}
}
class Task<T>
{
public void addOperation(Operation<T> operation)
{
}
}
interface Operation<T>
{
void onNewInput(T input);
}

Java - getting the signature of a method in an interface, and the same for its Proxy implementation

I'm looking for a way to extract the essence of a signature in Java. The reason is that I want to use the signature as a unique key in a Map for my java.lang.reflect.Proxies.
With this code:
public interface MyInterface {
public int compute(String s);
}
...
public static void main (String... args) throws Exception {
InvocationHandler handler = ...;
MyInterface i = (MyInterface) Proxy.newProxyInstance(
Beans.class.getClassLoader(),
new Class<?>[] { MyInterface.class },
handler);
Method m1 = MyInterface.class.getMethod("compute", String.class);
Method m2 = i.getClass().getMethod("compute", String.class);
System.out.printf("%b%f", m1.equals(m2));
}
The result is obviously false.
This code is not the code I'll use, because I need it inside the InvocationHandler, but I'm wondering if regarding the Proxy implementations and the interface, getting method.getName() and method.getParameterTypes() is enough or I should use method.getTypeParameters() and method.getParameterAnnotations() as well?
In short: how to get a method signature that is the same for an interface and its java.lang.reflect.Proxy implementations?
I think you want the Method passed in the InvocationHandler.
package playtest;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import static junit.framework.Assert.*;
interface MyInterface {
void run();
void about();
void run(String j);
}
public class TestProxyClass {
#Test
public void testDeclaringClass() throws Exception {
final Map<Method, Runnable> actions = new HashMap<Method, Runnable>();
actions.put(MyInterface.class.getMethod("run"), new Runnable() {
#Override
public void run() {
System.out.println("run");
}
} );
actions.put(MyInterface.class.getMethod("run", String.class), new Runnable() {
#Override
public void run() {
System.out.println("run string");
}
} );
actions.put(MyInterface.class.getMethod("about"), new Runnable() {
#Override
public void run() {
System.out.println("about");
}
} );
MyInterface face = (MyInterface) Proxy.newProxyInstance(getClass().getClassLoader(),
new Class<?>[] { MyInterface.class }, new InvocationHandler() {
#Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
actions.get(method).run();
return null;
}
} );
face.run();
face.about();
face.run("Hello");
}
}
What about using the result of Method.toGenericString as the key? The string it returns includes all details of the signature, as far as I can tell.
Another thing that might be useful is: Method m2 = i.getClass().getMethod("compute", String.class).getDeclaringClass().getMethod("compute", String.class);. That might result in m1.equals(m2) returning true.
I just had a bug with code doing this, I took name, genericReturnType and genericParameterTypes, and I had problems when working with proxies because I lost the generic information.
I switched to use name, returnType and parameterTypes and it works fine...
I also considered using declaringClass but I removed it and don't remember exactly how I did this...

Callback functions in Java

Is there a way to pass a call back function in a Java method?
The behavior I'm trying to mimic is a .Net Delegate being passed to a function.
I've seen people suggesting creating a separate object but that seems overkill, however I am aware that sometimes overkill is the only way to do things.
If you mean somthing like .NET anonymous delegate, I think Java's anonymous class can be used as well.
public class Main {
public interface Visitor{
int doJob(int a, int b);
}
public static void main(String[] args) {
Visitor adder = new Visitor(){
public int doJob(int a, int b) {
return a + b;
}
};
Visitor multiplier = new Visitor(){
public int doJob(int a, int b) {
return a*b;
}
};
System.out.println(adder.doJob(10, 20));
System.out.println(multiplier.doJob(10, 20));
}
}
Since Java 8, there are lambda and method references:
Oracle Docs: Lambda Expressions
Oracle Docs: Method References
For example, if you want a functional interface A -> B, you can use:
import java.util.function.Function;
public MyClass {
public static String applyFunction(String name, Function<String,String> function){
return function.apply(name);
}
}
And here is how you can call it:
MyClass.applyFunction("42", str -> "the answer is: " + str);
// returns "the answer is: 42"
Also you can pass class method. For example:
#Value // lombok
public class PrefixAppender {
private String prefix;
public String addPrefix(String suffix){
return prefix +":"+suffix;
}
}
Then you can do:
PrefixAppender prefixAppender= new PrefixAppender("prefix");
MyClass.applyFunction("some text", prefixAppender::addPrefix);
// returns "prefix:some text"
Note:
Here I used the functional interface Function<A,B>, but there are many others in the package java.util.function. Most notable ones are
Supplier: void -> A
Consumer: A -> void
BiConsumer: (A,B) -> void
Function: A -> B
BiFunction: (A,B) -> C
and many others that specialize on some of the input/output type. Then, if it doesn't provide the one you need, you can create your own FunctionalInterface:
#FunctionalInterface
interface Function3<In1, In2, In3, Out> { // (In1,In2,In3) -> Out
public Out apply(In1 in1, In2 in2, In3 in3);
}
Example of use:
String computeAnswer(Function3<String, Integer, Integer, String> f){
return f.apply("6x9=", 6, 9);
}
computeAnswer((question, a, b) -> question + "42");
// "6*9=42"
And you can also do that with thrown exception:
#FunctionalInterface
interface FallibleFunction<In, Out, Ex extends Exception> {
Out get(In input) throws Ex;
}
public <Ex extends IOException> String yo(FallibleFunction<Integer, String, Ex> f) throws Ex {
return f.get(42);
}
For simplicity, you can use a Runnable:
private void runCallback(Runnable callback)
{
// Run callback
callback.run();
}
Usage:
runCallback(new Runnable()
{
#Override
public void run()
{
// Running callback
}
});
or with Java8 lambdas
runCallback(() -> {
// Running callback
});
yet i see there is most preferred way which was what i was looking for.. it's basically derived from these answers but i had to manipulate it to more more redundant and efficient.. and i think everybody looking for what i come up with
To the point::
first make an Interface that simple
public interface myCallback {
void onSuccess();
void onError(String err);
}
now to make this callback run when ever you wish to do to handle the results - more likely after async call and you wanna run some stuff which depends on these reuslts
// import the Interface class here
public class App {
public static void main(String[] args) {
// call your method
doSomething("list your Params", new myCallback(){
#Override
public void onSuccess() {
// no errors
System.out.println("Done");
}
#Override
public void onError(String err) {
// error happen
System.out.println(err);
}
});
}
private void doSomething(String param, // some params..
myCallback callback) {
// now call onSuccess whenever you want if results are ready
if(results_success)
callback.onSuccess();
else
callback.onError(someError);
}
}
doSomething is the function that takes some time you wanna add a callback to it to notify you when the results came, add the call back interface as a parameter to this method
hope my point is clear, enjoy ;)
A little nitpicking:
I've seem people suggesting creating a
separate object but that seems
overkill
Passing a callback includes creating a separate object in pretty much any OO language, so it can hardly be considered overkill. What you probably mean is that in Java, it requires you to create a separate class, which is more verbose (and more resource-intensive) than in languages with explicit first-class functions or closures. However, anonymous classes at least reduce the verbosity and can be used inline.
This is very easy in Java 8 with lambdas.
public interface Callback {
void callback();
}
public class Main {
public static void main(String[] args) {
methodThatExpectsACallback(() -> System.out.println("I am the callback."));
}
private static void methodThatExpectsACallback(Callback callback){
System.out.println("I am the method.");
callback.callback();
}
}
I found the idea of implementing using the reflect library interesting and came up with this which I think works quite well. The only down side is losing the compile time check that you are passing valid parameters.
public class CallBack {
private String methodName;
private Object scope;
public CallBack(Object scope, String methodName) {
this.methodName = methodName;
this.scope = scope;
}
public Object invoke(Object... parameters) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
Method method = scope.getClass().getMethod(methodName, getParameterClasses(parameters));
return method.invoke(scope, parameters);
}
private Class[] getParameterClasses(Object... parameters) {
Class[] classes = new Class[parameters.length];
for (int i=0; i < classes.length; i++) {
classes[i] = parameters[i].getClass();
}
return classes;
}
}
You use it like this
public class CallBackTest {
#Test
public void testCallBack() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
TestClass testClass = new TestClass();
CallBack callBack = new CallBack(testClass, "hello");
callBack.invoke();
callBack.invoke("Fred");
}
public class TestClass {
public void hello() {
System.out.println("Hello World");
}
public void hello(String name) {
System.out.println("Hello " + name);
}
}
}
A method is not (yet) a first-class object in Java; you can't pass a function pointer as a callback. Instead, create an object (which usually implements an interface) that contains the method you need and pass that.
Proposals for closures in Java—which would provide the behavior you are looking for—have been made, but none will be included in the upcoming Java 7 release.
When I need this kind of functionality in Java, I usually use the Observer pattern. It does imply an extra object, but I think it's a clean way to go, and is a widely understood pattern, which helps with code readability.
Check the closures how they have been implemented in the lambdaj library. They actually have a behavior very similar to C# delegates:
http://code.google.com/p/lambdaj/wiki/Closures
You also can do theCallback using the Delegate pattern:
Callback.java
public interface Callback {
void onItemSelected(int position);
}
PagerActivity.java
public class PagerActivity implements Callback {
CustomPagerAdapter mPagerAdapter;
public PagerActivity() {
mPagerAdapter = new CustomPagerAdapter(this);
}
#Override
public void onItemSelected(int position) {
// Do something
System.out.println("Item " + postion + " selected")
}
}
CustomPagerAdapter.java
public class CustomPagerAdapter {
private static final int DEFAULT_POSITION = 1;
public CustomPagerAdapter(Callback callback) {
callback.onItemSelected(DEFAULT_POSITION);
}
}
I tried using java.lang.reflect to implement 'callback', here's a sample:
package StackOverflowQ443708_JavaCallBackTest;
import java.lang.reflect.*;
import java.util.concurrent.*;
class MyTimer
{
ExecutorService EXE =
//Executors.newCachedThreadPool ();
Executors.newSingleThreadExecutor ();
public static void PrintLine ()
{
System.out.println ("--------------------------------------------------------------------------------");
}
public void SetTimer (final int timeout, final Object obj, final String methodName, final Object... args)
{
SetTimer (timeout, obj, false, methodName, args);
}
public void SetTimer (final int timeout, final Object obj, final boolean isStatic, final String methodName, final Object... args)
{
Class<?>[] argTypes = null;
if (args != null)
{
argTypes = new Class<?> [args.length];
for (int i=0; i<args.length; i++)
{
argTypes[i] = args[i].getClass ();
}
}
SetTimer (timeout, obj, isStatic, methodName, argTypes, args);
}
public void SetTimer (final int timeout, final Object obj, final String methodName, final Class<?>[] argTypes, final Object... args)
{
SetTimer (timeout, obj, false, methodName, argTypes, args);
}
public void SetTimer (final int timeout, final Object obj, final boolean isStatic, final String methodName, final Class<?>[] argTypes, final Object... args)
{
EXE.execute (
new Runnable()
{
public void run ()
{
Class<?> c;
Method method;
try
{
if (isStatic) c = (Class<?>)obj;
else c = obj.getClass ();
System.out.println ("Wait for " + timeout + " seconds to invoke " + c.getSimpleName () + "::[" + methodName + "]");
TimeUnit.SECONDS.sleep (timeout);
System.out.println ();
System.out.println ("invoking " + c.getSimpleName () + "::[" + methodName + "]...");
PrintLine ();
method = c.getDeclaredMethod (methodName, argTypes);
method.invoke (obj, args);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
PrintLine ();
}
}
}
);
}
public void ShutdownTimer ()
{
EXE.shutdown ();
}
}
public class CallBackTest
{
public void onUserTimeout ()
{
System.out.println ("onUserTimeout");
}
public void onTestEnd ()
{
System.out.println ("onTestEnd");
}
public void NullParameterTest (String sParam, int iParam)
{
System.out.println ("NullParameterTest: String parameter=" + sParam + ", int parameter=" + iParam);
}
public static void main (String[] args)
{
CallBackTest test = new CallBackTest ();
MyTimer timer = new MyTimer ();
timer.SetTimer ((int)(Math.random ()*10), test, "onUserTimeout");
timer.SetTimer ((int)(Math.random ()*10), test, "onTestEnd");
timer.SetTimer ((int)(Math.random ()*10), test, "A-Method-Which-Is-Not-Exists"); // java.lang.NoSuchMethodException
timer.SetTimer ((int)(Math.random ()*10), System.out, "println", "this is an argument of System.out.println() which is called by timer");
timer.SetTimer ((int)(Math.random ()*10), System.class, true, "currentTimeMillis");
timer.SetTimer ((int)(Math.random ()*10), System.class, true, "currentTimeMillis", "Should-Not-Pass-Arguments"); // java.lang.NoSuchMethodException
timer.SetTimer ((int)(Math.random ()*10), String.class, true, "format", "%d %X", 100, 200); // java.lang.NoSuchMethodException
timer.SetTimer ((int)(Math.random ()*10), String.class, true, "format", "%d %X", new Object[]{100, 200});
timer.SetTimer ((int)(Math.random ()*10), test, "NullParameterTest", new Class<?>[]{String.class, int.class}, null, 888);
timer.ShutdownTimer ();
}
}
I've recently started doing something like this:
public class Main {
#FunctionalInterface
public interface NotDotNetDelegate {
int doSomething(int a, int b);
}
public static void main(String[] args) {
// in java 8 (lambdas):
System.out.println(functionThatTakesDelegate((a, b) -> {return a*b;} , 10, 20));
}
public static int functionThatTakesDelegate(NotDotNetDelegate del, int a, int b) {
// ...
return del.doSomething(a, b);
}
}
it's a bit old, but nevertheless... I found the answer of Peter Wilkinson nice except for the fact that it does not work for primitive types like int/Integer.
The problem is the .getClass() for the parameters[i], which returns for instance java.lang.Integer, which on the other hand will not be correctly interpreted by getMethod(methodName,parameters[]) (Java's fault) ...
I combined it with the suggestion of Daniel Spiewak (in his answer to this); steps to success included: catching NoSuchMethodException -> getMethods() -> looking for the matching one by method.getName() -> and then explicitly looping through the list of parameters and applying Daniels solution, such identifying the type matches and the signature matches.
with java 8 this task is kinda easy, if you want to use callback in multi-thread scenario you can do something similar like the following:
public void methodA (int n, IntConsumer consumer) {
// create a thread
Thread t = new Thread(() -> {
// some time consuming operation
int result = IntStream.range(0, n).sum();
// after the result is ready do something with it.
consumer.accept(result);
});
t.start();
}
and to use this method do:
methodA(1000000, System.out::println);
public class HelloWorldAnonymousClasses {
//this is an interface with only one method
interface HelloWorld {
public void printSomething(String something);
}
//this is a simple function called from main()
public void sayHello() {
//this is an object with interface reference followed by the definition of the interface itself
new HelloWorld() {
public void printSomething(String something) {
System.out.println("Hello " + something);
}
}.printSomething("Abhi");
//imagine this as an object which is calling the function'printSomething()"
}
public static void main(String... args) {
HelloWorldAnonymousClasses myApp =
new HelloWorldAnonymousClasses();
myApp.sayHello();
}
}
//Output is "Hello Abhi"
Basically if you want to make the object of an interface it is
not possible, because interface cannot have objects.
The option is to let some class implement the interface and then call that function using the object of that class.
But this approach is really verbose.
Alternatively, write new HelloWorld() (*oberserve this is an interface not a class) and then follow it up with the defination of the interface methods itself. (*This defination is in reality the anonymous class).
Then you get the object reference through which you can call the method itself.
Create an Interface, and Create the Same Interface Property in Callback Class.
interface dataFetchDelegate {
void didFetchdata(String data);
}
//callback class
public class BackendManager{
public dataFetchDelegate Delegate;
public void getData() {
//Do something, Http calls/ Any other work
Delegate.didFetchdata("this is callbackdata");
}
}
Now in the class where you want to get called back implement the above Created Interface.
and Also Pass "this" Object/Reference of your class to be called back.
public class Main implements dataFetchDelegate
{
public static void main( String[] args )
{
new Main().getDatafromBackend();
}
public void getDatafromBackend() {
BackendManager inc = new BackendManager();
//Pass this object as reference.in this Scenario this is Main Object
inc.Delegate = this;
//make call
inc.getData();
}
//This method is called after task/Code Completion
public void didFetchdata(String callbackData) {
// TODO Auto-generated method stub
System.out.println(callbackData);
}
}
Simpliest and easiest way is by creating a reusable model and trigger.... https://onecompiler.com/java/3wejrcby2?fbclid=IwAR0dHbGDChRUJoCZ3CIDW-JQu7Dz3iYGNGYjxYVCPCWfEqQDogFGTwuOuO8

Categories