Bug 276219

Summary: Incorrect behaviour of BinaryType.getMethods() for class StringBuffer (jdk1.5-6)
Product: [Eclipse Project] JDT Reporter: Eugene Rogov <ewro>
Component: CoreAssignee: Srikanth Sankaran <srikanth_sankaran>
Status: VERIFIED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: david_audel, srikanth_sankaran
Version: 3.5   
Target Milestone: 3.5 RC1   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Eugene Rogov CLA 2009-05-13 22:11:28 EDT
Linux Ubuntu 9.04 x86
JDK 1.6.0_13
Eclipse 3.5.0 Build id: I20090313-0100 (M6)

Along with methods declared in type "StringBuffer", getMethods() returns some methods of supertype "AbstractStringBuilder", which does not match documentation.

Looks like the returned array contains methods of AbstractStringBuilder which are overridden in StringBuffer but with covariant return type.

This is contract violation, because according documentation IType.getMethods() must returns only methods declared in this type but not in supertypes.

Example of incorrect method returned:
java.lang.AbstractStringBuilder append(java.lang.Object)#2 [in StringBuffer [in StringBuffer.class [in java.lang [in /usr/lib/jvm/java-6-sun-1.6.0.13/jre/lib/rt.jar]]]]

I tested this with jdk1.6 but I believe this is reproducible for jdk1.5 also.
Comment 1 Srikanth Sankaran CLA 2009-05-14 02:04:10 EDT
Will investigate this.
Comment 2 Srikanth Sankaran CLA 2009-05-14 07:31:48 EDT
(In reply to comment #1)
> Will investigate this.

Reproduced the problem with Sun JDK 1.6.0_13,
Eclipse 3.5M7. Under investigation.




Comment 3 Srikanth Sankaran CLA 2009-05-15 06:18:30 EDT
(In reply to comment #2)
> (In reply to comment #1)
> > Will investigate this.
> Reproduced the problem with Sun JDK 1.6.0_13,
> Eclipse 3.5M7. Under investigation.


OK, After analysis, it appears that this case should be
closed as INVALID as actually there is no bug here.

The "extra" methods that are allegedly incorrectly returned
are not originating from the super class, but instead are
the "bridge" methods synthesized by the compiler on behalf
of the sub class (in this case StringBuffer) to implement
covariant return types.

That getMethods may return synthetic methods is documented
on IType.getMethods():

/**
* Returns the methods and constructors declared by this type.
* For binary types, this may include the special <code>&lt;clinit&gt;</code>; method
* and synthetic methods.

 ...
*/ 

A client that doesn't want to see these synthetic methods can trivially
filter them by code such as : 

    BinaryType b = ... 
    IMethod [] methods =  b.getMethods();
    for (int i = 0; i < methods.length; i++) {
	if (org.eclipse.jdt.core.Flags.isSynthetic(methods[i].getFlags()))
		continue;
         /* process methods[i] here */

For instance, the package explorer view uses this approach to
filter out the synthetic methods from the viewer.
Comment 4 Frederic Fusier CLA 2009-05-15 09:17:42 EDT
Verified for 3.5RC1
Comment 5 Eugene Rogov CLA 2009-05-17 20:48:06 EDT
Oh, now I see. Thank you for your time.