test different methods cases
This commit is contained in:
parent
9b4eccfc70
commit
e4fd680d15
3 changed files with 93 additions and 0 deletions
|
|
@ -22,6 +22,10 @@ public class MainActivity extends Activity {
|
|||
callVirtualMethodReflectCall();
|
||||
callConstructorVirtualMethodReflectConstr();
|
||||
callVirtualMethodReflectOldConst();
|
||||
callVirtualMethodCallAllScalar();
|
||||
callVirtualMethodReflectCallAllScalar();
|
||||
callVirtualMethodCallVarArg();
|
||||
callVirtualMethodReflectVarArg();
|
||||
} catch(Exception e) {
|
||||
Log.e("THESEUS", "Error: ", e);
|
||||
}
|
||||
|
|
@ -48,6 +52,61 @@ public class MainActivity extends Activity {
|
|||
Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee");
|
||||
Method mth = clz.getMethod("transfer", String.class);
|
||||
String newData = (String) mth.invoke(r, data);
|
||||
Utils.testIsReflectee(this, r);
|
||||
Utils.sink(this, newData);
|
||||
}
|
||||
|
||||
// A call to a virtual method with all scalar types.
|
||||
public void callVirtualMethodCallAllScalar()
|
||||
{
|
||||
String data = Utils.source("no reflect virt call all scalars");
|
||||
Reflectee r = new Reflectee("R3");
|
||||
String newData = r.transfer(true, (byte)42, (short)666, '*', 0xDEAD_BEEF, 0xD1AB011C_5EAF00DL, 0.99f, 3.1415926535897932384626433d, data);
|
||||
Utils.testIsReflectee(this, r);
|
||||
Utils.sink(this, newData);
|
||||
}
|
||||
|
||||
// A call to a virtual method through reflection with all scalar types.
|
||||
public void callVirtualMethodReflectCallAllScalar() throws
|
||||
ClassNotFoundException,
|
||||
NoSuchMethodException,
|
||||
IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
String data = Utils.source("reflect virt call all scalars");
|
||||
Reflectee r = new Reflectee("R4");
|
||||
ClassLoader cl = MainActivity.class.getClassLoader();
|
||||
Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee");
|
||||
Method mth = clz.getMethod("transfer", boolean.class, byte.class, short.class, char.class, int.class, long.class, float.class, double.class, String.class);
|
||||
String newData = (String) mth.invoke(r, true, (byte)42, (short)666, '*', 0xDEAD_BEEF, 0xD1AB011C_5EAF00DL, 0.99f, 3.1415926535897932384626433d, data);
|
||||
Utils.testIsReflectee(this, r);
|
||||
Utils.sink(this, newData);
|
||||
}
|
||||
|
||||
// A call to a virtual method with variable number of arg.
|
||||
public void callVirtualMethodCallVarArg()
|
||||
{
|
||||
String data = Utils.source("no reflect virt call variable arg numb");
|
||||
Reflectee r = new Reflectee("R5");
|
||||
String newData = r.transfer("aa", "bb", data, "cc");
|
||||
Utils.testIsReflectee(this, r);
|
||||
Utils.sink(this, newData);
|
||||
}
|
||||
|
||||
// A call to a virtual method through reflection with variable number of arg.
|
||||
public void callVirtualMethodReflectVarArg() throws
|
||||
ClassNotFoundException,
|
||||
NoSuchMethodException,
|
||||
IllegalAccessException,
|
||||
InvocationTargetException
|
||||
{
|
||||
String data = Utils.source("reflect virt call variable arg numb");
|
||||
Reflectee r = new Reflectee("R6");
|
||||
ClassLoader cl = MainActivity.class.getClassLoader();
|
||||
Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee");
|
||||
Method mth = clz.getMethod("transfer", String.class, String[].class);
|
||||
String newData = (String) mth.invoke(r, "aa", new String[] {"bb", data, "cc"});
|
||||
Utils.testIsReflectee(this, r);
|
||||
Utils.sink(this, newData);
|
||||
}
|
||||
|
||||
|
|
@ -67,6 +126,7 @@ public class MainActivity extends Activity {
|
|||
Object r = cst.newInstance(data);
|
||||
Method mth = clz.getMethod("transfer", String.class);
|
||||
String newData = (String) mth.invoke(r, "");
|
||||
Utils.testIsObject(this, r);
|
||||
Utils.sink(this, newData);
|
||||
}
|
||||
|
||||
|
|
@ -85,6 +145,7 @@ public class MainActivity extends Activity {
|
|||
Object r = clz.newInstance();
|
||||
Method mth = clz.getMethod("transfer", String.class);
|
||||
String newData = (String) mth.invoke(r, data);
|
||||
Utils.testIsObject(this, r);
|
||||
Utils.sink(this, newData);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,4 +16,28 @@ public class Reflectee {
|
|||
public String transfer(String data) {
|
||||
return name + data;
|
||||
}
|
||||
|
||||
public String transfer(
|
||||
boolean bool,
|
||||
byte by,
|
||||
short sh,
|
||||
char ch,
|
||||
int in,
|
||||
long lo,
|
||||
float fl,
|
||||
double dou,
|
||||
String str
|
||||
) {
|
||||
return name + " " + bool + " " + by + " " + sh + " " + ch + " " + in + " " + lo + " " + fl + " " + dou + " " + str;
|
||||
}
|
||||
public String transfer(
|
||||
String arg1,
|
||||
String... args
|
||||
) {
|
||||
String val = name + " " + arg1;
|
||||
for (String v : args) {
|
||||
val += " " + v;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.example.theseus;
|
|||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import com.example.theseus.reflection.Reflectee;
|
||||
|
||||
|
||||
public class Utils {
|
||||
|
|
@ -23,4 +24,11 @@ public class Utils {
|
|||
public static void sink(Activity ac, String data) {
|
||||
popup(ac, "Data leak:", data);
|
||||
}
|
||||
|
||||
public static void testIsObject(Activity ac, Object obj) {
|
||||
popup(ac, "Object", "Object was expected and found");
|
||||
}
|
||||
public static void testIsReflectee(Activity ac, Reflectee ref) {
|
||||
popup(ac, "Reflectee", "Reflectee was expected and found");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue