add inheritance tests

This commit is contained in:
Jean-Marie Mineau 2025-03-05 17:18:02 +01:00
parent e196f6b7e7
commit fbf6f3b995
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
5 changed files with 190 additions and 12 deletions

View file

@ -0,0 +1,10 @@
package com.example.theseus.reflection;
public class ChildReflectee extends Reflectee {
public static String staticOverridenTransfer(String data) {
return "Static Overrided Transfer ChildReflectee: " + data;
}
public String overridenTransfer(String data) {
return "Inherited Overrided Transfer ChildReflectee: " + data;
}
}

View file

@ -5,6 +5,7 @@ import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.View; import android.view.View;
@ -27,6 +28,9 @@ public class MainActivity extends Activity {
RelativeLayout relLayout = new RelativeLayout(this); RelativeLayout relLayout = new RelativeLayout(this);
relLayout.generateViewId(); relLayout.generateViewId();
ScrollView scrollView = new ScrollView(this);
scrollView.generateViewId();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT ViewGroup.LayoutParams.WRAP_CONTENT
@ -75,7 +79,16 @@ public class MainActivity extends Activity {
b9.generateViewId(); b9.generateViewId();
linLayout.addView(b9); linLayout.addView(b9);
relLayout.addView(linLayout); Button b10 = new Button(this);
b10.generateViewId();
linLayout.addView(b10);
Button b11 = new Button(this);
b11.generateViewId();
linLayout.addView(b11);
scrollView.addView(linLayout);
relLayout.addView(scrollView);
setContentView(relLayout); setContentView(relLayout);
b1.setText("Virtual control"); b1.setText("Virtual control");
@ -177,12 +190,40 @@ public class MainActivity extends Activity {
} }
} }
}); });
b10.setText("Extends control");
b10.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
callVirtualInherited();
callVirtualOverriden();
callStaticInherited();
callStaticOverriden();
} catch(Exception e) {
Log.e("THESEUS", "Error: ", e);
}
}
});
b11.setText("Extends rflct");
b11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
callVirtualInheritedReflect();
callVirtualOverridenReflect();
callStaticInheritedReflect();
callStaticOverridenReflect();
} catch(Exception e) {
Log.e("THESEUS", "Error: ", e);
}
}
});
} }
// A normal virtual method call // A normal virtual method call
public void callVirtualMethod() { public void callVirtualMethod() {
String data = Utils.source("no reflect virt call"); String data = Utils.source("no reflect virt call");
Reflectee r = new Reflectee("R1"); Reflectee r = new Reflectee("T1");
String newData = r.transfer(data); String newData = r.transfer(data);
Utils.sink(this, newData); Utils.sink(this, newData);
} }
@ -195,7 +236,7 @@ public class MainActivity extends Activity {
InvocationTargetException InvocationTargetException
{ {
String data = Utils.source("reflect virt call"); String data = Utils.source("reflect virt call");
Reflectee r = new Reflectee("R2"); Reflectee r = new Reflectee("R1");
ClassLoader cl = MainActivity.class.getClassLoader(); ClassLoader cl = MainActivity.class.getClassLoader();
Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee"); Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee");
Method mth = clz.getMethod("transfer", String.class); Method mth = clz.getMethod("transfer", String.class);
@ -208,7 +249,7 @@ public class MainActivity extends Activity {
public void callVirtualMethodCallAllScalar() public void callVirtualMethodCallAllScalar()
{ {
String data = Utils.source("no reflect virt call all scalars"); String data = Utils.source("no reflect virt call all scalars");
Reflectee r = new Reflectee("R3"); Reflectee r = new Reflectee("T2");
String newData = r.transfer(true, (byte)42, (short)666, '*', 0xDEAD_BEEF, 0xD1AB011C_5EAF00DL, 0.99f, 3.1415926535897932384626433d, data); String newData = r.transfer(true, (byte)42, (short)666, '*', 0xDEAD_BEEF, 0xD1AB011C_5EAF00DL, 0.99f, 3.1415926535897932384626433d, data);
Utils.testIsReflectee(this, r); Utils.testIsReflectee(this, r);
Utils.sink(this, newData); Utils.sink(this, newData);
@ -222,7 +263,7 @@ public class MainActivity extends Activity {
InvocationTargetException InvocationTargetException
{ {
String data = Utils.source("reflect virt call all scalars"); String data = Utils.source("reflect virt call all scalars");
Reflectee r = new Reflectee("R4"); Reflectee r = new Reflectee("R2");
ClassLoader cl = MainActivity.class.getClassLoader(); ClassLoader cl = MainActivity.class.getClassLoader();
Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee"); 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); Method mth = clz.getMethod("transfer", boolean.class, byte.class, short.class, char.class, int.class, long.class, float.class, double.class, String.class);
@ -235,7 +276,7 @@ public class MainActivity extends Activity {
public void callVirtualMethodCallVarArg() public void callVirtualMethodCallVarArg()
{ {
String data = Utils.source("no reflect virt call variable arg numb"); String data = Utils.source("no reflect virt call variable arg numb");
Reflectee r = new Reflectee("R5"); Reflectee r = new Reflectee("T3");
String newData = r.transfer("aa", "bb", data, "cc"); String newData = r.transfer("aa", "bb", data, "cc");
Utils.testIsReflectee(this, r); Utils.testIsReflectee(this, r);
Utils.sink(this, newData); Utils.sink(this, newData);
@ -249,7 +290,7 @@ public class MainActivity extends Activity {
InvocationTargetException InvocationTargetException
{ {
String data = Utils.source("reflect virt call variable arg numb"); String data = Utils.source("reflect virt call variable arg numb");
Reflectee r = new Reflectee("R6"); Reflectee r = new Reflectee("R3");
ClassLoader cl = MainActivity.class.getClassLoader(); ClassLoader cl = MainActivity.class.getClassLoader();
Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee"); Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee");
Method mth = clz.getMethod("transfer", String.class, String[].class); Method mth = clz.getMethod("transfer", String.class, String[].class);
@ -299,7 +340,7 @@ public class MainActivity extends Activity {
// A normal virtual method call // A normal virtual method call
public void callStaticMethod() { public void callStaticMethod() {
String data = Utils.source("R7 no reflect"); String data = Utils.source("T4 control static");
String newData = Reflectee.staticTransfer(data); String newData = Reflectee.staticTransfer(data);
Utils.sink(this, newData); Utils.sink(this, newData);
} }
@ -311,7 +352,7 @@ public class MainActivity extends Activity {
IllegalAccessException, IllegalAccessException,
InvocationTargetException InvocationTargetException
{ {
String data = Utils.source("R8 reflect"); String data = Utils.source("R4 reflect static");
ClassLoader cl = MainActivity.class.getClassLoader(); ClassLoader cl = MainActivity.class.getClassLoader();
Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee"); Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee");
Method mth = clz.getMethod("staticTransfer", String.class); Method mth = clz.getMethod("staticTransfer", String.class);
@ -319,7 +360,87 @@ public class MainActivity extends Activity {
Utils.sink(this, newData); Utils.sink(this, newData);
} }
// TODO: Interface, Inheritance public void callVirtualInherited() {
String data = Utils.source("T5 control extend virt");
String newData = new ChildReflectee().inheritedTransfer(data);
Utils.sink(this, newData);
}
public void callVirtualOverriden() {
String data = Utils.source("T6 control extend virt override");
String newData = new ChildReflectee().overridenTransfer(data);
Utils.sink(this, newData);
}
public void callStaticInherited() {
String data = Utils.source("T7 control extend static");
String newData = ChildReflectee.staticInheritedTransfer(data);
Utils.sink(this, newData);
}
public void callStaticOverriden() {
String data = Utils.source("T8 control extend static override");
String newData = ChildReflectee.staticOverridenTransfer(data);
Utils.sink(this, newData);
}
public void callVirtualInheritedReflect() throws
ClassNotFoundException,
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException
{
String data = Utils.source("R5 reflect extend virt");
ChildReflectee obj = new ChildReflectee();
ClassLoader cl = MainActivity.class.getClassLoader();
Class clz = cl.loadClass("com.example.theseus.reflection.ChildReflectee");
Method mth = clz.getMethod("inheritedTransfer", String.class);
String newData = (String) mth.invoke(obj, data);
Utils.testIsChildReflectee(this, obj);
Utils.sink(this, newData);
}
public void callVirtualOverridenReflect() throws
ClassNotFoundException,
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException
{
String data = Utils.source("R6 reflect extend virt override");
ChildReflectee obj = new ChildReflectee();
ClassLoader cl = MainActivity.class.getClassLoader();
Class clz = cl.loadClass("com.example.theseus.reflection.ChildReflectee");
Method mth = clz.getMethod("overridenTransfer", String.class);
String newData = (String) mth.invoke(obj, data);
Utils.testIsChildReflectee(this, obj);
Utils.sink(this, newData);
}
public void callStaticInheritedReflect() throws
ClassNotFoundException,
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException
{
String data = Utils.source("R7 reflect extend static");
ClassLoader cl = MainActivity.class.getClassLoader();
Class clz = cl.loadClass("com.example.theseus.reflection.ChildReflectee");
Method mth = clz.getMethod("staticInheritedTransfer", String.class);
String newData = (String) mth.invoke(null, data);
Utils.sink(this, newData);
}
public void callStaticOverridenReflect() throws
ClassNotFoundException,
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException
{
String data = Utils.source("R8 reflect extend static override");
ClassLoader cl = MainActivity.class.getClassLoader();
Class clz = cl.loadClass("com.example.theseus.reflection.ChildReflectee");
Method mth = clz.getMethod("staticOverridenTransfer", String.class);
String newData = (String) mth.invoke(null, data);
Utils.sink(this, newData);
}
// TODO: Interface
// TODO: many argument methods // TODO: many argument methods
// TODO: factory patern // TODO: factory patern
} }

View file

@ -46,4 +46,20 @@ public class Reflectee {
) { ) {
return "Static Transfer: " + data; return "Static Transfer: " + data;
} }
public static String staticInheritedTransfer(String data) {
return "Static Inherited Transfer Reflectee: " + data;
}
public static String staticOverridenTransfer(String data) {
return "Static Inherited Transfer Reflectee: " + data;
}
public String inheritedTransfer(String data) {
return "Inherited Transfer Reflectee: " + data;
}
public String overridenTransfer(String data) {
return "Inherited Transfer Reflectee: " + data;
}
} }

View file

@ -3,6 +3,7 @@ package com.example.theseus;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import com.example.theseus.reflection.Reflectee; import com.example.theseus.reflection.Reflectee;
import com.example.theseus.reflection.ChildReflectee;
public class Utils { public class Utils {
@ -26,9 +27,12 @@ public class Utils {
} }
public static void testIsObject(Activity ac, Object obj) { public static void testIsObject(Activity ac, Object obj) {
popup(ac, "Object", "Object was expected and found"); //popup(ac, "Object", "Object was expected and found");
} }
public static void testIsReflectee(Activity ac, Reflectee ref) { public static void testIsReflectee(Activity ac, Reflectee ref) {
popup(ac, "Reflectee", "Reflectee was expected and found"); //popup(ac, "Reflectee", "Reflectee was expected and found");
}
public static void testIsChildReflectee(Activity ac, ChildReflectee ref) {
//popup(ac, "ChildReflectee", "ChildReflectee was expected and found");
} }
} }

View file

@ -13,12 +13,39 @@ import java.util.Arrays;
import android.util.Log; import android.util.Log;
import java.util.Random;
import com.example.theseus.Utils; import com.example.theseus.Utils;
public class MainActivity extends Activity { public class MainActivity extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
boolean t = (new Random().nextInt(1000)) < 500;
if (t) {
Log.i("THESEUS", "OK");
} else {
Log.i("THESEUS", "NOP");
}
boolean t0 = (new Random().nextInt(1000)) < 500;
boolean t1 = (new Random().nextInt(1000)) < 500;
boolean t2 = (new Random().nextInt(1000)) < 500;
boolean t3 = (new Random().nextInt(1000)) < 500;
boolean t4 = (new Random().nextInt(1000)) < 500;
boolean t5 = (new Random().nextInt(1000)) < 500;
boolean t6 = (new Random().nextInt(1000)) < 500;
boolean t7 = (new Random().nextInt(1000)) < 500;
boolean t8 = (new Random().nextInt(1000)) < 500;
boolean t9 = (new Random().nextInt(1000)) < 500;
boolean t10 = (new Random().nextInt(1000)) < 500;
boolean t11 = (new Random().nextInt(1000)) < 500;
boolean t12 = (new Random().nextInt(1000)) < 500;
boolean t13 = (new Random().nextInt(1000)) < 500;
boolean t14 = (new Random().nextInt(1000)) < 500;
boolean t15 = (new Random().nextInt(1000)) < 500;
Log.i("THESEUS", "" + t0+t1+t2+t3+t4+t5+t6+t7+t8+t9+t10+t11+t12+t13+t14+t15);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
try { try {
//callVirtualMethod(); //callVirtualMethod();