package fabricadeobjetos; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Marcelo Gomes de Paula * @email marcelogomesrp@gmail.com * @version 0.1 */ public class FabricaObjetos { private String classeName = "fileutils.Contato"; private Object objeto = null; private Class classe; public FabricaObjetos(String classeName) { this.classeName = classeName; try { //objeto = new Contato(); classe = Class.forName(classeName); objeto = classe.newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) { Logger.getLogger(FabricaObjetos.class.getName()).log(Level.SEVERE, null, ex); } } public void addStringValue(String nomeMetodo, String valor){ try { Method method = classe.getMethod(nomeMetodo, String.class ); Object arglist[] = new Object[1]; arglist[0] = valor; //Object retobj = method.invoke(objeto, arglist); method.invoke(objeto, arglist); } catch (IllegalAccessException ex) { Logger.getLogger(FabricaObjetos.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(FabricaObjetos.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(FabricaObjetos.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException | SecurityException ex) { Logger.getLogger(FabricaObjetos.class.getName()).log(Level.SEVERE, null, ex); } } public void addIntegerValue(String nomeMetodo, Integer valor){ try { Method method = classe.getMethod(nomeMetodo, Integer.class ); Object arglist[] = new Object[1]; arglist[0] = valor; //Object retobj = method.invoke(objeto, arglist); method.invoke(objeto, arglist); } catch (IllegalAccessException ex) { Logger.getLogger(FabricaObjetos.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { Logger.getLogger(FabricaObjetos.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(FabricaObjetos.class.getName()).log(Level.SEVERE, null, ex); } catch (NoSuchMethodException | SecurityException ex) { Logger.getLogger(FabricaObjetos.class.getName()).log(Level.SEVERE, null, ex); } } public Object getObjeto() { return objeto; } }
|
Nenhum comentário:
Postar um comentário