spring-projects/spring-framework · error · InvalidPropertyException
Field is not accessible
Error message
Field is not accessible
What it means
InvalidPropertyException thrown by DirectFieldAccessor.DirectFieldAccessorHandler.getValue() when java.lang.reflect.Field.get fails with IllegalAccessException or InaccessibleObjectException even after ReflectionUtils.makeAccessible. This is field-based (not accessor-based) access being blocked by the JVM, typically a module/permission boundary.
Source
Thrown at spring-beans/src/main/java/org/springframework/beans/DirectFieldAccessor.java:147
@Override
public TypeDescriptor getCollectionType(int nestingLevel) {
return new TypeDescriptor(this.resolvableType.getNested(nestingLevel).asCollection().getGeneric(),
null, this.field.getAnnotations());
}
@Override
public @Nullable TypeDescriptor nested(int level) {
return TypeDescriptor.nested(this.field, level);
}
@Override
public @Nullable Object getValue() throws Exception {
try {
ReflectionUtils.makeAccessible(this.field);
return this.field.get(getWrappedInstance());
}
catch (IllegalAccessException | InaccessibleObjectException ex) {
throw new InvalidPropertyException(getWrappedClass(),
this.field.getName(), "Field is not accessible", ex);
}
}
@Override
public void setValue(@Nullable Object value) throws Exception {
try {
ReflectionUtils.makeAccessible(this.field);
this.field.set(getWrappedInstance(), value);
}
catch (IllegalAccessException | InaccessibleObjectException ex) {
throw new InvalidPropertyException(getWrappedClass(), this.field.getName(),
"Field is not accessible", ex);
}
}
}
}View on GitHub (pinned to e8729d0438)
Solutions
- Add '--add-opens <module>/<package>=ALL-UNNAMED' (or the specific Spring module) to the JVM args.
- Avoid field access into JDK/JVM internal classes; switch to accessor-based BeanWrapper where possible.
- If on the classpath as a JPMS module, add 'opens' directives to module-info for the packages Spring must touch.
- Run on Java 8-15 if you cannot add opens and field access is mandatory (not recommended long-term).
Example fix
# before
java -jar app.jar # InaccessibleObjectException on field read
# after
java --add-opens java.base/java.lang=ALL-UNNAMED \
--add-opens my.module/com.example.beans=ALL-UNNAMED \
-jar app.jar Defensive patterns
Strategy: validation
Validate before calling
// Pre-flight field read accessibility under the current JVM
Field f = target.getClass().getDeclaredField(fieldName);
try {
ReflectionUtils.makeAccessible(f);
f.setAccessible(true);
// try a no-op get to confirm
f.get(target);
} catch (IllegalAccessException | InaccessibleObjectException e) {
throw new IllegalStateException("Field " + fieldName + " not accessible; add --add-opens", e);
} Type guard
static boolean isFieldReadable(Class<?> clazz, String fieldName) {
try {
Field f = clazz.getDeclaredField(fieldName);
f.setAccessible(true);
f.get(null); // works for static; for instance use a probe instance
return true;
} catch (Exception e) { return false; }
} Try / catch
try {
return accessor.getPropertyValue(name);
} catch (InvalidPropertyException ex) {
if ("Field is not accessible".equals(ex.getMessage())) {
log.error("Add --add-opens {}/{}=ALL-UNNAMED to JVM args",
fieldDeclaringModule(targetClass), fieldDeclaringPackage(targetClass));
}
throw ex;
} Prevention
- Add required --add-opens flags in your build/runtime scripts (Spring Boot's documented list).
- Prefer constructor/setter injection over field injection to sidestep this entirely.
- In tests, use spring-boot-starter-test which surfaces recommended opens.
- Audit module-info of your own modules to 'opens' bean packages.
When it happens
Trigger: Using DirectFieldAccessor to read a field on an instance of a class in a non-opened module under JPMS, or running under a SecurityManager that vetoes reflection; field belongs to a JDK class or a third-party module that does not 'open' its package to Spring.
Common situations: Injecting into JDK classes (java.lang, java.util) via field access; Spring test support reflecting into JDK types under Java 16+; SpringBoot on Java 17+ without the recommended --add-opens flags; third-party lib compiled as a JPMS module that doesn't open its packages.
Related errors
- Could not access aspect constructor: {}
- Could not access method [{method}]
- Illegal arguments for constructor
- Could not copy property '${targetPd.getName()}' from source
- Failed to find Java constructor for Kotlin primary construct
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/6581e11e09982ad1.json.
Report an issue: GitHub.