Skip to content

Commit

Permalink
handle primitive type
Browse files Browse the repository at this point in the history
  • Loading branch information
nnjun committed Feb 16, 2022
1 parent 0dd8223 commit 740e207
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 21 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ android {
}

buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
Expand Down
14 changes: 13 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile
-keep class top.niunaijun.blackreflection.** {*; }
-keep @top.niunaijun.blackreflection.annotation.BClass class * {*;}
-keep @top.niunaijun.blackreflection.annotation.BClassName class * {*;}
-keep @top.niunaijun.blackreflection.annotation.BClassNameNotProcess class * {*;}
-keepclasseswithmembernames class * {
@top.niunaijun.blackreflection.annotation.BField.* <methods>;
@top.niunaijun.blackreflection.annotation.BFieldNotProcess.* <methods>;
@top.niunaijun.blackreflection.annotation.BFieldSetNotProcess.* <methods>;
@top.niunaijun.blackreflection.annotation.BMethod.* <methods>;
@top.niunaijun.blackreflection.annotation.BStaticField.* <methods>;
@top.niunaijun.blackreflection.annotation.BStaticMethod.* <methods>;
}
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
package="top.niunaijun.blackreflection">

<application
android:name=".App"
android:name="top.niunaijun.app.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BlackReflection">
<activity android:name=".MainActivity">
<activity android:name="top.niunaijun.app.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"/>
<activity android:name="top.niunaijun.app.SecondActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.niunaijun.blackreflection;
package top.niunaijun.app;

import android.app.Application;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package top.niunaijun.blackreflection;
package top.niunaijun.app;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import top.niunaijun.blackreflection.ref.BRActivityThread;
import top.niunaijun.blackreflection.ref.BRActivityThreadH;
import top.niunaijun.blackreflection.ref.BRMainActivity;
import top.niunaijun.app.ref.BRActivityThread;
import top.niunaijun.app.ref.BRActivityThreadH;
import top.niunaijun.app.ref.BRMainActivity;
import top.niunaijun.blackreflection.R;


public class MainActivity extends AppCompatActivity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package top.niunaijun.blackreflection;
package top.niunaijun.app;

import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.Method;

import top.niunaijun.blackreflection.ref.BRAppCompatActivity;
import top.niunaijun.app.ref.BRAppCompatActivity;

/**
* Created by Milk on 2022/2/15.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.niunaijun.blackreflection.ref;
package top.niunaijun.app.ref;

import android.content.Intent;
import android.os.IBinder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.niunaijun.blackreflection.ref;
package top.niunaijun.app.ref;

import androidx.appcompat.app.ActionBar;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.niunaijun.blackreflection.ref;
package top.niunaijun.app.ref;

import top.niunaijun.blackreflection.annotation.BClass;
import top.niunaijun.blackreflection.annotation.BField;
Expand All @@ -13,7 +13,7 @@
* しーJ
* 此处无Bug
*/
@BClass(top.niunaijun.blackreflection.MainActivity.class)
@BClass(top.niunaijun.app.MainActivity.class)
public interface MainActivity {
@BStaticField
String TAGStatic();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:context="top.niunaijun.app.MainActivity">

<Button
android:id="@+id/btn_start"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ private static Class<?> parseBaseClass(String className) {
return boolean.class;
case "char":
return char.class;
case "int[]":
return int[].class;
case "byte[]":
return byte[].class;
case "short[]":
return short[].class;
case "long[]":
return long[].class;
case "float[]":
return float[].class;
case "double[]":
return double[].class;
case "boolean[]":
return boolean[].class;
case "char[]":
return char[].class;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package top.niunaijun.blackreflection;

/**
* Created by Milk on 2022/2/16.
* * ∧_∧
* (`・ω・∥
* 丶 つ0
* しーJ
* 此处无Bug
*/
public class BlackNullPointerException extends NullPointerException {
public BlackNullPointerException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ public static <T> T create(Class<T> clazz, final Object caller, boolean withExce
Object o = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String name = method.getName();
Class<?> returnType = method.getReturnType();

try {
boolean isStatic = weakCaller == null;

Object callerByWeak = isStatic ? null : weakCaller.get();

String name = method.getName();
// fidel
BField bField = method.getAnnotation(BField.class);
BFieldNotProcess bFieldNotProcess = method.getAnnotation(BFieldNotProcess.class);
Expand All @@ -79,12 +81,14 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
call = on.get(args);
} else {
if (callerByWeak == null) {
return null;
return generateNullValue(returnType);
}
call = on.get(callerByWeak);
}
return call;
}

// void
BFieldSetNotProcess bFieldSetNotProcess = method.getAnnotation(BFieldSetNotProcess.class);
if (bFieldSetNotProcess != null) {
// startsWith "set"
Expand All @@ -109,18 +113,22 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
call = on.call(args);
} else {
if (callerByWeak == null) {
return null;
return generateNullValue(returnType);
}
call = on.callByCaller(callerByWeak, args);
}
return call;
} catch (Throwable throwable) {
throwable.printStackTrace();
if (throwable instanceof BlackNullPointerException) {
throw throwable;
}
if (withException) {
throw throwable;
}
}
return null;

return generateNullValue(returnType);
}
});

Expand Down Expand Up @@ -184,4 +192,14 @@ private static Class<?> getClassNameByBlackClass(Class<?> clazz) throws ClassNot
return Class.forName(bClassNameNotProcess.value());
}
}

private static Object generateNullValue(Class<?> returnType) {
if (returnType == void.class) {
return 0;
}
if (returnType.isPrimitive()) {
throw new BlackNullPointerException("value is null!");
}
return null;
}
}

0 comments on commit 740e207

Please sign in to comment.