Skip to content

Commit c0c8c2d

Browse files
committed
Remove @classname and @MethodName, add @RemoteInterface instead.
1 parent f406551 commit c0c8c2d

File tree

6 files changed

+34
-93
lines changed

6 files changed

+34
-93
lines changed

andlinker/proguard-rules.pro

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
#-renamesourcefileattribute SourceFile
2222

2323
-dontwarn com.codezjx.andlinker.adapter.**
24-
-keep class * implements android.os.Parcelable {*;}
25-
-keep @com.codezjx.andlinker.annotation.ClassName class *
26-
-keepclassmembers class * {
27-
@com.codezjx.andlinker.annotation.MethodName <methods>;
28-
}
24+
-keep class * implements android.os.Parcelable {
25+
public void readFromParcel(android.os.Parcel);
26+
}
27+
-keep @com.codezjx.andlinker.annotation.RemoteInterface class * {*;}

andlinker/src/main/java/com/codezjx/andlinker/Invoker.java

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import android.os.RemoteCallbackList;
55
import android.os.RemoteException;
66

7-
import com.codezjx.andlinker.annotation.ClassName;
8-
import com.codezjx.andlinker.annotation.MethodName;
7+
import com.codezjx.andlinker.annotation.RemoteInterface;
98

109
import java.lang.annotation.Annotation;
1110
import java.lang.reflect.InvocationHandler;
@@ -31,14 +30,14 @@ final class Invoker {
3130
}
3231

