This is an automated email from the ASF dual-hosted git repository.

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git


The following commit(s) were added to refs/heads/master by this push:
     new bec28b33 JEXL-375: deal with classes as objects coherently in Sandbox;
bec28b33 is described below

commit bec28b3353fa5e158d72bf9c40c5020308e6ae40
Author: henrib <hen...@apache.org>
AuthorDate: Sun Jul 24 16:22:09 2022 +0200

    JEXL-375: deal with classes as objects coherently in Sandbox;
---
 .../internal/introspection/SandboxUberspect.java   | 11 +++++++----
 .../org/apache/commons/jexl3/Issues300Test.java    | 22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/jexl3/internal/introspection/SandboxUberspect.java
 
b/src/main/java/org/apache/commons/jexl3/internal/introspection/SandboxUberspect.java
index 16e80b67..e7ce2bf2 100644
--- 
a/src/main/java/org/apache/commons/jexl3/internal/introspection/SandboxUberspect.java
+++ 
b/src/main/java/org/apache/commons/jexl3/internal/introspection/SandboxUberspect.java
@@ -24,6 +24,7 @@ import org.apache.commons.jexl3.introspection.JexlPropertySet;
 import org.apache.commons.jexl3.introspection.JexlSandbox;
 import org.apache.commons.jexl3.introspection.JexlUberspect;
 
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
 
@@ -119,16 +120,17 @@ public final class SandboxUberspect implements 
JexlUberspect {
                                           final Object obj,
                                           final Object identifier) {
         if (obj != null) {
+            final Class<?> clazz = obj instanceof Class<?>? (Class<?>) obj : 
obj.getClass();
             if (identifier != null) {
                 final String property = identifier.toString();
-                final String actual = sandbox.read(obj.getClass(), property);
+                final String actual = sandbox.read(clazz, property);
                 if (actual != null) {
                     // no transformation, strict equality: use identifier 
before string conversion
                     final Object pty = eq(actual, property) ? identifier : 
actual;
                     return uberspect.getPropertyGet(resolvers, obj, pty);
                 }
             } else {
-                final String actual = sandbox.read(obj.getClass(), null);
+                final String actual = sandbox.read(clazz, null);
                 if (actual != JexlSandbox.NULL) {
                      return uberspect.getPropertyGet(resolvers, obj, null);
                 }
@@ -148,16 +150,17 @@ public final class SandboxUberspect implements 
JexlUberspect {
                                           final Object identifier,
                                           final Object arg) {
         if (obj != null) {
+            final Class<?> clazz = obj instanceof Class<?>? (Class<?>) obj : 
obj.getClass();
             if (identifier != null) {
                 final String property = identifier.toString();
-                final String actual = sandbox.write(obj.getClass(), property);
+                final String actual = sandbox.write(clazz, property);
                 if (actual != null) {
                     // no transformation, strict equality: use identifier 
before string conversion
                     final Object pty = eq(actual, property) ? identifier : 
actual;
                     return uberspect.getPropertySet(resolvers, obj, pty, arg);
                 }
             } else {
-                final String actual = sandbox.write(obj.getClass(), null);
+                final String actual = sandbox.write(clazz, null);
                 if (actual != JexlSandbox.NULL) {
                     return uberspect.getPropertySet(resolvers, obj, null, arg);
                 }
diff --git a/src/test/java/org/apache/commons/jexl3/Issues300Test.java 
b/src/test/java/org/apache/commons/jexl3/Issues300Test.java
index f3d2d1ce..12187785 100644
--- a/src/test/java/org/apache/commons/jexl3/Issues300Test.java
+++ b/src/test/java/org/apache/commons/jexl3/Issues300Test.java
@@ -18,6 +18,7 @@ package org.apache.commons.jexl3;
 
 import org.apache.commons.jexl3.internal.Engine32;
 import org.apache.commons.jexl3.internal.OptionsContext;
+import org.apache.commons.jexl3.introspection.JexlSandbox;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -852,4 +853,25 @@ public class Issues300Test {
         }
     }
 
+    @Test
+    public void test375() {
+        JexlSandbox jexlSandbox = new JexlSandbox(false);
+        jexlSandbox.allow(Type375.class.getName());
+        JexlEngine engine = new JexlBuilder().sandbox(jexlSandbox).create();
+
+        JexlContext context = new MapContext();
+        context.set("Type", Type375.class);
+
+        Object result = 
engine.createScript("Type.valueOf('DOMICILE')").execute(context);
+        Assert.assertEquals(Type375.DOMICILE, result);
+
+        result = engine.createScript("Type.DOMICILE").execute(context);
+        Assert.assertEquals(Type375.DOMICILE, result);
+    }
+
+    public enum Type375 {
+        DELIVERY_ADDRESS,
+        DOMICILE
+    }
+
 }

Reply via email to