You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reported by @vab2048 in #397. Thank you @vab2048 for reporting this and for providing an example.
If an interface is implemented by a record (with at least 1 field) and scanClasspathForConcreteTypes(true) is set then an exception is thrown.
For example consider:
publicinterfaceDog {
voidbark();
}
with a single implementation:
public record Rottweiler(Stringname) implementsDog {
@Overridepublicvoidbark() {
System.out.println("WOOF WOOF");
}
}
And we have the following record which has an interface (Dog) as a field:
public record DogOwner(StringownerName, Dogdog) {}
Now with the following test:
/** * Fails... when it should succeed. */@TestvoideasyRandom_DogOwner() {
vareasyRandom = getInstance();
// Should not throw.... but does...vardogOwner = easyRandom.nextObject(DogOwner.class);
System.out.println(dogOwner.toString());
}
privateEasyRandomgetInstance() {
EasyRandomParametersparameters = newEasyRandomParameters()
.objectFactory(newEasyRandomRecordFactory())
.scanClasspathForConcreteTypes(true)
;
returnnewEasyRandom(parameters);
}
fails with the exception:
org.jeasy.random.ObjectCreationException: Unable to create a random instance of type class com.example.demo.records.DogOwner
at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:172)
at org.jeasy.random.EasyRandom.nextObject(EasyRandom.java:100)
at com.example.demo.records.Tests.easyRandom_DogOwner(Tests.java:32)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
Caused by: org.jeasy.random.ObjectCreationException: Unable to create a random instance of type interface com.example.demo.records.Dog
at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:172)
at org.jeasy.random.EasyRandom.nextObject(EasyRandom.java:100)
at com.example.demo.records.EasyRandomRecordFactory.createRandomRecord(EasyRandomRecordFactory.java:33)
at com.example.demo.records.EasyRandomRecordFactory.createInstance(EasyRandomRecordFactory.java:22)
at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:147)
... 67 more
Caused by: java.lang.IllegalAccessException: Can not set final java.lang.String field com.example.demo.records.Rottweiler.name to java.lang.String
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:76)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:80)
at java.base/jdk.internal.reflect.UnsafeQualifiedObjectFieldAccessorImpl.set(UnsafeQualifiedObjectFieldAccessorImpl.java:79)
at java.base/java.lang.reflect.Field.set(Field.java:793)
at org.jeasy.random.util.ReflectionUtils.setFieldValue(ReflectionUtils.java:153)
at org.jeasy.random.util.ReflectionUtils.setProperty(ReflectionUtils.java:139)
at org.jeasy.random.FieldPopulator.populateField(FieldPopulator.java:105)
at org.jeasy.random.EasyRandom.populateField(EasyRandom.java:209)
at org.jeasy.random.EasyRandom.populateFields(EasyRandom.java:198)
at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:165)
I think its because it still attempts to create the record like it was a normal class. I've attached an example project which replicates this (which is attached).
Hi,
when it is planned to release v6?
The main page states: "As of November 15, 2020, Easy Random is in maintenance mode. This means only bug fixes will be addressed from now on (except for #397 which will be released when Java 16 is out)."
We are (almost) at Java 21 now and we still don't have record support. :)
Reported by @vab2048 in #397. Thank you @vab2048 for reporting this and for providing an example.
If an interface is implemented by a record (with at least 1 field) and
scanClasspathForConcreteTypes(true)
is set then an exception is thrown.For example consider:
with a single implementation:
And we have the following record which has an interface (
Dog
) as a field:Now with the following test:
fails with the exception:
I think its because it still attempts to create the record like it was a normal class. I've attached an example project which replicates this (which is attached).
easy-random-records-bug-example.zip
The text was updated successfully, but these errors were encountered: