Пример #1
0
        private IAnnotationValue FindRuleDependencyProperty(Tuple <RuleDependency, IElement> dependency, IAnnotationMirror annotationMirror, RuleDependencyProcessor.RuleDependencyProperty property)
        {
            IAnnotationValue recognizerValue = null;
            IAnnotationValue ruleValue       = null;
            IAnnotationValue versionValue    = null;
            IAnnotationValue dependentsValue = null;
            IDictionary <IExecutableElement, IAnnotationValue> values = annotationMirror.GetElementValues();

            foreach (KeyValuePair <IExecutableElement, IAnnotationValue> value in values.EntrySet())
            {
                IAnnotationValue annotationValue = value.Value;
                if ("rule()".Equals(value.Key.ToString()))
                {
                    ruleValue = annotationValue;
                    if (!(annotationValue.GetValue() is int))
                    {
                        processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Expected int constant for annotation property 'rule()'.", dependency.Item2, annotationMirror, annotationValue);
                        return(null);
                    }
                    if ((int)annotationValue.GetValue() != dependency.Item1.Rule())
                    {
                        // this is a valid dependency annotation, but not the one we're looking for
                        return(null);
                    }
                }
                else
                {
                    if ("recognizer()".Equals(value.Key.ToString()))
                    {
                        recognizerValue = annotationValue;
                        if (!(annotationValue.GetValue() is ITypeMirror))
                        {
                            processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Expected Class constant for annotation property 'recognizer()'.", dependency.Item2, annotationMirror, annotationValue);
                            return(null);
                        }
                        ITypeMirror annotationRecognizer = (ITypeMirror)annotationValue.GetValue();
                        ITypeMirror expectedRecognizer   = GetRecognizerType(dependency.Item1);
                        if (!processingEnv.GetTypeUtils().IsSameType(expectedRecognizer, annotationRecognizer))
                        {
                            // this is a valid dependency annotation, but not the one we're looking for
                            return(null);
                        }
                    }
                    else
                    {
                        if ("version()".Equals(value.Key.ToString()))
                        {
                            versionValue = annotationValue;
                            if (!(annotationValue.GetValue() is int))
                            {
                                processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Expected int constant for annotation property 'version()'.", dependency.Item2, annotationMirror, annotationValue);
                                return(null);
                            }
                            if ((int)annotationValue.GetValue() != dependency.Item1.Version())
                            {
                                // this is a valid dependency annotation, but not the one we're looking for
                                return(null);
                            }
                        }
                    }
                }
            }
            if (recognizerValue != null)
            {
                if (property == RuleDependencyProcessor.RuleDependencyProperty.Recognizer)
                {
                    return(recognizerValue);
                }
                else
                {
                    if (ruleValue != null)
                    {
                        if (property == RuleDependencyProcessor.RuleDependencyProperty.Rule)
                        {
                            return(ruleValue);
                        }
                        else
                        {
                            if (versionValue != null)
                            {
                                if (property == RuleDependencyProcessor.RuleDependencyProperty.Version)
                                {
                                    return(versionValue);
                                }
                                else
                                {
                                    if (property == RuleDependencyProcessor.RuleDependencyProperty.Dependents)
                                    {
                                        return(dependentsValue);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (recognizerValue == null)
            {
                processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Could not find 'recognizer()' element in annotation.", dependency.Item2, annotationMirror);
            }
            if (property == RuleDependencyProcessor.RuleDependencyProperty.Recognizer)
            {
                return(null);
            }
            if (ruleValue == null)
            {
                processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Could not find 'rule()' element in annotation.", dependency.Item2, annotationMirror);
            }
            if (property == RuleDependencyProcessor.RuleDependencyProperty.Rule)
            {
                return(null);
            }
            if (versionValue == null)
            {
                processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Could not find 'version()' element in annotation.", dependency.Item2, annotationMirror);
            }
            return(null);
        }
Пример #2
0
        private Tuple <IAnnotationMirror, IAnnotationValue> FindRuleDependencyProperty(Tuple <RuleDependency, IElement> dependency, RuleDependencyProcessor.RuleDependencyProperty property)
        {
            ITypeElement ruleDependencyTypeElement   = processingEnv.GetElementUtils().GetTypeElement(RuleDependencyClassName);
            ITypeElement ruleDependenciesTypeElement = processingEnv.GetElementUtils().GetTypeElement(RuleDependenciesClassName);
            IList <IAnnotationMirror> mirrors        = dependency.Item2.GetAnnotationMirrors();

            foreach (IAnnotationMirror annotationMirror in mirrors)
            {
                if (processingEnv.GetTypeUtils().IsSameType(ruleDependencyTypeElement.AsType(), annotationMirror.GetAnnotationType()))
                {
                    IAnnotationValue element = FindRuleDependencyProperty(dependency, annotationMirror, property);
                    if (element != null)
                    {
                        return(Tuple.Create(annotationMirror, element));
                    }
                }
                else
                {
                    if (processingEnv.GetTypeUtils().IsSameType(ruleDependenciesTypeElement.AsType(), annotationMirror.GetAnnotationType()))
                    {
                        IDictionary <IExecutableElement, IAnnotationValue> values = annotationMirror.GetElementValues();
                        foreach (KeyValuePair <IExecutableElement, IAnnotationValue> value in values.EntrySet())
                        {
                            if ("value()".Equals(value.Key.ToString()))
                            {
                                IAnnotationValue annotationValue = value.Value;
                                if (!(annotationValue.GetValue() is IList))
                                {
                                    processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Expected array of RuleDependency annotations for annotation property 'value()'.", dependency.Item2, annotationMirror, annotationValue);
                                    break;
                                }
                                IList <object> annotationValueList = (IList <object>)annotationValue.GetValue();
                                foreach (object obj in annotationValueList)
                                {
                                    if (!(obj is IAnnotationMirror))
                                    {
                                        processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Warning, "Expected RuleDependency annotation mirror for element of property 'value()'.", dependency.Item2, annotationMirror, annotationValue);
                                        break;
                                    }
                                    IAnnotationValue element = FindRuleDependencyProperty(dependency, (IAnnotationMirror)obj, property);
                                    if (element != null)
                                    {
                                        return(Tuple.Create((IAnnotationMirror)obj, element));
                                    }
                                }
                            }
                            else
                            {
                                processingEnv.GetMessager().PrintMessage(Diagnostic.Kind.Error, string.Format("Unexpected annotation property {0}.", value.Key.ToString()), dependency.Item2, annotationMirror, value.Value);
                            }
                        }
                    }
                }
            }
            return(null);
        }