How can I write code below in java version?
I have read similar questions, but they are confusing, they answered that java didn't have delegate feature like c# had, in other hand they answered with their delegate implementation in java, but nothing is similar with my condition.
I really hope it's clear on this question.
I have been getting stuck since a week
class Action
{
public delegate void ActionDelegate();
public static ActionDelegate OnAction;
public void DoAction()
{
Console.WriteLine("Action A");
if (!ReferenceEquals(OnAction, null))
OnAction();
}
}
class TaskA
{
public TaskA()
{
Action.OnAction += DoTaskA;
}
private void DoTaskA()
{
Console.WriteLine("Do Task A");
}
}
class TaskB
{
public TaskB()
{
Action.OnAction += DoTaskB;
}
private void DoTaskB()
{
Console.WriteLine("Do Task B");
}
}
class Program
{
static void Main(string[] args)
{
TaskA taskA = new TaskA();
TaskB task = new TaskB();
Action action = new Action();
action.DoAction();
}
}
Output:
Action A
Do Task A
Do Task B
Press any keys to continue...
something similar in java will be to use an interface
you can get the following results
Action A
Do Task A
Do Task B
with the codes below.
import java.util.ArrayList;
public class HelloWorld{
public static void main(String []args){
TaskA taskA = new TaskA();
TaskB task = new TaskB();
Action action = new Action();
action.doAction();
}
}
interface ActionDelegate {
void doAction();
}
class Action{
static public ArrayList<ActionDelegate> onAction = new ArrayList<>();
public void doAction(){
System.out.println("Action A");
for(ActionDelegate ad: onAction){
ad.doAction();
}
}
}
class TaskA implements ActionDelegate{
TaskA(){
Action.onAction.add(this);
}
public void doAction(){
System.out.println("Do Task A");
}
}
class TaskB implements ActionDelegate{
TaskB(){
Action.onAction.add(this);
}
public void doAction(){
System.out.println("Do Task B");
}
}
With the project safety-mirror on your classpath your get library support for delegates and events.
<dependency>
<groupId>com.github.hervian</groupId>
<artifactId>safety-mirror</artifactId>
<version>3.0.0</version>
</dependency>
Here's a snippet from the project's README:
Cheat sheet of features
Fun and friends: No more functional interfaces:
Fun.With0Params<String> myFunctionField = " hello world "::trim;
Fun.With2Params<Boolean, Object, Object> equals = Objects::equals;
public void foo(Fun.With1ParamAndVoid<String> printer) throws Exception {
printer.invoke("hello world);
}
foo(System.out::println); //This signature match the the Fun defined by method Foo. If it did not, the compiler would emit an error.
It is all type safe: you will get compile time errors if the Method Reference's signature does not match what is defined by the Fun subclass.
Method m1 = Fun.toMethod(String::isEmpty)
Method m2 = Fun.<String>toMethod(Class::forName)); // to get overloaded method you must specify parameters in generics
assertEquals("isEmpty", Fun.getName(String::isEmpty)); //use Fun's static getName method to get the method name. The Method objects returned from toMethod will not return the correct String.
Delegates in Java!
Delegate.With1Param<String, String> greetingsDelegate = new Delegate.With1Param<>();
greetingsDelegate.add(str -> "Hello " + str);
greetingsDelegate.add(str -> "Goodbye " + str);
DelegateInvocationResult<String> invocationResult = greetingsDelegate.invokeAndAggregateExceptions("Sir");
invocationResult.getFunctionInvocationResults().forEach(funInvRes -> System.out.println(funInvRes.getResult()));
//prints: "Hello sir" and "Goodbye Sir"
Events
//Create a private Delegate. Make sure it is private so only *you* can invoke it.
private static Delegate.With0Params<String> trimDelegate = new Delegate.With0Params<>();
//Create a public Event using the delegate you just created.
public static Event.With0Params<String> trimEvent= new Event.With0Params<>(trimDelegate);
Type safe method creation
Method m1 = Fun.toMethod(Thread::isAlive) // Get final method
Method m2 = Fun.toMethod(String::isEmpty); // Get method from final class
Method m3 = Fun.toMethod(BufferedReader::readLine); // Get method that throws checked exception
Method m4 = Fun.<String, Class[]>toMethod(getClass()::getDeclaredMethod); //to get vararg method you must specify parameters in generics
Method m5 = Fun.<String>toMethod(Class::forName); // to get overloaded method you must specify parameters in generics
Method m6 = Fun.toMethod(this::toString); //Works with inherited methods
Disclaimer: I am the author of the project.
Related
I am trying to access a method using reflection and one of the parameter is a callback. The callback type is generic interface of different class type. These classes are #SystemApi can be accessed by reflection. Here is the class I am working with above sayings.
Below is my sample code :
String sClassName = "android.telephony.euicc.EuiccCardManager";
Class classToInvestigate = Class.forName(sClassName);
Class interfaceclass = classToInvestigate.getClasses()[0]; //android.telephony.euicc.EuiccCardManager.ResultCallback
Method getallprofiles = classToInvestigate.getDeclaredMethod("requestAllProfiles", String.class,Executor.class, interfaceclass);
getallprofiles.invoke(null,getEid(), AsyncTask.THREAD_POOL_EXECUTOR,null);
In the above invoke signature as a last parameter I need to pass the callback created using reflection and which is equivalent or similar to the below callback sample.
ResultCallback<EuiccProfileInfo[]> callback =
new ResultCallback<EuiccProfileInfo[]>() {
#Override
public void onComplete(int resultCode, EuiccProfileInfo[] result) { }
};
The above ResultCallback interface can be found in the same class link I provided above and the same instance in interfaceclass field above.
EuiccProfileInfo is the another class need to access from reflection because it is #SystemApi
I have failed/stuck to translate the above callback logic with reflection.Anyone can help me on this?
Seems that you need to create a proxy class:
// that class you don't have access too. imagine that it is not here, it is just to show example signature of method we want to run.
interface Callback {
void run();
}
public static void main(String[] args) throws Exception {
Class<?> callbackClass = Class.forName("package.Callback");
Callback callback = Proxy.newProxyInstance(Main.class.getClassLoader(), new Class[]{callbackClass}, new CallbackInvocationHandler(() -> {
System.out.println("callback");
}));
callbackClass.getMethod("run").invoke(callback); // works!
}
static class CallbackInvocationHandler implements InvocationHandler {
private final Runnable myCallback;
CallbackInvocationHandler(Runnable myCallback) {
this.myCallback = myCallback;
}
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("toString") && (method.getParameterCount() == 0)) {
// just random implementation
return proxy.getClass() + "#" + Integer.toHexString(System.identityHashCode(proxy));
}
if (method.getName().equals("hashCode") && (method.getParameterCount() == 0)) {
return System.identityHashCode(proxy);
}
if (method.getName().equals("equals") && (method.getParameterCount() == 1) && (method.getParameterTypes()[0] == Object.class)) {
return proxy == args[0];
}
if (method.getName().equals("run") && (method.getParameterCount() == 0)) {
// do what you want.
myCallback.run();
return null;
}
throw new IllegalStateException("Method not implemented: " + method);
}
}
But if you really need to use something like that - there are huge chances you are doing something wrong, can't you just add dependency on that project? But you should not depend on system classes too.
Im beginner JAVA developer. Here is a method:
private Method getSomething()
{
for (Method m : getClass().getDeclaredMethods())
{
return m;
}
return notFound;
}
private void notFound()
{
throw new Exception();
}
it doesnt matter what it does - if it finds something, then returns a Method - if not, the notFound() method itself should be returned. So the hot spot is at the return notFound; line: if I use return notFound(); then it returns its value, not the method itself. I want something like a reference/pointer. So getSomething() returns something what can be called, and if the returned method is used wrong, it should trigger that Exception - so its not an option to replace return notFound; with throw new Exception(); !
Or the 2nd option is to create a lambda method....
You need to call
this.getClass().getMethod("notFound")
to get the notFound method of the current/this object's class.
So just do this:
return this.getClass().getMethod("notFound");
More details here:
Class.getMethod
EDIT:
You can retrieve i.e. get and call private methods too via reflection.
Here is an example.
import java.lang.reflect.Method;
public class Test001 {
public static void main(String[] args) throws Exception {
Test002 obj = new Test002();
Method m = obj.getClass().getDeclaredMethod("testMethod", int.class);
m.setAccessible(true);
m.invoke(obj, 10);
m.invoke(obj, 20);
System.out.println(m.getName());
}
}
class Test002 {
private void testMethod(int x){
System.out.println("Hello there: " + x);
}
}
You need to use reflection to achieve this:
http://docs.oracle.com/javase/tutorial/reflect/
e.g. to get all methods of a given class:
Class aClass = ...//obtain class object
Method[] methods = aClass.getMethods();
I would like to create an application which for example measures the execution time of a certain block of code. In this case it should have a structure like this:
public static long measureExecution(String code){
long start = System.nanoTime();
executeCode(code); // <----
long time = System.nanoTime() - start;
return time;
}
I'm curious about the method designated by the arrow, I need some sort of a placeholder. How should be this method implemented? Is it even possible to execute a custom Java code inside running Java application?
I was thinking that it can be done with some sort of overriding of another methods body, but I can't quite figure out how.
Thanks for your opinions!
You could pass a Runnable:
public static long measureExecution(Runnable code) {
long start = System.nanoTime();
code.run();
long time = System.nanoTime() - start;
return time;
}
At the place where you call the method, use an anonymous inner class to wrap the code you want to measure:
long time = measureExecution(new Runnable() {
#Override
public void run() {
System.out.println("Do something");
}
});
(If you were using Java 8, you could use a lambda expression instead of an anonymous inner class, which would make the code shorter and easier to read).
You can use OpenHFT/Java-Runtime-Compiler:
https://github.com/OpenHFT/Java-Runtime-Compiler
Also, you can use ToolProvider class (Compiler API), since java 1.6:
private Path compileJavaFile(Path javaFile, String className) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, javaFile.toFile().getAbsolutePath());
return javaFile.getParent().resolve(className);
}
You could use a Dynamic Proxy to wrap your methods invocation, here an example:
First you need to create InvocationHandler class:
public class MyInvocationHandler implements InvocationHandler {
private Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}
#Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("About to invoke " + method + "\n with argument " + args);
Object rv = method.invoke(target, args);
System.out.println(" Call returned " + rv);// here you could print the time instead
return rv;
}
}
Then Create a factory to get you object and Wrap it with the previous created proxy.
public class MyFactory {
public static MyInterface getMyInterface() {
MyInterface mc = new MyClass();
InvocationHandler h = new MyInvocationHandler(mc);
MyInterface mi = (MyInterface) Proxy.newProxyInstance(MyInterface.class.getClassLoader(),
new Class[] { MyInterface.class }, h);
return mi;
}
}
Hope that help you.
in the following code:
class Payment { }
class CashPayment extends Payment{ }
class Store{
public void acceptPayment (Payment p)
{System.out.println ("Store::Payment");}
}
class FastFoodStore extends Store {
public void acceptPayment (CashPayment c)
{System.out.println ("FastFoodStore::CashPayment");}
public void acceptPayment (Payment p)
{System.out.println ("FastFoodStore::Payment");}
}
//
public class Example {
public static void main(String [] args){
Store store1 = new Store();
FastFoodStore sandwitchPlace = new FastFoodStore ();
Store store2 = new FastFoodStore();
Payment p1 = new Payment();
CashPayment cp = new CashPayment();
Payment p2 = new CashPayment();
store1.acceptPayment (p1);
store1.acceptPayment (cp);
store1.acceptPayment (p2);
sandwitchPlace.acceptPayment (p1);
sandwitchPlace.acceptPayment (cp);
sandwitchPlace.acceptPayment (p2);
store2.acceptPayment (p1);
store2.acceptPayment (cp);
store2.acceptPayment (p2);
}
}
what I really don't understand is why
store2.acceptPayment (cp);
will display FastFoodStore::Payment but not FastFoodStore::CashPayment?
store2 will basically call the methods in FastFoodStore at runtime and pass a CashPayment type parameter. In that case, FastFoodStore::CashPayment is displayed.
Can someone please help?
There is a complicated division of effort between compile time and run time in deciding which method to call. See Method Invocation Expressions for the full story on this.
In your example, when the target expression is of type Store, the compiler will see only the Store acceptPayment method, which expects a Payment argument. That commits to calling a method that takes a Payment argument.
At run time, only the public void acceptPayment (Payment p) method in FastFoodStore, the class of the target object, is considered.
say I have three methods
method1()
method2()
method3()
and I have the user input a number corresponding to which method they want to run, is there a way to run it directly from their input? i.e. instead of having an if statement along the lines of
System.out.println("Which method would you like to run? 1/2/3");
String input = reader.readLine();
if(input == 1){method1();}
if(input == 2){method2();}
...
etc. and instead be able to have something like
System.out.println("Which method would you like to run? 1/2/3");
String input = reader.readLine();
method(input)();
?
Yes you could achieve that by using an interface as follows:
interface A {
void run();
}
public void method1() {}
public void method2() {}
public void mainMethod(String[] args) {
// Initialise the method map - note, you only have to do this once
// So, this initialisation code can go into a constructor
// And mothodMap can be declared as a final instance variable.
A methodOne = new A() { #Override public void run() { method1(); } };
A methodTwo = new A() { #Override public void run() { method2(); } };
Map<Integer, A> methodMap = new HashMap<>();
methodMap.put(1, methodOne);
methodMap.put(2, methodTwo);
Integer input = /* get it from user*/ 1;
A aMethod = methodMap.get(input);
aMethod.run();
}
No, not unless you use reflection. Java doesn't have function pointers, otherwise you could index to the appropriate function in an array. But what's wrong with if statements? They're more readable and secure..
If you're looking for a future-proof, more abstract solution, consider a strategy pattern:
// strategy
interface CommandMethod {
void runMethod();
}
// for every method 1 .. n
class CmdMethod1() implements CommandMethod {
void runMethod() {
// concrete implementation
}
}
// initialization ----------------
Map<String, CommandMethod> cmds = new HashMap<String, CommandMethod>();
cmds.put("1", new CmdMethod1());
// .. etc ..
cmds.put("n", new CmdMethodN());
// at the prompt:
System.out.println("Which method would you like to run? 1/2/3/.../n");
String input = reader.readLine();
cmds.get(input).runMethod(); // more like what you're going for ?
Not without having an if or switch statement (or reflection like paislee pointed out). If you wanted to do something like method(input); you would need the if/switch statement in another method:
....
String input = reader.readLine();
method(input);
}
private void method(int input) {
if (input == 1) {method1();}
if (input == 2) {method2();}
}
There's no way to do this. However, you can use if-statements or switch-case statements to make the "redirection" process less cumbersome. You might also consider creating a wrapper function to accept the user input to make your code cleaner.
The standard way of accomplishing this is to create a "functor"
public interface Functor
{
public void execute();
}
public class Method1 implements Functor
{
public void execute() { /* do something */ }
}
etc...
private Functor[] f = { new Method1(), new Method2(), new Method3() };
...
// Execute the method selected by i
f[i].execute();
Also take a look at the Callable interface
Just to update this. Another way this can be implemented is by using switch-case in a while true loop. And using scanner object static Scanner scanner = new Scanner(System.in); user input.
while(true){
System.out.print("Input: ");
int option = scanner.nextInt();
switch(option){
case 1 -> method1();
case 2 -> method2();
}
}
do remember to include a way to exit the while loop in any of the methods.