1

My project contains this package : com.X.Y.Z.controller

This package includes 3 files

ControllerA.java

 public class ControllerA {
    public static void insert(Context context, ModelA model) {/*do somethings*/}
  }

ControllerB.java

 public class ControllerB {
    public static void insert(Context context, ModelB model) {/*do somethings*/}
  }

MainController.java

I use following code to invoke insert method from Controller A or B it depends on some condition

public static void insert(Context context, Object object) {
  Class<?> clazz = Class.forName(mClass); //Controller A or B
  Method method = clazz.getMethod("insert", ?);
  method.invoke(null, ?);
}

how do i pass arguments ? object may be ModelA or ModelB

I apologize if my wording is not true

5
  • invoke(Object obj, Object... args) takes as arguments, the object the underlying method is invoked from, and the arguments used for the method call Commented Mar 1, 2017 at 6:57
  • 1
    You need to call clazz.getMethod("insert", Context.class, CCC) where CCC is either ModelA.class or ModelB.class. But I don't see where you have a way to get the Model class. Will this be passed as a parameter to insert, or will there be some other way to obtain it, or do you want to search the class for methods named insert and figure out the Model class by looking at the method parameters? Commented Mar 1, 2017 at 6:58
  • 1
    If ModelA and ModelB implement the same interface, for example Model, you can pass Context.class and Model.class as the 2nd and 3rd parameter of getMethod. Commented Mar 1, 2017 at 7:36
  • @ajb ModelA or ModelB Will this be passed as a parameter to insert Commented Mar 1, 2017 at 8:27
  • Thank you @Howard Wang ,I think this is the best way. But I am still looking for a better way Commented Mar 1, 2017 at 8:30

2 Answers 2

3

You pass the classes as varargs in the method lookup and the instance, which is null for a static invocation and the arguments in the invocation:

boolean useA = true; // use A or B variant:  
Method m = clazz.getMethod("insert", Context.class, useA ? ModelA.class : ModelB.class);  
m.invoke(null, context, object)
Sign up to request clarification or add additional context in comments.

1 Comment

I think he wants to use object as the last parameter to the invoked insert. There aren't any variables named modelA or modelB. But invoke takes a list of Objects, so you can just pass object as the last parameter and let invoke() worry about converting it.
0

Problem solved:)

ModelA and ModelB implement the same interface, for example Model (not important) ,model.getClass() is an important part of his

public static void insert(Context context, Model model) {
  Class<?> clazz = Class.forName(mClass); //Controller A or B
  Method method = clazz.getMethod("insert", Context.class, model.getClass());
  method.invoke(null, new Object[]{context, model}));
}

Now i can use this :

Controller.insert(this, myModelA);
Controller.insert(this, myModelB);

Thanks all.

1 Comment

The invoke() method is defined to take Object... as its arguments, so you don't need to create the array yourself, although it will work. method.invoke(null, context, model) should be equivalent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.