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

Unable to generate records with record component type #496

Open
fmbenhassine opened this issue Jun 30, 2023 · 1 comment
Open

Unable to generate records with record component type #496

fmbenhassine opened this issue Jun 30, 2023 · 1 comment
Labels

Comments

@fmbenhassine
Copy link
Member

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:

public interface Dog {
    void bark();
}

with a single implementation:

public record Rottweiler(String name) implements Dog {

    @Override
    public void bark() {
        System.out.println("WOOF WOOF");
    }
}

And we have the following record which has an interface (Dog) as a field:

public record DogOwner(String ownerName, Dog dog) {}

Now with the following test:

    /**
     * Fails... when it should succeed.
     */
    @Test
    void easyRandom_DogOwner() {
        var easyRandom = getInstance();
        // Should not throw.... but does...
        var dogOwner = easyRandom.nextObject(DogOwner.class);
        System.out.println(dogOwner.toString());
    }

    private EasyRandom getInstance() {
        EasyRandomParameters parameters = new EasyRandomParameters()
                .objectFactory(new EasyRandomRecordFactory())
                .scanClasspathForConcreteTypes(true)
                ;
        return new EasyRandom(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).


easy-random-records-bug-example.zip

@scsete
Copy link

scsete commented Sep 5, 2023

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. :)

Thanks for the info in advance.

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

No branches or pull requests

2 participants