From a365022185b7dae5ec7cce2c5df936c1fca576ed Mon Sep 17 00:00:00 2001 From: Jean-Marie 'Histausse' Mineau Date: Tue, 4 Mar 2025 16:36:59 +0100 Subject: [PATCH] add static method to test apk --- .../theseus/reflection/MainActivity.java | 56 ++++++++++++++++++- .../example/theseus/reflection/Reflectee.java | 6 ++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/test_apks/reflection/java/classes/com/example/theseus/reflection/MainActivity.java b/test_apks/reflection/java/classes/com/example/theseus/reflection/MainActivity.java index 6a2bd57..f7dd2ef 100644 --- a/test_apks/reflection/java/classes/com/example/theseus/reflection/MainActivity.java +++ b/test_apks/reflection/java/classes/com/example/theseus/reflection/MainActivity.java @@ -67,6 +67,14 @@ public class MainActivity extends Activity { b7.generateViewId(); linLayout.addView(b7); + Button b8 = new Button(this); + b8.generateViewId(); + linLayout.addView(b8); + + Button b9 = new Button(this); + b9.generateViewId(); + linLayout.addView(b9); + relLayout.addView(linLayout); setContentView(relLayout); @@ -147,6 +155,28 @@ public class MainActivity extends Activity { } } }); + + b8.setText("Static control"); + b8.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + try { + callStaticMethod(); + } catch(Exception e) { + Log.e("THESEUS", "Error: ", e); + } + } + }); + + b9.setText("Static rflct"); + b9.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + try { + callStaticMethodReflectCall(); + } catch(Exception e) { + Log.e("THESEUS", "Error: ", e); + } + } + }); } // A normal virtual method call @@ -267,7 +297,29 @@ public class MainActivity extends Activity { Utils.sink(this, newData); } + // A normal virtual method call + public void callStaticMethod() { + String data = Utils.source("R7 no reflect"); + String newData = Reflectee.staticTransfer(data); + Utils.sink(this, newData); + } + + // A call to a virtual method through reflection + public void callStaticMethodReflectCall() throws + ClassNotFoundException, + NoSuchMethodException, + IllegalAccessException, + InvocationTargetException + { + String data = Utils.source("R8 reflect"); + ClassLoader cl = MainActivity.class.getClassLoader(); + Class clz = cl.loadClass("com.example.theseus.reflection.Reflectee"); + Method mth = clz.getMethod("staticTransfer", String.class); + String newData = (String) mth.invoke(null, data); + Utils.sink(this, newData); + } + + // TODO: Interface, Inheritance // TODO: many argument methods - // TODO: static - // TODO: factory patern + // TODO: factory patern } diff --git a/test_apks/reflection/java/classes/com/example/theseus/reflection/Reflectee.java b/test_apks/reflection/java/classes/com/example/theseus/reflection/Reflectee.java index 65404b2..e8480ef 100644 --- a/test_apks/reflection/java/classes/com/example/theseus/reflection/Reflectee.java +++ b/test_apks/reflection/java/classes/com/example/theseus/reflection/Reflectee.java @@ -40,4 +40,10 @@ public class Reflectee { } return val; } + + public static String staticTransfer( + String data + ) { + return "Static Transfer: " + data; + } }