Annotation on Intersectingtype
Alex Buckley
alex.buckley at oracle.com
Tue Jan 21 14:08:28 PST 2014
Mohan,
Yes, it is possible. The class file can encode an annotation on the i'th
bound of the j'th type parameter of a generic class, interface, method,
or constructor. In the Language Model API, you need to get the
TypeParameterElement for T, then call getBounds() to get a list of
TypeMirrors, one mirror per bound. Each mirror should expose the
annotations appearing before the corresponding bound. Unfortunately,
you'll probably run into the bug that TypeParameterElements don't return
anything useful.
Joe, Eric,
I am suspicious of the following text from jx.l.m.t.IntersectionType:
"For example, the bound of the type parameter <T extends Number &
Runnable> is an (implicit) intersection type. As of RELEASE_8, this is
represented by an IntersectionType with Number and Runnable as its bounds."
While "Number & Runnable" is technically an intersection type, there is
no way to get a single IntersectionType mirror for the entire 'extends'
clause of a type parameter. A TypeParameterElement exposes a list of
mirrors. (I suppose it could be a one-element list, where the sole
mirror is an IntersectionType, but that would be a behavioral change
from SE 7.) Literally any other use of an intersection type would be a
better example here, e.g. in a cast.
Alex
On 1/19/2014 10:15 PM, Mohan Radhakrishnan wrote:
>
> Subject was missed.
>
> On Mon, Jan 20, 2014 at 11:44 AM, Mohan Radhakrishnan
> <radhakrishnan.mohan at gmail.com <mailto:radhakrishnan.mohan at gmail.com>>
> wrote:
>
> Hi,
>
> I was trying to detect an intersection type and print the
> annotation only if it is. Is that possible ? I may be
> misunderstanding this.
>
> Should I try this
> " if( typeMirror instanceof javax.lang.model.type.IntersectionType)
> { }" ?
>
>
>
> public class IntersectionType {
>
> @Retention(RetentionPolicy.RUNTIME)
> @Target(ElementType.TYPE_USE)
> public @interface TypeUses {
> TypeUse[] value();
> }
>
> @Retention(RetentionPolicy.RUNTIME)
> @Target(ElementType.TYPE_USE)
> @Repeatable(TypeUses.class)
> protected @interface TypeUse {
> int value();
> }
>
> class Test<T extends String & @IntersectionType.TypeUse(value =
> 2) Serializable>{
>
> }
>
> class Test1<T extends @IntersectionType.TypeUse(value = 2)String>{
>
> }
>
> }
>
> Thanks,
> Mohan
>
>
More information about the type-annotations-dev
mailing list