Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException: java.lang.String cannot be cast to [non String class] #99

Closed
GoogleCodeExporter opened this issue Mar 29, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
1.  Have a field of type javax.naming.ldap.LdapName in your class.
2.  Run EqualsVerifier.forClass([YourClass].class).verify()


What is the code (equals method, hashCode method, relevant fields) that
triggers the problem?

import javax.naming.ldap.LdapName;

public final class Foo {
    private LdapName dn;

    public Foo() {
    }

    public LdapName getDn() {
        return dn;
    }

    public void setDn(LdapName dn) {
        this.dn = dn;
    }

    @Override
    public int hashCode() {
        return dn.hashCode();
    }

    public boolean equals(Object other) {
        if (other instanceof Foo) {
            Foo f = (Foo) other;
            return dn == f.dn || dn.equals(f.dn);
        } else {
            return false;
        }
    }
}

What error message does EqualsVerifier give?

ClassCastException: java.lang.String cannot be cast to javax.naming.ldap.Rdn


What did you expect?

Verify should work perfectly with no problems.


What version of EqualsVerifier are you using?

1.5


Please provide any additional information below.

LdapName's own equals() implementation doesn't defer equals() to the underlying 
lists it's holding.  It walks through the lists:

        // Compare RDNs one by one for equality
        for (int i = 0; i < rdns.size(); i++) {
            // Compare a single pair of RDNs.
            Rdn rdn1 = rdns.get(i);
            Rdn rdn2 = that.rdns.get(i);
            if (!rdn1.equals(rdn2)) {
                return false;
            }
        }

The ClassCastException is caused by this line from the above:

 Rdn rdn1 = rdns.get(i);


Original issue reported on code.google.com by [email protected] on 28 Nov 2014 at 2:20

@GoogleCodeExporter
Copy link
Author

Hi Julius,

Thanks for the very detailed issue report -- it makes it so much easier for me 
to figure out what's going on :).

The issue you report is actually a known problem: see Issue 84 (specifically, 
comment #7 and further). Unfortunately, I don't have a good solution for this 
issue yet.

However, there is a workaround. You can add instances of LdapName to the prefab 
values using EqualsVerifier's withPrefabValues method.


Regards,
Jan

Original comment by [email protected] on 28 Nov 2014 at 9:50

  • Changed state: Duplicate
  • Added labels: ****
  • Removed labels: ****

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant