Skip to content

Commit 43e93bb

Browse files
author
Aleksander Mielczarek
committed
Remove RuntimePermissions from generated class
1 parent 47c46d6 commit 43e93bb

File tree

1 file changed

+24
-1
lines changed
  • androidannotationspermissionsdispatcherplugin/src/main/java/com/github/aleksandermielczarek/androidannotationspermissionsdispatcherplugin

1 file changed

+24
-1
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.github.aleksandermielczarek.androidannotationspermissionsdispatcherplugin;
22

33
import com.helger.jcodemodel.AbstractJClass;
4+
import com.helger.jcodemodel.JAnnotationUse;
45
import com.helger.jcodemodel.JBlock;
56
import com.helger.jcodemodel.JConditional;
7+
import com.helger.jcodemodel.JDefinedClass;
68
import com.helger.jcodemodel.JExpr;
79
import com.helger.jcodemodel.JFieldVar;
810
import com.helger.jcodemodel.JInvocation;
@@ -16,6 +18,9 @@
1618
import org.androidannotations.handler.BaseAnnotationHandler;
1719
import org.androidannotations.holder.EComponentHolder;
1820

21+
import java.lang.reflect.Field;
22+
import java.util.List;
23+
1924
import javax.lang.model.element.AnnotationMirror;
2025
import javax.lang.model.element.Element;
2126
import javax.lang.model.element.ExecutableElement;
@@ -81,7 +86,9 @@ public void process(Element element, EComponentHolder holder) throws Exception {
8186
}
8287
}
8388

84-
codeModelHelper.copyAnnotation(overrideMethod, findAnnotation(element));
89+
if (!removeRuntimePermissionsAnnotation(holder.getGeneratedClass())) {
90+
codeModelHelper.copyAnnotation(overrideMethod, findAnnotation(element));
91+
}
8592

8693
thenBlock.add(delegateCall);
8794

@@ -117,4 +124,20 @@ private boolean hasSpecialPermissions(AnnotationMirror annotationMirror) {
117124
private boolean isNeedsPermission(AnnotationMirror annotationMirror) {
118125
return annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("NeedsPermission");
119126
}
127+
128+
@SuppressWarnings("unchecked")
129+
private boolean removeRuntimePermissionsAnnotation(JDefinedClass activity) {
130+
try {
131+
Field annotationsField = activity.getClass().getDeclaredField("m_aAnnotations");
132+
annotationsField.setAccessible(true);
133+
List<JAnnotationUse> annotations = (List<JAnnotationUse>) annotationsField.get(activity);
134+
if (annotations == null) {
135+
return true;
136+
}
137+
annotations.removeIf(jAnnotationUse -> jAnnotationUse.getAnnotationClass().name().equals("RuntimePermissions"));
138+
return true;
139+
} catch (ClassCastException | NoSuchFieldException | IllegalAccessException e) {
140+
return false;
141+
}
142+
}
120143
}

0 commit comments

Comments
 (0)