Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/ognl/OgnlContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public class OgnlContext implements Map<String, Object> {
private static final String TRACE_EVALUATIONS_CONTEXT_KEY = "_traceEvaluations";
private static final String LAST_EVALUATION_CONTEXT_KEY = "_lastEvaluation";
private static final String KEEP_LAST_EVALUATION_CONTEXT_KEY = "_keepLastEvaluation";
private static final String IGNORE_READ_METHODS_CONTEXT_KEY = "_ignoreReadMethods";
private static final String PROPERTY_KEY_PREFIX = "ognl";
private static final boolean DEFAULT_IGNORE_READ_METHODS = false;
private static boolean DEFAULT_TRACE_EVALUATIONS = false;
private static boolean DEFAULT_KEEP_LAST_EVALUATION = false;

Expand All @@ -53,6 +55,7 @@ public class OgnlContext implements Map<String, Object> {
private Evaluation currentEvaluation;
private Evaluation lastEvaluation;
private boolean keepLastEvaluation = DEFAULT_KEEP_LAST_EVALUATION;
private boolean ignoreReadMethods = DEFAULT_IGNORE_READ_METHODS;

private final Map<String, Object> internalContext;

Expand All @@ -67,6 +70,7 @@ public class OgnlContext implements Map<String, Object> {
RESERVED_KEYS.put(TRACE_EVALUATIONS_CONTEXT_KEY, null);
RESERVED_KEYS.put(LAST_EVALUATION_CONTEXT_KEY, null);
RESERVED_KEYS.put(KEEP_LAST_EVALUATION_CONTEXT_KEY, null);
RESERVED_KEYS.put(IGNORE_READ_METHODS_CONTEXT_KEY, null);

try {
String property;
Expand Down Expand Up @@ -242,6 +246,26 @@ public void setKeepLastEvaluation(boolean value) {
keepLastEvaluation = value;
}


/**
* Returns true if read methods of properties are ignored when accessing properties. The default is false.
*
* @return true if read methods of properties are ignored when accessing properties, false otherwise.
*/
public boolean isIgnoreReadMethods() {
return ignoreReadMethods;
}


/**
* Sets read methods of properties are ignored when accessing properties. The default is false.
*
* @param value true if read methods of properties are ignored when accessing properties, false otherwise.
*/
public void setIgnoreReadMethods(boolean value) {
this.ignoreReadMethods = value;
}

public void setCurrentObject(Object value) {
currentObject = value;
}
Expand Down Expand Up @@ -476,6 +500,9 @@ public Object get(Object key) {
case OgnlContext.KEEP_LAST_EVALUATION_CONTEXT_KEY:
result = isKeepLastEvaluation() ? Boolean.TRUE : Boolean.FALSE;
break;
case OgnlContext.IGNORE_READ_METHODS_CONTEXT_KEY:
result = isIgnoreReadMethods() ? Boolean.TRUE : Boolean.FALSE;
break;
default:
throw new IllegalArgumentException("unknown reserved key '" + key + "'");
}
Expand Down Expand Up @@ -511,6 +538,10 @@ public Object put(String key, Object value) {
result = isKeepLastEvaluation() ? Boolean.TRUE : Boolean.FALSE;
setKeepLastEvaluation(OgnlOps.booleanValue(value));
break;
case OgnlContext.IGNORE_READ_METHODS_CONTEXT_KEY:
result = isIgnoreReadMethods() ? Boolean.TRUE : Boolean.FALSE;
setIgnoreReadMethods(OgnlOps.booleanValue(value));
break;
default:
throw new IllegalArgumentException("unknown reserved key '" + key + "'");
}
Expand Down Expand Up @@ -549,6 +580,9 @@ public Object remove(Object key) {
case OgnlContext.KEEP_LAST_EVALUATION_CONTEXT_KEY:
throw new IllegalArgumentException("Can't remove "
+ OgnlContext.KEEP_LAST_EVALUATION_CONTEXT_KEY + " from context");
case OgnlContext.IGNORE_READ_METHODS_CONTEXT_KEY:
throw new IllegalArgumentException("Can't remove "
+ OgnlContext.IGNORE_READ_METHODS_CONTEXT_KEY + " from context");
default:
throw new IllegalArgumentException("Unknown reserved key '" + key + "'");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ public static Object getMethodValue(OgnlContext context, Object target, String p
throws OgnlException, IllegalAccessException, NoSuchMethodException {
Object result = null;
Method m = getGetMethod((target == null) ? null : target.getClass(), propertyName);
if (m == null)
if (m == null && !context.isIgnoreReadMethods())
m = getReadMethod((target == null) ? null : target.getClass(), propertyName, null);

if (checkAccessAndExistence) {
Expand Down
19 changes: 15 additions & 4 deletions src/test/java/ognl/OgnlContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

class OgnlContextTest {

Expand Down Expand Up @@ -125,4 +122,18 @@ void currentEvaluation_shouldNotBeNull() throws OgnlException {
private static OgnlContext createOgnlContext() {
return new OgnlContext(null, null, new DefaultMemberAccess(false));
}

@Test
void ignoreReadMethod() {
OgnlContext context = createOgnlContext();
assertFalse(context.isIgnoreReadMethods());
assertEquals(Boolean.FALSE, context.get("_ignoreReadMethods"));
context.setIgnoreReadMethods(true);
assertTrue(context.isIgnoreReadMethods());
assertEquals(Boolean.TRUE, context.get("_ignoreReadMethods"));
assertEquals(Boolean.TRUE, context.put("_ignoreReadMethods", false));
assertFalse(context.isIgnoreReadMethods());
assertEquals(Boolean.FALSE, context.get("_ignoreReadMethods"));
assertThrows(IllegalArgumentException.class, () -> context.remove("_ignoreReadMethods"));
}
}
22 changes: 22 additions & 0 deletions src/test/java/ognl/TestObjectPropertyAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@

import java.beans.IntrospectionException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
* Tests various methods / functionality of {@link ObjectPropertyAccessor}.
Expand Down Expand Up @@ -68,6 +72,24 @@ public void setage(String age) {
}
}

public static class KafkaFetcher {
private final List<Future<?>> completedFutures = new ArrayList<>();

public boolean hasCompletedFutures() {
return !completedFutures.isEmpty();
}
}

public void testGetPossibleProperty() throws OgnlException {
OgnlContext context = (OgnlContext) this.context;
KafkaFetcher fetcher = new KafkaFetcher();
assertEquals(Boolean.FALSE, propertyAccessor.getPossibleProperty(context, fetcher, "completedFutures"));
OgnlContext defaultContext = Ognl.createDefaultContext(null, new ExcludedObjectMemberAccess(true));
defaultContext.setIgnoreReadMethods(true);
assertEquals(Collections.emptyList(), new ObjectPropertyAccessor().getPossibleProperty(defaultContext,
fetcher, "completedFutures"));
}

public void testSetPossibleProperty() throws OgnlException, IntrospectionException {
OgnlContext context = (OgnlContext) this.context;
SimplePublicClass simplePublic = new SimplePublicClass();
Expand Down
Loading