コード例 #1
0
        /**
         *      Returns OgnlRuntime.NotFound if the property does not exist.
         */
        public object getPossibleProperty(IDictionary context, object target, string name)          // throws OgnlException
        {
            object      result;
            OgnlContext ognlContext = (OgnlContext)context;

            if (context [("_compile")] != null)
            {
                Getter getter = getGetter(ognlContext, target, name);

                if (getter != NotFoundGetter)
                {
                    result = getter.get(ognlContext, target, name);
                }
                else
                {
                    try
                    {
                        result = OgnlRuntime.getFieldValue(ognlContext, target, name, true);
                    }
                    catch (Exception ex)
                    {
                        throw new OgnlException(name, ex);
                    }
                }
            }
            else
            {
                result = base.getPossibleProperty(context, target, name);
            }
            return(result);
        }
コード例 #2
0
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            IDictionary answer;

            if (className == null)
            {
                try {
                    answer = (IDictionary)DEFAULT_MAP_CLASS.GetConstructor(new Type[0]).Invoke(new object[0]);
                } catch (Exception ex) {
                    /* This should never happen */
                    throw new OgnlException("Default IDictionary class '" + DEFAULT_MAP_CLASS.Name + "' instantiation error", ex);
                }
            }
            else
            {
                try {
                    answer = (IDictionary)OgnlRuntime.classForName(context, className).GetConstructor(new Type[0]).Invoke(new object[0]);
                } catch (Exception ex) {
                    throw new OgnlException("IDictionary implementor '" + className + "' not found", ex);
                }
            }

            for (int i = 0; i < jjtGetNumChildren(); ++i)
            {
                ASTKeyValue kv = (ASTKeyValue)children[i];
                Node        k  = kv.getKey(),
                            v  = kv.getValue();

                answer.Add(k.getValue(context, source), (v == null) ? null : v.getValue(context, source));
            }
            return(answer);
        }
コード例 #3
0
 public object getProperty(IDictionary context, object target, object name)             // throws OgnlException
 {
     if ("x".Equals(name) || "y".Equals(name))
     {
         return(OgnlRuntime.getProperty((OgnlContext)context, target, name));
     }
     return(null);
 }
コード例 #4
0
ファイル: ASTStaticField.cs プロジェクト: KidFashion/OGNL.Net
        public override bool isNodeConstant(OgnlContext context) // throws OgnlException
        {
            bool      result = false;
            Exception reason = null;

            try {
                Type c = OgnlRuntime.classForName(context, className);

                /*
                 *  Check for virtual static field "class"; this cannot interfere with
                 *  normal static fields because it is a reserved word.  It is considered
                 *  constant.
                 */
                if (fieldName.Equals("class"))
                {
                    result = true;
                }
                else
                {
                    FieldInfo f = c.GetField(fieldName);
                    if (f == null)
                    {
                        // try to load Property
                        PropertyInfo p = c.GetProperty(fieldName);
                        if (p == null)
                        {
                            throw new MissingFieldException("Field or Property " + fieldName + " of class " + className + " is not found.");
                        }
                        else
                        if (!p.GetAccessors() [0].IsStatic)
                        {
                            throw new MissingFieldException("Property " + fieldName + " of class " + className + " is not static.");
                        }
                        // Property can't be constant.
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    if (!f.IsStatic)
                    {
                        throw new OgnlException("Field " + fieldName + " of class " + className + " is not static");
                    }

                    result = f.IsLiteral;
                }
            }   catch (TypeLoadException e)    { reason = e; }
            catch (MissingFieldException e)      { reason = e; }
            catch (SecurityException e)         { reason = e; }

            if (reason != null)
            {
                throw new OgnlException("Could not get static field " + fieldName + " from class " + className, reason);
            }
            return(result);
        }
コード例 #5
0
 public bool hasGetProperty(OgnlContext context, object target, object oname)          // throws OgnlException
 {
     try
     {
         return(OgnlRuntime.hasGetProperty(context, target, oname));
     }
     catch (/*Introspection*/ Exception ex)
     {
         throw new OgnlException("checking if " + target + " has gettable property " + oname, ex);
     }
 }
コード例 #6
0
ファイル: ASTProject.cs プロジェクト: KidFashion/OGNL.Net
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            Node              expr             = children[0];
            IList             answer           = new ArrayList();
            IElementsAccessor elementsAccessor = OgnlRuntime.getElementsAccessor(OgnlRuntime.getTargetClass(source));

            for (IEnumerator e = elementsAccessor.getElements(source); e.MoveNext();)
            {
                answer.Add(expr.getValue(context, e.Current));
            }
            return(answer);
        }
