This shows you the differences between the selected revision and the current version of the page.
| android:loadit | android:loadit 2009/10/22 07:46 current | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | <html> | ||
| + | package edu.purdue.cs.jtk; | ||
| + | import java.lang.reflect.InvocationTargetException; | ||
| + | import java.lang.reflect.Method; | ||
| + | |||
| + | public class LoadIt { | ||
| + | |||
| + | public static void main(String[] args) { | ||
| + | try { | ||
| + | loadAndRun("Loadee"); | ||
| + | } catch (IllegalArgumentException e) { | ||
| + | e.printStackTrace(); | ||
| + | } catch (IllegalAccessException e) { | ||
| + | e.printStackTrace(); | ||
| + | } catch (InvocationTargetException e) { | ||
| + | e.printStackTrace(); | ||
| + | } catch (ClassNotFoundException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | static String loadAndRun(String name) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException { | ||
| + | Class<?> c = loadClass(name); | ||
| + | Object o = createObject(c); | ||
| + | String s = invokeMethod(c, o); | ||
| + | return s; | ||
| + | } | ||
| + | |||
| + | static Class<?> loadClass(String name) throws ClassNotFoundException { | ||
| + | Class<?> myClass = null; | ||
| + | myClass = Class.forName(name); | ||
| + | return myClass; | ||
| + | } | ||
| + | |||
| + | static Object createObject(Class<?> myClass) { | ||
| + | Object myObject = null; | ||
| + | try { | ||
| + | myObject = myClass.newInstance(); | ||
| + | } catch (Exception e) { e.printStackTrace(); } | ||
| + | return myObject; | ||
| + | } | ||
| + | |||
| + | static String invokeMethod(Class<?> myClass, Object myObject) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { | ||
| + | String myReturnValue = null; | ||
| + | |||
| + | Method[] myMethods = myClass.getDeclaredMethods(); | ||
| + | Method myMethod = myMethods[0]; | ||
| + | myMethod.setAccessible(true); | ||
| + | Object[] myArguments = {"hello test", 12}; | ||
| + | myReturnValue = (String) myMethod.invoke(myObject, (Object[]) myArguments); | ||
| + | |||
| + | return myReturnValue; | ||
| + | } | ||
| + | } | ||
| + | </html> | ||