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


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

annotation with inheritance

Newsgroups comp.lang.java.programmer
Date 2016-05-30 23:44 -0700
Message-ID <63d09608-cdb4-4b42-a4cf-a81fd3cda780@googlegroups.com> (permalink)
Subject annotation with inheritance
From Philipp Kraus <philipp.kraus.flashpixx@gmail.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

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

csiph-web