reflection patches from Marco

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@2640 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
sjfink 2008-03-02 01:39:53 +00:00
parent d77357fd5a
commit 3d408fdd7e
6 changed files with 132 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package reflection;
public class Helper {
@SuppressWarnings("unused")
private final Object a, b;
public Helper() {
this.a = new Object();
this.b = new Object();
System.out.println("Helper constructor with no parameter invoked");
}
public Helper(Object a) {
this.a = a;
this.b = new Object();
System.out.println("Helper constructor with one parameter invoked");
}
public Helper(Object a, Object b) {
this.a = a;
this.b = b;
System.out.println("Helper constructor with two parameters invoked");
}
public void m(Object a, Object b, Object c) {
System.out.println("m method invoked");
}
public void n(Object a, Object b) {
System.out.println("n method invoked");
}
public static void s(Object a, Object b) {
System.out.println("s method invoked");
}
}

View File

@ -0,0 +1,17 @@
package reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Test of Method.invoke
*/
public class Reflect11 {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, InstantiationException,
IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {
Class c = Class.forName("java.lang.Integer");
Method[] m = c.getMethods();
m[0].invoke(new Integer(2), (Object[]) args);
}
}

View File

@ -0,0 +1,17 @@
package reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Test of Method.invoke
*/
public class Reflect12 {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, InstantiationException,
IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {
Class c = Class.forName("reflection.Helper");
Method[] m = c.getMethods();
m[0].invoke(new Helper(), new Object[3]);
}
}

View File

@ -0,0 +1,18 @@
package reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Test of Method.invoke
*/
public class Reflect13 {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, InstantiationException,
IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {
Class c = Class.forName("reflection.Helper");
Method[] m = c.getMethods();
int length = new Integer(args[0]).intValue();
m[0].invoke(new Helper(), new Object[length]);
}
}

View File

@ -0,0 +1,23 @@
package reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* Test of Method.invoke
*/
public class Reflect14 {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, InstantiationException,
IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {
Class c = Class.forName("reflection.Helper");
Method[] ms = c.getMethods();
for (Method m : ms) {
int mods = m.getModifiers();
if (Modifier.isStatic(mods) && m.getParameterTypes().length == 2) {
m.invoke(null, new Object[2]);
}
}
}
}

View File

@ -0,0 +1,23 @@
package reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* Test of Class.getConstructors().
*/
public class Reflect15 {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Class c = Class.forName("reflection.Helper");
Constructor[] ctors = c.getConstructors();
Helper h = null;
for (Constructor ctor : ctors) {
if (ctor.getParameterTypes().length == 2) {
h = (Helper) ctor.newInstance(new Object[] {new Object(), new Object()});
}
}
h.n(new Object(), new Object());
}
}