question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Annotation Aliases

See original GitHub issue

Provide an API to allow users to register their own annotations to be aliases to default ACF annotations + Configurations: Flags, Conditions, CommandPermission, Subcommand

Something like

manager.registerAnnotation(CustomAnnotation.class, Conditions.class, "conditionname:conditionconfig");
manager.registerParameterAnnotation(ParameterType.class, CustomAnnotation.class, Conditions.class, "conditionname:conditionconfig");
manager.registerParameterAnnotation(ParameterType.class, CustomAnnotation.class, Flags.class, "flagconfig=flagvalue");

Then using @CustomAnnotation will be same as putting

@Conditions("conditionname:conditionconfig") @Flags("flagconfig=flagvalue")

on a parameter or method

I will be personally working this issue, not looking for PR’s on it.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
kumpelblase2commented, Jun 11, 2019

Have you considered using meta-annotations (so using annotations on annotations) for this instead? Not to mention, basic support would be trivial to implement (given the current state of the code):

In Annotations.java#L53 if we change this:

- Annotation annotation = object.getAnnotation(annoClass);
+ Annotation annotation = getAnnotationRecursive(object, annoClass);

and define that function like so:

    private static Annotation getAnnotationRecursive(AnnotatedElement object, Class<? extends Annotation> annoClass) {
        if(object.isAnnotationPresent(annoClass)) {
            return object.getAnnotation(annoClass);
        } else {
            for (Annotation otherAnnotation : object.getDeclaredAnnotations()) {
                final Annotation annotationRecursive = getAnnotationRecursive(otherAnnotation.annotationType(), annoClass);
                if(annotationRecursive != null) {
                    return annotationRecursive;
                }
            }
        }
        return null;
    }

it would already find the necessary annotations. So we could implement your given example like this:

@Retention(RetentionPolicy.RUNTIME)
@Conditions("conditionname:confitionconfig")
@Flags("flagconfig=flagvalue")
public @interface CustomAnnotation {}

(The only additional thing that would be needed to change for @Flags to work like in the example is to allow that annotation to be present on annotation types, which it currently isn’t allowed to. @Conditions would already work like this.)

This is used quite extensively in Spring/Spring Boot (a little bit of reference here), though Spring goes quite a bit further (for example, also allowing for properties of the combined annotation to alias for properties on the composing annotations).

1reaction
aikarcommented, Jun 4, 2018

yes and no, work is actively being done in master for it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AliasFor Annotation in Spring - Baeldung
In the same annotation, we use @AliasFor to declare aliases for ... Then, we'll see the explicit usage of aliases in the custom...
Read more >
Create custom Annotation as alias for Framework-Annotation?
One approach is to write an annotation processor that transforms the AST (the compiler's internal representation of the source code).
Read more >
How to declare aliases for annotation attributes in java?
HI, When I am going through the @AliasFor annotation in spring, they have introduced the topic "declare aliases for annotation attributes" ?
Read more >
@AliasFor annotation Examples - LogicBig
@AliasFor annotation is used to declare aliases for annotation elements. Spring framework uses this annotation internally with a lot of ...
Read more >
AliasFor - Spring
Aliases within an annotation: within a single annotation, @AliasFor can be declared on a pair of attributes to signal that they are interchangeable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found