Explain how to use Java reflection to construct a generic dispatcher. Give Java code for a dispatcher whose signature is:

```
public void dispatch(Object target, Method aMethod, byte[] args)
```
The arguments supply the target object, the method to be invoked and the arguments for that method in an array of bytes.


Use the class Method. To invoke a method supply the object to be invoked and an array of Object containing the arguments. The arguments supplied in an array of bytes must be converted to an array of Object.
```
public void dispatch(Object target, Method aMethod, byte[] args)
throws RemoteException {

Object[] arguments = // extract arguments from array of bytes
try{
aMethod.invoke(target, arguments);
catch(...){}
}
```

Computer Science & Information Technology

You might also like to view...

Case-based Critical Thinking QuestionsCase 12-1Casey is using XML to store information about the students in the science classes that he teaches. He wants to design a DTD that he can use to validate the XML documents that he uses for this purpose, and he comes to you for help. Casey next wants to write a declaration for an element named "advanced" that he will use to record the fact that a student is advanced. This element will not contain any content. Which of the following is an appropriate element declaration for this element?

A. B. C. D.

Computer Science & Information Technology

Analyze the following code.

``` // Program 1 public class Test { public static void main(String[] args) { Object a1 = new A(); Object a2 = new A(); System.out.println(((A)a1).equals((A)a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } // Program 2 public class Test { public static void main(String[] args) { A a1 = new A(); A a2 = new A(); System.out.println(a1.equals(a2)); } } class A { int x; public boolean equals(A a) { return this.x == a.x; } } ``` a. Program 1 displays true and Program 2 displays true b. Program 1 displays false and Program 2 displays true c. Program 1 displays true and Program 2 displays false d. Program 1 displays false and Program 2 displays false

Computer Science & Information Technology

What is a rootkit and why is it so difficult to detect?

What will be an ideal response?

Computer Science & Information Technology

Write a program that requests the user to enter two numbers and prints the sum, product, difference and quotient of the two numbers.

What will be an ideal response?

Computer Science & Information Technology