Annotate类

Posted extjs4

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Annotate类相关的知识,希望对你有一定的参考价值。

 

在Annotate类中有个Annotator接口,定义如下:

/** A client that has annotations to add registers an annotator,
*  the method it will use to add the annotation.  There are no
*  parameters; any needed data should be captured by the
*  Annotator.
*/
public interface Annotator {
        void enterAnnotation();
        String toString();
}

 

其中的实现分部在MemberEnter与ClassReader类中,如下:

技术分享图片

 

方法1:

 /** Queue processing of an attribute default value. */
    void annotateDefaultValueLater(final JCExpression defaultValue,
                                    final Env<AttrContext> localEnv,
                                    final MethodSymbol m) {
        annotate.later(new Annotate.Annotator() {
                public String toString() {
                    return "annotate " + m.owner + "." +  m + " default " + defaultValue;
                }
                public void enterAnnotation() {
                    JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
                    try {
                        enterDefaultValue(defaultValue, localEnv, m);
                    } finally {
                        log.useSource(prev);
                    }
                }
            });
    }

    /** Enter a default value for an attribute method. */
    private void enterDefaultValue(final JCExpression defaultValue,
                                     final Env<AttrContext> localEnv,
                                     final MethodSymbol m) {
        m.defaultValue = annotate.enterAttributeValue(m.type.getReturnType(),defaultValue,localEnv);
    }

  

方法2:

 /** Queue annotations for later processing. */
    void annotateLater(final List<JCAnnotation> annotations,
                       final Env<AttrContext> localEnv,
                       final Symbol s) {
        if (annotations.isEmpty()) {
            return;
        }
        if (s.kind != PCK) {
            s.attributes_field = null; // mark it incomplete for now
        }
        
        annotate.later(new Annotate.Annotator() {
                public String toString() {
                    return "annotate " + annotations + " onto " + s + " in " + s.owner;
                }
                public void enterAnnotation() {
                    Assert.check(s.kind == PCK || s.attributes_field == null);
                    JavaFileObject prev = log.useSource(localEnv.toplevel.sourcefile);
                    try {
                        if (s.attributes_field != null &&
                            s.attributes_field.nonEmpty() &&
                            annotations.nonEmpty()) {
                            log.error(annotations.head.pos, "already.annotated", kindName(s), s);
                        }
                        enterAnnotations(annotations, localEnv, s);
                    } finally {
                        log.useSource(prev);
                    }
                }
            });
    }

 

其中调用了enterAnnotations()方法,代码如下:

 

 /** Enter a set of annotations. */
    private void enterAnnotations(List<JCAnnotation> annotations,Env<AttrContext> env,Symbol s) {
        ListBuffer<Attribute.Compound> buf =  new ListBuffer<Attribute.Compound>();
        Set<TypeSymbol> annotated = new HashSet<TypeSymbol>();
        if (!skipAnnotations) {
            for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) {
                JCAnnotation a = al.head;
                Attribute.Compound c = annotate.enterAnnotation(a, syms.annotationType, env);
                if (c == null) {
                    continue;
                }
                buf.append(c);
                // Note: @Deprecated has no effect on local variables and parameters
                if (!c.type.isErroneous()
                        && s.owner.kind != MTH
                        && types.isSameType(c.type, syms.deprecatedType)) {
                    s.flags_field |= Flags.DEPRECATED;
                }
                // Internally to java.lang.invoke, a @PolymorphicSignature annotation
                // acts like a classfile attribute.
                if (!c.type.isErroneous() &&
                        types.isSameType(c.type, syms.polymorphicSignatureType)) {
                    if (!target.hasMethodHandles()) {
                        // Somebody is compiling JDK7 source code to a JDK6 target.
                        // Make it an error, since it is unlikely but important.
                        log.error(env.tree.pos(), "wrong.target.for.polymorphic.signature.definition", target.name);
                    }
                    // Pull the flag through for better diagnostics, even on a bad target.
                    s.flags_field |= Flags.POLYMORPHIC_SIGNATURE;
                }
                if (!annotated.add(a.type.tsym)) {
                    log.error(a.pos, "duplicate.annotation");
                }
            }
        }
        s.attributes_field = buf.toList();
    }

 

  

 

 

 

