Skip to content

Commit

Permalink
1、减少反射次数
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyJingFish committed Apr 7, 2024
1 parent 91dddd3 commit a58e457
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
public final class AndroidAopJoinPoint {
private final Object target;
private final Class<?> targetClass;
// private final String targetClassName;
private Object[] mArgs;
private String[] mArgClassNames;
private final String targetMethodName;
Expand All @@ -25,24 +24,14 @@ public final class AndroidAopJoinPoint {
private String cutMatchClassName;
private String paramsKey;
private String methodKey;
private String targetClassName;
private final String targetClassName;

public AndroidAopJoinPoint(String targetClassName, Object target, String originalMethodName, String targetMethodName) {
this.targetClassName = targetClassName;
public AndroidAopJoinPoint(Class<?> clazz, Object target, String originalMethodName, String targetMethodName) {
this.targetClassName = clazz.getName();
this.target = target;
this.originalMethodName = originalMethodName;
this.targetMethodName = targetMethodName;
String key = targetClassName + "-" + target;
Class<?> clazz = AndroidAopBeanUtils.INSTANCE.getClassCache(key);
if (clazz == null){
try {
clazz = Class.forName(targetClassName);
AndroidAopBeanUtils.INSTANCE.putClassCache(key,clazz,target);
} catch (ClassNotFoundException e) {
throw new RuntimeException(targetClassName + "的类名不可被混淆");
}
}
targetClass = clazz;
this.targetClass = clazz;

}

Expand Down Expand Up @@ -206,7 +195,7 @@ private void getTargetMethod(){
originalMethod.setAccessible(true);
methodMap = new MethodMap(originalMethod,targetMethod);
AndroidAopBeanUtils.INSTANCE.putMethodMapCache(key,methodMap,target);
} catch (Exception e) {
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ internal object AndroidAopBeanUtils {
private val mMatchClassMethodMap = ConcurrentHashMap<String, MatchClassMethod?>()
private val mTargetReferenceMap = ConcurrentHashMap<String, KeyWeakReference<Any>>()
private val mTargetMethodMap = ConcurrentHashMap<String, MethodMap>()
private val mTargetClassMap = ConcurrentHashMap<String, Class<*>>()
private val mTargetKeyReferenceQueue = ReferenceQueue<Any>()
private val mSingleIO: ExecutorService = Executors.newSingleThreadExecutor()

Expand Down Expand Up @@ -94,19 +93,6 @@ internal object AndroidAopBeanUtils {
observeTarget(target,key)
}

fun getClassCache(key: String): Class<*>? {
val clazz = mTargetClassMap[key]
if (clazz != null){
removeWeaklyReachableObjectsOnIOThread()
}
return clazz
}

fun putClassCache(key: String, clazz:Class<*>, target:Any?) {
mTargetClassMap[key] = clazz
observeTarget(target,key)
}

private fun observeTarget(target : Any?,key :String){
mSingleIO.execute{
if (target != null){
Expand All @@ -132,7 +118,6 @@ internal object AndroidAopBeanUtils {
mBasePointCutMap.remove(ref.key)
mMatchClassMethodMap.remove(ref.key)
mTargetMethodMap.remove(ref.key)
mTargetClassMap.remove(ref.key)
}
} while (ref != null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ object WovenIntoCode {
ClassNameToConversions.getReturnXObject(returnType.name), "pointCut.joinPointExecute()"
)

val constructor = "\"$targetClassName\",${if(isStaticMethod)"null" else "\$0"},\"$oldMethodName\",\"$targetMethodName\"";
val constructor = "$targetClassName.class,${if(isStaticMethod)"null" else "\$0"},\"$oldMethodName\",\"$targetMethodName\"";
val body =
""" {AndroidAopJoinPoint pointCut = new AndroidAopJoinPoint($constructor);"""+
(if (cutClassName != null) " pointCut.setCutMatchClassName(\"$cutClassName\");\n" else "") +
Expand All @@ -206,10 +206,6 @@ object WovenIntoCode {
(if (isHasArgs) " Object[] args = new Object[]{$argsBuffer};\n" else "") +
(if (isHasArgs) " pointCut.setArgs(args);\n" else " pointCut.setArgs(null);\n") +
" "+returnStr+";}"
// val allSignature = ctMethod.signature
// printLog("returnType = ${returnType.name}")
// printLog("allSignature = $allSignature")
// printLog(body)
ctMethod.setBody(body)
InitConfig.putCutInfo(value)
} catch (e: NotFoundException) {
Expand Down

0 comments on commit a58e457

Please sign in to comment.