11package com .github .aleksandermielczarek .androidannotationspermissionsdispatcherplugin ;
22
33import com .helger .jcodemodel .AbstractJClass ;
4+ import com .helger .jcodemodel .JAnnotationUse ;
45import com .helger .jcodemodel .JBlock ;
56import com .helger .jcodemodel .JConditional ;
7+ import com .helger .jcodemodel .JDefinedClass ;
68import com .helger .jcodemodel .JExpr ;
79import com .helger .jcodemodel .JFieldVar ;
810import com .helger .jcodemodel .JInvocation ;
1618import org .androidannotations .handler .BaseAnnotationHandler ;
1719import org .androidannotations .holder .EComponentHolder ;
1820
21+ import java .lang .reflect .Field ;
22+ import java .util .List ;
23+
1924import javax .lang .model .element .AnnotationMirror ;
2025import javax .lang .model .element .Element ;
2126import 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