方法3:

/** Import statics types of a given name.  Non-types are handled in Attr.
     *  @param pos           Position to be used for error reporting.
     *  @param tsym          The class from which the name is imported.
     *  @param name          The (simple) name being imported.
     *  @param env           The environment containing the named import
     *                  scope to add to.
     */
    private void importNamedStatic(final DiagnosticPosition pos,
                                     final TypeSymbol tsym,
                                     final Name name,
                                     final Env<AttrContext> env) {
        if (tsym.kind != TYP) {
            log.error(DiagnosticFlag.RECOVERABLE, pos, "static.imp.only.classes.and.interfaces");
            return;
        }

        final Scope toScope = env.toplevel.namedImportScope;
        final PackageSymbol packge = env.toplevel.packge;
        final TypeSymbol origin = tsym;

        // enter imported types immediately
        new Object() {
            Set<Symbol> processed = new HashSet<Symbol>();
            void importFrom(TypeSymbol tsym) {
                if (tsym == null || !processed.add(tsym)) {
                    return;
                }

                // also import inherited names
                importFrom(types.supertype(tsym.type).tsym);
                for (Type t : types.interfaces(tsym.type)) {
                    importFrom(t.tsym);
                }

                for (Scope.Entry e = tsym.members().lookup(name); e.scope != null; e = e.next()) {
                    Symbol sym = e.sym;
                    if (sym.isStatic() &&
                        sym.kind == TYP &&
                        staticImportAccessible(sym, packge) &&
                        sym.isMemberOf(origin, types) &&
                        chk.checkUniqueStaticImport(pos, sym, toScope)) {
                        toScope.enter(sym, sym.owner.members(), origin.members());
                    }
                }
            }
        }.importFrom(tsym);

        // enter non-types before annotations that might use them
        annotate.earlier(new Annotate.Annotator() {
            Set<Symbol> processed = new HashSet<Symbol>();
            boolean found = false;

            public String toString() {
                return "import static " + tsym + "." + name;
            }
            void importFrom(TypeSymbol tsym) {
                if (tsym == null || !processed.add(tsym)) {
                    return;
                }

                // also import inherited names
                importFrom(types.supertype(tsym.type).tsym);
                for (Type t : types.interfaces(tsym.type))
                    importFrom(t.tsym);

                for (Scope.Entry e = tsym.members().lookup(name);e.scope != null;e = e.next()) {
                    Symbol sym = e.sym;
                    if (sym.isStatic() &&
                        staticImportAccessible(sym, packge) &&
                        sym.isMemberOf(origin, types)) {
                        found = true;
                        if (sym.kind == MTH ||
                            sym.kind != TYP && chk.checkUniqueStaticImport(pos, sym, toScope)) {
                            toScope.enter(sym, sym.owner.members(), origin.members());
                        }
                    }
                }
            }
            public void enterAnnotation() {
                JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
                try {
                    importFrom(tsym);
                    if (!found) {
                        log.error(pos, "cant.resolve.location",
                                  KindName.STATIC,
                                  name, List.<Type>nil(), List.<Type>nil(),
                                  Kinds.typeKindName(tsym.type),
                                  tsym.type);
                    }
                } finally {
                    log.useSource(prev);
                }
            }
        });
    }

  

