GuavaEclipsePlugin / GuavaEclipsePluginParent

"Guava Eclipse Plugin" is an eclipse plugin which will generate following methods using Google guava utility classes or jdk utility classes toString() [STRG + SHIFT + 4] - equals(Object object) & hashCode() [STRG + SHIFT + 5] - compareTo(...) [STRG + SHIFT + 6]
https://guavaeclipseplugin.github.io/
10 stars 4 forks source link

CompareTo Method generation also includes variables which are not comparable #15

Closed arolfes closed 9 years ago

arolfes commented 9 years ago

if you a variable which does not implement the comparable interface generated code does not compile.

Example

public class SampleBean implements Comparable<SampleBean> {

    private int testInt;

    private String testString;

    private Object testObject;

    @Override
    public int compareTo(SampleBean that){
        return ComparisonChain.start()
            .compare(this.testInt, that.testInt)
            .compare(this.testString, that.testString)
// The next line does not compile
// should be comment out
            .compare(this.testObject, that.testObject)
            .result();
        }
}
arolfes commented 9 years ago

for given example the output would be:

public class SampleBean implements Comparable<SampleBean> {

    private int testInt;

    private String testString;

    private Object testObject;

    @Override
    public int compareTo(SampleBean that) {
        return ComparisonChain.start().compare(this.testInt, that.testInt)
                .compare(this.testString, that.testString)
                // XXX field 'testObject' java.lang.Object is not comparable 
                //.compare(this.testObject, that.testObject)
                .result();
    }

}
arolfes commented 9 years ago

a more complex example

public class CompareTest implements Comparable<CompareTest> {

    private String[] arraySample1;

    private ClassWithComparable compEnabled;

    private ClassWithoutComparable compDisabled;

    private int intValue3;

    private SampleEnum enumValue1;

    private String simpleString1;

    private InterfaceWithComparable iFaceWithComp;

    private InterfaceWithoutComparable iFaceWithoutComp;

    private ClassExtendsSomeComparable compEnabled2;

    private ClassExtendSomeNonComparable compDisabled2;

    private AbstractClassWithComparable compEnabled3;

    private AbstractClassWithoutComparable compDisabled3;

    @Override
    public int compareTo(CompareTest that) {
        return ComparisonChain.start()
                // XXX field 'arraySample1' is an Array! and they are not comparable by default 
                //.compare(this.arraySample1, that.arraySample1)
                .compare(this.compEnabled, that.compEnabled)
                // XXX field 'compDisabled'  does not implements java.lang.Comparable 
                //.compare(this.compDisabled, that.compDisabled)
                .compare(this.intValue3, that.intValue3)
                .compare(this.enumValue1, that.enumValue1)
                .compare(this.simpleString1, that.simpleString1)
                .compare(this.iFaceWithComp, that.iFaceWithComp)
                // XXX field 'iFaceWithoutComp'  does not implements java.lang.Comparable 
                //.compare(this.iFaceWithoutComp, that.iFaceWithoutComp)
                .compare(this.compEnabled2, that.compEnabled2)
                // XXX field 'compDisabled2'  does not implements java.lang.Comparable 
                //.compare(this.compDisabled2, that.compDisabled2)
                .compare(this.compEnabled3, that.compEnabled3)
                // XXX field 'compDisabled3'  does not implements java.lang.Comparable 
                //.compare(this.compDisabled3, that.compDisabled3)
                .result();
    }

}
arolfes commented 9 years ago

fixed with latest snapshot 1.4.0.201507091317 https://GuavaEclipseHelperTeam.github.io/updateSite/snapshots/LATEST

i will prepare a full release

arolfes commented 9 years ago

2ebd7cf7fd3d67a2adcc5dfb6a3a33df54c08b4b