JDK-6962494 : Update documentation on Executable.getParameterAnnotations()
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 6u10,8
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2010-06-18
  • Updated: 2013-09-23
  • Resolved: 2013-09-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8
8 b108Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux domU-12-31-36-00-39-52 2.6.18-xenU-ec2-v1.0 #2 SMP Mon Feb 18 14:28:43 UTC 2008 x86_64 x86_64 x86_64 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
Constructor.getAnnotationParameters() returns an incorrect array when called on the constructor of an inner class with no parameters. According to the javadocs one would expect to get back an empty array, but instead what is returned is a singleton array containing as its element a single empty array.

I looked in the source for Constructor.java and believe that this line is the problem is in line 1 of the method:

 public Annotation[][] getParameterAnnotations() {
        int numParameters = parameterTypes.length;
        if (parameterAnnotations == null)
            return new Annotation[numParameters][0];

It seems like numParameters should instead be set as:

       if [clazz is nested and non static
            numParameters = parameterTypes.length - 1; // ignore implicit enclosing instance param
      else numParameters = parameterTypes.length




REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.lang.reflect.*;
import java.util.*;

public
class InnerClassTest
{
    private
    class Inner1
    {
        private Inner1() {}
    }

    public
    static
    void
    main( String[] args )
        throws Exception
    {
        Constructor< ? > noArg = Inner1.class.getDeclaredConstructors()[ 0 ];
        System.out.println(
            Arrays.deepToString( noArg.getParameterAnnotations() ) );
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
provide my own wrapper method to correct for this by only calling Constructor.getParameterAnnotations in cases when that method will return a correct result and supplying an empty array on its own otherwise

Comments
This is defunct now, with the java.lang.reflect.Parameter api in place. This bug will update the documentation for getParameterAnnotations() to refer to the Parameter API.
06-09-2013

This bug should be fixed once JDK-8004729 is in the platform.
07-12-2012

Addressing this bug would be greatly eased if "isSynthetic" information were available on a per-parameter basis, see JDK-4894447.
26-10-2012