RFR: JDK-8044629: (reflect) Constructor.getAnnotatedReceiverType() returns wrong value

Martin Buchholz martinrb at google.com
Thu Jul 21 17:49:36 UTC 2016


Hi Joel, Paul,

A coworker ran into the change of behavior here in jdk9.  Specifically, we
noticed that a local class constructor has a receiver parameter,
but getAnnotatedReceiverType returns null.  The changed jdk9 spec is
actually very clear about that:

http://download.java.net/java/jdk9/docs/api/java/lang/reflect/Constructor.html#getAnnotatedReceiverType--
"""If this Executable object represents a static method or represents a
constructor of a top level, static member, local, or anonymous class, then
the return value is null."""

BUT we can't think of any reason WHY a local inner class would be treated
differently from an inner member class.  Why not the simple and obvious
rule: if there is a receiver parameter, return an appropriate non-null
AnnotatedType?

You already have an excellent jtreg test, but here's what I was playing
with:

import java.lang.reflect.Constructor;

public class LocalClassReceiverTypeBug {
    public static void main(String[] args) throws Throwable {
        class StaticLocal {}
        printConstructor(StaticLocal.class);
        new LocalClassReceiverTypeBug().instanceMain();
    }

    public void instanceMain() throws Throwable {
        class InstanceLocal {}
        printConstructor(InstanceLocal.class);
        printConstructor(Inner.class);
        printConstructor(Nested.class);
    }

    static class Nested {}
    class Inner {}

    static void printConstructor(Class<?> klazz) {
        Constructor<?>[] constructors = klazz.getDeclaredConstructors();
        if (constructors.length != 1) throw new AssertionError();
        System.out.printf("constructor=%s%n", constructors[0]);
        System.out.printf("receiver type=%s%n",
                          constructors[0].getAnnotatedReceiverType());
    }
}



On Wed, Aug 13, 2014 at 1:54 AM, Joel Borggren-Franck <
joel.franck at oracle.com> wrote:

> Hi Paul,
>
> On 2014-06-24, Paul Sandoz wrote:
> >
> > On Jun 17, 2014, at 6:52 PM, Joel Borggrén-Franck <
> joel.franck at oracle.com> wrote:
> > >
> > > Can I get a review for this fix and javadoc clarification for
> https://bugs.openjdk.java.net/browse/JDK-8044629
> > >
> >
> > +1
> >
> > I never quite realised just how convoluted it was to determine that a
> class is an inner class.
>
> Neither did I until I had to implement it :)
>
> cheers
> /Joel
>


More information about the core-libs-dev mailing list