コード例 #7
0
        /**
         *  Returns true if this property is described by an IndexedPropertyDescriptor
         *  and that if followed by an index specifier it will call the index get/set
         *  methods rather than go through property accessors.
         */
        public int getIndexedPropertyType(OgnlContext context, object source) // throws OgnlException
        {
            if (!isIndexedAccess())
            {
                object property = getProperty(context, source);

                if (property is string)
                {
                    return(OgnlRuntime.getIndexedPropertyType(context, (source == null) ? null : source.GetType(), (string)property));
                }
            }
            return(OgnlRuntime.INDEXED_PROPERTY_NONE);
        }
コード例 #8
0
        public object callMethod(IDictionary context, object target, string methodName, object[] args) // throws MethodFailedException
        {
            Type   targetClass = (target == null) ? null : target.GetType();
            object source      = target;
            IList  methods     = OgnlRuntime.getMethods(targetClass, methodName, false);

            if ((methods == null) || (methods.Count == 0))
            {
                methods = OgnlRuntime.getMethods(targetClass, methodName, true);
                source  = targetClass;
            }
            return(OgnlRuntime.callAppropriateMethod((OgnlContext)context, target, target, methodName, null, methods, args));
        }
コード例 #9
0
        protected override void setValueBody(OgnlContext context, object target, object value) // throws OgnlException
        {
            if (indexedAccess && children [0] is ASTSequence)
            {
                // As property [index1, index2]...
                // Use Indexer.
                object [] indexParameters = ((ASTSequence)children [0]).getValues(context, context.getRoot());

                /*IndexerAccessor.setIndexerValue (target, value ,indexParameters) ;*/
                OgnlRuntime.setIndxerValue(context, target, "Indexer", value, indexParameters);
                return;
            }

            OgnlRuntime.setProperty(context, target, getProperty(context, target), value);
        }
コード例 #10
0
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            object[] args = OgnlRuntime.getObjectArrayPool().create(jjtGetNumChildren());
            object   root = context.getRoot();

            try {
                for (int i = 0, icount = args.Length; i < icount; ++i)
                {
                    args[i] = children[i].getValue(context, root);
                }

                return(OgnlRuntime.callStaticMethod(context, className, methodName, args));
            } finally {
                OgnlRuntime.getObjectArrayPool().recycle(args);
            }
        }
コード例 #11
0
 public void testPropertyDescriptorReflection()         // throws Exception
 {
     OgnlRuntime.getPropertyDescriptor(typeof(CollectionBase), "");
     // OgnlRuntime.getPropertyDescriptor(typeof (java.util.AbstractSequentialList), "");
     OgnlRuntime.getPropertyDescriptor(typeof(System.Array), "");
     OgnlRuntime.getPropertyDescriptor(typeof(ArrayList), "");
     // OgnlRuntime.getPropertyDescriptor(typeof (java.util.BitSet), "");
     OgnlRuntime.getPropertyDescriptor(typeof(System.DateTime), "");
     OgnlRuntime.getPropertyDescriptor(typeof(FieldInfo), "");
     // OgnlRuntime.getPropertyDescriptor(typeof (java.util.LinkedList), "");
     OgnlRuntime.getPropertyDescriptor(typeof(IList), "");
     OgnlRuntime.getPropertyDescriptor(typeof(IEnumerator), "");
     // OgnlRuntime.getPropertyDescriptor(typeof (java.lang.ThreadLocal), "");
     OgnlRuntime.getPropertyDescriptor(typeof(Uri), "");
     OgnlRuntime.getPropertyDescriptor(typeof(Stack), "");
 }