3332
private void handleCallbackClass(Class<?> clazz, boolean isRegister) {
34-
ClassName className = clazz.getAnnotation(ClassName.class);
35-
if (className == null) {
36-
throw new IllegalArgumentException("Callback interface doesn't has any annotation.");
33+
if (!clazz.isAnnotationPresent(RemoteInterface.class)) {
34+
throw new IllegalArgumentException("Callback interface doesn't has @RemoteInterface annotation.");
3735
}
36+
String className = clazz.getSimpleName();
3837
if (isRegister) {
39-
mCallbackClassTypes.putIfAbsent(className.value(), clazz);
38+
mCallbackClassTypes.putIfAbsent(className, clazz);
4039
} else {
41-
mCallbackClassTypes.remove(className.value());
40+
mCallbackClassTypes.remove(className);
4241
}
4342
}
4443

@@ -51,11 +50,11 @@ private void handleObject(Object target, boolean isRegister) {
5150
throw new IllegalArgumentException("Remote object must extend just one interface.");
5251
}
5352
Class<?> clazz = interfaces[0];
54-
ClassName classNameAnnotation = clazz.getAnnotation(ClassName.class);
55-
if (classNameAnnotation == null) {
56-
throw new IllegalArgumentException("Interface doesn't has any annotation.");
53+
if (!clazz.isAnnotationPresent(RemoteInterface.class)) {
54+
throw new IllegalArgumentException("Interface doesn't has @RemoteInterface annotation.");
5755
}
5856
// Cache all annotation method
57+
String clsName = clazz.getSimpleName();
5958
Method[] methods = clazz.getDeclaredMethods();
6059
for (Method method : methods) {
6160
// The compiler sometimes creates synthetic bridge methods as part of the
@@ -65,21 +64,17 @@ private void handleObject(Object target, boolean isRegister) {
6564
if (method.isBridge()) {
6665
continue;
6766
}
68-
MethodName methodNameAnnotation = method.getAnnotation(MethodName.class);
69-
if (methodNameAnnotation != null) {
70-
String clsName = classNameAnnotation.value();
71-
String methodName = methodNameAnnotation.value();
72-
String key = createMethodExecutorKey(clsName, methodName);
73-
if (isRegister) {
74-
MethodExecutor executor = new MethodExecutor(target, method);
75-
MethodExecutor preExecutor = mMethodExecutors.putIfAbsent(key, executor);
76-
if (preExecutor != null) {
77-
throw new IllegalStateException("Key conflict with class:" + clsName + " method:" + methodName
78-
+ ". Please try another class/method name with annotation @ClassName/@MethodName.");
79-
}
80-
} else {
81-
mMethodExecutors.remove(key);
67+
String methodName = method.getName();
68+
String key = createMethodExecutorKey(clsName, methodName);
69+
if (isRegister) {
70+
MethodExecutor executor = new MethodExecutor(target, method);
71+
MethodExecutor preExecutor = mMethodExecutors.putIfAbsent(key, executor);
72+
if (preExecutor != null) {
73+
throw new IllegalStateException("Key conflict with class:" + clsName + " method:" + methodName
74+
+ ". Please try another class/method name.");
8275
}
76+
} else {
77+
mMethodExecutors.remove(key);
8378
}
8479
// Cache callback class if exist
8580
Class<?>[] paramCls = method.getParameterTypes();
@@ -156,7 +151,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
156151
int cookiePid = (int) mCallbackList.getBroadcastCookie(i);
157152
if (cookiePid == pid) {
158153
try {
159-
Request request = createCallbackRequest(parseClassName(service), parseMethodName(method), args);
154+
Request request = createCallbackRequest(service.getSimpleName(), method.getName(), args);
160155
Response response = mCallbackList.getBroadcastItem(i).callback(request);
161156
result = response.getResult();
162157
if (response.getStatusCode() != Response.STATUS_CODE_SUCCESS) {
@@ -182,24 +177,6 @@ private Request createCallbackRequest(String targetClass, String methodName, Obj
182177
return new Request(targetClass, methodName, wrappers);
183178
}
184179

185-
private String parseClassName(Class<?> clazz) {
186-
ClassName className = clazz.getAnnotation(ClassName.class);
187-
String classNameStr = "";
188-
if (className != null) {
189-
classNameStr = className.value();
190-
}
191-
return classNameStr;
192-
}
193-
194-
private String parseMethodName(Method method) {
195-
MethodName methodName = method.getAnnotation(MethodName.class);
196-
String methodNameStr = "";
197-
if (methodName != null) {
198-
methodNameStr = methodName.value();
199-
}
200-
return methodNameStr;
201-
}
202-
203180
private Class<?> getCallbackClass(String className) {
204181
return mCallbackClassTypes.get(className);
205182
}

andlinker/src/main/java/com/codezjx/andlinker/ParameterHandler.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.codezjx.andlinker;
22

3-
import com.codezjx.andlinker.annotation.ClassName;
43
import com.codezjx.andlinker.annotation.In;
54
import com.codezjx.andlinker.annotation.Inout;
65
import com.codezjx.andlinker.annotation.Out;
@@ -25,11 +24,7 @@ final class CallbackHandler<T> implements ParameterHandler<T> {
2524
@Override
2625
public void apply(RequestBuilder builder, T value, int index) {
2726
Logger.d("CallbackHandler", "ParameterHandler mParamType:" + mParamType + " value:" + value);
28-
ClassName annotation = mParamType.getAnnotation(ClassName.class);
29-
String className = (annotation != null) ? annotation.value() : "";
30-
if (Utils.isStringBlank(className)) {
31-
throw new IllegalArgumentException("Callback type must provide @ClassName");
32-
}
27+
String className = mParamType.getSimpleName();
3328
CallbackTypeWrapper wrapper = new CallbackTypeWrapper(className);
3429
builder.applyWrapper(index, wrapper);
3530
}

andlinker/src/main/java/com/codezjx/andlinker/ServiceMethod.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.codezjx.andlinker;
22

33
import com.codezjx.andlinker.annotation.Callback;
4-
import com.codezjx.andlinker.annotation.ClassName;
54
import com.codezjx.andlinker.annotation.In;
65
import com.codezjx.andlinker.annotation.Inout;
7-
import com.codezjx.andlinker.annotation.MethodName;
86
import com.codezjx.andlinker.annotation.OneWay;
97
import com.codezjx.andlinker.annotation.Out;
108

@@ -78,10 +76,14 @@ static final class Builder {
7876

7977
ServiceMethod build() {
8078
mCallAdapter = createCallAdapter();
81-
parseClassName(mMethod);
79+
mClassName = mMethod.getDeclaringClass().getSimpleName();
80+
mMethodName = mMethod.getName();
8281

8382
for (Annotation annotation : mMethodAnnotations) {
84-
parseMethodAnnotation(annotation);
83+
if (annotation instanceof OneWay) {
84+
mOneWay = true;
85+
break;
86+
}
8587
}
8688

8789
int parameterCount = mParameterAnnotationsArray.length;
@@ -100,13 +102,6 @@ ServiceMethod build() {
100102
return new ServiceMethod(this);
101103
}
102104

103-
private void parseClassName(Method method) {
104-
ClassName className = method.getDeclaringClass().getAnnotation(ClassName.class);
105-
if (className != null) {
106-
mClassName = className.value();
107-
}
108-
}
109-
110105
private CallAdapter createCallAdapter() {
111106
Type returnType = mMethod.getGenericReturnType();
112107
if (Utils.hasUnresolvableType(returnType)) {
@@ -122,14 +117,6 @@ private CallAdapter createCallAdapter() {
122117
}
123118
}
124119

125-
private void parseMethodAnnotation(Annotation annotation) {
126-
if (annotation instanceof MethodName) {
127-
mMethodName = ((MethodName) annotation).value();
128-
} else if (annotation instanceof OneWay) {
129-
mOneWay = true;
130-
}
131-
}
132-
133120
private ParameterHandler<?> parseParameter(int p, Type parameterType, Annotation[] annotations) {
134121
Class<?> rawParameterType = Utils.getRawType(parameterType);
135122
if (annotations == null || annotations.length == 0) {

andlinker/src/main/java/com/codezjx/andlinker/annotation/MethodName.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

andlinker/src/main/java/com/codezjx/andlinker/annotation/ClassName.java renamed to andlinker/src/main/java/com/codezjx/andlinker/annotation/RemoteInterface.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import static java.lang.annotation.RetentionPolicy.RUNTIME;
88

99
/**
10-
* Specify the remote service interface name, usually a full class name, eg: com.your.package.InterfaceName.
10+
* Specify the interface as remote service interface.
1111
*/
1212
@Target(TYPE)
1313
@Retention(RUNTIME)
14-
public @interface ClassName {
15-
String value();
14+
public @interface RemoteInterface {
15+
1616
}

0 commit comments

Comments
 (0)