From 70750acc7c4aa31b3ed390b6249a8a097565c076 Mon Sep 17 00:00:00 2001 From: Oasis Date: Wed, 26 Jun 2019 18:31:46 +0800 Subject: [PATCH] UPDATE: Add @SourceClass for mirroring hidden class with Hack.Mirror. --- library/src/main/java/com/oasisfeng/hack/Hack.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/oasisfeng/hack/Hack.java b/library/src/main/java/com/oasisfeng/hack/Hack.java index 8c8e828..2742593 100644 --- a/library/src/main/java/com/oasisfeng/hack/Hack.java +++ b/library/src/main/java/com/oasisfeng/hack/Hack.java @@ -8,6 +8,8 @@ import com.oasisfeng.android.util.Suppliers; import com.oasisfeng.deagle.BuildConfig; import java.io.IOException; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -68,6 +70,7 @@ public class Hack { * @see #mirrorStaticMethod(String, Object, Object...) */ public interface Mirror {} + @Retention(RetentionPolicy.RUNTIME) public @interface SourceClass { String value(); } public interface HackedObject { > M with(final Class mirror_class); @@ -165,6 +168,13 @@ public class Hack { //noinspection unchecked return (Class) source_type; } else { + final SourceClass target_class = mirror_class.getAnnotation(SourceClass.class); + if (target_class != null) try { //noinspection unchecked + return (Class) Class.forName(target_class.value()); + } catch (final ClassNotFoundException e) { + return null; + } + final Class enclosing_class = mirror_class.getEnclosingClass(); @SuppressWarnings("unchecked") final Class source_enclosing_class = enclosing_class != null ? getSourceClassFromMirror((Class) enclosing_class) : null; if (source_enclosing_class == null) @@ -201,7 +211,7 @@ public class Hack { @Override public Object invoke(final Object proxy, final Method mirror_method, final Object[] args) throws Throwable { final Object source_result; try { - final Method source_method = findSourceMethodForMirror(mirror_method, source_class); + final Method source_method = findSourceMethodForMirror(mirror_method, source_class); // TODO: Cache source_method.setAccessible(true); source_result = source_method.invoke(mSource, args); } catch (final IllegalAccessException/* should not happen */| NoSuchMethodException e) {