コード例 #12
0
        public virtual object getProperty(IDictionary context, object target, object oname)          // throws OgnlException
        {
            object result      = OgnlRuntime.NotFound;
            Node   currentNode = ((OgnlContext)context).getCurrentNode();

            bool indexedAccess = false;

            if (currentNode == null)
            {
                throw new OgnlException("node is null for '" + oname + "'");
            }
            if (!(currentNode is ASTProperty))
            {
                currentNode = currentNode.jjtGetParent();
            }
            if (currentNode is ASTProperty)
            {
                indexedAccess = ((ASTProperty)currentNode).isIndexedAccess();
            }
            if (((oname is string) && isPropertyName((string)oname)) &&
                (!indexedAccess ||
                 !OgnlRuntime.hasSetIndexer((OgnlContext)context, target, target.GetType(), 1)))
            {
                result = javaGetProperty(oname, context, target);
            }
            else
            {
                // will use index Access property

                /*
                 * PropertyInfo indexer = IndexerAccessor.getIndexer (target, new object [] {oname}) ;
                 * if (indexer != null)
                 * result = indexer.GetValue (target , new object[] {oname}) ;
                 * else
                 * // throw new NoSuchPropertyException(target, oname.ToString ()) ;
                 * // TODO: support old java style property: [propertyName].
                 * result = javaGetProperty (oname, context, target) ;
                 */
                result = OgnlRuntime.getIndxerValue((OgnlContext)context, target, oname, new object[] { oname });
            }


            return(result);
        }
