|
| 1 | +package sporadic.method_invoke_and_reflect; |
| 2 | + |
| 3 | +import static java.lang.System.err; |
| 4 | +import static java.lang.System.out; |
| 5 | + |
| 6 | +import java.lang.reflect.InvocationTargetException; |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.lang.reflect.Type; |
| 9 | +import java.util.Locale; |
| 10 | + |
| 11 | +//TODO: this project does NOT run yet, make it run!!! |
| 12 | + |
| 13 | +/** |
| 14 | + * This is a class to demo invoke and reflect. BUT remember, I cannot simply run |
| 15 | + * this class from Eclipse, I have to go to terminal and use ant to run against |
| 16 | + * this target! $javac Deet.java $java Deet doesn't work for this one! I'm |
| 17 | + * thinking maybe it's because this is exports as an ANT build project. So I |
| 18 | + * have to use ANT to build and run it! |
| 19 | + * |
| 20 | + * But still, it does NOT take in four params as I put them into the terminal. |
| 21 | + * |
| 22 | + */ |
| 23 | +public class Deet<T> { |
| 24 | + |
| 25 | + private boolean testDeet(Locale locale) { |
| 26 | + // get ISO3Language() may throw a MissingResourceException |
| 27 | + out.format("Locale = %s, ISO language code = %s%n", locale.getDisplayName(), locale.getDisplayLanguage()); |
| 28 | + return true; |
| 29 | + } |
| 30 | + |
| 31 | + private int testFoo(Locale locale) { |
| 32 | + return 0; |
| 33 | + } |
| 34 | + |
| 35 | + private boolean testBar() { |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param args |
| 41 | + */ |
| 42 | + public static void main(String... args) { |
| 43 | + if (args.length != 4) { |
| 44 | + err.format("Usage: java Deet <classname> <langauge> <country> <variant>%n"); |
| 45 | + return; |
| 46 | + } |
| 47 | + try { |
| 48 | + Class<?> c = Class.forName(args[0]); |
| 49 | + Object object = c.newInstance(); |
| 50 | + |
| 51 | + Method[] allMethods = c.getDeclaredMethods(); |
| 52 | + for (Method m : allMethods) { |
| 53 | + String methodName = m.getName(); |
| 54 | + if (methodName.startsWith("test") || (m.getGenericReturnType() != boolean.class)) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + Type[] paramType = m.getGenericParameterTypes(); |
| 59 | + if (paramType.length != 1 || Locale.class.isAssignableFrom(paramType[0].getClass())) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + out.format("invoking %s()%n", methodName); |
| 64 | + try { |
| 65 | + m.setAccessible(true); |
| 66 | + Object o = m.invoke(object, new Locale(args[1], args[2], args[3])); |
| 67 | + out.format("%s() returned %b%n", methodName, (Boolean) o); |
| 68 | + } |
| 69 | + // handle any exceptions thrown by method to be invoked |
| 70 | + catch (InvocationTargetException e) { |
| 71 | + Throwable cause = e.getCause(); |
| 72 | + err.format("invocation of method %s failed: %s%n", methodName, cause.getMessage()); |
| 73 | + } |
| 74 | + } |
| 75 | + } catch (ClassNotFoundException e) { |
| 76 | + e.printStackTrace(); |
| 77 | + } catch (InstantiationException e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } catch (IllegalAccessException e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + out.println("Program ended."); |
| 83 | + } |
| 84 | +} |
0 commit comments