question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression: Unable to create a random instance (brother subclasses)

See original GitHub issue

Consider this snippet:

import lombok.Data;
import lombok.experimental.Accessors;
import org.jeasy.random.EasyRandom;
import org.jeasy.random.EasyRandomParameters;

public class EasyRandomTest {

    public static void main(String[] args) {
        EasyRandom easyRandom = new EasyRandom(new EasyRandomParameters());

        System.out.println(easyRandom.nextObject(BulkOperationObjectError.class));
    }

    @Data
    @Accessors(chain = true)
    public static class ObjectError {
        private String objectName;
    }

    @Data
    @Accessors(chain = true)
    public static class BulkOperationObjectError extends ObjectError {
        private FieldError fieldError;
    }

    @Data
    @Accessors(chain = true)
    public static class FieldError extends ObjectError {
        @Override
        public FieldError setObjectName(String objectName) {
            super.setObjectName(objectName);
            return this;
        }
    }
}

I minimized the code as much as possible. In 4.2.0 it successfully creates a random instance. In 4.3.0-SNAPSHOT it fails with exception:

Exception in thread "main" org.jeasy.random.ObjectCreationException: Unable to create a random instance of type class com.trilogy.restapi.errors.EasyRandomTest$BulkOperationObjectError
	at org.jeasy.random.EasyRandom.doPopulateBean(EasyRandom.java:172)
	at org.jeasy.random.EasyRandom.nextObject(EasyRandom.java:100)
	at com.XXX.restapi.errors.EasyRandomTest.main(EasyRandomTest.java:14)
Caused by: java.lang.IllegalArgumentException: com.trilogy.restapi.errors.EasyRandomTest$BulkOperationObjectError is not assignable from com.XXX.restapi.errors.EasyRandomTest$FieldError
	at org.apache.commons.beanutils.MethodUtils.getAccessibleMethod(MethodUtils.java:793)
	at org.apache.commons.beanutils.PropertyUtilsBean.getWriteMethod(PropertyUtilsBean.java:1319)
	at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2094)
	at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1915)
	at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2022)
	at org.jeasy.random.util.ReflectionUtils.setProperty(ReflectionUtils.java:153)
	at org.jeasy.random.FieldPopulator.populateField(FieldPopulator.java:102)
	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)
	... 2 more

I did not investigate it deeper yet, but for sure it may become a problem in case of version upgrade.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
seregamorphcommented, Nov 2, 2020

I raised an issue https://issues.apache.org/jira/browse/BEANUTILS-541 To address an issue in easy-random we have at least two ways:

  • write our own setters resolver (complicated, we should respect all possible issues like assignment rules, primitive types, etc), the impl can be copied from spring-beans BeanUtils (dependency does not look like a good idea)
  • Workaround with cache eviction:
// ReflectionUtils.java
...
    } catch (IntrospectionException | IllegalAccessException e) {
        try { // to call fluent setter
            flushIntrospectorCaches(object.getClass());
            PROPERTY_UTILS_BEAN.setProperty(object, field.getName(), value);
        } catch (NoSuchMethodException noSuchMethodException) {
            // otherwise, set field using reflection
            setFieldValue(object, field, value);
        }
    }
}

private static void flushIntrospectorCaches(Class<?> type) {
    // https://issues.apache.org/jira/browse/BEANUTILS-541
    Introspector.flushFromCaches(type);
    Class<?> superType = type.getSuperclass();
    if (superType != null && superType != Object.class) {
        flushIntrospectorCaches(superType);
    }
}
0reactions
seregamorphcommented, Nov 3, 2020

The second problem with overriden field cannot be workarounded with caches flush and recreating the PropertyUtilsBean object. See #438 with detailed explanations

Read more comments on GitHub >

github_iconTop Results From Across the Web

Alternative to reflection to generate objects of random ...
I have a problem with a program in Java. My program have an interface Bonus and nine subclasses that extend Bonus . I...
Read more >
caret: Classification and Regression Training
Bagging can also be used to create the models. ... CAIMAN brothers: A family of powerful classification and class modeling techniques.
Read more >
Random Forest Regression: When Does It Fail and Why?
In this article, we'll look at a major problem with using Random Forest for Regression which is extrapolation.
Read more >
the Twisted documentation!
This document describes the optional dependencies that Twisted supports. The dependencies are python packages that. Twisted's developers have found useful ...
Read more >
[Example code]-Using function output for instance initialisation in ...
I'm trying to do this: class Color: def __init__(self, r, g, b): self.r = r self.g = g self.b = b im_rgb =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found