コード例 #13
0
        public static bool operin(object v1, object v2) // throws OgnlException
        {
            if (v2 == null)                             // A null collection is always treated as empty
            {
                return(false);
            }

            IElementsAccessor elementsAccessor = OgnlRuntime.getElementsAccessor(OgnlRuntime.getTargetClass(v2));

            for (IEnumerator e = elementsAccessor.getElements(v2); e.MoveNext();)
            {
                object o = e.Current;

                if (equal(v1, o))
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #14
0
        ///<summary>
        /// Returns OgnlRuntime.NotFound if the property does not exist.
        ///</summary>
        public object setPossibleProperty(IDictionary context, object target, string name, object value)          // throws OgnlException
        {
            object      result      = null;
            OgnlContext ognlContext = (OgnlContext)context;

            try
            {
                if (!OgnlRuntime.setMethodValue(ognlContext, target, name, value, true))
                {
                    result = OgnlRuntime.setFieldValue(ognlContext, target, name, value) ? null : OgnlRuntime.NotFound;
                }
            }
            catch (OgnlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new OgnlException(name, ex);
            }
            return(result);
        }
コード例 #15
0
        public virtual void setProperty(IDictionary context, object target, object oname, object value)          // throws OgnlException
        {
            object result      = OgnlRuntime.NotFound;
            string name        = oname.ToString();
            Node   currentNode = ((OgnlContext)context).getCurrentNode();

            bool indexedAccess = false;

            if (currentNode == null)
            {
                throw new OgnlException("node is null for '" + oname + "'");
            }
            if (!(currentNode is ASTProperty))
            {
                currentNode = currentNode.jjtGetParent();
            }
            if (currentNode is ASTProperty)
            {
                indexedAccess = ((ASTProperty)currentNode).isIndexedAccess();
            }
            if (!indexedAccess || !OgnlRuntime.hasSetIndexer((OgnlContext)context, target, target.GetType(), 1))
            {
                if ((result = setPossibleProperty(context, target, name, value)) == OgnlRuntime.NotFound)
                {
                    throw new NoSuchPropertyException(target, name);
                }
            }
            else
            {
                /*
                 * PropertyInfo indexer = IndexerAccessor.getIndexer (target, new object[] {oname}) ;
                 * if (indexer == null)
                 * throw new NoSuchPropertyException(target, name) ;
                 *
                 * indexer.SetValue (target , value , new object[] {oname}) ;
                 */
                OgnlRuntime.setIndxerValue((OgnlContext)context, target, oname, value, new object[] { oname });
            }
        }
コード例 #16
0
ファイル: SimpleNode.cs プロジェクト: KidFashion/OGNL.Net
        public object getValue(OgnlContext context, object source)
        {
            if (context.getTraceEvaluations())
            {
                EvaluationPool pool          = OgnlRuntime.getEvaluationPool();
                object         result        = null;
                Exception      evalException = null;
                Evaluation     evaluation    = pool.create(this, source);

                context.pushEvaluation(evaluation);
                try {
                    result = evaluateGetValueBody(context, source);
                } catch (OgnlException ex) {
                    evalException = ex;
                    throw ex;
                } catch (Exception ex) {
                    evalException = ex;
                    throw ex;
                } finally {
                    Evaluation eval = context.popEvaluation();

                    eval.setResult(result);
                    if (evalException != null)
                    {
                        eval.setException(evalException);
                    }
                    if ((evalException == null) && (context.getRootEvaluation() == null) && !context.getKeepLastEvaluation())
                    {
                        pool.recycleAll(eval);
                    }
                }
                return(result);
            }
            else
            {
                return(evaluateGetValueBody(context, source));
            }
        }
コード例 #17
0
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            if (indexedAccess && children [0] is ASTSequence)
            {
                // As property [index1, index2]...
                // Use Indexer.
                object [] indexParameters = ((ASTSequence)children [0]).getValues(context, context.getRoot());

                /* return IndexerAccessor.getIndexerValue (source, indexParameters) ; */
                return(OgnlRuntime.getIndxerValue(context, source, "Indexer", indexParameters));
            }
            object result;

            object property = getProperty(context, source);
            Node   indexSibling;

            result = OgnlRuntime.getProperty(context, source, property);
            if (result == null)
            {
                result = OgnlRuntime.getNullHandler(OgnlRuntime.getTargetClass(source)).nullPropertyValue(context, source, property);
            }
            return(result);
        }
コード例 #18
0
ファイル: ASTMethod.cs プロジェクト: KidFashion/OGNL.Net
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            object[] args = OgnlRuntime.getObjectArrayPool().create(jjtGetNumChildren());

            try {
                object result,
                       root = context.getRoot();

                for (int i = 0, icount = args.Length; i < icount; ++i)
                {
                    args[i] = children[i].getValue(context, root);
                }
                result = OgnlRuntime.callMethod(context, source, methodName, null, args);
                if (result == null)
                {
                    NullHandler nh = OgnlRuntime.getNullHandler(OgnlRuntime.getTargetClass(source));

                    result = nh.nullMethodResult(context, source, methodName, args);
                }
                return(result);
            } finally {
                OgnlRuntime.getObjectArrayPool().recycle(args);
            }
        }
コード例 #19
0
ファイル: SimpleNode.cs プロジェクト: KidFashion/OGNL.Net
        public void setValue(OgnlContext context, object target, object value)
        {
            if (context.getTraceEvaluations())
            {
                EvaluationPool pool          = OgnlRuntime.getEvaluationPool();
                Exception      evalException = null;
                Evaluation     evaluation    = pool.create(this, target, true);

                context.pushEvaluation(evaluation);
                try {
                    evaluateSetValueBody(context, target, value);
                } catch (OgnlException ex) {
                    evalException = ex;
                    ex.setEvaluation(evaluation);
                    throw ex;
                } catch (Exception ex) {
                    evalException = ex;
                    throw ex;
                } finally {
                    Evaluation eval = context.popEvaluation();

                    if (evalException != null)
                    {
                        eval.setException(evalException);
                    }
                    if ((evalException == null) && (context.getRootEvaluation() == null) && !context.getKeepLastEvaluation())
                    {
                        pool.recycleAll(eval);
                    }
                }
            }
            else
            {
                evaluateSetValueBody(context, target, value);
            }
        }
