Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:FlyJingFish/AndroidAOP
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyJingFish committed Nov 30, 2023
2 parents 25ec3cf + 0ea5612 commit 63c0d2e
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 27 deletions.
105 changes: 99 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
buildscript {
dependencies {
//必须项 👇
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:1.1.0'
classpath 'io.github.FlyJingFish.AndroidAop:android-aop-plugin:1.1.1'
}
}
plugins {
Expand Down Expand Up @@ -69,12 +69,12 @@ plugins {
dependencies {
//必须项 👇
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:1.1.0'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:1.1.0'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-core:1.1.1'
implementation 'io.github.FlyJingFish.AndroidAop:android-aop-annotation:1.1.1'
//非必须项 👇,如果你想自定义切面需要用到,⚠️支持Java和Kotlin代码写的切面
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:1.1.0'
ksp 'io.github.FlyJingFish.AndroidAop:android-aop-ksp:1.1.1'
//非必须项 👇,如果你想自定义切面需要用到,⚠️只适用于Java代码写的切面
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:1.1.0'
annotationProcessor 'io.github.FlyJingFish.AndroidAop:android-aop-processor:1.1.1'
//⚠️上边的 android-aop-ksp 和 android-aop-processor 二选一
}
```
Expand Down Expand Up @@ -121,7 +121,20 @@ android {

### 这块强调一下 @OnLifecycle

**@OnLifecycle 加到的方法所属对象必须是属于直接或间接继承自 FragmentActivity 或 Fragment的方法才有用,或者注解方法的对象实现 LifecycleOwner 也可以**
- **1、@OnLifecycle 加到的方法所属对象必须是属于直接或间接继承自 FragmentActivity 或 Fragment的方法才有用,或者注解方法的对象实现 LifecycleOwner 也可以**
- 2、如果第1点不符合的情况下,可以给切面方法第一个参数设置为第1点的类型,在调用切面方法传入也是可以的,例如:

```java
public class StaticClass {
@SingleClick(5000)
@OnLifecycle(Lifecycle.Event.ON_RESUME)
public static void onStaticPermission(MainActivity activity, int maxSelect , ThirdActivity.OnPhotoSelectListener back){
back.onBack();
}

}
```


### 下面再着重介绍下 @TryCatch @Permission @CustomIntercept

Expand Down Expand Up @@ -353,6 +366,86 @@ class MatchTestMatchMethod : MatchClassMethod {

- 又或者你想在三方库某个方法上设置切面,可以直接设置对应类名,对应方法,然后 type = MatchType.SELF,这样可以侵入三方库的代码,当然这么做记得修改上文提到的 androidAopConfig 的配置

#### 切面启示

1、不知道大家有没有这样的需求,有一个接口在多处使用,这种情况大家可能写一个工具类封装一下。

其实对于这种需求,可以做一个注解切面,在切面处理时可以在请求完数据后,给切面方法传回去即可,例如:

```kotlin
@AndroidAopPointCut(CommonDataCut::class)
@Target(
AnnotationTarget.FUNCTION
)
@Retention(AnnotationRetention.RUNTIME)
@Keep
annotation class CommonData
```
```kotlin
class CommonDataCut : BasePointCut<CommonData> {
override fun invoke(
joinPoint: ProceedJoinPoint,
anno: CommonData
): Any? {
// 在这写网络请求数据,数据返回后调用 joinPoint.proceed(data) 把数据传回方法
joinPoint.proceed(data)
return null
}
}
```
```kotlin
@CommonData
fun onTest(data:Data){
//因为切面已经把数据传回来了,所以数据不再为null
}
//在调用方法时随便传个null,当进入到切面后得到数据,在进入方法后数据就有了
binding.btnSingleClick.setOnClickListener {
onTest(null)
}

```
2、另外对于切面注解是没办法传入对象什么的,或者数据是动态的,那怎么办呢?

```kotlin
@AndroidAopPointCut(CommonDataCut::class)
@Target(
AnnotationTarget.FUNCTION
)
@Retention(AnnotationRetention.RUNTIME)
@Keep
annotation class CommonData

```
```kotlin
class CommonDataCut : BasePointCut<CommonData> {
override fun invoke(
joinPoint: ProceedJoinPoint,
anno: CommonData
): Any? {
if (!args.isNullOrEmpty()) {
val arg1 = args[0] // 这个就是传入的数据,这样可以随便往切面内传数据了


}
return joinPoint.proceed()
}
}

```
```kotlin
@CommonData
fun onTest(number:Int){

}

binding.btnSingleClick.setOnClickListener {
//在调用方法时传入动态数据
onTest(1)
}

```

3、综上所述,其实切面能给我们开发带来很多便携之处,关键看大家怎么用了

#### 混淆规则

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ public void setArgClassNames(String[] argClassNames) {
}

public Object joinPointExecute() {
ProceedJoinPoint proceedJoinPoint = new ProceedJoinPoint();
ProceedJoinPoint proceedJoinPoint = new ProceedJoinPoint(targetClass);
proceedJoinPoint.target = target;
proceedJoinPoint.args = mArgs;
proceedJoinPoint.setOriginalMethod(originalMethod);
proceedJoinPoint.setTargetMethod(targetMethod);
proceedJoinPoint.setTargetClass(targetClass);
Annotation[] annotations = originalMethod.getAnnotations();
Object[] returnValue = new Object[1];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package com.flyjingfish.android_aop_annotation;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public final class ProceedJoinPoint {
@Nullable
public Object[] args;
@Nullable
public Object target;
@NotNull
public Class<?> targetClass;
private Method targetMethod;
private Method originalMethod;
private AopMethod targetAopMethod;
private OnInvokeListener onInvokeListener;
private boolean hasNext;

public ProceedJoinPoint(Class<?> targetClass) {
this.targetClass = targetClass;
}

@Nullable
public Object proceed(){
return proceed(args);
}
@Nullable
public Object proceed(Object... args){
this.args = args;
try {
Expand All @@ -33,6 +46,7 @@ public Object proceed(Object... args){
}
}

@NotNull
public AopMethod getTargetMethod() {
return targetAopMethod;
}
Expand All @@ -46,15 +60,17 @@ void setOriginalMethod(Method originalMethod) {
targetAopMethod = new AopMethod(originalMethod);
}

@Nullable
public Object getTarget() {
return target;
}

@NotNull
public Class<?> getTargetClass() {
return targetClass;
}

void setTargetClass(Class<?> targetClass) {
void setTargetClass(@NotNull Class<?> targetClass) {
this.targetClass = targetClass;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.flyjingfish.android_aop_annotation.base.BasePointCut;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
Expand All @@ -14,5 +15,5 @@
@Target({ElementType.ANNOTATION_TYPE})
@Retention(SOURCE)
public @interface AndroidAopPointCut {
Class<? extends BasePointCut> value();
Class<? extends BasePointCut<? extends Annotation>> value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.flyjingfish.android_aop_annotation.base.BasePointCut
import com.flyjingfish.android_aop_annotation.base.MatchClassMethod
import java.util.concurrent.ConcurrentHashMap

object AndroidAopBeanUtils {
internal object AndroidAopBeanUtils {
private val basePointCutMap = ConcurrentHashMap<String, BasePointCut<Annotation>?>()
private val matchClassMethodMap = ConcurrentHashMap<String, MatchClassMethod?>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,66 @@ import com.flyjingfish.android_aop_core.utils.AppExecutors

class OnLifecycleCut : BasePointCut<OnLifecycle> {
override fun invoke(joinPoint: ProceedJoinPoint, anno: OnLifecycle): Any? {
if (Looper.getMainLooper() == Looper.myLooper()){
if (Looper.getMainLooper() == Looper.myLooper()) {
invokeLifecycle(joinPoint, anno)
}else{
} else {
AppExecutors.mainThread().execute {
invokeLifecycle(joinPoint, anno)
}
}
return null
}

private fun invokeLifecycle(joinPoint: ProceedJoinPoint, annotation: OnLifecycle){
private fun invokeLifecycle(joinPoint: ProceedJoinPoint, annotation: OnLifecycle) {
when (val target = joinPoint.target) {
is LifecycleOwner -> {
addObserver(target,joinPoint, annotation)
}
else -> {
val args = joinPoint.args
if (!args.isNullOrEmpty()) {
val arg1 = args[0]
if (arg1 is LifecycleOwner){
addObserver(arg1,joinPoint, annotation)
}else{
joinPoint.proceed()
}
}else{
joinPoint.proceed()
}
}
}

}

private fun addObserver(
lifecycleOwner: LifecycleOwner,
joinPoint: ProceedJoinPoint,
annotation: OnLifecycle
) {
when (lifecycleOwner) {
is Fragment -> {
target.viewLifecycleOwner.lifecycle.addObserver(object : LifecycleEventObserver{
lifecycleOwner.viewLifecycleOwner.lifecycle.addObserver(object :
LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
if (event == annotation.value){
if (event == annotation.value) {
source.lifecycle.removeObserver(this)
joinPoint.proceed()
}
}
})
}

is LifecycleOwner -> {
target.lifecycle.addObserver(object : LifecycleEventObserver{
else -> {
lifecycleOwner.lifecycle.addObserver(object : LifecycleEventObserver {
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
if (event == annotation.value){
if (event == annotation.value) {
source.lifecycle.removeObserver(this)
joinPoint.proceed()
}
}
})
}

else -> {
joinPoint.proceed()
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.flyjingfish.androidaop.test2;


import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.flyjingfish.android_aop_core.annotations.OnLifecycle;
import com.flyjingfish.android_aop_core.annotations.SingleClick;
import com.flyjingfish.androidaop.MainActivity;
import com.flyjingfish.androidaop.ThirdActivity;

public class StaticClass {
@SingleClick(5000)
@OnLifecycle(Lifecycle.Event.ON_RESUME)
public static void onStaticPermission(MainActivity activity, int maxSelect , ThirdActivity.OnPhotoSelectListener back){
back.onBack();
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ DEVELOPER_ID=FlyJingFish
DEVELOPER_NAME=FlyJingFish
DEVELOPER_EMAIL=[email protected]

TestVersion = 1.1.0
TestVersion = 1.1.1
SonatypeTestCode = 1297
LocalTest = true
4 changes: 2 additions & 2 deletions version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Thu Nov 30 12:28:58 CST 2023
PROJ_VERSION=1.1.0
#Thu Nov 30 22:52:48 CST 2023
PROJ_VERSION=1.1.1

0 comments on commit 63c0d2e

Please sign in to comment.