JDK-8142904 : The "getDeclaredMethods" function returns a inherited method
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 8u66,9
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: other
  • CPU: x86
  • Submitted: 2015-11-12
  • Updated: 2021-04-13
  • Resolved: 2021-04-13
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.
Other
tbd_minorResolved
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
MAC OS X 10.8.5

A DESCRIPTION OF THE PROBLEM :
The "getDeclaredMethods" function should return only the methods that are declared in the input class A. However, when the modifier of the superclass of A is "abstract class" (without public), the function also returns the methods of the superclass.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public  interface Posicion {
    public void obtenerCoordenadaX();
    public void obtenerCoordenadaY();
}

abstract class Vehiculo implements Posicion {
    public void obtenerCoordenadaX(){};
    public void obtenerCoordenadaY(){};
}

public class Coche extends Vehiculo {
}

import java.lang.reflect.Method;

public class TestCase {
    public static void main(String[] args) {
        Class<?> clazz = Coche.class;
        Method[] methods = clazz.getDeclaredMethods();
    }
}
---------- END SOURCE ----------


Comments
The methods in question returned by getDeclaredMethods for the Coche class, are not present in the source of the class; they are bridge methods added by the compiler. This can be seen in the javap output: public void obtenerCoordenadaY(); descriptor: ()V flags: (0x1041) ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #7 // Method Vehiculo.obtenerCoordenadaY:()V 4: return LineNumberTable: line 1: 0 public void obtenerCoordenadaX(); descriptor: ()V flags: (0x1041) ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial #10 // Method Vehiculo.obtenerCoordenadaX:()V 4: return LineNumberTable: line 1: 0 The bridge methods are added in a case like this where a public class public methods from a non-public superclass to allow for the possibility of reflective access of the subclasses methods (JDK-6342411). More explicit documentation of this situation should be added (JDK-8265174). Closing this issue as not a bug.
13-04-2021

This is same as JDK-6815786. Attached Test case executed on JDK 8u66 - Fail JDK 8u72 - Fail JDK 9ea b85 - Fail The Java API documentation states that "This includes public, protected, default (package) access, and private methods, but excludes inherited methods. " , so the API should not be returning inherited methods. Moving across to dev - team to evaluate.
13-11-2015