理解'元素'和' TypeMirror'在`UnmodifiableCollection`中

时间:2015-07-11 12:36:57

标签: java reflection java-8

如果我们考虑任何泛型类型,例如,下面给出的代码取自java.util.Collections

static class UnmodifiableCollection<E> implements Collection<E>, Serializable {
        private static final long serialVersionUID = 1820017752578914078L;

        final Collection<? extends E> c;

        UnmodifiableCollection(Collection<? extends E> c) {
            if (c==null)
                throw new NullPointerException();
            this.c = c;
        }

        public int size()                   {return c.size();}
        public boolean isEmpty()            {return c.isEmpty();}
        public boolean contains(Object o)   {return c.contains(o);}
        public Object[] toArray()           {return c.toArray();}
        public <T> T[] toArray(T[] a)       {return c.toArray(a);}
        public String toString()            {return c.toString();}

        public Iterator<E> iterator() {
           ...
        }

        public boolean add(E e) {
            throw new UnsupportedOperationException();
        }
        public boolean remove(Object o) {
            throw new UnsupportedOperationException();
        }

        public boolean containsAll(Collection<?> coll) {
            return c.containsAll(coll);
        }
        public boolean addAll(Collection<? extends E> coll) {
            throw new UnsupportedOperationException();
        }
        public boolean removeAll(Collection<?> coll) {
            throw new UnsupportedOperationException();
        }
        public boolean retainAll(Collection<?> coll) {
            throw new UnsupportedOperationException();
        }
        public void clear() {
            throw new UnsupportedOperationException();
        }
        @Override
        public boolean removeIf(Predicate<? super E> filter) {
             throw new UnsupportedOperationException();
        } 
    }

我想明白,

1) 如何为上述代码识别TypeElement包的ExecutableElementVariableElementAnnotationMirrorjavax.lang.model.element个角色?

2) 如何为上述代码识别DeclaredType包的WildcardTypeExecutableTypeMirrorTypejavax.lang.model.type个角色?

3)AnnotationMirrorelement中提到的基于镜像的反射系统吗?

注意:type&amp; Object.getClass()包是元级别工具,与传统反射系统(Type.class方法或FLT_MAX文字)不同

1 个答案:

答案 0 :(得分:0)

*TypeAnnotationMirror类都是javax.lang.model.element包的一部分,在构建自定义注释处理器时使用它。这个基类是javax.annotation.processing.Processor,这是一个开始寻求帮助的好地方。

基本上,所有这些类型都代表调用注释处理器的编译器的抽象语法树中的节点。它们是只读的,这意味着您只能添加自定义错误,但无法修改源代码。