コード例 #20
0
        /* MethodAccessor interface */
        public object callStaticMethod(IDictionary context, Type targetClass, string methodName, object[] args) // throws MethodFailedException
        {
            IList methods = OgnlRuntime.getMethods(targetClass, methodName, true);

            return(OgnlRuntime.callAppropriateMethod((OgnlContext)context, targetClass, null, methodName, null, methods, args));
        }
コード例 #21
0
ファイル: ASTStaticField.cs プロジェクト: KidFashion/OGNL.Net
 protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
 {
     return(OgnlRuntime.getStaticField(context, className, fieldName));
 }
コード例 #22
0
 public override void setUp()
 {
     base.setUp();
     OgnlRuntime.setPropertyAccessor(typeof(Blah), new BlahPropertyAccessor());
 }
コード例 #23
0
 ///<summary>
 /// This method can be called when the last evaluation has been used
 /// and can be returned for reuse in the free pool maintained by the
 /// runtime.  This is not a necessary step, but is useful for keeping
 /// memory usage down.  This will recycle the last evaluation and then
 /// set the last evaluation to null.
 ///</summary>
 public void recycleLastEvaluation()
 {
     OgnlRuntime.getEvaluationPool().recycleAll(lastEvaluation);
     lastEvaluation = null;
 }
コード例 #24
0
        /// <summary>
        /// Returns the value converted numerically to the given class type
        /// </summary>
        /// <remarks>
        /// This method also detects when arrays are being converted and
        /// converts the components of one array to the type of the other.
        /// </remarks>
        /// <param name="value">an object to be converted to the given type</param>
        ///<param name="toType">class type to be converted to</param>
        /// <returns>converted value of the type given, or value if the value
        ///                cannot be converted to the given type.</returns>
        ///
        public static object convertValue(object value, Type toType)
        {
            object result = null;

            if (value != null)
            {
                /* If array -> array then convert components of array individually */
                if (value.GetType().IsArray&& toType.IsArray)
                {
                    Type componentType = toType.GetElementType();

                    result = Array.CreateInstance(componentType, ((Array)value).Length);
                    for (int i = 0, icount = ((Array)value).Length; i < icount; i++)
                    {
                        ((Array)result).SetValue(convertValue(((Array)value).GetValue(i), componentType), i);
                    }
                }
                else
                {
                    if ((toType == typeof(int)) || (toType == typeof(uint)))
                    {
                        result = (int)longValue(value);
                    }
                    if ((toType == typeof(Double)) || (toType == typeof(double)))
                    {
                        result = doubleValue(value);
                    }
                    if ((toType == typeof(Boolean) || (toType == typeof(bool))))
                    {
                        result = booleanValue(value);                                                                                  // ? Boolean.TRUE : Boolean.FALSE;
                    }
                    if ((toType == typeof(Byte) || (toType == typeof(byte))))
                    {
                        result = (byte)longValue(value);
                    }
                    if ((toType == typeof(char)) || (toType == typeof(char)))
                    {
                        result = ((char)longValue(value));
                    }
                    if ((toType == typeof(short)) || (toType == typeof(ushort)))
                    {
                        result = (short)longValue(value);
                    }
                    if ((toType == typeof(long)) || (toType == typeof(ulong)))
                    {
                        result = (longValue(value));
                    }
                    if ((toType == typeof(float)) || (toType == typeof(Single)))
                    {
                        result = (float)(doubleValue(value));
                    }
                    // ignore if ( toType == BigInteger.class )                                       result = bigIntValue(value);
                    if (toType == typeof(decimal))
                    {
                        result = bigDecValue(value);
                    }
                    if (toType == typeof(string))
                    {
                        result = stringValue(value);
                    }
                    if (toType.IsEnum)
                    {
                        result = enumValue(value, toType);
                    }
                }
            }
            else
            {
                if (toType.IsPrimitive)
                {
                    result = OgnlRuntime.getPrimitiveDefaultValue(toType);
                }
                else
                // support Enum
                if (toType.IsEnum)
                {
                    result = Enum.GetValues(toType).GetValue(0);
                }
            }
            return(result);
        }
