Anonymous avatar Anonymous committed fea2cdf

Reduced some overhead with unneccesarily getting values from expressions when compiling.

Comments (0)

Files changed (9)

src/java/ognl/ASTAdd.java

                 context.setCurrentType(_getterClass);
             }
 
-            try {
-
+            try
+            {
                 Object contextObj = getValueBody(context, target);
                 context.setCurrentObject(contextObj);
-
-            } catch (Throwable t) {
-                // ignore
+            } catch (Throwable t)
+            {
+                throw OgnlOps.castToRuntime(t);
             }
 
             return result;

src/java/ognl/ASTProperty.java

     {
         Class type = context.getCurrentType();
         Class prevType = context.getPreviousType();
-        try {
-            if (!isIndexedAccess()) {
+        try
+        {
+            if (!isIndexedAccess())
+            {
                 Object property = getProperty(context, source);
 
-                if (property instanceof String) { return OgnlRuntime.getIndexedPropertyType(context,
-                                                                                            (source == null) ? null : OgnlRuntime.getCompiler().getInterfaceClass(source.getClass()), (String) property); }
+                if (property instanceof String)
+                {
+                    return OgnlRuntime.getIndexedPropertyType(context, (source == null)
+                            ? null
+                            : OgnlRuntime.getCompiler().getInterfaceClass(source.getClass()), (String) property);
+                }
             }
 
             return OgnlRuntime.INDEXED_PROPERTY_NONE;
-        } finally {
+        } finally
+        {
             context.setCurrentObject(source);
             context.setCurrentType(type);
             context.setPreviousType(prevType);
             throws OgnlException
     {
         Object property = getProperty(context, source);
-        
+
         Object result = OgnlRuntime.getProperty(context, source, property);
 
         if (result == null)
     {
         String result;
 
-        if (isIndexedAccess()) {
+        if (isIndexedAccess())
+        {
             result = "[" + _children[0] + "]";
-        } else {
+        } else
+        {
             result = ((ASTConst) _children[0]).getValue().toString();
         }
         return result;
         Method m = null;
 
         try {
-            /*System.out.println("astproperty is indexed? : " + isIndexedAccess() + " child: " + _children[0].getClass().getName()
-                               + " target: " + target.getClass().getName() + " current object: " + context.getCurrentObject().getClass().getName());*/
+            /* System.out.println("astproperty is indexed? : " + isIndexedAccess() + " child: " + _children[0].getClass().getName()
+                               + " target: " + target.getClass().getName() + " current object: " + context.getCurrentObject().getClass().getName()); */
 
             if (isIndexedAccess())
             {
 
             PropertyDescriptor pd = OgnlRuntime.getPropertyDescriptor(context.getCurrentObject().getClass(), name);
 
+            if (pd != null && pd.getReadMethod() != null
+                && !context.getMemberAccess().isAccessible(context, context.getCurrentObject(), pd.getReadMethod(), name))
+            {
+                throw new UnsupportedCompilationException("Member access forbidden for property " + name + " on class " + context.getCurrentObject().getClass());
+            }
+
             if (this.getIndexedPropertyType(context, context.getCurrentObject()) > 0 && pd != null)
             {
                 // if an indexed method accessor need to use special property descriptors to find methods
 
                 if (pd instanceof IndexedPropertyDescriptor)
+                {
                     m = ((IndexedPropertyDescriptor) pd).getIndexedReadMethod();
-                else
+                } else
                 {
                     if (pd instanceof ObjectIndexedPropertyDescriptor)
                         m = ((ObjectIndexedPropertyDescriptor) pd).getIndexedReadMethod();
                         + " current type " + context.getCurrentType() + " current accessor " + context.getCurrentAccessor()
                     + " prev type " + context.getPreviousType() + " prev accessor " + context.getPreviousAccessor()); */
 
-
                 PropertyAccessor pa = OgnlRuntime.getPropertyAccessor(context.getCurrentObject().getClass());
 
                 if (context.getCurrentObject().getClass().isArray())
 
             PropertyDescriptor pd = OgnlRuntime.getPropertyDescriptor(OgnlRuntime.getCompiler().getInterfaceClass(context.getCurrentObject().getClass()), name);
 
-            if (pd != null && this.getIndexedPropertyType(context, context.getCurrentObject()) > 0){
+            if (pd != null)
+            {
+                Method pdMethod = lastChild(context) ? pd.getWriteMethod() : pd.getReadMethod();
 
+                if (pdMethod != null && !context.getMemberAccess().isAccessible(context, context.getCurrentObject(), pdMethod, name))
+                {
+                    throw new UnsupportedCompilationException("Member access forbidden for property " + name + " on class " + context.getCurrentObject().getClass());
+                }
+            }
+
+            if (pd != null && this.getIndexedPropertyType(context, context.getCurrentObject()) > 0)
+            {
                 // if an indexed method accessor need to use special property descriptors to find methods
 
                 if (pd instanceof IndexedPropertyDescriptor) {

src/java/ognl/ASTStaticMethod.java

             if (clazz == null || m == null)
                 throw new UnsupportedCompilationException("Unable to find class/method combo " + _className + " / "  + _methodName);
 
-            if ((_children != null) && (_children.length > 0)) {
+            if (!context.getMemberAccess().isAccessible(context, clazz, m, _methodName))
+            {
+                throw new UnsupportedCompilationException("Method is not accessible, check your jvm runtime security settings. " +
+                                                          "For static class method " + _className + " / "  + _methodName);
+            }
 
+            if ((_children != null) && (_children.length > 0))
+            {
                 Class[] parms = m.getParameterTypes();
 
-                for(int i = 0; i < _children.length; i++) {
-
-                    if (i > 0) {
+                for(int i = 0; i < _children.length; i++)
+                {
+                    if (i > 0)
+                    {
                         result = result + ", ";
                     }
 
                         parmString = "null";
 
                     // to undo type setting of constants when used as method parameters
-                    if (ASTConst.class.isInstance(_children[i])) {
-
+                    if (ASTConst.class.isInstance(_children[i]))
+                    {
                         context.setCurrentType(prevType);
                     }
 
                     parmString = ExpressionCompiler.getRootExpression(_children[i], context.getRoot(), context) + parmString;
 
                     String cast = "";
-                    if (ExpressionCompiler.shouldCast(_children[i])) {
-
+                    if (ExpressionCompiler.shouldCast(_children[i]))
+                    {
                         cast = (String)context.remove(ExpressionCompiler.PRE_CAST);
                     }
+
                     if (cast == null)
                         cast = "";
 
                     if (NodeType.class.isAssignableFrom(_children[i].getClass()))
                         valueClass = ((NodeType)_children[i]).getGetterClass();
 
-                    if (valueClass != parms[i]) {
+                    if (valueClass != parms[i])
+                    {
+                        if (parms[i].isArray())
+                        {
+                            parmString = OgnlRuntime.getCompiler()
+                                    .createLocalReference(context,
+                                                          "(" + ExpressionCompiler.getCastString(parms[i])
+                                                          + ")ognl.OgnlOps.toArray(" + parmString + ", " + parms[i].getComponentType().getName()
+                                                          + ".class, true)",
+                                                          parms[i]
+                                    );
 
-                        if (parms[i].isArray()) {
+                        } else  if (parms[i].isPrimitive())
+                        {
+                            Class wrapClass = OgnlRuntime.getPrimitiveWrapperClass(parms[i]);
 
                             parmString = OgnlRuntime.getCompiler().createLocalReference(context,
-                                    "(" + ExpressionCompiler.getCastString(parms[i])
-                                    + ")ognl.OgnlOps.toArray(" + parmString + ", " + parms[i].getComponentType().getName()
-                                    + ".class, true)",
-                                    parms[i]
-                            );
-                            
-                        } else  if (parms[i].isPrimitive()) {
-
-                            Class wrapClass = OgnlRuntime.getPrimitiveWrapperClass(parms[i]);
-                            
-                            parmString = OgnlRuntime.getCompiler().createLocalReference(context,
-                                    "((" + wrapClass.getName()
-                                    + ")ognl.OgnlOps.convertValue(" + parmString + ","
-                                    + wrapClass.getName() + ".class, true))."
-                                    + OgnlRuntime.getNumericValueGetter(wrapClass),
-                                    parms[i]
+                                                                                        "((" + wrapClass.getName()
+                                                                                        + ")ognl.OgnlOps.convertValue(" + parmString + ","
+                                                                                        + wrapClass.getName() + ".class, true))."
+                                                                                        + OgnlRuntime.getNumericValueGetter(wrapClass),
+                                                                                        parms[i]
                             );
 
-                        } else if (parms[i] != Object.class) {
-
-                            parmString = OgnlRuntime.getCompiler().createLocalReference(context,
-                                    "(" + parms[i].getName() + ")ognl.OgnlOps.convertValue(" + parmString + "," + parms[i].getName() + ".class)",
-                                    parms[i]
-                            );
+                        } else if (parms[i] != Object.class)
+                        {
+                            parmString = OgnlRuntime.getCompiler()
+                                    .createLocalReference(context,
+                                                          "(" + parms[i].getName() + ")ognl.OgnlOps.convertValue(" + parmString + "," + parms[i].getName() + ".class)",
+                                                          parms[i]
+                                    );
                         } else if ((NodeType.class.isInstance(_children[i])
-                                && ((NodeType)_children[i]).getGetterClass() != null
-                                && Number.class.isAssignableFrom(((NodeType)_children[i]).getGetterClass()))
-                                || valueClass.isPrimitive()) {
-
+                                    && ((NodeType)_children[i]).getGetterClass() != null
+                                    && Number.class.isAssignableFrom(((NodeType)_children[i]).getGetterClass()))
+                                   || valueClass.isPrimitive())
+                        {
                             parmString = " ($w) " + parmString;
-                        } else if (valueClass.isPrimitive()) {
+                        } else if (valueClass.isPrimitive())
+                        {
                             parmString = "($w) " + parmString;
                         }
                     }
 
             result += ")";
 
-            try {
-
+            try
+            {
                 Object contextObj = getValueBody(context, target);
                 context.setCurrentObject(contextObj);
-
-            } catch (Throwable t) {
+            } catch (Throwable t)
+            {
                 // ignore
             }
 

src/java/ognl/MethodAccessor.java

  * This interface defines methods for calling methods in a target object.
  * Methods are broken up into static and instance methods for convenience.
  * indexes into the target object, which must be an array.
- * 
+ *
  * @author Luke Blanshard (blanshlu@netscape.net)
  * @author Drew Davidson (drew@ognl.org)
  */

src/java/ognl/OgnlRuntime.java

         if (m == null)
             m = getReadMethod((target == null) ? null : target.getClass(), propertyName, 0);
 
-        if (checkAccessAndExistence) {
-            if ((m == null) || !context.getMemberAccess().isAccessible(context, target, m, propertyName)) {
+        if (checkAccessAndExistence)
+        {
+            if ((m == null) || !context.getMemberAccess().isAccessible(context, target, m, propertyName))
+            {
                 result = NotFound;
             }
         }
         return result;
     }
 
-    public static boolean isMethodAccessible(OgnlContext context, Object target, Method method,
-                                             String propertyName)
+    public static boolean isMethodAccessible(OgnlContext context, Object target, Method method, String propertyName)
     {
         return (method != null) && context.getMemberAccess().isAccessible(context, target, method, propertyName);
     }
                                    Node[] children, boolean includeStatic)
             throws Exception
     {
-        Class[] parms = null;
+        Class[] parms;
         if (children != null && children.length > 0)
         {
             parms = new Class[children.length];

src/java/ognl/enhance/ExpressionCompiler.java

 
         // must evaluate expression value at least once if object isn't null
 
-        if (root != null)
-            Ognl.getValue(expression, context, root);
+        //if (root != null)
+            //Ognl.getValue(expression, context, root);
 
         CtClass nodeClass = getCtClass(Node.class);
         CtMethod setExpression = null;
 
         body = body.replaceAll("\\.\\.", ".");
 
-//        System.out.println("Getter Body: ===================================\n" + body);
+        //System.out.println("Getter Body: ===================================\n" + body);
         valueGetter.setBody(body);
         newClass.addMethod(valueGetter);
 

src/test/java/org/ognl/test/InterfaceInheritanceTest.java

             {ROOT, "myMap[null] = 25", new Integer(25)},
             {ROOT, "myMap[null]", new Integer(25), new Integer(50), new Integer(50)},
             {ROOT, "beans.testBean", ROOT.getBeans().getBean("testBean")},
-            {ROOT, "beans.evenOdd.next", ((EvenOdd) ROOT.getBeans().getBean("evenOdd")).getNext()},
+            {ROOT, "beans.evenOdd.next", "even"},
             {ROOT, "map.comp.form.clientId", "form1"},
             {ROOT, "map.comp.getCount(genericIndex)", Integer.valueOf(0)},
             {ROOT, "map.customList.total", Integer.valueOf(1)},

src/test/java/org/ognl/test/MemberAccessTest.java

 public class MemberAccessTest extends OgnlTestCase {
 
     private static Simple ROOT = new Simple();
-    
+
     private static Object[][] TESTS = {
             {"@Runtime@getRuntime()", OgnlException.class},
             {"@System@getProperty('java.specification.version')", System.getProperty("java.specification.version")},
     {
         TestSuite result = new TestSuite();
 
-        for (int i = 0; i < TESTS.length; i++) {
+        for (int i = 0; i < TESTS.length; i++)
+        {
             result.addTest(new MemberAccessTest((String) TESTS[i][0] + " (" + TESTS[i][1] + ")", ROOT,
-                    (String) TESTS[i][0], TESTS[i][1]));
+                                                (String) TESTS[i][0], TESTS[i][1]));
         }
+        
         return result;
     }
 
     public void setUp()
     {
         super.setUp();
+        
         /* Should allow access at all to the Simple class except for the bigIntValue property */
         _context.setMemberAccess(new DefaultMemberAccess(false) {
 

src/test/java/org/ognl/test/OgnlTestCase.java

     public SimpleNode getExpression()
             throws Exception
     {
-        if (_expression == null) {
+        if (_expression == null)
+        {
             _expression = (SimpleNode) Ognl.parseExpression(_expressionString);
         }
 
-        if (_compileExpressions) {
+        if (_compileExpressions)
+        {
             _expression = (SimpleNode) Ognl.compileExpression(_context, _root, _expressionString);
         }
 
 
             assertEquals(_expectedResult, Ognl.getValue(expr, _context, _root));
 
-            if (hasSetValue) {
-
+            if (hasSetValue)
+            {
                 testedResult = hasExpectedAfterSetResult ? expectedAfterSetResult : setValue;
                 Ognl.setValue(expr, _context, _root, setValue);
 
             }
 
         } catch (Exception ex) {
+            System.out.println("Caught exception " + ex);
             if (NullPointerException.class.isInstance(ex))
                 ex.printStackTrace();
 
                 && Exception.class.isAssignableFrom(((RuntimeException) ex).getCause().getClass()))
                 ex = (Exception) ((RuntimeException) ex).getCause();
 
-            if (testedResult instanceof Class) {
+            if (testedResult instanceof Class)
+            {
                 assertTrue(Exception.class.isAssignableFrom((Class) testedResult));
             } else
                 throw ex;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.