Skip to content

Commit

Permalink
Merge pull request #14 from FlyJingFish/dev_update
Browse files Browse the repository at this point in the history
Dev update
  • Loading branch information
FlyJingFish authored Apr 3, 2024
2 parents 9bd3c41 + 392611b commit c9e0d9d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 70 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
//必须项 👇
plugins {
...
id "io.github.FlyJingFish.AndroidAop.android-aop" version "1.5.1"
id "io.github.FlyJingFish.AndroidAop.android-aop" version "1.5.2"
}
```

Expand All @@ -75,7 +75,7 @@ plugins {
buildscript {
dependencies {
//必须项 👇
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:1.5.1'
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:1.5.2'
}
}
```
Expand All @@ -86,7 +86,7 @@ buildscript {
plugins {
//必须项 👇
id "io.github.FlyJingFish.AndroidAop.android-aop" version "1.5.1" apply false
id "io.github.FlyJingFish.AndroidAop.android-aop" version "1.5.2" apply false
}
```

Expand Down Expand Up @@ -133,17 +133,17 @@ plugins {
dependencies {
//必须项 👇
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:1.5.1'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:1.5.1'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:1.5.2'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:1.5.2'
//必须项 👇如果您项目内已经有了这项不用加也可以
implementation 'androidx.appcompat:appcompat:1.3.0' // 至少在1.3.0及以上
//非必须项 👇,如果你想自定义切面需要用到,⚠️支持Java和Kotlin代码写的切面
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:1.5.1'
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:1.5.2'
//非必须项 👇,如果你想自定义切面需要用到,⚠️只适用于Java代码写的切面
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:1.5.1'
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:1.5.2'
//⚠️上边的 android-aop-ksp 和 android-aop-processor 二选一
}
```
Expand Down
14 changes: 7 additions & 7 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Add directly to ```build.gradle``` of **app**
//Required items 👇
plugins {
...
id "io.github.FlyJingFish.AndroidAop.android-aop" version "1.5.1"
id "io.github.FlyJingFish.AndroidAop.android-aop" version "1.5.2"
}
```

Expand All @@ -85,11 +85,11 @@ plugins {
buildscript {
dependencies {
//Required items 👇
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:1.5.1'
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:1.5.2'
}
}
```
</details>
Expand Down Expand Up @@ -141,12 +141,12 @@ plugins {
dependencies {
//Required items 👇
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:1.5.1'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:1.5.1'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:1.5.2'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:1.5.2'
//Optional 👇, if you want to customize aspects, you need to use them, ⚠️supports aspects written in Java and Kotlin code
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:1.5.1'
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:1.5.2'
//Optional 👇, if you want to customize aspects, you need to use them, ⚠️only applies to aspects written in Java code
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:1.5.1'
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:1.5.2'
//⚠️Choose one of the above android-aop-ksp and android-aop-processor
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.flyjingfish.android_aop_annotation.base.BasePointCut;
import com.flyjingfish.android_aop_annotation.base.MatchClassMethod;
import com.flyjingfish.android_aop_annotation.utils.AndroidAopBeanUtils;
import com.flyjingfish.android_aop_annotation.utils.MethodMap;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand All @@ -22,17 +23,27 @@ public final class AndroidAopJoinPoint {
private Method targetMethod;
private Method originalMethod;
private String cutMatchClassName;
private String paramsKey;
private String methodKey;
private String targetClassName;

public AndroidAopJoinPoint(String targetClassName, Object target, String originalMethodName, String targetMethodName) {
// this.targetClassName = targetClassName;
try {
targetClass = Class.forName(targetClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(targetClassName + "的类名不可被混淆");
}
this.targetClassName = targetClassName;
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;

}


Expand All @@ -58,7 +69,7 @@ public Object joinPointExecute() {
String annotationName = annotation.annotationType().getName();
String cutClassName = AndroidAopBeanUtils.INSTANCE.getCutClassName(annotationName);
if (cutClassName != null) {
BasePointCut<Annotation> basePointCut = AndroidAopBeanUtils.INSTANCE.getBasePointCut(proceedJoinPoint, cutClassName, annotationName);
BasePointCut<Annotation> basePointCut = AndroidAopBeanUtils.INSTANCE.getBasePointCut(proceedJoinPoint, cutClassName, annotationName,targetClassName,methodKey);
if (basePointCut != null) {
PointCutAnnotation pointCutAnnotation = new PointCutAnnotation(annotation, basePointCut);
basePointCuts.add(pointCutAnnotation);
Expand All @@ -67,7 +78,7 @@ public Object joinPointExecute() {
}

if (cutMatchClassName != null) {
MatchClassMethod matchClassMethod = AndroidAopBeanUtils.INSTANCE.getMatchClassMethod(proceedJoinPoint, cutMatchClassName);
MatchClassMethod matchClassMethod = AndroidAopBeanUtils.INSTANCE.getMatchClassMethod(proceedJoinPoint, cutMatchClassName,targetClassName,methodKey);
PointCutAnnotation pointCutAnnotation = new PointCutAnnotation(matchClassMethod);
basePointCuts.add(pointCutAnnotation);
}
Expand Down Expand Up @@ -133,10 +144,37 @@ public String toString() {

public void setArgs(Object[] args) {
this.mArgs = args;
getTargetMethod();
}

private void getTargetMethod(){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("(");
if (mArgClassNames != null && mArgClassNames.length > 0){
int index = 0;
for (String argClassName : mArgClassNames) {
stringBuilder.append(argClassName);
if (index != mArgClassNames.length - 1){
stringBuilder.append(",");
}
index++;
}
}
stringBuilder.append(")");
paramsKey = stringBuilder.toString();
methodKey = originalMethodName + paramsKey;

String key = targetClassName +"-" + target + "-" + methodKey;
MethodMap methodMap = AndroidAopBeanUtils.INSTANCE.getMethodMapCache(key);
if (methodMap != null){
targetMethod = methodMap.getTargetMethod();
originalMethod = methodMap.getOriginalMethod();
return;
}
try {
Class<?>[] classes;
if (mArgClassNames != null && mArgClassNames.length > 0) {
classes = new Class[args.length];
classes = new Class[mArgClassNames.length];
int index = 0;
for (String className : mArgClassNames) {
try {
Expand All @@ -150,45 +188,25 @@ public void setArgs(Object[] args) {
} else {
classes = new Class<?>[0];
}
Class<?> tClass = null;
if (target != null) {
tClass = target.getClass();
}
if (tClass == null) {
tClass = targetClass;
}
Class<?> tClass = targetClass;
if (tClass == null) {
throw new RuntimeException("织入代码异常");
}
try {
targetMethod = tClass.getDeclaredMethod(targetMethodName, classes);
} catch (NoSuchMethodException e) {
try {
targetMethod = tClass.getMethod(targetMethodName, classes);
} catch (NoSuchMethodException ex) {
targetMethod = targetClass.getDeclaredMethod(targetMethodName, classes);
}
}
targetMethod = tClass.getDeclaredMethod(targetMethodName, classes);
try {
originalMethod = tClass.getDeclaredMethod(originalMethodName, classes);
} catch (NoSuchMethodException e) {
try {
originalMethod = tClass.getMethod(originalMethodName, classes);
} catch (NoSuchMethodException ex) {
try {
originalMethod = targetClass.getDeclaredMethod(originalMethodName, classes);
} catch (NoSuchMethodException exc) {
String realMethodName = getRealMethodName(originalMethodName);
if (realMethodName == null){
throw new RuntimeException(exc);
}
originalMethod = targetClass.getDeclaredMethod(realMethodName, classes);
}
} catch (NoSuchMethodException exc) {
String realMethodName = getRealMethodName(originalMethodName);
if (realMethodName == null){
throw new RuntimeException(exc);
}
originalMethod = tClass.getDeclaredMethod(realMethodName, classes);
}
targetMethod.setAccessible(true);
originalMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
methodMap = new MethodMap(originalMethod,targetMethod);
AndroidAopBeanUtils.INSTANCE.putMethodMapCache(key,methodMap,target);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal object AndroidAopBeanUtils {
private val mBasePointCutMap = ConcurrentHashMap<String, BasePointCut<Annotation>?>()
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 All @@ -22,15 +24,13 @@ internal object AndroidAopBeanUtils {
return JoinAnnoCutUtils.getCutClassName(className)
}

fun getBasePointCut(joinPoint: ProceedJoinPoint, clsName: String,annotationName : String): BasePointCut<Annotation>? {
val className = joinPoint.targetClass.name
val methodName = joinPoint.targetMethod.name
val key = "$className-${joinPoint.target}-$methodName-$annotationName"
fun getBasePointCut(joinPoint: ProceedJoinPoint, cutClassName: String, annotationName : String,targetClassName:String, methodKey : String): BasePointCut<Annotation>? {
val key = "$targetClassName-${joinPoint.target}-$methodKey-$annotationName"
var basePointCut: BasePointCut<Annotation>? = mBasePointCutMap[key]
if (basePointCut == null) {
basePointCut = getNewPointCut(clsName)
basePointCut = getNewPointCut(cutClassName)
mBasePointCutMap[key] = basePointCut
observeTarget(joinPoint, key)
observeTarget(joinPoint.target, key)
}else{
removeWeaklyReachableObjectsOnIOThread()
}
Expand All @@ -54,15 +54,13 @@ internal object AndroidAopBeanUtils {
}


fun getMatchClassMethod(joinPoint: ProceedJoinPoint, clsName: String): MatchClassMethod {
val className = joinPoint.targetClass.name
val methodName = joinPoint.targetMethod.name
val key = "$className-${joinPoint.target}-$methodName-$clsName"
fun getMatchClassMethod(joinPoint: ProceedJoinPoint, cutClassName: String, targetClassName:String,methodKey : String): MatchClassMethod {
val key = "$targetClassName-${joinPoint.target}-$methodKey-$cutClassName"
var matchClassMethod: MatchClassMethod? = mMatchClassMethodMap[key]
if (matchClassMethod == null) {
matchClassMethod = getNewMatchClassMethod(clsName)
matchClassMethod = getNewMatchClassMethod(cutClassName)
mMatchClassMethodMap[key] = matchClassMethod
observeTarget(joinPoint, key)
observeTarget(joinPoint.target, key)
}else{
removeWeaklyReachableObjectsOnIOThread()
}
Expand All @@ -83,9 +81,34 @@ internal object AndroidAopBeanUtils {
return matchClassMethod
}

private fun observeTarget(joinPoint: ProceedJoinPoint,key :String){
fun getMethodMapCache(key: String): MethodMap? {
val methodMap = mTargetMethodMap[key]
if (methodMap != null){
removeWeaklyReachableObjectsOnIOThread()
}
return methodMap
}

fun putMethodMapCache(key: String, methodMap:MethodMap, target:Any?) {
mTargetMethodMap[key] = methodMap
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{
val target = joinPoint.target
if (target != null){
val weakReference = KeyWeakReference(target,mTargetKeyReferenceQueue,key)
mTargetReferenceMap[key] = weakReference
Expand All @@ -108,6 +131,8 @@ internal object AndroidAopBeanUtils {
mTargetReferenceMap.remove(ref.key)
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
@@ -0,0 +1,5 @@
package com.flyjingfish.android_aop_annotation.utils

import java.lang.reflect.Method

class MethodMap(val originalMethod: Method,val targetMethod: Method)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ DEVELOPER_ID=FlyJingFish
DEVELOPER_NAME=FlyJingFish
DEVELOPER_EMAIL=[email protected]

TestVersion = 1.5.1
TestVersion = 1.5.2
SonatypeTestCode = 1395
TestType = 0
# 0 mavenLocal 1 SonatypeCache 2 mavenCentral
Expand Down
4 changes: 2 additions & 2 deletions version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Wed Apr 03 11:58:38 CST 2024
PROJ_VERSION=1.5.1
#Wed Apr 03 22:52:18 CST 2024
PROJ_VERSION=1.5.2

0 comments on commit c9e0d9d

Please sign in to comment.