Skip to content

Commit c9de8dd

Browse files
committed
Fix not compiling code when AA is running first
It is possible that the AA processor is running first, then the PermissionDispatcher processor. In that case, AA claims the @RuntimePermissions annotation, so the latter processor cannot generate the permission handling helper class. This commit removes the @RuntimePermissions handler from this plugin, so it always can be claimed by PermissionDispatcher, regardless of processor order. #7
1 parent 7a3ff98 commit c9de8dd

File tree

3 files changed

+21
-33
lines changed

3 files changed

+21
-33
lines changed

androidannotationspermissionsdispatcherplugin/src/main/java/com/github/aleksandermielczarek/androidannotationspermissionsdispatcherplugin/AndroidAnnotationsPermissionsDispatcherPlugin.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public String getName() {
2020
@Override
2121
public List<AnnotationHandler<?>> getHandlers(AndroidAnnotationsEnvironment androidAnnotationEnv) {
2222
List<AnnotationHandler<?>> handlers = new ArrayList<>();
23-
handlers.add(new RuntimePermissionsHandler(androidAnnotationEnv));
2423
handlers.add(new NeedsPermissionHandler(androidAnnotationEnv));
2524
return handlers;
2625
}

androidannotationspermissionsdispatcherplugin/src/main/java/com/github/aleksandermielczarek/androidannotationspermissionsdispatcherplugin/NeedsPermissionHandler.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
import com.helger.jcodemodel.JVar;
1111
import org.androidannotations.AndroidAnnotationsEnvironment;
1212
import org.androidannotations.ElementValidation;
13+
import org.androidannotations.annotations.EActivity;
14+
import org.androidannotations.annotations.EFragment;
1315
import org.androidannotations.handler.BaseAnnotationHandler;
1416
import org.androidannotations.holder.EComponentHolder;
1517

18+
import javax.lang.model.element.AnnotationMirror;
1619
import javax.lang.model.element.Element;
1720
import javax.lang.model.element.ExecutableElement;
1821
import javax.lang.model.element.TypeElement;
@@ -25,10 +28,12 @@ public NeedsPermissionHandler(AndroidAnnotationsEnvironment environment) {
2528

2629
@Override
2730
protected void validate(Element element, ElementValidation validation) {
28-
validatorHelper.enclosingElementHasEActivityOrEFragment(element, validation);
29-
validatorHelper.isNotPrivate(element, validation);
30-
validatorHelper.isNotFinal(element, validation);
31-
validatorHelper.returnTypeIsVoid((ExecutableElement) element, validation);
31+
if (validatorHelper.elementHasAnnotation(EActivity.class, element.getEnclosingElement())
32+
|| validatorHelper.elementHasAnnotation(EFragment.class, element.getEnclosingElement())) {
33+
validatorHelper.isNotPrivate(element, validation);
34+
validatorHelper.isNotFinal(element, validation);
35+
validatorHelper.returnTypeIsVoid((ExecutableElement) element, validation);
36+
}
3237
}
3338

3439
@Override
@@ -64,10 +69,22 @@ public void process(Element element, EComponentHolder holder) throws Exception {
6469
delegateCall.arg(overrideMethod.varParam());
6570
}
6671

72+
codeModelHelper.copyAnnotation(overrideMethod, findAnnotation(element));
73+
6774
thenBlock.add(delegateCall);
6875

6976
JBlock elseBlock = conditional._else();
7077
elseBlock.assign(dispatcherCalledField, JExpr.FALSE);
7178
elseBlock.add(previousMethodBody);
7279
}
80+
81+
private AnnotationMirror findAnnotation(Element element) {
82+
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
83+
if (annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("NeedsPermission")) {
84+
return annotationMirror;
85+
}
86+
}
87+
88+
throw new IllegalStateException("Handled annotation should be on the method!");
89+
}
7390
}

androidannotationspermissionsdispatcherplugin/src/main/java/com/github/aleksandermielczarek/androidannotationspermissionsdispatcherplugin/RuntimePermissionsHandler.java

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

0 commit comments

Comments
 (0)