jeudi 28 avril 2016

Android create custom annotations

I'm learning how to create own annotations. I have read this: http://ift.tt/1eywcSw but I have to pass all classes with my annotation - thats bad. So I have read this: http://ift.tt/1qWQQ8Z Unfortunately it throws an exception:

java.lang.NoSuchFieldException: No field mDexs in class Ldalvik/system/PathClassLoader; (declaration of 'dalvik.system.PathClassLoader' appears in /system/framework/core-libart.jar)

basicly:

Failed to get mDexs field

My goal:

I want to create custom annotation Permission - and i did:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Permission {
    String value() default "none";
}

Now, every time method with this annotation is run I want to check some conditions conditions and decide if method should be running... like:

@Permission(value = "testValue")
private void foo() {
    // do stuff if permission allows
}

Why i want to do that? I think it will be good replacement for if statements:

private void foo() {
    if(MyFooClass.STATIC_BOOLEAN_FIELD)
       // do stuff
}

I have a lot of that if statement in my project and I want to get rid of it somehow

Is that even possible? And safe? And good idea?





Aucun commentaire:

Enregistrer un commentaire