Skip to content

Commit d6e2a6a

Browse files
committed
made SubscriberInfo an interface, removed next subscriber info class, added shouldCheckSuperclass
1 parent 9f81069 commit d6e2a6a

File tree

5 files changed

+115
-65
lines changed

5 files changed

+115
-65
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (C) 2012-2015 Markus Junginger, greenrobot (http://greenrobot.de)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.greenrobot.event;
17+
18+
import java.lang.reflect.Method;
19+
20+
/** Base class for generated subscriber meta info classes created by annotation processing. */
21+
public abstract class AbstractSubscriberInfo implements SubscriberInfo {
22+
private final Class subscriberClass;
23+
private final Class<? extends SubscriberInfo> superSubscriberInfoClass;
24+
private final boolean shouldCheckSuperclass;
25+
26+
protected AbstractSubscriberInfo(Class subscriberClass, Class<? extends SubscriberInfo> superSubscriberInfoClass,
27+
boolean shouldCheckSuperclass) {
28+
this.subscriberClass = subscriberClass;
29+
this.superSubscriberInfoClass = superSubscriberInfoClass;
30+
this.shouldCheckSuperclass = shouldCheckSuperclass;
31+
}
32+
33+
@Override
34+
public Class getSubscriberClass() {
35+
return subscriberClass;
36+
}
37+
38+
@Override
39+
public Class<? extends SubscriberInfo> getSuperSubscriberInfoClass() {
40+
return superSubscriberInfoClass;
41+
}
42+
43+
@Override
44+
public boolean shouldCheckSuperclass() {
45+
return shouldCheckSuperclass;
46+
}
47+
48+
protected SubscriberMethod createSubscriberMethod(String methodName, Class<?> eventType) {
49+
return createSubscriberMethod(methodName, eventType, ThreadMode.POSTING, 0, false);
50+
}
51+
52+
protected SubscriberMethod createSubscriberMethod(String methodName, Class<?> eventType, ThreadMode threadMode) {
53+
return createSubscriberMethod(methodName, eventType, threadMode, 0, false);
54+
}
55+
56+
protected SubscriberMethod createSubscriberMethod(String methodName, Class<?> eventType, ThreadMode threadMode,
57+
int priority, boolean sticky) {
58+
try {
59+
Method method = subscriberClass.getDeclaredMethod(methodName, eventType);
60+
return new SubscriberMethod(method, eventType, threadMode, priority, sticky);
61+
} catch (NoSuchMethodException e) {
62+
throw new EventBusException("Could not find subscriber method in " + subscriberClass +
63+
". Maybe a missing ProGuard rule?", e);
64+
}
65+
}
66+
67+
}
Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,27 @@
1+
/*
2+
* Copyright (C) 2012-2015 Markus Junginger, greenrobot (http://greenrobot.de)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package de.greenrobot.event;
217

3-
import java.lang.reflect.Method;
4-
518
/** Base class for generated index classes created by annotation processing. */
6-
public abstract class SubscriberInfo {
7-
final Class subscriberClass;
8-
final Class superSubscriberInfoClass;
9-
final Class nextSubscriberInfoClass;
10-
11-
protected SubscriberInfo(Class subscriberClass, Class superSubscriberInfoClass, Class nextSubscriberInfoClass) {
12-
this.subscriberClass = subscriberClass;
13-
this.superSubscriberInfoClass = superSubscriberInfoClass;
14-
this.nextSubscriberInfoClass = nextSubscriberInfoClass;
15-
}
16-
17-
abstract protected SubscriberMethod[] createSubscriberMethods();
18-
19-
protected SubscriberMethod createSubscriberMethod(String methodName, Class<?> eventType) {
20-
return createSubscriberMethod(methodName, eventType, ThreadMode.POSTING, 0, false);
21-
22-
}
19+
public interface SubscriberInfo {
20+
Class<?> getSubscriberClass();
2321

24-
protected SubscriberMethod createSubscriberMethod(String methodName, Class<?> eventType, ThreadMode threadMode) {
25-
return createSubscriberMethod(methodName, eventType, threadMode, 0, false);
26-
}
22+
SubscriberMethod[] getSubscriberMethods();
2723

28-
protected SubscriberMethod createSubscriberMethod(String methodName, Class<?> eventType, ThreadMode threadMode,
29-
int priority, boolean sticky) {
30-
try {
31-
Method method = subscriberClass.getDeclaredMethod(methodName, eventType);
32-
return new SubscriberMethod(method, eventType, threadMode, priority, sticky);
33-
} catch (NoSuchMethodException e) {
34-
throw new EventBusException("Could not find subscriber method in " + subscriberClass +
35-
". Maybe a missing ProGuard rule?", e);
36-
}
37-
}
24+
Class<? extends SubscriberInfo> getSuperSubscriberInfoClass();
3825

26+
boolean shouldCheckSuperclass();
3927
}

EventBus/src/de/greenrobot/event/SubscriberInfoIndex.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright (C) 2012-2015 Markus Junginger, greenrobot (http://greenrobot.de)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package de.greenrobot.event;
217

318
/**

EventBus/src/de/greenrobot/event/SubscriberMethodFinder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
7575
while (findState.clazz != null) {
7676
findState.subscriberInfo = getSubscriberInfo(findState);
7777
if (findState.subscriberInfo != null) {
78-
SubscriberMethod[] array = findState.subscriberInfo.createSubscriberMethods();
78+
SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
7979
for (SubscriberMethod subscriberMethod : array) {
8080
if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
8181
findState.subscriberMethods.add(subscriberMethod);
@@ -117,10 +117,10 @@ private FindState prepareFindState() {
117117
}
118118

119119
private SubscriberInfo getSubscriberInfo(FindState findState) {
120-
if (findState.subscriberInfo != null && findState.subscriberInfo.superSubscriberInfoClass != null) {
120+
if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfoClass() != null) {
121121
try {
122-
SubscriberInfo superclassInfo = (SubscriberInfo) findState.subscriberInfo.superSubscriberInfoClass.newInstance();
123-
if (findState.clazz == superclassInfo.subscriberClass) {
122+
SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfoClass().newInstance();
123+
if (findState.clazz == superclassInfo.getSubscriberClass()) {
124124
return superclassInfo;
125125
}
126126
} catch (IllegalAccessException | InstantiationException e) {

EventBusAnnotationProcessor/src/de/greenrobot/event/annotationprocessor/EventBusAnnotationProcessor.java

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -212,21 +212,19 @@ private void createInfoFiles() {
212212
JavaFileObject sourceFile = processingEnv.getFiler().createSourceFile(myPackage + '.' + infoClassName);
213213
writer = new BufferedWriter(sourceFile.openWriter());
214214
writer.write("package " + myPackage + ";\n\n");
215-
writer.write("import de.greenrobot.event.SubscriberInfo;\n");
215+
writer.write("import de.greenrobot.event.AbstractSubscriberInfo;\n");
216216
writer.write("import de.greenrobot.event.SubscriberMethod;\n");
217217
writer.write("import de.greenrobot.event.ThreadMode;\n\n");
218218
writer.write("/** This class is generated by EventBus, do not edit. */\n");
219-
writer.write("public class " + infoClassName + " extends SubscriberInfo {\n");
219+
writer.write("public class " + infoClassName + " extends AbstractSubscriberInfo {\n");
220220
writer.write(" public " + infoClassName + "() {\n");
221-
TypeElement nextEntry = nextEntry(entries, entry, i);
222-
String next = getNextValue(myPackage, nextEntry);
223221
String infoSuperClass = getSuperclassInfoClass(subscriberClass, myPackage);
224-
writeLine(writer, 2, "super(" + subscriberClassName + ".class,", infoSuperClass + ",", next + ");");
222+
writeLine(writer, 2, "super(" + subscriberClassName + ".class,", infoSuperClass + ",", "/* TODO */ true);");
225223
writer.write(" }\n\n");
226-
writer.write(" protected SubscriberMethod[] createSubscriberMethods() {\n");
224+
writer.write(" public SubscriberMethod[] getSubscriberMethods() {\n");
227225
writer.write(" return new SubscriberMethod[] {\n");
228-
Set<String> methodSignatures = new HashSet<String>();
229-
writeMethods(writer, entry.getValue(), methodSignatures, myPackage);
226+
Set<String> methodSignatures = new HashSet<>();
227+
writeCreateSubscriberMethods(writer, entry.getValue(), methodSignatures, myPackage);
230228
writer.write(" };\n");
231229
writer.write(" }\n}\n");
232230
} catch (IOException e) {
@@ -307,27 +305,9 @@ private PackageElement getPackageElement(TypeElement subscriberClass) {
307305
return (PackageElement) candidate;
308306
}
309307

310-
private TypeElement nextEntry(List<Map.Entry<TypeElement, List<ExecutableElement>>> entries,
311-
Map.Entry<TypeElement, List<ExecutableElement>> current, int currentIdx) {
312-
for (int i = currentIdx + 1; ; i++) {
313-
if (i == entries.size()) {
314-
i = 0;
315-
}
316-
if (i == currentIdx) {
317-
return null;
318-
} else {
319-
Map.Entry<TypeElement, List<ExecutableElement>> candidate = entries.get(i);
320-
if (!classesToSkip.contains(candidate.getKey())) {
321-
return candidate.getKey();
322-
}
323-
}
324-
}
325-
}
326-
327-
private void writeMethods(BufferedWriter writer, List<ExecutableElement> methods, Set<String> methodSignatures,
328-
String myPackage) throws IOException {
308+
private void writeCreateSubscriberMethods(BufferedWriter writer, List<ExecutableElement> methods, Set<String> methodSignatures,
309+
String myPackage) throws IOException {
329310
for (ExecutableElement method : methods) {
330-
331311
List<? extends VariableElement> parameters = method.getParameters();
332312
TypeMirror paramType = parameters.get(0).asType();
333313
TypeElement paramElement = (TypeElement) processingEnv.getTypeUtils().asElement(paramType);
@@ -341,7 +321,7 @@ private void writeMethods(BufferedWriter writer, List<ExecutableElement> methods
341321
String methodName = method.getSimpleName().toString();
342322

343323
Subscribe subscribe = method.getAnnotation(Subscribe.class);
344-
List<String> parts = new ArrayList<String>();
324+
List<String> parts = new ArrayList<>();
345325
parts.add("createSubscriberMethod(\"" + methodName + "\",");
346326
String lineEnd = "),";
347327
if (subscribe.priority() == 0 && !subscribe.sticky()) {

0 commit comments

Comments
 (0)