Giter VIP home page Giter VIP logo

Comments (23)

mannodermaus avatar mannodermaus commented on May 18, 2024

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.

hotchemi avatar hotchemi commented on May 18, 2024

Oh it's really nice idea!

from permissionsdispatcher.

Fleker avatar Fleker commented on May 18, 2024

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.

hotchemi avatar hotchemi commented on May 18, 2024

@Fleker Do u use AndroidAnnotations?

from permissionsdispatcher.

Fleker avatar Fleker commented on May 18, 2024

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.

hotchemi avatar hotchemi commented on May 18, 2024

OK but the error message is wired...

from permissionsdispatcher.

hotchemi avatar hotchemi commented on May 18, 2024

I'm gonna delete validation. If user don't annotate proper class, it causes compile error so it doesn't matter.

from permissionsdispatcher.

WonderCsabo avatar WonderCsabo commented on May 18, 2024

@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.

hotchemi avatar hotchemi commented on May 18, 2024

@WonderCsabo Thanks, I changed my mind!

from permissionsdispatcher.

hotchemi avatar hotchemi commented on May 18, 2024

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.

WonderCsabo avatar WonderCsabo commented on May 18, 2024

@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.

hotchemi avatar hotchemi commented on May 18, 2024

@WonderCsabo Oh really? I tried to add this test data but it doesn't go well...anything wrong?

from permissionsdispatcher.

WonderCsabo avatar WonderCsabo commented on May 18, 2024

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.

hotchemi avatar hotchemi commented on May 18, 2024

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.

hotchemi avatar hotchemi commented on May 18, 2024

@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.

WonderCsabo avatar WonderCsabo commented on May 18, 2024

from permissionsdispatcher.

hotchemi avatar hotchemi commented on May 18, 2024

I wrote like below.
static class SuperActivity extends Activity {}
I think it must extends android.app.Activity right? It's just static.

from permissionsdispatcher.

WonderCsabo avatar WonderCsabo commented on May 18, 2024

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.

hotchemi avatar hotchemi commented on May 18, 2024

@WonderCsabo Oh sorry that was my mistake...

from permissionsdispatcher.

BukT0p avatar BukT0p commented on May 18, 2024

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.

hotchemi avatar hotchemi commented on May 18, 2024

Could u paste the code of fragment? I guess you don't define @NeedsPermission.

from permissionsdispatcher.

BukT0p avatar BukT0p commented on May 18, 2024

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.

hotchemi avatar hotchemi commented on May 18, 2024

@BukT0p Umm it's make sense because u don't use original fragment. Could u send pull request?

from permissionsdispatcher.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.