openpojo icon indicating copy to clipboard operation
openpojo copied to clipboard

LocalDateTime: Warning: An illegal reflective access operation has occurred

Open seanjgildea opened this issue 4 years ago • 4 comments

When testing the Getter and Setter on a LocalDateTime field, the following warnings occur. How do we handle testing LocalDateTime fields?

WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.openpojo.reflection.impl.PojoFieldImpl (file:/C:/Users/treadstone/.m2/repository/com/openpojo/openpojo/0.8.10/openpojo-0.8.10.jar) to field java.time.LocalDateTime.serialVersionUID WARNING: Please consider reporting this to the maintainers of com.openpojo.reflection.impl.PojoFieldImpl WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release

seanjgildea avatar Jul 22 '19 13:07 seanjgildea

Can you please provide the code snippet for your tests?

oshoukry avatar Jul 26 '19 00:07 oshoukry

it's a old one, but we are facing the same problem. here are a possbility to reproduce:

import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.impl.PojoClassFactory;
import com.openpojo.validation.ValidatorBuilder;
import com.openpojo.validation.test.impl.GetterTester;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

class DemoTest {

    @Test
    void validate() {
        final PojoClass pojo = PojoClassFactory.getPojoClass(Demo.class);
        ValidatorBuilder.create().with(new GetterTester()).build().validate(pojo);
    }
}

class Demo {
    private LocalDateTime created;

    public LocalDateTime getCreated() {
        return created;
    }

    public void setCreated(LocalDateTime created) {
        this.created = created;
    }
}

NorthernKgalagadi avatar Feb 22 '21 10:02 NorthernKgalagadi

With the release of the new LTS Java 17, this issue now causes test failure. It seems related to issue #110

/usr/lib/jvm/java-17-oracle/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:~/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/212.5284.40/lib/idea_rt.jar=34677:~/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/212.5284.40/bin -Dfile.encoding=UTF-8 -classpath <removed> com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 dev.davejoyce.employee.model.EmployeeTest
12:08:04.900 [main] INFO com.openpojo.log.LoggerFactory - Logging subsystem initialized to [com.openpojo.log.impl.SLF4JLogger]
12:08:04.907 [main] DEBUG com.openpojo.validation.test.impl.GetterTester - Testing Field [PojoFieldImpl [field=private long dev.davejoyce.employee.model.Employee.id, fieldGetter=PojoMethodImpl [method=getId args=[] return=long], fieldSetter=PojoMethodImpl [method=setId args=[long] return=void]]] with value [-7283292713372533778]
12:08:04.910 [main] INFO com.openpojo.validation.affirm.Affirmation - Dynamically setting affirmation implementation = [com.openpojo.validation.affirm.JavaAssertionAffirmation [@75db5df9: ]]
12:08:04.910 [main] DEBUG com.openpojo.validation.test.impl.GetterTester - Testing Field [PojoFieldImpl [field=private final java.lang.String dev.davejoyce.employee.model.Employee.firstName, fieldGetter=PojoMethodImpl [method=getFirstName args=[] return=class java.lang.String], fieldSetter=null]] with value [NYPn09]
12:08:04.911 [main] DEBUG com.openpojo.validation.test.impl.GetterTester - Testing Field [PojoFieldImpl [field=private final java.lang.String dev.davejoyce.employee.model.Employee.middleName, fieldGetter=PojoMethodImpl [method=getMiddleName args=[] return=class java.lang.String], fieldSetter=null]] with value [wj]
12:08:04.911 [main] DEBUG com.openpojo.validation.test.impl.GetterTester - Testing Field [PojoFieldImpl [field=private final java.lang.String dev.davejoyce.employee.model.Employee.lastName, fieldGetter=PojoMethodImpl [method=getLastName args=[] return=class java.lang.String], fieldSetter=null]] with value [1mU7vuxEaB]

java.lang.reflect.InaccessibleObjectException: Unable to make field private static final long java.time.LocalDate.serialVersionUID accessible: module java.base does not "opens java.time" to unnamed module @ba4d54

	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
	at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
	at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
	at com.openpojo.reflection.impl.PojoFieldImpl.<init>(PojoFieldImpl.java:53)
	at com.openpojo.reflection.impl.PojoFieldFactory.getPojoFields(PojoFieldFactory.java:44)
	at com.openpojo.reflection.service.impl.DefaultPojoClassLookupService.getPojoClass(DefaultPojoClassLookupService.java:67)
	at com.openpojo.reflection.impl.PojoClassFactory.getPojoClass(PojoClassFactory.java:42)
	at com.openpojo.random.impl.DefaultRandomGenerator.doGenerate(DefaultRandomGenerator.java:48)
	at com.openpojo.random.RandomFactory.getRandomValue(RandomFactory.java:99)
	at com.openpojo.random.RandomFactory.getRandomValue(RandomFactory.java:107)
	at com.openpojo.validation.test.impl.GetterTester.run(GetterTester.java:46)
	at com.openpojo.validation.utils.ValidationHelper.runValidation(ValidationHelper.java:102)
	at com.openpojo.validation.impl.DefaultValidator.validate(DefaultValidator.java:46)
	at dev.davejoyce.employee.model.EmployeeTest.validateGettersAndSetters(EmployeeTest.java:67)
	...

Process finished with exit code 255

Code snippets:


import lombok.Data;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Optional;

/**
 * An employee of the company.
 */
@Data
public class Employee {

    private long id;
    private final String firstName;
    private final String middleName;
    private final String lastName;
    private LocalDate hireDate;
    private boolean active = false;

    /**
     * Get years this employee has been with the company.
     *
     * @return number of years this employee has been employed
     */
    public int getYearsEmployed() {
        return Optional.ofNullable(hireDate)
                       .map(dt -> Math.toIntExact(ChronoUnit.YEARS.between(dt, LocalDate.now())))
                       .orElse(0);
    }

}

class EmployeeTest {

    private Employee employee;

    @BeforeEach
    void initializeEmployee() {
        employee = new Employee("John", "Jacob", "Jingleheimer-Schmidt");
    }

    @Test
    void getYearsEmployed_HiredThreeYearsAgo() {
        final LocalDate hireDate = LocalDate.now().minusYears(3L);
        employee.setHireDate(hireDate);
        int actual = employee.getYearsEmployed();
        assertEquals(3, actual);
    }

    @Test
    void getYearsEmployed_HiredToday() {
        final LocalDate hireDate = LocalDate.now();
        employee.setHireDate(hireDate);
        int actual = employee.getYearsEmployed();
        assertEquals(0, actual);
    }

    @Test
    void validateGettersAndSetters() {
        PojoClass employeePojo = PojoClassFactory.getPojoClass(Employee.class);
        Validator validator = ValidatorBuilder.create()
                                              .with(new GetterMustExistRule())
                                              .with(new SetterMustExistRule())
                                              .with(new GetterTester())
                                              .with(new SetterTester())
                                              .with(new SerializableMustHaveSerialVersionUIDRule())
                                              .with(new DefaultValuesNullTester())
                                              .build();
        validator.validate(employeePojo);
    }

}

davejoyce avatar Sep 19 '21 16:09 davejoyce

Is there any update on this issue as I see no activity on this project since last year?

Hitesh-Kirtane avatar Sep 27 '22 19:09 Hitesh-Kirtane