Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Reflection: Create an implementing class

Class someInterface = Class.fromName("some.package.SomeInterface"); 

How do I now create a new class that implements someInterface?

I need to create a new class, and pass it to a function that needs a SomeInterface as an argument.

like image 325
Isaac Waller Avatar asked Jul 04 '09 19:07

Isaac Waller


People also ask

How do you create a class object using reflection?

We can use newInstance() method on the constructor object to instantiate a new instance of the class. Since we use reflection when we don't have the classes information at compile time, we can assign it to Object and then further use reflection to access it's fields and invoke it's methods.

How is Java reflection implemented?

The main entry point of any Reflection activity is the Class class. From it you can get Method , Field , Class , Constructor , and Annotation instances. The implementation is done in native C code (and/or C++). Each JDK might differ, but you can look up the source code if it's available and you have patience.

What is reflection class in Java?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.


1 Answers

Easily, java.lang.reflect.Proxy to the rescue!

Full working example:

interface IRobot {      String Name();      String Name(String title);      void Talk();      void Talk(String stuff);      void Talk(int stuff);      void Talk(String stuff, int more_stuff);      void Talk(int stuff, int more_stuff);      void Talk(int stuff, String more_stuff); }  public class ProxyTest {     public static void main(String args[]) {         IRobot robot = (IRobot) java.lang.reflect.Proxy.newProxyInstance(                 IRobot.class.getClassLoader(),                 new java.lang.Class[] { IRobot.class },                 new java.lang.reflect.InvocationHandler() {              @Override             public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws java.lang.Throwable {                 String method_name = method.getName();                 Class<?>[] classes = method.getParameterTypes();                  if (method_name.equals("Name")) {                     if (args == null) {                         return "Mr IRobot";                     } else {                         return args[0] + " IRobot";                     }                 } else if (method_name.equals("Talk")) {                     switch (classes.length) {                         case 0:                             System.out.println("Hello");                             break;                         case 1:                             if (classes[0] == int.class) {                                 System.out.println("Hi. Int: " + args[0]);                             } else {                                 System.out.println("Hi. String: " + args[0]);                             }                             break;                         case 2:                             if (classes[0] == String.class) {                                 System.out.println("Hi. String: " + args[0] + ". Int: " + args[1]);                             } else {                                 if (classes[1] == String.class) {                                     System.out.println("Hi. int: " + args[0] + ". String: " + args[1]);                                 } else {                                     System.out.println("Hi. int: " + args[0] + ". Int: " + args[1]);                                 }                             }                             break;                     }                 }                 return null;             }         });          System.out.println(robot.Name());         System.out.println(robot.Name("Dr"));         robot.Talk();         robot.Talk("stuff");         robot.Talk(100);         robot.Talk("stuff", 200);         robot.Talk(300, 400);         robot.Talk(500, "stuff");     } } 
like image 69
Pacerier Avatar answered Sep 23 '22 23:09

Pacerier