diff --git a/README.md b/README.md index e5d62fa..f96b0c5 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ allprojects { 2) In your library/build.gradle add: ```groovy dependencies { - implementation 'com.github.ChickenHook:RestrictionBypass:2.1' + implementation 'com.github.ChickenHook:RestrictionBypass:2.2' } ``` ## Usage diff --git a/restrictionbypass/src/androidTest/java/org/chickenhook/restrictionbypass/ReflectionHelperTest.java b/restrictionbypass/src/androidTest/java/org/chickenhook/restrictionbypass/ReflectionHelperTest.java index f157603..df65af0 100644 --- a/restrictionbypass/src/androidTest/java/org/chickenhook/restrictionbypass/ReflectionHelperTest.java +++ b/restrictionbypass/src/androidTest/java/org/chickenhook/restrictionbypass/ReflectionHelperTest.java @@ -1,16 +1,29 @@ package org.chickenhook.restrictionbypass; import org.chickenhook.restrictionbypass.helpers.Reflection; +import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; public class ReflectionHelperTest { - Object reflectiveField = new Object(); + Object reflectiveField; + + @Before + public void setUp() { + reflectiveField = new Object(); + } @Test public void getReflective() throws Exception { assertSame(Reflection.getReflective(this, "reflectiveField"), reflectiveField); } + + @Test + public void setReflective() throws Exception { + Reflection.setReflective(this, "reflectiveField", null); + assertNull(reflectiveField); + } } diff --git a/restrictionbypass/src/main/java/org/chickenhook/restrictionbypass/helpers/Reflection.java b/restrictionbypass/src/main/java/org/chickenhook/restrictionbypass/helpers/Reflection.java index 22294f0..308295c 100644 --- a/restrictionbypass/src/main/java/org/chickenhook/restrictionbypass/helpers/Reflection.java +++ b/restrictionbypass/src/main/java/org/chickenhook/restrictionbypass/helpers/Reflection.java @@ -38,4 +38,35 @@ T getReflective(@Nullable Object obj, @NonNull Class cls, @NonNull String f.setAccessible(true); return (T) f.get(obj); } + + + + /** + * Set a member of the given object + * @param obj containing the member + * @param field the member name + * @param value the value to be set + * @throws NoSuchFieldException when field was found + * @throws IllegalAccessException when field was not accessible + */ + public static @Nullable + void setReflective(@NonNull Object obj, @NonNull String field, @Nullable Object value) throws NoSuchFieldException, IllegalAccessException { + setReflective(obj, obj.getClass(), field, value); + } + + /** + * Set a member of the given object + * @param obj containing the member + * @param cls super class of the obj + * @param field the member name + * @param value the value to be set + * @throws NoSuchFieldException when field was found + * @throws IllegalAccessException when field was not accessible + */ + public static @Nullable + void setReflective(@Nullable Object obj, @NonNull Class cls, @NonNull String field, @Nullable Object value) throws NoSuchFieldException, IllegalAccessException { + Field f = cls.getDeclaredField(field); + f.setAccessible(true); + f.set(obj, value); + } }