List of usage examples for java.lang.reflect Method invoke
@CallerSensitive @ForceInline @HotSpotIntrinsicCandidate public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
From source file:Main.java
public static void main(String[] args) throws Exception { Class computerClass = MyClass.class; Method[] methods = computerClass.getDeclaredMethods(); MyClass computer = new MyClass(); for (Method method : methods) { Object result = method.invoke(computer, new Object[0]); System.out.println(method.getName() + ": " + result); }//from ww w . j ava 2s. c om }
From source file:MainClass.java
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Method m = PrintStream.class.getDeclaredMethod("println", String.class); m.invoke(System.out, "Hello, World!"); }
From source file:MyClass.java
public static void main(String[] args) { Class<MyClass> myClass = MyClass.class; try {//from w w w. j a v a2 s. co m MyClass p = myClass.newInstance(); Method setName = myClass.getMethod("setName", String.class); setName.invoke(p, "abc"); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) { System.out.println(e.getMessage()); } }
From source file:Main.java
public static void main(String... args) throws Exception { Main t = new Main(); Method m = Main.class.getMethod("methodName", int.class); String returnVal = (String) m.invoke(t, 5); System.out.println(returnVal); }
From source file:Main.java
public static void main(String args[]) throws Exception { String name = "java.lang.String"; String methodName = "toLowerCase"; Class cl = Class.forName(name); java.lang.reflect.Constructor constructor = cl.getConstructor(new Class[] { String.class }); Object invoker = constructor.newInstance(new Object[] { "AAA" }); Class arguments[] = new Class[] {}; java.lang.reflect.Method objMethod = cl.getMethod(methodName, arguments); Object result = objMethod.invoke(invoker, (Object[]) arguments); System.out.println(result);//from w ww . j a v a2 s.c o m }
From source file:GetMethod.java
public static void main(String[] args) { Method[] ms = String.class.getMethods(); for (int i = 0; i < ms.length; i++) { // System.out.println(ms[i].getName()); }//w w w . j ava2s .c o m Method[] ims = XMLReader.class.getMethods(); for (int i = 0; i < ims.length; i++) { // System.out.println(ims[i].getName()); } Method[] sms = Size.class.getMethods(); for (int i = 0; i < sms.length; i++) { // System.out.println(sms[i].getName()); } String pc = new String(); try { Method setCpu = String.class.getMethod("toString", String.class); setCpu.invoke(pc, "Intel 3.0"); System.out.println(pc.toString()); } catch (Exception e) { e.printStackTrace(); } }
From source file:MethodTroubleReturns.java
public static void main(String... args) { try {/*from w w w . j av a2 s. com*/ MethodTroubleReturns mtr = new MethodTroubleReturns(); Class<?> c = mtr.getClass(); Method m = c.getDeclaredMethod("drinkMe", int.class); m.invoke(mtr, -1); // production code should handle these exceptions more gracefully } catch (InvocationTargetException x) { Throwable cause = x.getCause(); System.err.format("drinkMe() failed: %s%n", cause.getMessage()); } catch (Exception x) { x.printStackTrace(); } }
From source file:X.java
public static void main(String[] args) { try {/* w w w . j a v a2s . c om*/ Class<?> clazz = Class.forName("X"); X x = (X) clazz.newInstance(); Class[] argTypes = { String.class }; Method method = clazz.getMethod("objectMethod", argTypes); Object[] data = { "Hello" }; method.invoke(x, data); // Output: Instance method: Hello method = clazz.getMethod("classMethod", (Class<?>[]) null); method.invoke(null, (Object[]) null); // Output: Class method } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { Class c = Class.forName("MyClass"); Method m = c.getDeclaredMethod("say", new Class[] { String.class, String.class }); Object i = c.newInstance();/*from w w w . ja v a2 s.com*/ Object r = m.invoke(i, new Object[] { "Hello", "World" }); }
From source file:MyClassLoader.java
static public void main(String args[]) throws Exception { MyClassLoader x = new MyClassLoader(); Class c = x.loadClass(args[0]); Class getArg1[] = { (new String[1]).getClass() }; Method m = c.getMethod("main", getArg1); String[] my1 = { "arg1 passed", "arg2 passed" }; Object myarg1[] = { my1 };/*from w w w . ja v a2 s .co m*/ m.invoke(null, myarg1); }