Ejemplo n.º 1
0
        public void SetUp()
        {
            arrayNodes    = new ExprArrayNode[4];
            arrayNodes[0] = new ExprArrayNode();

            // no coercion array
            arrayNodes[1] = new ExprArrayNode();
            arrayNodes[1].AddChildNode(new SupportExprNode(2));
            arrayNodes[1].AddChildNode(new SupportExprNode(3));

            // coercion
            arrayNodes[2] = new ExprArrayNode();
            arrayNodes[2].AddChildNode(new SupportExprNode(1.5D));
            arrayNodes[2].AddChildNode(new SupportExprNode(1));

            // mixed types
            arrayNodes[3] = new ExprArrayNode();
            arrayNodes[3].AddChildNode(new SupportExprNode("a"));
            arrayNodes[3].AddChildNode(new SupportExprNode(1));

            for (var i = 0; i < arrayNodes.Length; i++)
            {
                arrayNodes[i].Validate(SupportExprValidationContextFactory.MakeEmpty(container));
            }
        }
Ejemplo n.º 2
0
 public ExprArrayNodeForge(
     ExprArrayNode parent,
     Type arrayReturnType,
     object[] constantResult)
 {
     _constantResult = constantResult;
     Parent = parent;
     ArrayReturnType = arrayReturnType;
     IsMustCoerce = false;
     Coercer = null;
 }
Ejemplo n.º 3
0
 public ExprArrayNodeForge(
     ExprArrayNode parent,
     Type arrayReturnType,
     bool mustCoerce,
     Coercer coercer,
     object constantResult)
 {
     _constantResult = (Array) constantResult;
     Parent = parent;
     ArrayReturnType = arrayReturnType;
     IsMustCoerce = mustCoerce;
     Coercer = coercer;
 }
Ejemplo n.º 4
0
        public override ExprNode Validate(ExprValidationContext validationContext)
        {
            // Resolve target class
            Type targetClass = null;
            if (_numArrayDimensions != 0) {
                targetClass = TypeHelper.GetPrimitiveTypeForName(_classIdent);
            }

            if (targetClass == null) {
                try {
                    targetClass = validationContext.ImportService
                        .ResolveClass(_classIdent, false, validationContext.ClassProvidedExtension);
                }
                catch (ImportException) {
                    throw new ExprValidationException("Failed to resolve new-operator class name '" + _classIdent + "'");
                }
            }

            // handle non-array
            if (_numArrayDimensions == 0) {
                InstanceManufacturerFactory manufacturerFactory = InstanceManufacturerFactoryFactory.GetManufacturer(
                    targetClass,
                    validationContext.ImportService,
                    this.ChildNodes);
                _forge = new ExprNewInstanceNodeNonArrayForge(this, targetClass, manufacturerFactory);
                return null;
            }

            // determine array initialized or not
            var targetClassArray = TypeHelper.GetArrayType(targetClass, _numArrayDimensions);
            if (ChildNodes.Length == 1 && ChildNodes[0] is ExprArrayNode) {
                _arrayInitializedByExpr = true;
            } else {
                foreach (ExprNode child  in ChildNodes) {
                    var evalType = child.Forge.EvaluationType;
                    if (!TypeHelper.IsInt32(evalType)) {
                        string message = "New-keyword with an array-type result requires an Integer-typed dimension but received type '" +
                                         evalType.CleanName() +
                                         "'";
                        throw new ExprValidationException(message);
                    }
                }
            }

            // handle array initialized by dimension only
            if (!_arrayInitializedByExpr) {
                _forge = new ExprNewInstanceNodeArrayForge(this, targetClass, targetClassArray);
                return null;
            }

            // handle array initialized by array expression
            if (_numArrayDimensions < 1 || _numArrayDimensions > 2) {
                throw new IllegalStateException("Num-array-dimensions unexpected at " + _numArrayDimensions);
            }

            ExprArrayNode arrayNode = (ExprArrayNode) ChildNodes[0];

            // handle 2-dimensional array validation
            if (_numArrayDimensions == 2) {
                foreach (ExprNode inner in arrayNode.ChildNodes) {
                    if (!(inner is ExprArrayNode)) {
                        throw new ExprValidationException(
                            "Two-dimensional array element does not allow element expression '" +
                            ExprNodeUtilityPrint.ToExpressionStringMinPrecedenceSafe(inner) +
                            "'");
                    }

                    ExprArrayNode innerArray = (ExprArrayNode) inner;
                    innerArray.OptionalRequiredType = targetClass;
                    innerArray.Validate(validationContext);
                }

                arrayNode.OptionalRequiredType = targetClassArray.GetElementType();
            }
            else {
                arrayNode.OptionalRequiredType = targetClass;
            }

            arrayNode.Validate(validationContext);

            _forge = new ExprNewInstanceNodeArrayForge(this, targetClass, targetClassArray);
            return null;
        }