public class Main {
public static void main(String[] args) {
try {
String[] strArr = new String[] {
"aaa", "bbbb", "ccc"
};
// 入参类型
strArr,
});
} catch (Exception e) {
e.printStackTrace();
try {
Class<?> classTest = Class.forName( "com.Main$Test");
// 入参类型(和下面调用时,传入的入参值,比较类似)
Method methodPrint = classTest.getMethod("print", new Class[] {
String[].class
});
methodPrint.invoke(classTest, new Object[] {
new String[] {
"hello", "world"
}
});
} catch (Exception e) {
e.printStackTrace();
try {
Class<?> classSubTest = Class.forName("com.Main$SubTest");
Constructor<?> consSubTest = classSubTest.getConstructor(String.class, String.class);
Class<?> classTest = Class.forName(" com.Main$Test");
// 入参类型
// 参数类型,不能使用:new classSubTest[].class
Object[] temp = (Object[]) java.lang.reflect.Array.newInstance(classSubTest, 2);
temp[0] = consSubTest.newInstance("key1", "value1");
temp[1] = consSubTest.newInstance("key2", "value2");
// temp[2] = cons.newInstance("key2", "value2");// ArrayIndexOutOfBoundsException 2
methodPrintSubTest.invoke(classTest, new Object[] {
temp
});
} catch (Exception e) {
e.printStackTrace();
}
}
public static class Test {
public static void print(String[] strs) {
for (String item : strs) {
System.out.println("str:" + item);
}
}
public static void printSubTest(SubTest[] subTests) {
for (SubTest item : subTests) {
System.out.println("subTest:" + item);
}
}
}
public static class SubTest {
private String key;
private String value;
public SubTest(String key, String value) {
this.key = key;
this.value = value;
}
@Override
public String toString() {
return "SubTest [key=" + key + ", value=" + value + "]";
}
}
public static void main(String[] args) {
try {
String[] strArr = new String[] {
"aaa", "bbbb", "ccc"
};
// 入参类型
Method methodPrint = Test.class.getMethod("print", String[].class);
// 注意:需要在原有数组外面,再包一层new Object[]数组
strArr,
});
} catch (Exception e) {
e.printStackTrace();
}
try {
Class<?> classTest = Class.forName( "com.Main$Test");
// 入参类型(和下面调用时,传入的入参值,比较类似)
Method methodPrint = classTest.getMethod("print", new Class[] {
String[].class
});
methodPrint.invoke(classTest, new Object[] {
new String[] {
"hello", "world"
}
});
} catch (Exception e) {
e.printStackTrace();
}
try {
Class<?> classSubTest = Class.forName("com.Main$SubTest");
Constructor<?> consSubTest = classSubTest.getConstructor(String.class, String.class);
Class<?> classTest = Class.forName(" com.Main$Test");
// 入参类型
// 参数类型,不能使用:new classSubTest[].class
// classSubTest.getClasses():返回类定义的公共的内部类,以及从父类、父接口那里继承来的内部类
// getDeclaredClasses():返回类中定义的公共、私有、保护的内部类
Method methodPrintSubTest = classTest.getMethod("printSubTest",
java.lang.reflect.Array.newInstance(classSubTest, 2).getClass());// TODO 是否还有其他方法?Object[] temp = (Object[]) java.lang.reflect.Array.newInstance(classSubTest, 2);
temp[0] = consSubTest.newInstance("key1", "value1");
temp[1] = consSubTest.newInstance("key2", "value2");
// temp[2] = cons.newInstance("key2", "value2");// ArrayIndexOutOfBoundsException 2
methodPrintSubTest.invoke(classTest, new Object[] {
temp
});
} catch (Exception e) {
e.printStackTrace();
}
}
public static class Test {
public static void print(String[] strs) {
for (String item : strs) {
System.out.println("str:" + item);
}
}
public static void printSubTest(SubTest[] subTests) {
for (SubTest item : subTests) {
System.out.println("subTest:" + item);
}
}
}
public static class SubTest {
private String key;
private String value;
public SubTest(String key, String value) {
this.key = key;
this.value = value;
}
@Override
public String toString() {
return "SubTest [key=" + key + ", value=" + value + "]";
}
}
}
参考:
http://blog.csdn.net/cskgnt/article/details/7816212