方法4:

 /** Import all static members of a class or package on demand.
     *  @param pos           Position to be used for error reporting.
     *  @param tsym          The class or package the members of which are imported.
     *  @param toScope   The (import) scope in which imported classes are entered.
     */
    private void importStaticAll(int pos,
                                 final TypeSymbol tsym,
                                 Env<AttrContext> env) {
        final JavaFileObject sourcefile = env.toplevel.sourcefile;
        final Scope toScope = env.toplevel.starImportScope;
        final PackageSymbol packge = env.toplevel.packge;
        final TypeSymbol origin = tsym;

        // enter imported types immediately
        new Object() {
            Set<Symbol> processed = new HashSet<Symbol>();
            void importFrom(TypeSymbol tsym) {
                if (tsym == null || !processed.add(tsym)) {
                    return;
                }
                // also import inherited names
                importFrom(types.supertype(tsym.type).tsym);
                for (Type t : types.interfaces(tsym.type)) {
                    importFrom(t.tsym);
                }

                final Scope fromScope = tsym.members();
                for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
                    Symbol sym = e.sym;
                    if (sym.kind == TYP &&
                        (sym.flags() & STATIC) != 0 &&
                        staticImportAccessible(sym, packge) &&
                        sym.isMemberOf(origin, types) &&
                        !toScope.includes(sym)
                    ){
                        toScope.enter(sym, fromScope, origin.members());
                    }
                }
            }
        }.importFrom(tsym);

        // enter non-types before annotations that might use them
        annotate.earlier(new Annotate.Annotator() {
            Set<Symbol> processed = new HashSet<Symbol>();

            public String toString() {
                return "import static " + tsym + ".*" + " in " + sourcefile;
            }
            void importFrom(TypeSymbol tsym) {
                if (tsym == null || !processed.add(tsym)) {
                    return;
                }

                // also import inherited names
                importFrom(types.supertype(tsym.type).tsym);
                for (Type t : types.interfaces(tsym.type)) {
                    importFrom(t.tsym);
                }

                final Scope fromScope = tsym.members();
                for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
                    Symbol sym = e.sym;
                    if (sym.isStatic() && sym.kind != TYP &&
                        staticImportAccessible(sym, packge) &&
                        !toScope.includes(sym) &&
                        sym.isMemberOf(origin, types)) {
                        toScope.enter(sym, fromScope, origin.members());
                    }
                }
            }
            public void enterAnnotation() {
                importFrom(tsym);
            }
        });
    }

  

  

 

技术分享图片

继承体系如下图:

技术分享图片  技术分享图片技术分享图片

 

 

 

类1:

class AnnotationCompleter extends AnnotationDeproxy implements Annotate.Annotator {
        final Symbol sym;
        final List<CompoundAnnotationProxy> l;
        final JavaFileObject classFile;
        @Override
        public String toString() {
            return " ClassReader annotate " + sym.owner + "." + sym + " with " + l;
        }
        AnnotationCompleter(Symbol sym, List<CompoundAnnotationProxy> l) {
            this.sym = sym;
            this.l = l;
            this.classFile = currentClassFile;
        }
        // implement Annotate.Annotator.enterAnnotation()
        public void enterAnnotation() {
            JavaFileObject previousClassFile = currentClassFile;
            try {
                currentClassFile = classFile;
                List<Attribute.Compound> newList = deproxyCompoundList(l);
                sym.attributes_field = ((sym.attributes_field == null)
                                        ? newList
                                        : newList.prependList(sym.attributes_field));
            } finally {
                currentClassFile = previousClassFile;
            }
        }
    }

  

类2:

class AnnotationDefaultCompleter extends AnnotationDeproxy implements Annotate.Annotator {
        final MethodSymbol sym;
        final Attribute value;
        final JavaFileObject classFile = currentClassFile;
        @Override
        public String toString() {
            return " ClassReader store default for " + sym.owner + "." + sym + " is " + value;
        }
        AnnotationDefaultCompleter(MethodSymbol sym, Attribute value) {
            this.sym = sym;
            this.value = value;
        }
        // implement Annotate.Annotator.enterAnnotation()
        public void enterAnnotation() {
            JavaFileObject previousClassFile = currentClassFile;
            try {
                currentClassFile = classFile;
                sym.defaultValue = deproxy(sym.type.getReturnType(), value);
            } finally {
                currentClassFile = previousClassFile;
            }
        }
    }

  

 

 

 

 

 

 

 

 

 

 

 

  

以上是关于Annotate类的主要内容,如果未能解决你的问题,请参考以下文章

IDEA的Annotate或Annotate with Git Blame

IDEA的Annotate或Annotate with Git Blame

Matplotlib箭头风格与标注文本连接|plt.annotate函数与connectionstyle参数的使用与案例代码

响应实体类

如何通过单击片段内的线性布局从片段类开始新活动?下面是我的代码,但这不起作用

Django:替代使用 annotate(Count()) 来提高速度