예제 #1
0
        private int[] GetRuleVersions(ITypeMirror recognizerClass, string[] ruleNames)
        {
            int[]            versions = new int[ruleNames.Length];
            IList <IElement> elements = processingEnv.GetElementUtils().GetAllMembers((ITypeElement)processingEnv.GetTypeUtils().AsElement(recognizerClass));

            foreach (IElement element in elements)
            {
                if (element.GetKind() != ElementKind.Field)
                {
                    continue;
                }
                IVariableElement field         = (IVariableElement)element;
                bool             isStatic      = element.GetModifiers().Contains(Modifier.Static);
                object           constantValue = field.GetConstantValue();
                bool             isInteger     = constantValue is int;
                string           name          = field.GetSimpleName().ToString();
                if (isStatic && isInteger && name.StartsWith("RULE_"))
                {
                    try
                    {
                        name = Sharpen.Runtime.Substring(name, "RULE_".Length);
                        if (name.IsEmpty() || !System.Char.IsLower(name[0]))
                        {
                            continue;
                        }
                        int index = (int)constantValue;
                        if (index < 0 || index >= versions.Length)
                        {
                            string message = string.Format("Rule index {0} for rule '{1}' out of bounds for recognizer {2}.", index, name, recognizerClass.ToString());
                            processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, message, element);
                            continue;
                        }
                        if (name.IndexOf(ATNSimulator.RuleVariantDelimiter) >= 0)
                        {
                            // ignore left-factored pseudo-rules
                            continue;
                        }
                        IExecutableElement ruleMethod = GetRuleMethod(recognizerClass, name);
                        if (ruleMethod == null)
                        {
                            string message = string.Format("Could not find rule method for rule '{0}' in recognizer {1}.", name, recognizerClass.ToString());
                            processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, message, element);
                            continue;
                        }
                        RuleVersion ruleVersion = ruleMethod.GetAnnotation <RuleVersion>();
                        int         version     = ruleVersion != null?ruleVersion.Value() : 0;

                        versions[index] = version;
                    }
                    catch (ArgumentException)
                    {
                        processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, "Exception occurred while validating rule dependencies.", element);
                    }
                }
            }
            return(versions);
        }
예제 #2
0
        private bool HasRuleVersionAnnotation(IExecutableElement method)
        {
            ITypeElement ruleVersionAnnotationElement = processingEnv.GetElementUtils().GetTypeElement(RuleVersionClassName);

            if (ruleVersionAnnotationElement == null)
            {
                return(false);
            }
            foreach (IAnnotationMirror annotation in method.GetAnnotationMirrors())
            {
                if (processingEnv.GetTypeUtils().IsSameType(annotation.GetAnnotationType(), ruleVersionAnnotationElement.AsType()))
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #3
0
        private IExecutableElement GetRuleMethod(ITypeMirror recognizerClass, string name)
        {
            IList <IElement> elements = processingEnv.GetElementUtils().GetAllMembers((ITypeElement)processingEnv.GetTypeUtils().AsElement(recognizerClass));

            foreach (IElement element in elements)
            {
                if (element.GetKind() != ElementKind.Method)
                {
                    continue;
                }
                IExecutableElement method = (IExecutableElement)element;
                if (method.GetSimpleName().ContentEquals(name) && HasRuleVersionAnnotation(method))
                {
                    return(method);
                }
            }
            return(null);
        }