aucd29 / cloning

Automatically exported from code.google.com/p/cloning
Other
0 stars 0 forks source link

Enum Cloning throws IllegalArgumentException #20

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Steps to rep
1. Create a Enum with a body including a static final field.
2. But it into a Map.
3. Clone the Map.

The cloner throws an java.lang.IllegalArgumentException: Can not set final 
java.lang.String field com.customweb.payment.bean.context.BuildMode.name to 
com.customweb.payment.bean.context.BuildMode$6 (full stack trace is attached).

Cloner Version:
1.9.0

The exception should be fixable by using Class.getDeclaringClass() instead of 
Class.getClass() for checking if a given object is an Enum or not in 
com.rits.cloning.Cloner on line 443 (see also 
http://stackoverflow.com/questions/5758660/java-enum-getdeclaringclass-vs-getcla
ss?rq=1). 

Original issue reported on code.google.com by thomas.h...@customweb.ch on 20 Jul 2013 at 9:40

Attachments:

GoogleCodeExporter commented 9 years ago
The working condition is:
if ((o instanceof Enum) && ((Enum<?>)o).getDeclaringClass().isEnum()) return o;

Original comment by thomas.h...@customweb.ch on 21 Jul 2013 at 6:59

GoogleCodeExporter commented 9 years ago
public enum TestEnum
{
    A("a"), B("b"), C("c");

    private String name;

    private TestEnum(String name)
    {
        this.name = name;
    }

    public static Object o = new Object();
}

    public void testCloneEnumInMapIssue20()
    {
        Map<Integer, TestEnum> m = new HashMap<Integer, TestEnum>();
        m.put(1, TestEnum.A);
        m.put(2, TestEnum.B);
        m.put(3, TestEnum.C);
        cloner.deepClone(m);
    }

The test passes without any problems. Any idea how to make it fail?

Original comment by kostas.k...@googlemail.com on 6 Aug 2013 at 8:11

GoogleCodeExporter commented 9 years ago
I use a Enum with some abstract method:

public enum TestEnum
{
    A("a") {
        public String someMethod() {
            return "Some String from A";
        }
    }, 

    B("b") {
        public String someMethod() {
            return "Some String from B";
        }
    }, 

    C("c"){
        public String someMethod() {
            return "Some String from C";
        }
    };

    private final String name;

    private TestEnum(String name)
    {
        this.name = name;
    }

    public abstract String someMethod();

    public static Object o = new Object();
}

Original comment by thomas.h...@customweb.ch on 7 Aug 2013 at 6:17

GoogleCodeExporter commented 9 years ago
I couldn't reproduce your exception but I got issues when I have enum 
inheritance.

I've uploaded 1.9.1-SNAPSHOT in sonatype snapshot repository and you can give 
it a try.

https://oss.sonatype.org/content/repositories/snapshots/

Original comment by kostas.k...@googlemail.com on 7 Aug 2013 at 8:06

GoogleCodeExporter commented 9 years ago
I assume this is fixed.

Original comment by kostas.k...@googlemail.com on 20 Aug 2013 at 8:08

GoogleCodeExporter commented 9 years ago
Sorry. Yes, I have test it!

Original comment by thomas.h...@customweb.ch on 20 Aug 2013 at 8:21