Bug 83527

Summary: Wrong fullyQualifiedName for inner classes
Product: [Eclipse Project] JDT Reporter: Thomas Buechner <buechner>
Component: CoreAssignee: Jerome Lanneluc <jerome_lanneluc>
Status: VERIFIED FIXED QA Contact:
Severity: major    
Priority: P3    
Version: 3.1   
Target Milestone: 3.1 M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Thomas Buechner CLA 2005-01-24 07:35:40 EST
Wrong fullyQualifiedName for inner classes

In the following example there exist two anonymous inner classes with the 
fullyQualifiedName=="test.Directories$1" which is wrong.
This is because 
org.eclipse.jdt.internal.core.CompilationUnitStructureRequestor.resolveDuplicat
es(SourceRefElement handle) doesnt increment the occurenceCount of a newly 
created type because of a wrong implementation of equals() of 
org.eclipse.jdt.internal.core.SourceType if there are two inner classes which 
are not declared in the same method.
Because of this two different sourceTypes with the same occurenceCount=1 are 
created. 

Example:
package test;

public class Directories {
    public static final Interface i2 = new Interface(){};
    public static final Interface i3 = new Interface(){};
}

I fixed this problem with the following corrections in SourceType.java:

- additional implementation:
public int hashCode() {
    return Util.combineHashCodes(getElementName().hashCode(), 
this.occurrenceCount);
}

- changed implementation of equals(o):
public boolean equals(Object o) {
	if (!(o instanceof SourceType)) return false;
    SourceType other = (SourceType) o;
    boolean namesAndOccurence =  getElementName().equals(other.getElementName
())  && this.occurrenceCount == other.occurrenceCount;
    if(!namesAndOccurence) {
        return false;
    }
    else {
        JavaElement thisCompilationUnit = getCompilationUnit(this);
        JavaElement otherCompilationUnit = getCompilationUnit(other);
        return thisCompilationUnit.equals(otherCompilationUnit);
    }
}
private static JavaElement getCompilationUnit(JavaElement javaElement) {
    if(javaElement instanceof CompilationUnit) {
        return javaElement;
    }
    if(javaElement.parent != null) {
        return getCompilationUnit(javaElement.parent);
    }
    else {
        return null;
    }
}
Comment 1 Thomas Buechner CLA 2005-03-15 08:30:45 EST
fixed in version 3.1M5a
Comment 2 Olivier Thomann CLA 2005-06-24 13:20:59 EDT
Verified.