public static CodegenExpression CodegenUnderlying( ExprDotMethodForgeNoDuck forge, CodegenExpression inner, Type innerType, CodegenMethodScope codegenMethodScope, ExprForgeCodegenSymbol exprSymbol, CodegenClassScope codegenClassScope) { var underlyingType = forge.Method.DeclaringType; var methodNode = codegenMethodScope.MakeChild( forge.Method.ReturnType.GetBoxedType(), typeof(ExprDotMethodForgeNoDuckEvalUnderlying), codegenClassScope) .AddParam(typeof(EventBean), "target"); methodNode.Block .IfRefNullReturnNull("target") .DeclareVar( underlyingType, "underlying", Cast(underlyingType, ExprDotName(Ref("target"), "Underlying"))) .MethodReturn( CodegenPlain(forge, Ref("underlying"), innerType, methodNode, exprSymbol, codegenClassScope)); return LocalMethod(methodNode, inner); }
internal ExprDotMethodForgeNoDuckEvalPlain( ExprDotMethodForgeNoDuck forge, ExprEvaluator[] parameters) { this.forge = forge; this.parameters = parameters; }
public static CodegenExpression CodegenWrapArray( ExprDotMethodForgeNoDuck forge, CodegenExpression inner, Type innerType, CodegenMethodScope codegenMethodScope, ExprForgeCodegenSymbol exprSymbol, CodegenClassScope codegenClassScope) { var methodNode = codegenMethodScope.MakeChild( typeof(ICollection<object>), typeof(ExprDotMethodForgeNoDuckEvalWrapArray), codegenClassScope) .AddParam(innerType, "target"); var returnType = forge.Method.ReturnType; methodNode.Block .DeclareVar( returnType.GetBoxedType(), "array", CodegenPlain(forge, Ref("target"), innerType, methodNode, exprSymbol, codegenClassScope)) .MethodReturn( CollectionUtil.ArrayToCollectionAllowNullCodegen( methodNode, returnType, Ref("array"), codegenClassScope)); return LocalMethod(methodNode, inner); }
public ExprDotMethodForgeNoDuckEvalUnderlying( ExprDotMethodForgeNoDuck forge, ExprEvaluator[] parameters) : base(forge, parameters) { }
public static CodegenExpression CodegenPlain( ExprDotMethodForgeNoDuck forge, CodegenExpression inner, Type innerType, CodegenMethodScope codegenMethodScope, ExprForgeCodegenSymbol exprSymbol, CodegenClassScope codegenClassScope) { var method = forge.Method; var returnType = method.ReturnType.GetBoxedType(); Type instanceType; IList<Type> methodParameters; if (method.IsExtensionMethod()) { methodParameters = method.GetParameterTypes().Skip(1).ToList(); instanceType = method.GetParameters()[0].ParameterType; } else { methodParameters = method.GetParameterTypes().ToList(); instanceType = method.DeclaringType; } var methodNode = codegenMethodScope .MakeChild(returnType, typeof(ExprDotMethodForgeNoDuckEvalPlain), codegenClassScope) .AddParam(instanceType.GetBoxedType(), "target"); var block = methodNode.Block; if (innerType.CanBeNull() && returnType != typeof(void)) { block.IfRefNullReturnNull("target"); } var args = new List<CodegenExpression>(); //[forge.Parameters.Length]; for (var i = 0; i < forge.Parameters.Length; i++) { var name = "p" + i; var evaluationType = forge.Parameters[i].EvaluationType; block.DeclareVar( evaluationType, name, forge.Parameters[i].EvaluateCodegen(evaluationType, methodNode, exprSymbol, codegenClassScope)); CodegenExpression reference = Ref(name); if (evaluationType.IsNullable() && !methodParameters[i].IsNullable()) { reference = Unbox(reference); } args.Add(reference); } CodegenExpression target = Ref("target"); if (instanceType != instanceType.GetBoxedType()) { target = CodegenExpressionBuilder.Unbox(target); } var tryBlock = block.TryCatch(); CodegenExpression invocation; if (method.IsExtensionMethod()) { args.Insert(0, target); invocation = StaticMethod(method.DeclaringType, method.Name, args.ToArray()); } else if (method.IsStatic) { invocation = StaticMethod(method.DeclaringType, method.Name, args.ToArray()); } else { invocation = ExprDotMethod(target, method.Name, args.ToArray()); } CodegenStatementTryCatch tryCatch; if (returnType == typeof(void)) { tryCatch = tryBlock.Expression(invocation).TryEnd(); } else { tryCatch = tryBlock.TryReturn(invocation); } var catchBlock = tryCatch.AddCatch(typeof(Exception), "t"); catchBlock.DeclareVar<object[]>( "args", NewArrayByLength(typeof(object), Constant(forge.Parameters.Length))); for (var i = 0; i < forge.Parameters.Length; i++) { catchBlock.AssignArrayElement("args", Constant(i), args[i]); } catchBlock.StaticMethod( typeof(ExprDotMethodForgeNoDuckEvalPlain), METHOD_HANDLETARGETEXCEPTION, Constant(forge.OptionalStatementName), Constant(method.Name), Constant(method.GetParameterTypes()), ExprDotMethodChain(Ref("target")).Add("GetType").Get("FullName"), Ref("args"), Ref("t")); if (returnType == typeof(void)) { block.MethodEnd(); } else { block.MethodReturn(ConstantNull()); } return LocalMethod(methodNode, inner); }
public ExprDotMethodForgeNoDuckEvalWrapArray( ExprDotMethodForgeNoDuck forge, ExprEvaluator[] parameters) : base(forge, parameters) { }
public static ExprDotNodeRealizedChain GetChainEvaluators( int? streamOfProviderIfApplicable, EPType inputType, IList<ExprChainedSpec> chainSpec, ExprValidationContext validationContext, bool isDuckTyping, ExprDotNodeFilterAnalyzerInput inputDesc) { IList<ExprDotForge> methodForges = new List<ExprDotForge>(); var currentInputType = inputType; EnumMethodEnum? lastLambdaFunc = null; var lastElement = chainSpec.IsEmpty() ? null : chainSpec[chainSpec.Count - 1]; FilterExprAnalyzerAffector filterAnalyzerDesc = null; Deque<ExprChainedSpec> chainSpecStack = new ArrayDeque<ExprChainedSpec>(chainSpec); while (!chainSpecStack.IsEmpty()) { var chainElement = chainSpecStack.RemoveFirst(); lastLambdaFunc = null; // reset // compile parameters for chain element var paramForges = new ExprForge[chainElement.Parameters.Count]; var paramTypes = new Type[chainElement.Parameters.Count]; for (var i = 0; i < chainElement.Parameters.Count; i++) { paramForges[i] = chainElement.Parameters[i].Forge; paramTypes[i] = paramForges[i].EvaluationType; } // check if special 'size' method if (currentInputType is ClassMultiValuedEPType) { var type = (ClassMultiValuedEPType) currentInputType; if (chainElement.Name.Equals("size", StringComparison.InvariantCultureIgnoreCase) && paramTypes.Length == 0 && lastElement == chainElement) { var sizeExpr = new ExprDotForgeArraySize(); methodForges.Add(sizeExpr); currentInputType = sizeExpr.TypeInfo; continue; } if (chainElement.Name.Equals("get", StringComparison.InvariantCultureIgnoreCase) && paramTypes.Length == 1 && Boxing.GetBoxedType(paramTypes[0]) == typeof(int?)) { var componentType = Boxing.GetBoxedType(type.Component); var get = new ExprDotForgeArrayGet(paramForges[0], componentType); methodForges.Add(get); currentInputType = get.TypeInfo; continue; } } // determine if there is a matching method var matchingMethod = false; var methodTarget = GetMethodTarget(currentInputType); if (methodTarget != null) { try { GetValidateMethodDescriptor( methodTarget, chainElement.Name, chainElement.Parameters, validationContext); matchingMethod = true; } catch (ExprValidationException) { // expected } } if (EnumMethodEnumExtensions.IsEnumerationMethod(chainElement.Name) && (!matchingMethod || methodTarget.IsArray || methodTarget.IsGenericCollection())) { var enumerationMethod = EnumMethodEnumExtensions.FromName(chainElement.Name); if (enumerationMethod == null) { throw new EPException("unable to determine enumeration method from name"); } var eval = TypeHelper.Instantiate<ExprDotForgeEnumMethod>( enumerationMethod.Value.GetImplementation()); eval.Init( streamOfProviderIfApplicable, enumerationMethod.Value, chainElement.Name, currentInputType, chainElement.Parameters, validationContext); currentInputType = eval.TypeInfo; if (currentInputType == null) { throw new IllegalStateException( "Enumeration method '" + chainElement.Name + "' has not returned type information"); } methodForges.Add(eval); lastLambdaFunc = enumerationMethod; continue; } // resolve datetime if (DatetimeMethodEnumHelper.IsDateTimeMethod(chainElement.Name) && (!matchingMethod || methodTarget == typeof(DateTimeEx) || methodTarget == typeof(DateTimeOffset) || methodTarget == typeof(DateTime))) { var dateTimeMethod = DatetimeMethodEnumHelper.FromName(chainElement.Name); var datetimeImpl = ExprDotDTFactory.ValidateMake( validationContext.StreamTypeService, chainSpecStack, dateTimeMethod, chainElement.Name, currentInputType, chainElement.Parameters, inputDesc, validationContext.ImportService.TimeAbacus, null, validationContext.TableCompileTimeResolver); currentInputType = datetimeImpl.ReturnType; if (currentInputType == null) { throw new IllegalStateException( "Date-time method '" + chainElement.Name + "' has not returned type information"); } methodForges.Add(datetimeImpl.Forge); filterAnalyzerDesc = datetimeImpl.IntervalFilterDesc; continue; } // try to resolve as property if the last method returned a type if (currentInputType is EventEPType) { var inputEventType = (EventTypeSPI) ((EventEPType) currentInputType).EventType; var type = inputEventType.GetPropertyType(chainElement.Name); var getter = inputEventType.GetGetterSPI(chainElement.Name); if (type != null && getter != null) { var noduck = new ExprDotForgeProperty( getter, EPTypeHelper.SingleValue(Boxing.GetBoxedType(type))); methodForges.Add(noduck); currentInputType = EPTypeHelper.SingleValue(EPTypeHelper.GetClassSingleValued(noduck.TypeInfo)); continue; } } // Finally try to resolve the method if (methodTarget != null) { try { // find descriptor again, allow for duck typing var desc = GetValidateMethodDescriptor( methodTarget, chainElement.Name, chainElement.Parameters, validationContext); paramForges = desc.ChildForges; ExprDotForge forge; if (currentInputType is ClassEPType) { // if followed by an enumeration method, convert array to collection if (desc.ReflectionMethod.ReturnType.IsArray && !chainSpecStack.IsEmpty() && EnumMethodEnumExtensions.IsEnumerationMethod(chainSpecStack.First.Name)) { forge = new ExprDotMethodForgeNoDuck( validationContext.StatementName, desc.ReflectionMethod, paramForges, ExprDotMethodForgeNoDuck.DuckType.WRAPARRAY); } else { forge = new ExprDotMethodForgeNoDuck( validationContext.StatementName, desc.ReflectionMethod, paramForges, ExprDotMethodForgeNoDuck.DuckType.PLAIN); } } else { forge = new ExprDotMethodForgeNoDuck( validationContext.StatementName, desc.ReflectionMethod, paramForges, ExprDotMethodForgeNoDuck.DuckType.UNDERLYING); } methodForges.Add(forge); currentInputType = forge.TypeInfo; } catch (Exception e) { if (!isDuckTyping) { throw new ExprValidationException(e.Message, e); } var duck = new ExprDotMethodForgeDuck( validationContext.StatementName, validationContext.ImportService, chainElement.Name, paramTypes, paramForges); methodForges.Add(duck); currentInputType = duck.TypeInfo; } continue; } var message = "Could not find event property, enumeration method or instance method named '" + chainElement.Name + "' in " + EPTypeHelper.ToTypeDescriptive(currentInputType); throw new ExprValidationException(message); } var intermediateEvals = methodForges.ToArray(); if (lastLambdaFunc != null) { ExprDotForge finalEval = null; if (currentInputType is EventMultiValuedEPType) { var mvType = (EventMultiValuedEPType) currentInputType; var tableMetadata = validationContext.TableCompileTimeResolver.ResolveTableFromEventType(mvType.Component); if (tableMetadata != null) { finalEval = new ExprDotForgeUnpackCollEventBeanTable(mvType.Component, tableMetadata); } else { finalEval = new ExprDotForgeUnpackCollEventBean(mvType.Component); } } else if (currentInputType is EventEPType) { var epType = (EventEPType) currentInputType; var tableMetadata = validationContext.TableCompileTimeResolver.ResolveTableFromEventType(epType.EventType); if (tableMetadata != null) { finalEval = new ExprDotForgeUnpackBeanTable(epType.EventType, tableMetadata); } else { finalEval = new ExprDotForgeUnpackBean(epType.EventType); } } if (finalEval != null) { methodForges.Add(finalEval); } } var unpackingForges = methodForges.ToArray(); return new ExprDotNodeRealizedChain(intermediateEvals, unpackingForges, filterAnalyzerDesc); }
public static ExprDotNodeRealizedChain GetChainEvaluators( int?streamOfProviderIfApplicable, EPType inputType, IList <Chainable> chainSpec, ExprValidationContext validationContext, bool isDuckTyping, ExprDotNodeFilterAnalyzerInput inputDesc) { var methodForges = new List <ExprDotForge>(); var currentInputType = inputType; EnumMethodDesc lastLambdaFunc = null; var lastElement = chainSpec.IsEmpty() ? null : chainSpec[chainSpec.Count - 1]; FilterExprAnalyzerAffector filterAnalyzerDesc = null; Deque <Chainable> chainSpecStack = new ArrayDeque <Chainable>(chainSpec); while (!chainSpecStack.IsEmpty()) { var chainElement = chainSpecStack.RemoveFirst(); var parameters = chainElement.GetParametersOrEmpty(); var chainElementName = chainElement.GetRootNameOrEmptyString(); var last = chainSpecStack.IsEmpty(); lastLambdaFunc = null; // reset // compile parameters for chain element var paramForges = new ExprForge[parameters.Count]; var paramTypes = new Type[parameters.Count]; for (var i = 0; i < parameters.Count; i++) { paramForges[i] = parameters[i].Forge; paramTypes[i] = paramForges[i].EvaluationType; } // check if special 'size' method if (currentInputType is ClassMultiValuedEPType) { var type = (ClassMultiValuedEPType)currentInputType; if (string.Equals(chainElementName, "size", StringComparison.InvariantCultureIgnoreCase) && paramTypes.Length == 0 && lastElement == chainElement) { ExprDotForge size; var containerType = ((ClassMultiValuedEPType)currentInputType).Container; if (containerType.IsArray) { size = new ExprDotForgeSizeArray(); } else { size = new ExprDotForgeSizeCollection(); } methodForges.Add(size); currentInputType = size.TypeInfo; continue; } if (string.Equals(chainElementName, "get", StringComparison.InvariantCultureIgnoreCase) && paramTypes.Length == 1 && paramTypes[0].GetBoxedType() == typeof(int?)) { ExprDotForge get; var componentType = type.Component.GetBoxedType(); if (type.Container.IsArray) { get = new ExprDotForgeGetArray(paramForges[0], componentType); } else { get = new ExprDotForgeGetCollection(paramForges[0], componentType); } methodForges.Add(get); currentInputType = get.TypeInfo; continue; } if (chainElement is ChainableArray && type.Container.IsArray) { var array = (ChainableArray)chainElement; var typeInfo = currentInputType; var indexExpr = ChainableArray.ValidateSingleIndexExpr( array.Indexes, () => "operation on type " + typeInfo.ToTypeDescriptive()); var componentType = type.Component.GetBoxedType(); var get = new ExprDotForgeGetArray(indexExpr.Forge, componentType); methodForges.Add(get); currentInputType = get.TypeInfo; continue; } } // determine if there is a matching method or property var methodTarget = GetMethodTarget(currentInputType); var matchingMethod = false; if (methodTarget != null && (!(chainElement is ChainableArray))) { try { GetValidateMethodDescriptor(methodTarget, chainElementName, parameters, validationContext); matchingMethod = true; } catch (ExprValidationException) { // expected } } if (EnumMethodResolver.IsEnumerationMethod(chainElementName, validationContext.ImportService) && (!matchingMethod || methodTarget.IsArray || methodTarget.IsGenericCollection())) { var enumerationMethod = EnumMethodResolver.FromName(chainElementName, validationContext.ImportService); if (enumerationMethod == null) { throw new EPException("unable to determine enumeration method from name"); } var eval = enumerationMethod.Factory.Invoke(chainElement.GetParametersOrEmpty().Count); if (currentInputType is ClassEPType classEpType && classEpType.Clazz.IsGenericCollection() && !classEpType.Clazz.IsArray) { currentInputType = EPTypeHelper.CollectionOfSingleValue(typeof(object), null); } eval.Init(streamOfProviderIfApplicable, enumerationMethod, chainElementName, currentInputType, parameters, validationContext); currentInputType = eval.TypeInfo; if (currentInputType == null) { throw new IllegalStateException("Enumeration method '" + chainElementName + "' has not returned type information"); } methodForges.Add(eval); lastLambdaFunc = enumerationMethod; continue; } // resolve datetime if (DatetimeMethodResolver.IsDateTimeMethod(chainElementName, validationContext.ImportService) && (!matchingMethod || methodTarget == typeof(DateTimeEx) || methodTarget == typeof(DateTimeOffset) || methodTarget == typeof(DateTime))) { var datetimeMethod = DatetimeMethodResolver.FromName(chainElementName, validationContext.ImportService); var datetimeImpl = ExprDotDTFactory.ValidateMake( validationContext.StreamTypeService, chainSpecStack, datetimeMethod, chainElementName, currentInputType, parameters, inputDesc, validationContext.ImportService.TimeAbacus, validationContext.TableCompileTimeResolver, validationContext.ImportService, validationContext.StatementRawInfo); currentInputType = datetimeImpl.ReturnType; if (currentInputType == null) { throw new IllegalStateException( "Date-time method '" + chainElementName + "' has not returned type information"); } methodForges.Add(datetimeImpl.Forge); filterAnalyzerDesc = datetimeImpl.IntervalFilterDesc; continue; } // try to resolve as property if the last method returned a type if (currentInputType is EventEPType) { if (chainElement is ChainableArray) { throw new ExprValidationException("Could not perform array operation on type " + currentInputType.ToTypeDescriptive()); } var inputEventType = (EventTypeSPI)((EventEPType)currentInputType).EventType; var type = inputEventType.GetPropertyType(chainElementName); var getter = inputEventType.GetGetterSPI(chainElementName); var fragmentType = inputEventType.GetFragmentType(chainElementName); ExprDotForge forge; if (type != null && getter != null) { if (fragmentType == null || last) { forge = new ExprDotForgeProperty(getter, EPTypeHelper.SingleValue(type.GetBoxedType())); currentInputType = forge.TypeInfo; } else { if (!fragmentType.IsIndexed) { currentInputType = EPTypeHelper.SingleEvent(fragmentType.FragmentType); } else { currentInputType = EPTypeHelper.ArrayOfEvents(fragmentType.FragmentType); } forge = new ExprDotForgePropertyFragment(getter, currentInputType); } methodForges.Add(forge); continue; } } if (currentInputType is EventMultiValuedEPType eventMultiValuedEpType && chainElement is ChainableArray chainableArray) { var inputEventType = (EventTypeSPI)eventMultiValuedEpType.Component; var typeInfo = currentInputType; var indexExpr = ChainableArray.ValidateSingleIndexExpr( chainableArray.Indexes, () => "operation on type " + typeInfo.ToTypeDescriptive()); currentInputType = EPTypeHelper.SingleEvent(inputEventType); var forge = new ExprDotForgeEventArrayAtIndex(currentInputType, indexExpr); methodForges.Add(forge); continue; } // Finally try to resolve the method if (methodTarget != null && !(chainElement is ChainableArray)) { try { // find descriptor again, allow for duck typing var desc = GetValidateMethodDescriptor(methodTarget, chainElementName, parameters, validationContext); paramForges = desc.ChildForges; ExprDotForge forge; if (currentInputType is ClassEPType) { // if followed by an enumeration method, convert array to collection if (desc.ReflectionMethod.ReturnType.IsArray && !chainSpecStack.IsEmpty() && EnumMethodResolver.IsEnumerationMethod(chainSpecStack.First.GetRootNameOrEmptyString(), validationContext.ImportService)) { forge = new ExprDotMethodForgeNoDuck( validationContext.StatementName, desc.ReflectionMethod, paramForges, ExprDotMethodForgeNoDuck.DuckType.WRAPARRAY); } else { forge = new ExprDotMethodForgeNoDuck( validationContext.StatementName, desc.ReflectionMethod, paramForges, ExprDotMethodForgeNoDuck.DuckType.PLAIN); } } else { forge = new ExprDotMethodForgeNoDuck( validationContext.StatementName, desc.ReflectionMethod, paramForges, ExprDotMethodForgeNoDuck.DuckType.UNDERLYING); } methodForges.Add(forge); currentInputType = forge.TypeInfo; } catch (Exception e) { if (!isDuckTyping) { throw new ExprValidationException(e.Message, e); } var duck = new ExprDotMethodForgeDuck( validationContext.StatementName, validationContext.ImportService, chainElementName, paramTypes, paramForges); methodForges.Add(duck); currentInputType = duck.TypeInfo; } continue; } string message; if (!(chainElement is ChainableArray)) { message = "Could not find event property or method named '" + chainElementName + "' in " + currentInputType.ToTypeDescriptive(); } else { message = "Could not perform array operation on type " + currentInputType.ToTypeDescriptive(); } throw new ExprValidationException(message); } var intermediateEvals = methodForges.ToArray(); if (lastLambdaFunc != null) { ExprDotForge finalEval = null; if (currentInputType is EventMultiValuedEPType mvType) { var tableMetadata = validationContext.TableCompileTimeResolver.ResolveTableFromEventType(mvType.Component); if (tableMetadata != null) { finalEval = new ExprDotForgeUnpackCollEventBeanTable(mvType.Component, tableMetadata); } else { finalEval = new ExprDotForgeUnpackCollEventBean(mvType.Component); } } else if (currentInputType is EventEPType epType) { var tableMetadata = validationContext.TableCompileTimeResolver.ResolveTableFromEventType(epType.EventType); if (tableMetadata != null) { finalEval = new ExprDotForgeUnpackBeanTable(epType.EventType, tableMetadata); } else { finalEval = new ExprDotForgeUnpackBean(epType.EventType); } } if (finalEval != null) { methodForges.Add(finalEval); } } var unpackingForges = methodForges.ToArray(); return(new ExprDotNodeRealizedChain(intermediateEvals, unpackingForges, filterAnalyzerDesc)); }