コード例 #25
0
        private Getter getGetter(OgnlContext context, object target, string propertyName)         // throws OgnlException
        {
            Getter      result;
            Type        targetClass = target.GetType();
            IDictionary propertyMap;

            if ((propertyMap = (IDictionary)seenGetMethods.get(targetClass)) == null)
            {
                propertyMap = new HashMap(101);
                seenGetMethods.put(targetClass, propertyMap);
            }
            if ((result = (Getter)propertyMap.get(propertyName)) == null)
            {
                try
                {
                    MethodInfo method = OgnlRuntime.getGetMethod(context, targetClass, propertyName);

                    if (method != null)
                    {
                        if (method.IsPublic)
                        {
                            if (method.ReturnType.IsPrimitive)
                            {
                                propertyMap.Add(propertyName, result = generateGetter(context,
                                                                                      "java.lang.object\t\tresult;\n" +
                                                                                      targetClass.Name + "\t" + "t0 = (" + targetClass.Name + ")$2;\n" +
                                                                                      "\n" +
                                                                                      "try {\n" +
                                                                                      "   result = new " + getPrimitiveWrapperClass(method.ReturnType).Name + "(t0." + method.Name + "());\n" +
                                                                                      "} catch (java.lang.Exception ex) {\n" +
                                                                                      "    throw new java.lang.RuntimeException(ex);\n" +
                                                                                      "}\n" +
                                                                                      "return result;"
                                                                                      ));
                            }
                            else
                            {
                                propertyMap.Add(propertyName, result = generateGetter(context,
                                                                                      "java.lang.object\t\tresult;\n" +
                                                                                      targetClass.Name + "\t" + "t0 = (" + targetClass.Name + ")$2;\n" +
                                                                                      "\n" +
                                                                                      "try {\n" +
                                                                                      "   result = t0." + method.Name + "();\n" +
                                                                                      "} catch (java.lang.Exception ex) {\n" +
                                                                                      "    throw new java.lang.RuntimeException(ex);\n" +
                                                                                      "}\n" +
                                                                                      "return result;"
                                                                                      ));
                            }
                        }
                        else
                        {
                            propertyMap.Add(propertyName, result = DefaultGetter);
                        }
                    }
                    else
                    {
                        propertyMap.Add(propertyName, result = NotFoundGetter);
                    }
                }
                catch (Exception ex)
                {
                    throw new OgnlException("getting getter", ex);
                }
            }
            return(result);
        }
コード例 #26
0
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            object result = source;

            for (int i = 0, ilast = children.Length - 1; i <= ilast; ++i)
            {
                bool handled = false;

                if (i < ilast)
                {
                    if (children[i] is ASTProperty)
                    {
                        ASTProperty propertyNode = (ASTProperty)children[i];
                        int         indexType    = propertyNode.getIndexedPropertyType(context, result);

                        if ((indexType != OgnlRuntime.INDEXED_PROPERTY_NONE) && (children[i + 1] is ASTProperty))
                        {
                            ASTProperty indexNode = (ASTProperty)children[i + 1];

                            if (indexNode.isIndexedAccess())
                            {
                                object index = indexNode.getProperty(context, result);

                                if (index is DynamicSubscript)
                                {
                                    if (indexType == OgnlRuntime.INDEXED_PROPERTY_INT)
                                    {
                                        object array = propertyNode.getValue(context, result);
                                        int    len   = ((Array)array).Length;

                                        switch (((DynamicSubscript)index).getFlag())
                                        {
                                        case DynamicSubscript.ALL:
                                            result = Array.CreateInstance(array.GetType().GetElementType(), len);
                                            Array.Copy((Array)array, 0, (Array)result, 0, len);
                                            handled = true;
                                            i++;
                                            break;

                                        case DynamicSubscript.FIRST:
                                            index = ((len > 0) ? 0 : -1);
                                            break;

                                        case DynamicSubscript.MID:
                                            index = ((len > 0) ? (len / 2) : -1);
                                            break;

                                        case DynamicSubscript.LAST:
                                            index = ((len > 0) ? (len - 1) : -1);
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        if (indexType == OgnlRuntime.INDEXED_PROPERTY_OBJECT)
                                        {
                                            throw new OgnlException("DynamicSubscript '" + indexNode + "' not allowed for object indexed property '" + propertyNode + "'");
                                        }
                                    }
                                }
                                if (!handled)
                                {
                                    result  = OgnlRuntime.getIndexedProperty(context, result, propertyNode.getProperty(context, result).ToString(), index);
                                    handled = true;
                                    i++;
                                }
                            }
                        }
                    }
                }
                if (!handled)
                {
                    result = children[i].getValue(context, result);
                }
            }
            return(result);
        }
