2222import javax .lang .model .element .VariableElement ;
2323import javax .lang .model .type .DeclaredType ;
2424import javax .lang .model .type .TypeKind ;
25+ import javax .lang .model .type .TypeMirror ;
2526import javax .tools .Diagnostic ;
2627import javax .tools .JavaFileObject ;
2728
3132@ SupportedAnnotationTypes ("de.greenrobot.event.Subscribe" )
3233public class EventBusAnnotationProcessor extends AbstractProcessor {
3334 public static final String CLASS_POSTFIX = "_EventBusInfo" ;
35+ public static final String JAVA_LANG_PREFIX = "java.lang." ;
36+ public static final int JAVA_LANG_PREFIX_LENGTH = JAVA_LANG_PREFIX .length ();
3437
3538 /** Found subscriber methods for a class (without superclasses). */
3639 private final Map <TypeElement , List <ExecutableElement >> methodsByClass =
@@ -220,7 +223,7 @@ private void writeSources() {
220223 writer .write (" protected SubscriberMethod[] createSubscriberMethods() {\n " );
221224 writer .write (" return new SubscriberMethod[] {\n " );
222225 Set <String > methodSignatures = new HashSet <String >();
223- writeMethods (writer , entry .getValue (), methodSignatures );
226+ writeMethods (writer , entry .getValue (), methodSignatures , myPackage );
224227 writer .write (" };\n " );
225228 writer .write (" }\n }\n " );
226229 } catch (IOException e ) {
@@ -269,10 +272,15 @@ private String getNextValue(String myPackage, TypeElement nextEntry) throws IOEx
269272 return nextValue ;
270273 }
271274
272- private String getClassString (TypeElement subscriberClass , String subscriberPackage ) {
273- String className = subscriberClass .getQualifiedName ().toString ();
274- if (!subscriberPackage .isEmpty () && className .startsWith (subscriberPackage )) {
275- className = className .substring (subscriberPackage .length () + 1 );
275+ private String getClassString (TypeElement typeElement , String myPackage ) {
276+ String className = typeElement .getQualifiedName ().toString ();
277+ int lastPeriod = className .lastIndexOf ('.' );
278+ if (!myPackage .isEmpty () && className .startsWith (myPackage ) && lastPeriod == myPackage .length ()) {
279+ // TODO detect nested types also
280+
281+ className = className .substring (myPackage .length () + 1 );
282+ } else if (className .startsWith (JAVA_LANG_PREFIX ) && lastPeriod == JAVA_LANG_PREFIX_LENGTH - 1 ) {
283+ className = className .substring (JAVA_LANG_PREFIX_LENGTH );
276284 }
277285 return className ;
278286 }
@@ -302,14 +310,16 @@ private TypeElement nextEntry(List<Map.Entry<TypeElement, List<ExecutableElement
302310 }
303311 }
304312
305- private void writeMethods (BufferedWriter writer , List <ExecutableElement > methods , Set <String > methodSignatures ) throws IOException {
313+ private void writeMethods (BufferedWriter writer , List <ExecutableElement > methods , Set <String > methodSignatures ,
314+ String myPackage ) throws IOException {
306315 for (ExecutableElement method : methods ) {
307316
308317 List <? extends VariableElement > parameters = method .getParameters ();
309- VariableElement param = parameters .get (0 );
310- DeclaredType paramType = (DeclaredType ) param .asType ();
318+ TypeMirror paramType = parameters .get (0 ).asType ();
319+ TypeElement paramElement = (TypeElement ) processingEnv .getTypeUtils ().asElement (paramType );
320+ String eventClass = getClassString (paramElement , myPackage ) + ".class" ;
311321
312- String methodSignature = method + ">" + paramType ;
322+ String methodSignature = method + ">" + paramElement . getQualifiedName () ;
313323 if (!methodSignatures .add (methodSignature )) {
314324 continue ;
315325 }
@@ -322,13 +332,13 @@ private void writeMethods(BufferedWriter writer, List<ExecutableElement> methods
322332 String lineEnd = ")," ;
323333 if (subscribe .priority () == 0 && !subscribe .sticky ()) {
324334 if (subscribe .threadMode () == ThreadMode .POSTING ) {
325- parts .add (paramType . toString () + ".class" + lineEnd );
335+ parts .add (eventClass + lineEnd );
326336 } else {
327- parts .add (paramType . toString () + ".class ," );
337+ parts .add (eventClass + "," );
328338 parts .add ("ThreadMode." + subscribe .threadMode ().name () + lineEnd );
329339 }
330340 } else {
331- parts .add (paramType . toString () + ".class ," );
341+ parts .add (eventClass + "," );
332342 parts .add ("ThreadMode." + subscribe .threadMode ().name () + "," );
333343 parts .add (subscribe .priority () + "," );
334344 parts .add (subscribe .sticky () + lineEnd );
@@ -337,7 +347,7 @@ private void writeMethods(BufferedWriter writer, List<ExecutableElement> methods
337347
338348 processingEnv .getMessager ().printMessage (Diagnostic .Kind .NOTE , "Indexed @Subscribe at " +
339349 method .getEnclosingElement ().getSimpleName () + "." + methodName +
340- "(" + paramType . asElement () .getSimpleName () + ")" );
350+ "(" + paramElement .getSimpleName () + ")" );
341351
342352 }
343353 }
0 commit comments