-
Type:
Enhancement
-
Resolution: Rejected
-
Priority:
P4
-
Affects Version/s: 6
-
Component/s: core-libs
-
x86
-
linux
A DESCRIPTION OF THE REQUEST :
Many methods on Class come as two variants.
getDeclaredXxxx() returns the Xxxx declared by a given class.
getXxxx() returns the pulbic Xxxx available for a given class (including super classes etc).
However getInterfaces() only returns declared interfaces. There is no method which actually returns all interfaces.
JUSTIFICATION :
I believe the Javadoc does not make this clear.
"Determines the interfaces implemented by the class or interface represented by this object." Here "Implemented" cannot mean "implements" literally or specifically as interfaces "extends" other interfaces.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
getInterfaces() should return all interfaces for which
clazz.newInstance() instanceof Interface
is true. There would be a getDeclaredInterfaces() to be consistent with getDeclaredXxxx() methods.
However, given the current behaviour which is unlikely to change. I suggest...
- Making sure getInterfaces() make sclear that only declared interfaces are returned. c.f. getDeclaredMethods()
"Returns an array of <code>Method</code> objects reflecting all the
methods declared by the class or interface represented by this
<code>Class</code> object."
- Add a getAllInterfaces() method which returns all interfaces an instance of this class implements. The order should be depth first to be consistent with getMethods() and getFields().
ACTUAL -
See Expected Behaviour
---------- BEGIN SOURCE ----------
public static void main(String... args) {
try {
assert false;
throw new IllegalArgumentException("-ea option must be on.");
} catch (AssertionError expected) {
// expected.
}
Object map = new ConcurrentSkipListMap();
assert map instanceof Map;
assert map instanceof NavigableMap;
assert map instanceof SortedMap;
assert map instanceof ConcurrentMap;
assert map instanceof ConcurrentNavigableMap;
assert map instanceof Cloneable;
assert map instanceof Serializable;
// lists three interfaces!
printInterfaces(map);
// for comparison, these interfaces are not defined in code ?but are implied? (perhaps stated in the JLS)
Object bytes = new byte[0];
assert bytes instanceof Cloneable;
assert bytes instanceof Serializable;
printInterfaces(bytes);
}
private static void printInterfaces(Object map) {
System.out.println("The interfaces of "+map.getClass()+" are;");
for (Class<?> iClass : map.getClass().getInterfaces())
System.out.println(iClass);
System.out.println();
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
navigate super classes and interfaces recursively.
Many methods on Class come as two variants.
getDeclaredXxxx() returns the Xxxx declared by a given class.
getXxxx() returns the pulbic Xxxx available for a given class (including super classes etc).
However getInterfaces() only returns declared interfaces. There is no method which actually returns all interfaces.
JUSTIFICATION :
I believe the Javadoc does not make this clear.
"Determines the interfaces implemented by the class or interface represented by this object." Here "Implemented" cannot mean "implements" literally or specifically as interfaces "extends" other interfaces.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
getInterfaces() should return all interfaces for which
clazz.newInstance() instanceof Interface
is true. There would be a getDeclaredInterfaces() to be consistent with getDeclaredXxxx() methods.
However, given the current behaviour which is unlikely to change. I suggest...
- Making sure getInterfaces() make sclear that only declared interfaces are returned. c.f. getDeclaredMethods()
"Returns an array of <code>Method</code> objects reflecting all the
methods declared by the class or interface represented by this
<code>Class</code> object."
- Add a getAllInterfaces() method which returns all interfaces an instance of this class implements. The order should be depth first to be consistent with getMethods() and getFields().
ACTUAL -
See Expected Behaviour
---------- BEGIN SOURCE ----------
public static void main(String... args) {
try {
assert false;
throw new IllegalArgumentException("-ea option must be on.");
} catch (AssertionError expected) {
// expected.
}
Object map = new ConcurrentSkipListMap();
assert map instanceof Map;
assert map instanceof NavigableMap;
assert map instanceof SortedMap;
assert map instanceof ConcurrentMap;
assert map instanceof ConcurrentNavigableMap;
assert map instanceof Cloneable;
assert map instanceof Serializable;
// lists three interfaces!
printInterfaces(map);
// for comparison, these interfaces are not defined in code ?but are implied? (perhaps stated in the JLS)
Object bytes = new byte[0];
assert bytes instanceof Cloneable;
assert bytes instanceof Serializable;
printInterfaces(bytes);
}
private static void printInterfaces(Object map) {
System.out.println("The interfaces of "+map.getClass()+" are;");
for (Class<?> iClass : map.getClass().getInterfaces())
System.out.println(iClass);
System.out.println();
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
navigate super classes and interfaces recursively.
- relates to
-
JDK-8023020 (reflect) javadoc for Class.getInterfaces() should be updated to match behaviour
-
- Closed
-