コード例 #27
0
        protected override void setValueBody(OgnlContext context, object target, object value) // throws OgnlException
        {
            bool handled = false;

            for (int i = 0, ilast = children.Length - 2; i <= ilast; ++i)
            {
                if (i == ilast)
                {
                    if (children[i] is ASTProperty)
                    {
                        ASTProperty propertyNode = (ASTProperty)children[i];
                        int         indexType    = propertyNode.getIndexedPropertyType(context, target);

                        if ((indexType != OgnlRuntime.INDEXED_PROPERTY_NONE) && (children[i + 1] is ASTProperty))
                        {
                            ASTProperty indexNode = (ASTProperty)children[i + 1];

                            if (indexNode.isIndexedAccess())
                            {
                                object index = indexNode.getProperty(context, target);

                                if (index is DynamicSubscript)
                                {
                                    if (indexType == OgnlRuntime.INDEXED_PROPERTY_INT)
                                    {
                                        object array = propertyNode.getValue(context, target);
                                        int    len   = ((Array)array).Length;

                                        switch (((DynamicSubscript)index).getFlag())
                                        {
                                        case DynamicSubscript.ALL:
                                            Array.Copy((Array)target, 0, (Array)value, 0, len);
                                            handled = true;
                                            i++;
                                            break;

                                        case DynamicSubscript.FIRST:
                                            index = ((len > 0) ? 0 : -1);
                                            break;

                                        case DynamicSubscript.MID:
                                            index = ((len > 0) ? (len / 2) : -1);
                                            break;

                                        case DynamicSubscript.LAST:
                                            index = ((len > 0) ? (len - 1) : -1);
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        if (indexType == OgnlRuntime.INDEXED_PROPERTY_OBJECT)
                                        {
                                            throw new OgnlException("DynamicSubscript '" + indexNode + "' not allowed for object indexed property '" + propertyNode + "'");
                                        }
                                    }
                                }
                                if (!handled)
                                {
                                    OgnlRuntime.setIndexedProperty(context, target, propertyNode.getProperty(context, target).ToString(), index, value);
                                    handled = true;
                                    i++;
                                }
                            }
                        }
                    }
                }
                if (!handled)
                {
                    target = children[i].getValue(context, target);
                }
            }
            if (!handled)
            {
                children[children.Length - 1].setValue(context, target, value);
            }
        }
コード例 #28
0
 public override void setUp()
 {
     base.setUp();
     OgnlRuntime.setNullHandler(typeof(CorrectedObject), new CorrectedObjectNullHandler("corrected"));
 }
コード例 #29
0
        /*===================================================================
        *       Public static methods
        *  ===================================================================*/
        public static void main(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].Equals("-time"))
                {
                    TIME_MODE = true;
                    MAX_TIME  = Util.ParseLong(args[++i]);
                }
                else if (args[i].Equals("-iterations"))
                {
                    ITERATIONS_MODE = true;
                    MAX_ITERATIONS  = (int)Util.ParseLong(args[++i]);
                }
            }
            if (!TIME_MODE && !ITERATIONS_MODE)
            {
                TIME_MODE = true;
                MAX_TIME  = 1000;
            }
            OgnlRuntime.setPropertyAccessor(typeof(object), new CompilingPropertyAccessor());
            try
            {
                Performance[] tests = new Performance[]
                {
                    new Performance("Constant",
                                    "100 + 20 * 5",
                                    "testConstantExpression"),
                    new Performance("Single Property",
                                    "bean2",
                                    "testSinglePropertyExpression"),
                    new Performance("Property Navigation",
                                    "bean2.bean3.value",
                                    "testPropertyNavigationExpression"),
                    new Performance("Property Navigation and Comparison",
                                    "bean2.bean3.value <= 24",
                                    "testPropertyNavigationAndComparisonExpression"),
                    new Performance("Property Navigation with Indexed Access",
                                    "bean2.bean3.indexedValue[25]",
                                    "testIndexedPropertyNavigationExpression"),
                    new Performance("Property Navigation with Map Access",
                                    "bean2.bean3.map['foo']",
                                    "testPropertyNavigationWithMapExpression"),
                };

                for (int i = 0; i < tests.Length; i++)
                {
                    Performance perf = tests[i];

                    try
                    {
                        Results javaResults        = perf.testJava(),
                                interpretedResults = perf.testExpression(false),
                                compiledResults    = perf.testExpression(true);

                        Console.WriteLine(perf.getName() + ": " + perf.getExpression().ToString());
                        Console.WriteLine("       java: " + javaResults.iterations + " iterations in " + javaResults.time + " ms");
                        Console.WriteLine("   compiled: " + compiledResults.iterations + " iterations in " + compiledResults.time + " ms (" + (compiledResults.getFactor(javaResults).ToString("0.0")) + " times slower than java)");
                        Console.WriteLine("interpreted: " + interpretedResults.iterations + " iterations in " + interpretedResults.time + " ms (" + /*FACTOR_FORMAT.format*/ (interpretedResults.getFactor(javaResults).ToString("0.0")) + " times slower than java)");
                        Console.WriteLine();
                    }
                    catch (OgnlException ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
コード例 #30
0
ファイル: ASTCtor.cs プロジェクト: KidFashion/OGNL.Net
        protected override object getValueBody(OgnlContext context, object source) // throws OgnlException
        {
            object result,
                   root = context.getRoot();
            int count   = jjtGetNumChildren();

            object[] args = OgnlRuntime.getObjectArrayPool().create(count);

            try {
                for (int i = 0; i < count; ++i)
                {
                    args[i] = children[i].getValue(context, root);
                }
                if (isArray)
                {
                    if (args.Length == 1)
                    {
                        try {
                            Type  componentClass = OgnlRuntime.classForName(context, className);
                            IList sourceList     = null;
                            int   size;

                            if (args[0] is IList)
                            {
                                sourceList = (IList)args[0];
                                size       = sourceList.Count;
                            }
                            else
                            {
                                size = (int)OgnlOps.longValue(args[0]);
                            }
                            result = Array.CreateInstance(componentClass, size);
                            if (sourceList != null)
                            {
                                TypeConverter converter = context.getTypeConverter();

                                for (int i = 0, icount = sourceList.Count; i < icount; i++)
                                {
                                    object o = sourceList [i];

                                    if ((o == null) || componentClass.IsInstanceOfType(o))
                                    {
                                        ((Array)result).SetValue(o, i);
                                    }
                                    else
                                    {
                                        ((Array)result).SetValue(converter.convertValue(context, null, null, null, o, componentClass), i);
                                    }
                                }
                            }
                        } catch (TypeLoadException ex) {
                            throw new OgnlException("array component class '" + className + "' not found", ex);
                        }
                    }
                    else
                    {
                        throw new OgnlException("only expect array size or fixed initializer list");
                    }
                }
                else
                {
                    result = OgnlRuntime.callConstructor(context, className, args);
                }

                return(result);
            } finally {
                OgnlRuntime.getObjectArrayPool().recycle(args);
            }
        }