極簡単なリフレクションの例。
コピペ支援的な感じで。
コピペ支援的な感じで。
import java.lang.reflect.Constructor;
public class ReflectionTest {
public static void main(String[] args) {
try {
// create String instance
String string_classStr = "java.lang.String";
Class<String> string_class = (Class<String>) Class.forName(string_classStr);
Class string_argTypes[] = { String.class };
Constructor<String> string_constructor = string_class.getConstructor(string_argTypes);
Object string_constructorArgs[] = { "TESTING@TESTONG" };
String string = (String) string_constructor.newInstance(string_constructorArgs);
System.out.println(string);
// call toString() method
Method string_toString = string_class.getMethod("toString", null);
String dst_string = (String) string_toString.invoke(string, null);
System.out.println(dst_string);
// call split("@") method
Class string_split_argTypes[] = { String.class };
Object string_split_args[] = {"@"};
Method string_split = string_class.getMethod("split", string_split_argTypes);
String[] string_splitArray = (String[]) string_split.invoke(string, string_split_args);
for (String str : string_splitArray) {
System.out.println(str);
}
// call String.format("num1 : %d, num2 : %d", 13, 14) methods (static method, and "13, 14" is var args)
Class string_format_argTypes[] = { String.class, Object[].class };
Object string_format_args[] = { "num1 : %d, num2 : %d", new Object[]{13, 14} };
Method string_format = string_class.getMethod("format", string_format_argTypes);
dst_string = (String) string_format.invoke(null, string_format_args);
System.out.println(dst_string);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
追記 reflection の綴りをがっつり間違えていたので修正しました。
public class ReflectionTest {
public static void main(String[] args) {
try {
// create String instance
String string_classStr = "java.lang.String";
Class<String> string_class = (Class<String>) Class.forName(string_classStr);
Class string_argTypes[] = { String.class };
Constructor<String> string_constructor = string_class.getConstructor(string_argTypes);
Object string_constructorArgs[] = { "TESTING@TESTONG" };
String string = (String) string_constructor.newInstance(string_constructorArgs);
System.out.println(string);
// call toString() method
Method string_toString = string_class.getMethod("toString", null);
String dst_string = (String) string_toString.invoke(string, null);
System.out.println(dst_string);
// call split("@") method
Class string_split_argTypes[] = { String.class };
Object string_split_args[] = {"@"};
Method string_split = string_class.getMethod("split", string_split_argTypes);
String[] string_splitArray = (String[]) string_split.invoke(string, string_split_args);
for (String str : string_splitArray) {
System.out.println(str);
}
// call String.format("num1 : %d, num2 : %d", 13, 14) methods (static method, and "13, 14" is var args)
Class string_format_argTypes[] = { String.class, Object[].class };
Object string_format_args[] = { "num1 : %d, num2 : %d", new Object[]{13, 14} };
Method string_format = string_class.getMethod("format", string_format_argTypes);
dst_string = (String) string_format.invoke(null, string_format_args);
System.out.println(dst_string);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
0 件のコメント:
コメントを投稿