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

Regression in Exception field generation in java 9 #280

Closed
Spikhalskiy opened this issue Nov 2, 2017 · 5 comments
Closed

Regression in Exception field generation in java 9 #280

Spikhalskiy opened this issue Nov 2, 2017 · 5 comments
Labels
Milestone

Comments

@Spikhalskiy
Copy link

Spikhalskiy commented Nov 2, 2017

Found a regression with java 9 and the last release version of random-beans:

public class TestBean {
    private Exception exception;

    public Exception getException() {
        return exception;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }
}

public class TestClass {
    @Test
    public void test() {
        EnhancedRandom enhancedRandom = EnhancedRandomBuilder
                .aNewEnhancedRandomBuilder()
                .overrideDefaultInitialization(true)
                .build();
        enhancedRandom.nextObject(TestBean.class);
    }
}
<dependency>
    <groupId>io.github.benas</groupId>
    <artifactId>random-beans</artifactId>
    <version>3.7.0</version>
</dependency>

This test works fine on java 8, but fails on java 9 with:

io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class com.spikhalskiy.TestBean

	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.EnhancedRandomImpl.nextObject(EnhancedRandomImpl.java:77)
	at com.spikhalskiy.TestClass.test(TestClass.java:17)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Exception for field: exception of class: com.spikhalskiy.TestBean
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 24 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Exception
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 27 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Throwable for field: cause of class: java.lang.Exception
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 29 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Throwable
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 32 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Throwable for field: cause of class: java.lang.Throwable
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 34 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Throwable
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 37 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Throwable for field: cause of class: java.lang.Throwable
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 39 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Throwable
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 42 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Throwable for field: cause of class: java.lang.Throwable
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 44 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Throwable
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 47 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Throwable for field: cause of class: java.lang.Throwable
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 49 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Throwable
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 52 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Throwable for field: cause of class: java.lang.Throwable
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 54 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Throwable
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 57 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Throwable for field: cause of class: java.lang.Throwable
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 59 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Throwable
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 62 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Throwable for field: cause of class: java.lang.Throwable
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 64 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Throwable
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 67 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Throwable for field: cause of class: java.lang.Throwable
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 69 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Throwable
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 72 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Throwable for field: cause of class: java.lang.Throwable
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 74 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Throwable
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 77 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: [Ljava.lang.StackTraceElement; for field: stackTrace of class: java.lang.Throwable
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 79 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.StackTraceElement
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.ArrayPopulator.getRandomArray(ArrayPopulator.java:57)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:108)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 82 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create type: java.lang.Class for field: declaringClassObject of class: java.lang.StackTraceElement
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:85)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateField(EnhancedRandomImpl.java:158)
	at io.github.benas.randombeans.EnhancedRandomImpl.populateFields(EnhancedRandomImpl.java:149)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:120)
	... 85 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to generate a random instance of type class java.lang.Class
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:124)
	at io.github.benas.randombeans.FieldPopulator.generateRandomValue(FieldPopulator.java:122)
	at io.github.benas.randombeans.FieldPopulator.populateField(FieldPopulator.java:81)
	... 88 more
Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create an instance of type: class java.lang.Class
	at io.github.benas.randombeans.ObjectFactory.createInstance(ObjectFactory.java:66)
	at io.github.benas.randombeans.EnhancedRandomImpl.doPopulateBean(EnhancedRandomImpl.java:109)
	... 90 more
Caused by: java.lang.IllegalAccessError: java.lang.Class
	at java.base/jdk.internal.reflect.GeneratedSerializationConstructorAccessor2.newInstance(Unknown Source)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
	at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
	at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
	at io.github.benas.randombeans.ObjectFactory.createNewInstance(ObjectFactory.java:79)
	at io.github.benas.randombeans.ObjectFactory.createInstance(ObjectFactory.java:64)
	... 91 more

It could be related to a new field transient Class<?> StackTraceElement#declaringClassObject added in jdk9.

@fmbenhassine
Copy link
Member

Hi,

Thank you for reporting this.

Random Beans is Java 9 compatible since v3.7.0 (see #243). Which version are you using?

Have you tried to use the --permit-illegal-access flag?

Kr
Mahmoud

@Spikhalskiy
Copy link
Author

Spikhalskiy commented Nov 8, 2017

@benas as pointed in the original post by the pom definition, the problem presented in the last 3.7.0 version

@Spikhalskiy
Copy link
Author

Spikhalskiy commented Nov 8, 2017

Adding --illegal-access=permit doesn't help.
And I don't think that it can. random-beans tries to create an instance of java.lang.Class:

Caused by: io.github.benas.randombeans.api.ObjectGenerationException: Unable to create an instance of type: class java.lang.Class
	at io.github.benas.randombeans.ObjectFactory.createInstance(ObjectFactory.java:66)

This code:

ObjectFactory factory = new io.github.benas.randombeans.ObjectFactory();
factory.createInstance(java.lang.Class.class);

doesn't work in both Java 8 and Java 9.

So, looks like random-beans doesn't handle field of the type Class properly and the bug caused not by the changes in the reflective access in Java 9, but by the fact that StackTraceElement in JDK 9 code got a field of type Class which breaks random-beans.

@fmbenhassine
Copy link
Member

Hi,

the fact that StackTraceElement in JDK 9 code got a field of type Class which breaks random-beans.

Yes! This is the issue (nothing to do with reflective access). Current version (3.x line) is not meant to be used with Java 9 (even if there is a lot of effort already done to make it Java 9 compatible). So let me plan this issue for v4 which wil require Java 9 (new entry will be added here).

Anyway, thank you for pointing out this issue and keep tuned for v4.

Kr
Mahmoud

@fmbenhassine
Copy link
Member

Hi @Spikhalskiy,

I fixed this issue by excluding fields of type Class from the object graph being randomized. It should work fine now with Java 9+. I deployed the fix to maven central in v3.8.0-SNAPSHOT. You can give it a try already (see how to use the snapshot version here).

I'm closing this issue as it will be part of v3.8, but if you still have any problem, please leave a comment here. Thank you for reporting this out!

Best regards,
Mahmoud

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