3333@ SupportedAnnotationTypes ("org.greenrobot.eventbus.Subscribe" )
3434@ SupportedOptions ("eventBusIndex" )
3535public class EventBusAnnotationProcessor extends AbstractProcessor {
36- public static final String INFO_CLASS_POSTFIX = "_EventBusInfo" ;
3736 public static final String OPTION_EVENT_BUS_INDEX = "eventBusIndex" ;
3837
3938 /** Found subscriber methods for a class (without superclasses). */
@@ -52,6 +51,15 @@ public SourceVersion getSupportedSourceVersion() {
5251 public boolean process (Set <? extends TypeElement > annotations , RoundEnvironment env ) {
5352 Messager messager = processingEnv .getMessager ();
5453 try {
54+ String index = processingEnv .getOptions ().get (OPTION_EVENT_BUS_INDEX );
55+ if (index == null ) {
56+ messager .printMessage (Diagnostic .Kind .ERROR , "No option " + OPTION_EVENT_BUS_INDEX +
57+ " passed to annotation processor" );
58+ return false ;
59+ }
60+ int lastPeriod = index .lastIndexOf ('.' );
61+ String indexPackage = lastPeriod != -1 ? index .substring (0 , lastPeriod ) : null ;
62+
5563 round ++;
5664 messager .printMessage (Diagnostic .Kind .NOTE , "Processing round " + round + ", new annotations: " +
5765 !annotations .isEmpty () + ", processingOver: " + env .processingOver ());
@@ -71,18 +79,10 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
7179 "Unexpected processing state: annotations still available after writing." );
7280 }
7381 collectSubscribers (annotations , env , messager );
74- checkForSubscribersToSkip (messager );
82+ checkForSubscribersToSkip (messager , indexPackage );
7583
7684 if (!methodsByClass .isEmpty ()) {
77- // Nor now, we just use a single index and skip individual files: createInfoFiles();
78-
79- String index = processingEnv .getOptions ().get (OPTION_EVENT_BUS_INDEX );
80- if (index != null ) {
81- createInfoIndexFile (index );
82- } else {
83- messager .printMessage (Diagnostic .Kind .ERROR , "No option " + OPTION_EVENT_BUS_INDEX +
84- " passed to annotation processor." );
85- }
85+ createInfoIndexFile (index );
8686 } else {
8787 messager .printMessage (Diagnostic .Kind .WARNING , "No @Subscribe annotations found" );
8888 }
@@ -136,12 +136,12 @@ private boolean checkHasNoErrors(ExecutableElement element, Messager messager) {
136136 return true ;
137137 }
138138
139- private void checkForSubscribersToSkip (Messager messager ) {
139+ private void checkForSubscribersToSkip (Messager messager , String myPackage ) {
140140 for (Map .Entry <TypeElement , List <ExecutableElement >> entry : methodsByClass .entrySet ()) {
141141 TypeElement skipCandidate = entry .getKey ();
142142 TypeElement subscriberClass = skipCandidate ;
143143 while (subscriberClass != null ) {
144- if (!subscriberClass . getModifiers (). contains ( Modifier . PUBLIC )) {
144+ if (!isVisible ( myPackage , subscriberClass )) {
145145 boolean added = classesToSkip .add (skipCandidate );
146146 if (added ) {
147147 String msg ;
@@ -159,9 +159,8 @@ private void checkForSubscribersToSkip(Messager messager) {
159159 if (methods != null ) {
160160 for (ExecutableElement method : methods ) {
161161 VariableElement param = method .getParameters ().get (0 );
162- DeclaredType paramType = (DeclaredType ) param .asType ();
163- Set <Modifier > eventClassModifiers = paramType .asElement ().getModifiers ();
164- if (!eventClassModifiers .contains (Modifier .PUBLIC )) {
162+ TypeElement eventTypeElement = (TypeElement ) ((DeclaredType ) param .asType ()).asElement ();
163+ if (!isVisible (myPackage , eventTypeElement )) {
165164 boolean added = classesToSkip .add (skipCandidate );
166165 if (added ) {
167166 String msg ;
0 commit comments