WW-5326 refactor(ognl): parameterize all accessor implementations with StrutsContext

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Lukasz Lenart
2026-04-06 18:31:58 +02:00
parent 47718f5a8a
commit f7923fc704
12 changed files with 65 additions and 62 deletions
@@ -26,7 +26,6 @@ import org.apache.struts2.util.ValueStack;
import ognl.MethodFailedException;
import ognl.NoSuchPropertyException;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import ognl.OgnlRuntime;
import org.apache.commons.lang3.BooleanUtils;
@@ -34,6 +33,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.StrutsException;
import org.apache.struts2.ognl.StrutsContext;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
@@ -64,7 +64,7 @@ public class CompoundRootAccessor implements RootAccessor, InternalDestroyable {
* Used by OGNl to generate bytecode
*/
@Override
public String getSourceAccessor(OgnlContext context, Object target, Object index) {
public String getSourceAccessor(StrutsContext context, Object target, Object index) {
return null;
}
@@ -72,7 +72,7 @@ public class CompoundRootAccessor implements RootAccessor, InternalDestroyable {
* Used by OGNl to generate bytecode
*/
@Override
public String getSourceSetter(OgnlContext context, Object target, Object index) {
public String getSourceSetter(StrutsContext context, Object target, Object index) {
return null;
}
@@ -96,7 +96,7 @@ public class CompoundRootAccessor implements RootAccessor, InternalDestroyable {
}
@Override
public void setProperty(OgnlContext context, Object target, Object name, Object value) throws OgnlException {
public void setProperty(StrutsContext context, Object target, Object name, Object value) throws OgnlException {
CompoundRoot root = (CompoundRoot) target;
for (Object o : root) {
@@ -138,7 +138,7 @@ public class CompoundRootAccessor implements RootAccessor, InternalDestroyable {
}
@Override
public Object getProperty(OgnlContext context, Object target, Object name) throws OgnlException {
public Object getProperty(StrutsContext context, Object target, Object name) throws OgnlException {
CompoundRoot root = (CompoundRoot) target;
if (name instanceof Integer index) {
@@ -182,7 +182,7 @@ public class CompoundRootAccessor implements RootAccessor, InternalDestroyable {
}
@Override
public Object callMethod(OgnlContext context, Object target, String name, Object[] objects) throws MethodFailedException {
public Object callMethod(StrutsContext context, Object target, String name, Object[] objects) throws MethodFailedException {
CompoundRoot root = (CompoundRoot) target;
if ("describe".equals(name)) {
@@ -270,12 +270,12 @@ public class CompoundRootAccessor implements RootAccessor, InternalDestroyable {
}
@Override
public Object callStaticMethod(OgnlContext transientVars, Class aClass, String s, Object[] objects) throws MethodFailedException {
public Object callStaticMethod(StrutsContext transientVars, Class aClass, String s, Object[] objects) throws MethodFailedException {
return null;
}
@Override
public Class classForName(String className, OgnlContext context) throws ClassNotFoundException {
public Class classForName(String className, StrutsContext context) throws ClassNotFoundException {
Object root = Ognl.getRoot(context);
if (disallowCustomOgnlMap) {
@@ -19,20 +19,20 @@
package org.apache.struts2.ognl.accessor;
import ognl.ObjectPropertyAccessor;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.apache.struts2.dispatcher.HttpParameters;
import org.apache.struts2.ognl.StrutsContext;
public class HttpParametersPropertyAccessor extends ObjectPropertyAccessor {
public class HttpParametersPropertyAccessor extends ObjectPropertyAccessor<StrutsContext> {
@Override
public Object getProperty(OgnlContext context, Object target, Object oname) throws OgnlException {
public Object getProperty(StrutsContext context, Object target, Object oname) throws OgnlException {
HttpParameters parameters = (HttpParameters) target;
return parameters.get(String.valueOf(oname)).getObject();
}
@Override
public void setProperty(OgnlContext context, Object target, Object oname, Object value) throws OgnlException {
public void setProperty(StrutsContext context, Object target, Object oname, Object value) throws OgnlException {
throw new OgnlException("Access to " + target.getClass().getName() + " is read-only!");
}
}
@@ -21,12 +21,12 @@ package org.apache.struts2.ognl.accessor;
import org.apache.struts2.conversion.impl.XWorkConverter;
import org.apache.struts2.util.reflection.ReflectionContextState;
import ognl.ObjectPropertyAccessor;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.apache.struts2.ognl.StrutsContext;
public class ObjectAccessor extends ObjectPropertyAccessor {
public class ObjectAccessor extends ObjectPropertyAccessor<StrutsContext> {
@Override
public Object getProperty(OgnlContext map, Object o, Object o1) throws OgnlException {
public Object getProperty(StrutsContext map, Object o, Object o1) throws OgnlException {
Object obj = super.getProperty(map, o, o1);
map.put(XWorkConverter.LAST_BEAN_CLASS_ACCESSED, o.getClass());
@@ -20,10 +20,10 @@ package org.apache.struts2.ognl.accessor;
import org.apache.struts2.ognl.ObjectProxy;
import org.apache.struts2.util.reflection.ReflectionContextState;
import ognl.OgnlContext;
import ognl.OgnlException;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
import org.apache.struts2.ognl.StrutsContext;
/**
* Is able to access (set/get) properties on a given object.
@@ -33,13 +33,13 @@ import ognl.PropertyAccessor;
*
* @author Gabe
*/
public class ObjectProxyPropertyAccessor implements PropertyAccessor {
public class ObjectProxyPropertyAccessor implements PropertyAccessor<StrutsContext> {
/**
* Used by OGNl to generate bytecode
*/
@Override
public String getSourceAccessor(OgnlContext context, Object target, Object index) {
public String getSourceAccessor(StrutsContext context, Object target, Object index) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
@@ -47,12 +47,13 @@ public class ObjectProxyPropertyAccessor implements PropertyAccessor {
* Used by OGNl to generate bytecode
*/
@Override
public String getSourceSetter(OgnlContext context, Object target, Object index) {
public String getSourceSetter(StrutsContext context, Object target, Object index) {
return null;
}
@Override
public Object getProperty(OgnlContext context, Object target, Object name) throws OgnlException {
@SuppressWarnings("unchecked")
public Object getProperty(StrutsContext context, Object target, Object name) throws OgnlException {
ObjectProxy proxy = (ObjectProxy) target;
setupContext(context, proxy);
@@ -61,7 +62,8 @@ public class ObjectProxyPropertyAccessor implements PropertyAccessor {
}
@Override
public void setProperty(OgnlContext context, Object target, Object name, Object value) throws OgnlException {
@SuppressWarnings("unchecked")
public void setProperty(StrutsContext context, Object target, Object name, Object value) throws OgnlException {
ObjectProxy proxy = (ObjectProxy) target;
setupContext(context, proxy);
@@ -75,7 +77,7 @@ public class ObjectProxyPropertyAccessor implements PropertyAccessor {
* @param context
* @param proxy
*/
private void setupContext(OgnlContext context, ObjectProxy proxy) {
private void setupContext(StrutsContext context, ObjectProxy proxy) {
ReflectionContextState.setLastBeanClassAccessed(context, proxy.getLastClassAccessed());
ReflectionContextState.setLastBeanPropertyAccessed(context, proxy.getLastPropertyAccessed());
}
@@ -19,14 +19,14 @@
package org.apache.struts2.ognl.accessor;
import ognl.ObjectPropertyAccessor;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.apache.struts2.dispatcher.Parameter;
import org.apache.struts2.ognl.StrutsContext;
public class ParameterPropertyAccessor extends ObjectPropertyAccessor {
public class ParameterPropertyAccessor extends ObjectPropertyAccessor<StrutsContext> {
@Override
public Object getProperty(OgnlContext context, Object target, Object oname) throws OgnlException {
public Object getProperty(StrutsContext context, Object target, Object oname) throws OgnlException {
if (target instanceof Parameter parameter) {
if ("value".equalsIgnoreCase(String.valueOf(oname))) {
throw new OgnlException("Access to " + oname + " is not allowed! Call parameter name directly!");
@@ -37,7 +37,7 @@ public class ParameterPropertyAccessor extends ObjectPropertyAccessor {
}
@Override
public void setProperty(OgnlContext context, Object target, Object oname, Object value) throws OgnlException {
public void setProperty(StrutsContext context, Object target, Object oname, Object value) throws OgnlException {
if (target instanceof Parameter) {
throw new OgnlException("Access to " + target.getClass().getName() + " is read-only!");
} else {
@@ -25,10 +25,10 @@ import org.apache.struts2.inject.Inject;
import org.apache.struts2.ognl.OgnlUtil;
import org.apache.struts2.util.reflection.ReflectionContextState;
import ognl.ObjectPropertyAccessor;
import ognl.OgnlContext;
import ognl.OgnlException;
import ognl.OgnlRuntime;
import ognl.SetPropertyAccessor;
import org.apache.struts2.ognl.StrutsContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -40,7 +40,7 @@ import java.util.Map;
/**
* @author Gabe
*/
public class XWorkCollectionPropertyAccessor extends SetPropertyAccessor {
public class XWorkCollectionPropertyAccessor extends SetPropertyAccessor<StrutsContext> {
private static final Logger LOG = LogManager.getLogger(XWorkCollectionPropertyAccessor.class);
@@ -87,7 +87,7 @@ public class XWorkCollectionPropertyAccessor extends SetPropertyAccessor {
* @see ognl.PropertyAccessor#getProperty(java.util.Map, Object, Object)
*/
@Override
public Object getProperty(OgnlContext context, Object target, Object key) throws OgnlException {
public Object getProperty(StrutsContext context, Object target, Object key) throws OgnlException {
LOG.trace("Entering getProperty()");
//check if it is a generic type property.
@@ -186,7 +186,7 @@ public class XWorkCollectionPropertyAccessor extends SetPropertyAccessor {
* Gets an indexed Map by a given key property with the key being
* the value of the property and the value being the
*/
private Map getSetMap(OgnlContext context, Collection collection, String property) throws OgnlException {
private Map getSetMap(StrutsContext context, Collection collection, String property) throws OgnlException {
LOG.trace("getting set Map");
String path = ReflectionContextState.getCurrentPropertyPath(context);
@@ -211,7 +211,7 @@ public class XWorkCollectionPropertyAccessor extends SetPropertyAccessor {
/*
* gets a bean with the given
*/
public Object getPropertyThroughIteration(OgnlContext context, Collection collection, String property, Object key)
public Object getPropertyThroughIteration(StrutsContext context, Collection collection, String property, Object key)
throws OgnlException {
//TODO
for (Object currTest : collection) {
@@ -224,7 +224,7 @@ public class XWorkCollectionPropertyAccessor extends SetPropertyAccessor {
}
@Override
public void setProperty(OgnlContext context, Object target, Object name, Object value) throws OgnlException {
public void setProperty(StrutsContext context, Object target, Object name, Object value) throws OgnlException {
Class lastClass = (Class) context.get(XWorkConverter.LAST_BEAN_CLASS_ACCESSED);
String lastProperty = (String) context.get(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED);
Class convertToClass = objectTypeDeterminer.getElementClass(lastClass, lastProperty, name);
@@ -256,7 +256,7 @@ public class XWorkCollectionPropertyAccessor extends SetPropertyAccessor {
super.setProperty(context, target, name, realValue);
}
private Object getRealValue(OgnlContext context, Object value, Class convertToClass) {
private Object getRealValue(StrutsContext context, Object value, Class convertToClass) {
if (value == null || convertToClass == null) {
return value;
}
@@ -20,15 +20,15 @@ package org.apache.struts2.ognl.accessor;
import ognl.EnumerationPropertyAccessor;
import ognl.ObjectPropertyAccessor;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.apache.struts2.ognl.StrutsContext;
public class XWorkEnumerationAccessor extends EnumerationPropertyAccessor {
public class XWorkEnumerationAccessor extends EnumerationPropertyAccessor<StrutsContext> {
private final ObjectPropertyAccessor opa = new ObjectPropertyAccessor();
private final ObjectPropertyAccessor<StrutsContext> opa = new ObjectPropertyAccessor<>();
@Override
public void setProperty(OgnlContext context, Object target, Object name, Object value) throws OgnlException {
public void setProperty(StrutsContext context, Object target, Object name, Object value) throws OgnlException {
opa.setProperty(context, target, name, value);
}
}
@@ -20,15 +20,15 @@ package org.apache.struts2.ognl.accessor;
import ognl.IteratorPropertyAccessor;
import ognl.ObjectPropertyAccessor;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.apache.struts2.ognl.StrutsContext;
public class XWorkIteratorPropertyAccessor extends IteratorPropertyAccessor {
public class XWorkIteratorPropertyAccessor extends IteratorPropertyAccessor<StrutsContext> {
private final ObjectPropertyAccessor opa = new ObjectPropertyAccessor();
private final ObjectPropertyAccessor<StrutsContext> opa = new ObjectPropertyAccessor<>();
@Override
public void setProperty(OgnlContext context, Object target, Object name, Object value) throws OgnlException {
public void setProperty(StrutsContext context, Object target, Object name, Object value) throws OgnlException {
opa.setProperty(context, target, name, value);
}
}
@@ -25,9 +25,9 @@ import org.apache.struts2.inject.Inject;
import org.apache.struts2.ognl.OgnlUtil;
import org.apache.struts2.util.reflection.ReflectionContextState;
import ognl.ListPropertyAccessor;
import ognl.OgnlContext;
import ognl.OgnlException;
import ognl.PropertyAccessor;
import org.apache.struts2.ognl.StrutsContext;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.StrutsException;
@@ -41,7 +41,7 @@ import java.util.List;
*
* @author Gabriel Zimmerman
*/
public class XWorkListPropertyAccessor extends ListPropertyAccessor {
public class XWorkListPropertyAccessor extends ListPropertyAccessor<StrutsContext> {
private XWorkCollectionPropertyAccessor _sAcc = new XWorkCollectionPropertyAccessor();
@@ -57,7 +57,7 @@ public class XWorkListPropertyAccessor extends ListPropertyAccessor {
}
@Inject("java.util.Collection")
public void setXWorkCollectionPropertyAccessor(PropertyAccessor acc) {
public void setXWorkCollectionPropertyAccessor(PropertyAccessor<StrutsContext> acc) {
this._sAcc = (XWorkCollectionPropertyAccessor) acc;
}
@@ -82,7 +82,7 @@ public class XWorkListPropertyAccessor extends ListPropertyAccessor {
}
@Override
public Object getProperty(OgnlContext context, Object target, Object name) throws OgnlException {
public Object getProperty(StrutsContext context, Object target, Object name) throws OgnlException {
if (ReflectionContextState.isGettingByKeyProperty(context)
|| name.equals(XWorkCollectionPropertyAccessor.KEY_PROPERTY_FOR_CREATION)) {
@@ -137,7 +137,7 @@ public class XWorkListPropertyAccessor extends ListPropertyAccessor {
}
@Override
public void setProperty(OgnlContext context, Object target, Object name, Object value)
public void setProperty(StrutsContext context, Object target, Object name, Object value)
throws OgnlException {
Class lastClass = (Class) context.get(XWorkConverter.LAST_BEAN_CLASS_ACCESSED);
@@ -185,7 +185,7 @@ public class XWorkListPropertyAccessor extends ListPropertyAccessor {
super.setProperty(context, target, name, realValue);
}
private Object getRealValue(OgnlContext context, Object value, Class convertToClass) {
private Object getRealValue(StrutsContext context, Object value, Class convertToClass) {
if (value == null || convertToClass == null) {
return value;
}
@@ -24,8 +24,8 @@ import org.apache.struts2.conversion.impl.XWorkConverter;
import org.apache.struts2.inject.Inject;
import org.apache.struts2.util.reflection.ReflectionContextState;
import ognl.MapPropertyAccessor;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.apache.struts2.ognl.StrutsContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -37,7 +37,7 @@ import java.util.Map;
*
* @author Gabriel Zimmerman
*/
public class XWorkMapPropertyAccessor extends MapPropertyAccessor {
public class XWorkMapPropertyAccessor extends MapPropertyAccessor<StrutsContext> {
private static final Logger LOG = LogManager.getLogger(XWorkMapPropertyAccessor.class);
@@ -63,7 +63,7 @@ public class XWorkMapPropertyAccessor extends MapPropertyAccessor {
}
@Override
public Object getProperty(OgnlContext context, Object target, Object name) throws OgnlException {
public Object getProperty(StrutsContext context, Object target, Object name) throws OgnlException {
LOG.trace("Entering getProperty ({},{},{})", context, target, name);
ReflectionContextState.updateCurrentPropertyPath(context, name);
@@ -123,7 +123,7 @@ public class XWorkMapPropertyAccessor extends MapPropertyAccessor {
}
@Override
public void setProperty(OgnlContext context, Object target, Object name, Object value) throws OgnlException {
public void setProperty(StrutsContext context, Object target, Object name, Object value) throws OgnlException {
LOG.trace("Entering setProperty({},{},{},{})", context, target, name, value);
Object key = getKey(context, name);
@@ -131,7 +131,7 @@ public class XWorkMapPropertyAccessor extends MapPropertyAccessor {
map.put(key, getValue(context, value));
}
private Object getValue(OgnlContext context, Object value) {
private Object getValue(StrutsContext context, Object value) {
Class lastClass = (Class) context.get(XWorkConverter.LAST_BEAN_CLASS_ACCESSED);
String lastProperty = (String) context.get(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED);
if (lastClass == null || lastProperty == null) {
@@ -144,7 +144,7 @@ public class XWorkMapPropertyAccessor extends MapPropertyAccessor {
return xworkConverter.convertValue(context, value, elementClass);
}
private Object getKey(OgnlContext context, Object name) {
private Object getKey(StrutsContext context, Object name) {
Class lastClass = (Class) context.get(XWorkConverter.LAST_BEAN_CLASS_ACCESSED);
String lastProperty = (String) context.get(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED);
if (lastClass == null || lastProperty == null) {
@@ -21,9 +21,9 @@ package org.apache.struts2.ognl.accessor;
import org.apache.struts2.util.reflection.ReflectionContextState;
import ognl.MethodFailedException;
import ognl.ObjectMethodAccessor;
import ognl.OgnlContext;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;
import org.apache.struts2.ognl.StrutsContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -38,12 +38,13 @@ import java.util.Collection;
* @author Patrick Lightbody
* @author tmjee
*/
public class XWorkMethodAccessor extends ObjectMethodAccessor {
public class XWorkMethodAccessor extends ObjectMethodAccessor<StrutsContext> {
private static final Logger LOG = LogManager.getLogger(XWorkMethodAccessor.class);
@Override
public Object callMethod(OgnlContext context, Object object, String string, Object[] objects) throws MethodFailedException {
@SuppressWarnings("unchecked")
public Object callMethod(StrutsContext context, Object object, String string, Object[] objects) throws MethodFailedException {
//Collection property accessing
//this if statement ensures that ognl
@@ -94,7 +95,7 @@ public class XWorkMethodAccessor extends ObjectMethodAccessor {
}
}
private Object callMethodWithDebugInfo(OgnlContext context, Object object, String methodName, Object[] objects) throws MethodFailedException {
private Object callMethodWithDebugInfo(StrutsContext context, Object object, String methodName, Object[] objects) throws MethodFailedException {
try {
return super.callMethod(context, object, methodName, objects);
} catch (MethodFailedException e) {
@@ -109,7 +110,7 @@ public class XWorkMethodAccessor extends ObjectMethodAccessor {
}
@Override
public Object callStaticMethod(OgnlContext context, Class aClass, String string, Object[] objects) throws MethodFailedException {
public Object callStaticMethod(StrutsContext context, Class aClass, String string, Object[] objects) throws MethodFailedException {
boolean e = ReflectionContextState.isDenyMethodExecution(context);
if (!e) {
@@ -119,7 +120,7 @@ public class XWorkMethodAccessor extends ObjectMethodAccessor {
}
}
private Object callStaticMethodWithDebugInfo(OgnlContext context, Class aClass, String methodName,
private Object callStaticMethodWithDebugInfo(StrutsContext context, Class aClass, String methodName,
Object[] objects) throws MethodFailedException {
try {
return super.callStaticMethod(context, aClass, methodName, objects);
@@ -21,15 +21,15 @@ package org.apache.struts2.ognl.accessor;
import org.apache.struts2.conversion.impl.XWorkConverter;
import org.apache.struts2.util.reflection.ReflectionContextState;
import ognl.ObjectPropertyAccessor;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.apache.struts2.ognl.StrutsContext;
/**
* @author Gabe
*/
public class XWorkObjectPropertyAccessor extends ObjectPropertyAccessor {
public class XWorkObjectPropertyAccessor extends ObjectPropertyAccessor<StrutsContext> {
@Override
public Object getProperty(OgnlContext context, Object target, Object oname) throws OgnlException {
public Object getProperty(StrutsContext context, Object target, Object oname) throws OgnlException {
//set the last set objects in the context
//so if the next objects accessed are
//Maps or Collections they can use the information