Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit84a50a2

Browse files
method invoke and reflect
1 parentcb20cde commit84a50a2

File tree

1 file changed

+84
-0
lines changed
  • RandomProblems/src/sporadic/method_invoke_and_reflect

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
packagesporadic.method_invoke_and_reflect;
2+
3+
importstaticjava.lang.System.err;
4+
importstaticjava.lang.System.out;
5+
6+
importjava.lang.reflect.InvocationTargetException;
7+
importjava.lang.reflect.Method;
8+
importjava.lang.reflect.Type;
9+
importjava.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+
publicclassDeet<T> {
24+
25+
privatebooleantestDeet(Localelocale) {
26+
// get ISO3Language() may throw a MissingResourceException
27+
out.format("Locale = %s, ISO language code = %s%n",locale.getDisplayName(),locale.getDisplayLanguage());
28+
returntrue;
29+
}
30+
31+
privateinttestFoo(Localelocale) {
32+
return0;
33+
}
34+
35+
privatebooleantestBar() {
36+
returntrue;
37+
}
38+
39+
/**
40+
* @param args
41+
*/
42+
publicstaticvoidmain(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+
Objectobject =c.newInstance();
50+
51+
Method[]allMethods =c.getDeclaredMethods();
52+
for (Methodm :allMethods) {
53+
StringmethodName =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+
Objecto =m.invoke(object,newLocale(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 (InvocationTargetExceptione) {
71+
Throwablecause =e.getCause();
72+
err.format("invocation of method %s failed: %s%n",methodName,cause.getMessage());
73+
}
74+
}
75+
}catch (ClassNotFoundExceptione) {
76+
e.printStackTrace();
77+
}catch (InstantiationExceptione) {
78+
e.printStackTrace();
79+
}catch (IllegalAccessExceptione) {
80+
e.printStackTrace();
81+
}
82+
out.println("Program ended.");
83+
}
84+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp