×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Mario Wereka
Added: Jun 7, 2016 5:29 PM
Views: 8
invoca un metodo de una clase sabiendo su nombre
  1.  
  2.  
  3. For those who want a straight-forward code example in Java 7:
  4.  
  5. Dog class:
  6.  
  7. package com.mypackage.bean;
  8.  
  9. public class Dog {
  10.     private String name;
  11.     private int age;
  12.  
  13.     public Dog() {
  14.         // empty constructor
  15.     }
  16.  
  17.     public Dog(String name, int age) {
  18.         this.name = name;
  19.         this.age = age;
  20.     }
  21.  
  22.     public String getName() {
  23.         return name;
  24.     }
  25.  
  26.     public void setName(String name) {
  27.         this.name = name;
  28.     }
  29.  
  30.     public int getAge() {
  31.         return age;
  32.     }
  33.  
  34.     public void setAge(int age) {
  35.         this.age = age;
  36.     }
  37.  
  38.     public void printDog(String name, int age) {
  39.         System.out.println(name + " is " + age + " year(s) old.");
  40.     }
  41. }
  42.  
  43. ReflectionDemo class:
  44.  
  45. package com.mypackage.demo;
  46.  
  47. import java.lang.reflect.*;
  48.  
  49. public class ReflectionDemo {
  50.  
  51.     public static void main(String[] args) throws Exception {
  52.         String dogClassName = "com.mypackage.bean.Dog";
  53.         Class<?> dogClass = Class.forName(dogClassName); // convert string classname to class
  54.         Object dog = dogClass.newInstance(); // invoke empty constructor
  55.  
  56.         String methodName = "";
  57.  
  58.         // with single parameter, return void
  59.         methodName = "setName";
  60.         Method setNameMethod = dog.getClass().getMethod(methodName, String.class);
  61.         setNameMethod.invoke(dog, "Mishka"); // pass arg
  62.  
  63.         // without parameters, return string
  64.         methodName = "getName";
  65.         Method getNameMethod = dog.getClass().getMethod(methodName);
  66.         String name = (String) getNameMethod.invoke(dog); // explicit cast
  67.  
  68.         // with multiple parameters
  69.         methodName = "printDog";
  70.         Class<?>[] paramTypes = {String.class, int.class};
  71.         Method printDogMethod = dog.getClass().getMethod(methodName, paramTypes);
  72.         printDogMethod.invoke(dog, name, 3); // pass args
  73.     }
  74. }
  75.  
  76. Output: Mishka is 3 year(s) old.
  77.  
  78. You can invoke the constructor with parameters this way:
  79.  
  80. Constructor<?> dogConstructor = dogClass.getConstructor(String.class, int.class);
  81. Object dog = dogConstructor.newInstance("Hachiko", 10);
  82.  
  83. Alternatively, you can remove
  84.  
  85. String dogClassName = "com.mypackage.bean.Dog";
  86. Class<?> dogClass = Class.forName(dogClassName);
  87. Object dog = dogClass.newInstance();
  88.  
  89. and do
  90.  
  91. Dog dog = new Dog();
  92.  
  93. Method method = Dog.class.getMethod(methodName, ...);
  94. method.invoke(dog, ...);
  95.  
  96. Suggested reading: Creating New Class Instances
  97.  
Comments disabled