OpenPojo / openpojo

POJO Testing & Identity Management Made Trivial
http://openpojo.com
Apache License 2.0
156 stars 40 forks source link

Exception when creating instance for Map<String, SomeType[]> field #68

Closed drdamour closed 9 years ago

drdamour commented 9 years ago
java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl cannot be cast to java.lang.Class
    at com.openpojo.reflection.impl.ParameterizableFactory$ParameterizableImpl.getType(ParameterizableFactory.java:46)
    at com.openpojo.random.RandomFactory.getRandomValue(RandomFactory.java:108)
    at com.openpojo.random.map.util.MapHelper.buildMap(MapHelper.java:49)
    at com.openpojo.random.map.util.BaseMapRandomGenerator.doGenerate(BaseMapRandomGenerator.java:38)
    at com.openpojo.random.map.util.BaseMapRandomGenerator.doGenerate(BaseMapRandomGenerator.java:30)
    at com.openpojo.random.RandomFactory.getRandomValue(RandomFactory.java:112)
    at com.openpojo.validation.test.impl.SetterTester.run(SetterTester.java:43)
    at com.openpojo.validation.utils.ValidationHelper.runValidation(ValidationHelper.java:104)
    at com.openpojo.validation.impl.DefaultValidator.validate(DefaultValidator.java:46)

for a field, i believe it's because the type of the value is not considered paramaterized.

Specifically

        public Class<?> getType() {
            return (Class<?>) Resolver.resolve(type);
        }

in ParameterizableFactory

because GenericArrayType is return from Resolver.resolve, but that's not a class<?>

oshoukry commented 9 years ago

I wrote a simple test, but its passing on Java 1.8, and 1.7, can you please attach an actual failing sample & let me know what is the running environment?

    // This test case in a test class.
    @Test
    public void issueTest() {
        Validator validator = ValidatorBuilder.create()
                .with(new SetterTester())
                .with(new GetterTester())
                .build();
        validator.validate(PojoClassFactory.getPojoClass(TestClass.class));
    }

// the following two classes are one separate class files each independent.
public class TestClass {
    private Map<String, SomeType[]> myMap;

    public Map<String, SomeType[]> getMyMap() {
        return myMap;
    }

    public void setMyMap(Map<String, SomeType[]> myMap) {
        this.myMap = myMap;
    }

public class SomeType {
}
drdamour commented 9 years ago

indeed, the problem does not occur in 1.7. I am able to recreate it with your given test in 1.6.0_65

Do you support JRE 1.6? we're still restrained to 1.6 in some of our legacy systems

oshoukry commented 9 years ago

Let me test on 1.6 and update this.

oshoukry commented 9 years ago

OpenPojo-0.8.1 is out with the fix, please update and let me know if you see any other issues.

drdamour commented 8 years ago

finally got around to testing this, works! thanks!