Signature of MethodHandleInfo.reflectAs is not specific enough

Ali Ebrahimi ali.ebrahimi1781 at
Tue Feb 25 11:13:33 UTC 2014

I know, this is too late, but I want to share my suggestion:

public <T extends AccessibleObject&AnnotatedElement> T reflectAs(Class<?
super T> expected, MethodHandles.Lookup lookup)

Member mr = reflectAs(Member.class, MethodHandles.lookup());
AnnotatedElement ae = reflectAs(AnnotatedElement.class,

AnnotatedElement am = reflectAs(Member.class, MethodHandles.lookup());

Field fd= reflectAs(Field.class, MethodHandles.lookup());
Constructor cr = reflectAs(Constructor.class, MethodHandles.lookup());
Method md = reflectAs(Method.class, MethodHandles.lookup());

Field fm= reflectAs(Member.class, MethodHandles.lookup());
Constructor cm = reflectAs(Member.class, MethodHandles.lookup());
Method mm = reflectAs(Member.class, MethodHandles.lookup());

Field fa= reflectAs(AnnotatedElement.class, MethodHandles.lookup());
Constructor ca = reflectAs(AnnotatedElement.class, MethodHandles.lookup());
Method ma = reflectAs(AnnotatedElement.class, MethodHandles.lookup());

Member mf= reflectAs(Field.class, MethodHandles.lookup());
Member mc = reflectAs(Constructor.class, MethodHandles.lookup());
Member mrm = reflectAs(Method.class, MethodHandles.lookup());

AnnotatedElement af= reflectAs(Field.class, MethodHandles.lookup());
AnnotatedElement ac = reflectAs(Constructor.class, MethodHandles.lookup());
AnnotatedElement aem = reflectAs(Method.class, MethodHandles.lookup());

Method mdc = reflectAs(Constructor.class, MethodHandles.lookup());//fails
Constructor crm = reflectAs(Method.class, MethodHandles.lookup());//fails
Constructor cf = reflectAs(Field.class, MethodHandles.lookup());//fails
Field fc = reflectAs(Constructor.class, MethodHandles.lookup());//fails
AnnotatedElement as = reflectAs(String.class,
String ss = reflectAs(String.class, MethodHandles.lookup());//fails
String sm = reflectAs(Method.class, MethodHandles.lookup());//fails


Ali Ebrahimi

On Mon, Nov 11, 2013 at 1:59 AM, Remi Forax <forax at> wrote:

> The is a stupid issue with the signature of MethodHandleInfo.reflectAs,
> j.l.r.Field, Method or Constructor implement two interfaces Member and
> AnnotatedElement, with the current signature, the code
>    info.reflectAs(Member.class, lookup)
> works but the code
>    info.reflectAs(AnnotatedElement.class, lookup)
> doesn't work.
> Because there is no way to do an 'or' between several bounds of
> a type variable, I think that the signature of reflectAs should be
> changed from :
>    public <T extends Member> T reflectAs(Class<T> expected, Lookup lookup);
> to
>    public <T> T reflectAs(Class<T> expected, Lookup lookup);
> and the javadoc should be modified to explain that a Member or
> AnnotatedElement are
> valid bounds of T.
> As a side effect, the signature of MethodHandles.reflectAs(Class<T>,
> MethodHandle)
> should be updated accordingly.
> There is a workaround, one can write:
>    (AnnotatedElement)info.reflectAs(Member.class, lookup)
> but it's at best weird.
> cheers,
> Rémi
> _______________________________________________
> mlvm-dev mailing list
> mlvm-dev at

More information about the core-libs-dev mailing list