Comments (23)
Rather than checking the class name for "Activity" or "Fragment", the processor should check if the class annotated with @RuntimePermissions
is a subtype of android.app.Activity
, android.app.Fragment
or android.support.v4.app.Fragment
.
In an annotation processor I recently wrote, I also only allow for some annotated elements to be of a certain type. Applying this to PermissionsDispatcher could look something like this:
// PermissionsProcessor.java:
public synchronized void init(ProcessingEnvironment env) {
// ...
this.types = env.getTypeUtils();
this.elements = env.getElementUtils();
}
// RuntimePermissionsAnnotatedElement.java:
class RuntimePermissionsAnnotatedElement {
//...
private final TypeMirror type;
RuntimePermissionsAnnotatedElement(TypeElement element) {
// ...
type = element.asType();
}
public TypeMirror asType() {
return type;
}
}
// Now, to check if an annotated element is an Activity:
// "types" are the TypeUtils retrieved from the ProcessingEnvironment at init time
// "elements" are the ElementUtils retrieved from the ProcessingEnvironment at init time
TypeMirror activityType = types.getDeclaredType(elements.getTypeElement("android.app.Activity"));
RuntimePermissionsAnnotatedElement element = // ...
if (types.isSubType(element.asType(), activityType)) {
// This is an activity sub-class.
} else if (//Same for Fragment) {
} else if (// Same for Support Fragment) {
} else {
// Invalid element
throw new WrongClassException(...);
}
from permissionsdispatcher.
Oh it's really nice idea!
from permissionsdispatcher.
Any update on this? I also ran into the WrongClassException
when trying to compile one of my activities that was an AppCompatActivity
. I don't know why that was happening, but a fix would be appreciated.
from permissionsdispatcher.
@Fleker Do u use AndroidAnnotations?
from permissionsdispatcher.
I spent some more time and figured out my problem. I think it had to do with trying to annotate a private method and it broke on compilation. Now my issue has been resolved.
from permissionsdispatcher.
OK but the error message is wired...
from permissionsdispatcher.
I'm gonna delete validation. If user don't annotate proper class, it causes compile error so it doesn't matter.
from permissionsdispatcher.
@hotchemi sorry, i cannot say it is a good idea. Generating a non-compilable class is not user friendly, and should be always avoided. Moreover, it will not be clear to the user that the processor is buggy, or she/he used it in a wrong way.
Why don't you just apply the easy fix which was already proposed?
from permissionsdispatcher.
@WonderCsabo Thanks, I changed my mind!
from permissionsdispatcher.
I realized this implementation can't catch nested super class problem.
I mean if the target extends AppCompatActivity it doesn't go well in this case...
from permissionsdispatcher.
@hotchemi I think you are wrong. types.isSubType(element.asType(), activityType))
will return true if the actual element is a subclass of AppCompatActivity
.
from permissionsdispatcher.
@WonderCsabo Oh really? I tried to add this test data but it doesn't go well...anything wrong?
from permissionsdispatcher.
I think the test data is OK. However isSubTypeOf
definitely works on multi-level inheritance. It should, as defined in the JLS, but i also double checked. Maybe you are calling it wrong somewhere.
from permissionsdispatcher.
Oh thanks.
Actually we define checking in these lines and use them in these lines...
In this case, ClassType.getClassType
returns null
with the last data.
from permissionsdispatcher.
@WonderCsabo @aurae
I realized wired thing.
Instead of original extended Activity, I tried to use actual FragmentActivity, and the test passed.
With debug, it seems that original activity doesn't have inheritance information, I'm not sure why...
from permissionsdispatcher.
from permissionsdispatcher.
I wrote like below.
static class SuperActivity extends Activity {}
I think it must extends android.app.Activity right? It's just static.
from permissionsdispatcher.
You deleted the commit unfortunately, but i think your test case looked like this:
public class MyChildClass extends MyChildClass.SuperClass {
static class SuperClass extends Object {}
}
This cannot compile, because of cyclic inheritance. So that is why isSuperType
returned false
.
from permissionsdispatcher.
@WonderCsabo Oh sorry that was my mistake...
from permissionsdispatcher.
Hi, its me, again. Tried to use this lib one more time and got this message:
`Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
permissions.dispatcher.processor.exceptions.NotDefinedException: @NeedsPermission or @NeedsPermissions are not defined`
I have my fragment generated and dispatcher is generated too. But there is no code for calling the dispatcher inside the generated fragment. I can't find much details on that in log.
from permissionsdispatcher.
Could u paste the code of fragment? I guess you don't define @NeedsPermission.
from permissionsdispatcher.
As stated before I have my TestFragment_ generated as well as TestFragmentPermissionsDispatcher but in TestFragment_ there are no references to Dispatcher. I have compilation error https://gist.github.com/BukT0p/572e431288b698f25de0
from permissionsdispatcher.
@BukT0p Umm it's make sense because u don't use original fragment. Could u send pull request?
from permissionsdispatcher.
Related Issues (20)
- [KTX] System settings doesn't show up again, when user select nothing in location permission settings and press back button in Android 11
- Same permission with different callbacks is missing.
- When will you release new version? (Use ContextCompat.checkSelfPermission )
- Doesnโt support Android 12 data access auditing HOT 2
- New one HOT 1
- [ktx] Construct PermissionsRequester from Fragment may cause that callbacks (requiresPermission, onPermissionDenied, etc.) are called after Fragment instance is already dead HOT 4
- [ktx] onNeverAskAgain not called HOT 4
- I don't know why it doesn't work
- Ktx-1.1.4 HOT 1
- miweb
- IllegalArgumentException with annotation after updating to Kotlin 1.6.10 HOT 2
- Back in business!
- Kotlin 1.7 Support HOT 10
- @OnNeverAskAgain called along with @NeedsPermission if we ask permission in onViewCreated() of Fragment
- kotlin 1.8 Modifiers [PRIVATE] are not allowed on Kotlin parameters. Allowed modifiers: [VARARG, NOINLINE, CROSSINLINE] HOT 5
- Is there someone maintaining this repo? Is there any updated fork? HOT 1
- Android 13 storage permission in @NeedsPermission HOT 5
- fhun
- Add support for ksp
- `This library is no longer maintained.` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from permissionsdispatcher.