Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34421 > unrolled thread

annotation with inheritance

Started byPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
First post2016-05-30 23:44 -0700
Last post2016-05-31 08:56 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  annotation with inheritance Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-05-30 23:44 -0700
    Re: annotation with inheritance Andreas Leitgeb <avl@logic.at> - 2016-05-31 08:56 +0000

#34421 — annotation with inheritance

FromPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
Date2016-05-30 23:44 -0700
Subjectannotation with inheritance
Message-ID<63d09608-cdb4-4b42-a4cf-a81fd3cda780@googlegroups.com>
Hello,

I have defined two annotation:

@Target( ElementType.TYPE )
@Retention( RetentionPolicy.RUNTIME )
public @interface IBlacklist
{
    Class<?>[] classes() default {};
}

and

@Target( ElementType.TYPE )
@Retention( RetentionPolicy.RUNTIME )
public @interface IWhitelist
{
    Class<?>[] classes() default {};
}


Now I would like to write the checking of the two annotation

static boolean hasAnnotation( final Class<?> p_class, Class<???> p_annotation )
{
    return p_class.isAnnotationPresent( p_annotation )
    && (
            ( p_class.getAnnotation( p_annotation ).classes().length == 0 )
            || ( Arrays.stream( p_class.getAnnotation( p_annotation ).classes() )
                .parallel()
                .filter( p_class::equals )
                .findFirst()
                .isPresent()
            )
         );
}


I replace the ??? with "? extends Annotation", but on this the .classes() methods of the annotation is not found. After that I create a generic parameter T with "T extends IBlacklist & IWhitelist", this removes the error on my hasAnnotation method, but I cannot call it with

hasAnnotation( mytestingclass.class, IBlacklist.class ) 
hasAnnotation( mytestingclass.class, IWhitelist.class ) 

So I need imho an annotation inheritance like

@Target( ElementType.TYPE )
@Retention( RetentionPolicy.RUNTIME )
public @interface IBaseAnnotation
{
    Class<?>[] classes() default {};
}


@Target( ElementType.TYPE )
@Retention( RetentionPolicy.RUNTIME )
public @interface IBlacklist extends IBaseAnnotation
{
}

and

@Target( ElementType.TYPE )
@Retention( RetentionPolicy.RUNTIME )
public @interface IWhitelist extends IBaseAnnotation
{
}

so I can define my hasAnnotation with

static boolean hasAnnotation( final Class<?> p_class, Class<? extends IBaseAnnotation> p_annotation )

but the "extends" is not allowed on annotations. 
How can I create a "annotation inheritance" with an equal method on both sub annotation elements?

Thanks

Phil

[toc] | [next] | [standalone]


#34422

FromAndreas Leitgeb <avl@logic.at>
Date2016-05-31 08:56 +0000
Message-ID<slrnnkqkeq.456.avl@login.logic.tuwien.ac.at>
In reply to#34421
Philipp Kraus <philipp.kraus.flashpixx@gmail.com> wrote:
> I have defined two annotation:
> @Target( ElementType.TYPE )
> @Retention( RetentionPolicy.RUNTIME )
> public @interface IBlacklist
> {
>     Class<?>[] classes() default {};
> }
> and
> @Target( ElementType.TYPE )
> @Retention( RetentionPolicy.RUNTIME )
> public @interface IWhitelist
> {
>     Class<?>[] classes() default {};
> }
> Now I would like to write the checking of the two annotation
> static boolean hasAnnotation( final Class<?> p_class, Class<???> p_annotation )

This can't work. No type-parameterization ever has an impact on
what members you can access on the thusly-parameterized-typed object.

I think, you might try just providing an overload specifically
for each of your two annotations. In that case you would (iiuc)
replace the whole Class<???> (and not just the ???) by each of
your Annotation types, yielding two methods with apparently(*)
similar bodies. 

(*): they're not actually equal bodies: it's more like having
two methods, one calling append("") to a StringBuilder,
and the other calling append("") on a java.awt.TextArea .

There were more things that baffled me in the code, but those might be
due to my not-yet-experience with streams & lambdas. (p_class::equals)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web