Exemplo n.º 1
0
        public override ExprNode Validate(ExprValidationContext validationContext)
        {
            Type targetClass;
            try {
                targetClass = validationContext.ImportService.ResolveClass(classIdent, false);
            }
            catch (ImportException) {
                throw new ExprValidationException("Failed to resolve new-operator class name '" + classIdent + "'");
            }

            InstanceManufacturerFactory manufacturerFactory =
                InstanceManufacturerFactoryFactory.GetManufacturer(
                    targetClass,
                    validationContext.ImportService,
                    ChildNodes);
            forge = new ExprNewInstanceNodeForge(this, targetClass, manufacturerFactory);
            return null;
        }
Exemplo n.º 2
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;
        }