Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
milovetingting committed Apr 30, 2020
1 parent 4cb9382 commit 3904ab9
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 130 deletions.
33 changes: 0 additions & 33 deletions Proxy/app/src/main/java/com/wangyz/proxy/DynamicProxy.java

This file was deleted.

20 changes: 20 additions & 0 deletions Proxy/app/src/main/java/com/wangyz/proxy/EventType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.wangyz.proxy;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author wangyz
* @time 2020/4/30 8:58
* @description EventType
*/
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventType {

Class listenerType();

String listenerSetter();
}
64 changes: 39 additions & 25 deletions Proxy/app/src/main/java/com/wangyz/proxy/InjectHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.view.View;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
Expand All @@ -20,35 +21,48 @@ public static void inject(final Activity target) {
}
Class<? extends Activity> clz = target.getClass();
Method[] declaredMethods = clz.getDeclaredMethods();
for (final Method method : declaredMethods) {
if (method.isAnnotationPresent(OnClick.class)) {
OnClick annotation = method.getAnnotation(OnClick.class);
int[] resIds = annotation.value();
for (int resId : resIds) {
final View view = target.findViewById(resId);
final Object proxyInstance = Proxy.newProxyInstance(InjectHelper.class.getClassLoader(), new Class[]{View.OnClickListener.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return method.invoke(target, view);
for (Method method : declaredMethods) {
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (annotationType.isAnnotationPresent(EventType.class)) {
EventType eventType = annotationType.getAnnotation(EventType.class);
Class listenerType = eventType.listenerType();
String listenerSetter = eventType.listenerSetter();
try {
Method valueMethod = annotationType.getDeclaredMethod("value");
int[] ids = (int[]) valueMethod.invoke(annotation);
method.setAccessible(true);
ListenerInvocationHandler invocationHandler = new ListenerInvocationHandler(method, target);
Object proxyInstance = Proxy.newProxyInstance(target.getClassLoader(), new Class[]{listenerType}, invocationHandler);
for (int id : ids) {
View view = target.findViewById(id);
Method setter = view.getClass().getMethod(listenerSetter, listenerType);
setter.invoke(view, proxyInstance);
}
});
view.setOnClickListener((View.OnClickListener) proxyInstance);
}
} else if (method.isAnnotationPresent(OnLongClick.class)) {
OnLongClick annotation = method.getAnnotation(OnLongClick.class);
int[] resIds = annotation.value();
for (int resId : resIds) {
final View view = target.findViewById(resId);
final Object proxyInstance = Proxy.newProxyInstance(InjectHelper.class.getClassLoader(), new Class[]{View.OnLongClickListener.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
return method.invoke(target, view);
}
});
view.setOnLongClickListener((View.OnLongClickListener) proxyInstance);
} catch (Exception e) {

}
}
}
}
}

static class ListenerInvocationHandler<T> implements InvocationHandler {

private Method method;

private T target;

public ListenerInvocationHandler(Method method, T target) {
this.method = method;
this.target = target;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return this.method.invoke(target, args);
}
}

}
13 changes: 0 additions & 13 deletions Proxy/app/src/main/java/com/wangyz/proxy/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Player player = new PlayerImpl();
Player proxy = new ProxyImpl(player);
proxy.play();

DynamicProxy dynamicProxy = new DynamicProxy(player);
Player proxy2 = dynamicProxy.getProxy();
proxy2.play();

OtherPlayerImpl player2 = new OtherPlayerImpl();
dynamicProxy.setPlayer(player2);
proxy2.play();

InjectHelper.inject(this);
}

Expand Down
3 changes: 3 additions & 0 deletions Proxy/app/src/main/java/com/wangyz/proxy/OnClick.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.wangyz.proxy;

import android.view.View;

import androidx.annotation.IdRes;

import java.lang.annotation.ElementType;
Expand All @@ -12,6 +14,7 @@
* @time 2020/4/29 8:57
* @description OnClick
*/
@EventType(listenerType = View.OnClickListener.class,listenerSetter = "setOnClickListener")
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OnClick {
Expand Down
3 changes: 3 additions & 0 deletions Proxy/app/src/main/java/com/wangyz/proxy/OnLongClick.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.wangyz.proxy;

import android.view.View;

import androidx.annotation.IdRes;

import java.lang.annotation.ElementType;
Expand All @@ -12,6 +14,7 @@
* @time 2020/4/29 8:57
* @description OnClick
*/
@EventType(listenerType = View.OnLongClickListener.class, listenerSetter = "setOnLongClickListener")
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OnLongClick {
Expand Down
13 changes: 0 additions & 13 deletions Proxy/app/src/main/java/com/wangyz/proxy/OtherPlayerImpl.java

This file was deleted.

12 changes: 0 additions & 12 deletions Proxy/app/src/main/java/com/wangyz/proxy/Player.java

This file was deleted.

14 changes: 0 additions & 14 deletions Proxy/app/src/main/java/com/wangyz/proxy/PlayerImpl.java

This file was deleted.

20 changes: 0 additions & 20 deletions Proxy/app/src/main/java/com/wangyz/proxy/ProxyImpl.java

This file was deleted.

0 comments on commit 3904ab9

Please sign in to comment.