langtools/src/share/classes/com/sun/tools/javac/model/JavacAnnoConstructs.java
author jlahoda
Wed, 09 Oct 2013 13:09:31 +0200
changeset 21006 534673718919
parent 20249 93f8eae31092
permissions -rw-r--r--
8025087: Annotation processing api returns default modifier for interface static method Summary: ClassReader must not set Flags.DEFAULT for interface static methods Reviewed-by: vromero, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
     1
/*
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
     4
 *
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    10
 *
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    15
 * accompanied this code).
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    16
 *
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    20
 *
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    23
 * questions.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    24
 */
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    25
package com.sun.tools.javac.model;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    26
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    27
import java.lang.annotation.Annotation;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    28
import java.lang.annotation.Inherited;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    29
import java.lang.reflect.InvocationTargetException;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    30
import java.lang.reflect.Method;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    31
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    32
import com.sun.tools.javac.code.Attribute;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    33
import com.sun.tools.javac.code.Kinds;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    34
import com.sun.tools.javac.code.Symbol;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    35
import com.sun.tools.javac.code.Symbol.ClassSymbol;
17278
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    36
import com.sun.tools.javac.code.Symbol.TypeVariableSymbol;
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    37
import com.sun.tools.javac.code.TargetType;
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    38
import com.sun.tools.javac.code.Type;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    39
import com.sun.tools.javac.code.Type.AnnotatedType;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    40
import com.sun.tools.javac.util.ListBuffer;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    41
import static com.sun.tools.javac.code.TypeTag.CLASS;
17278
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    42
import com.sun.tools.javac.util.List;
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    43
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    44
/**
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    45
 * Utility methods for operating on annotated constructs.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    46
 *
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    47
 * <p><b>This is NOT part of any supported API.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    48
 * If you write code that depends on this, you do so at your own
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    49
 * risk.  This code and its internal interfaces are subject to change
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    50
 * or deletion without notice.</b></p>
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    51
 */
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    52
public class JavacAnnoConstructs {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    53
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    54
    // <editor-fold defaultstate="collapsed" desc="Symbols">
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    55
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    56
    /**
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    57
     * An internal-use utility that creates a runtime view of an
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    58
     * annotation. This is the implementation of
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    59
     * Element.getAnnotation(Class).
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    60
     */
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    61
    public static <A extends Annotation> A getAnnotation(Symbol annotated,
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    62
                                                         Class<A> annoType) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    63
        if (!annoType.isAnnotation())
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    64
            throw new IllegalArgumentException("Not an annotation type: "
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    65
                                               + annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    66
        Attribute.Compound c;
17278
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    67
        if (annotated.kind == Kinds.TYP &&
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    68
                annotated instanceof ClassSymbol) {
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    69
            c = getAttributeOnClass((ClassSymbol)annotated, annoType);
17278
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    70
        } else if (annotated.kind == Kinds.TYP &&
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    71
                   annotated instanceof TypeVariableSymbol) {
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    72
            c = getAttributeOnTypeVariable((TypeVariableSymbol)annotated, annoType);
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    73
        } else {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    74
            c = getAttribute(annotated, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    75
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    76
        return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    77
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    78
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    79
    // Helper to getAnnotation[s]
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    80
    private static <A extends Annotation> Attribute.Compound getAttribute(Symbol annotated,
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    81
                                                                          Class<A> annoType) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    82
        String name = annoType.getName();
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    83
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    84
        for (Attribute.Compound anno : annotated.getRawAttributes()) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    85
            if (name.equals(anno.type.tsym.flatName().toString()))
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    86
                return anno;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    87
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    88
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    89
        return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    90
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    91
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
    92
    // Helper to getAnnotation[s]
17278
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    93
    private static <A extends Annotation> Attribute.Compound
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    94
            getAttributeOnTypeVariable(TypeVariableSymbol annotated, Class<A> annoType) {
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    95
        String name = annoType.getName();
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    96
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    97
        // Declaration annotations on type variables are stored in type attributes
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    98
        // on the owner of the TypeVariableSymbol
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
    99
        List<Attribute.Compound> res = List.nil();
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   100
        List<Attribute.TypeCompound> candidates = annotated.owner.getRawTypeAttributes();
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   101
        for (Attribute.TypeCompound anno : candidates)
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   102
            if (anno.position.type == TargetType.CLASS_TYPE_PARAMETER ||
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   103
                    anno.position.type == TargetType.METHOD_TYPE_PARAMETER)
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   104
                if (name.equals(anno.type.tsym.flatName().toString()))
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   105
                    return anno;
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   106
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   107
        return null;
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   108
    }
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   109
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   110
    // Helper to getAnnotation[s]
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   111
    private static <A extends Annotation> Attribute.Compound getAttributeOnClass(
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   112
            ClassSymbol annotated,
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   113
            final Class<A> annoType)
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   114
    {
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   115
        boolean inherited = annoType.isAnnotationPresent(Inherited.class);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   116
        Attribute.Compound result = null;
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   117
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   118
        result = getAttribute(annotated, annoType);
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   119
        if (result != null || !inherited)
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   120
            return result;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   121
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   122
        while ((annotated = nextSupertypeToSearch(annotated)) != null) {
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   123
            result = getAttribute(annotated, annoType);
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   124
            if (result != null)
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   125
                return result;
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   126
        }
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   127
        return null; // no more supertypes to search
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   128
    }
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   129
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   130
    /**
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   131
     * Returns the next type to search for inherited annotations or {@code null}
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   132
     * if the next type can't be found.
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   133
     */
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   134
    private static ClassSymbol nextSupertypeToSearch(ClassSymbol annotated) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   135
        if (annotated.name == annotated.name.table.names.java_lang_Object)
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   136
            return null;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   137
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   138
        Type sup = annotated.getSuperclass();
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   139
        if (!sup.hasTag(CLASS) || sup.isErroneous())
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   140
            return null;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   141
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   142
        return (ClassSymbol) sup.tsym;
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   143
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   144
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   145
    /**
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   146
     * An internal-use utility that creates a runtime view of
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   147
     * annotations. This is the implementation of
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   148
     * Element.getAnnotations(Class).
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   149
     */
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   150
    public static <A extends Annotation> A[] getAnnotationsByType(Symbol annotated,
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   151
            Class<A> annoType)
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   152
    {
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   153
        if (!annoType.isAnnotation())
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   154
            throw new IllegalArgumentException("Not an annotation type: "
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   155
                                               + annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   156
        // If annoType does not declare a container this is equivalent to wrapping
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   157
        // getAnnotation(...) in an array.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   158
        Class <? extends Annotation> containerType = getContainer(annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   159
        if (containerType == null) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   160
            A res = getAnnotation(annotated, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   161
            int size;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   162
            if (res == null) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   163
                size = 0;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   164
            } else {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   165
                size = 1;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   166
            }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   167
            @SuppressWarnings("unchecked") // annoType is the Class for A
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   168
            A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   169
            if (res != null)
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   170
                arr[0] = res;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   171
            return arr;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   172
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   173
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   174
        // So we have a containing type
19248
46cb3cd6023b 8009367: Wrong kind of name used in comparison in javax.lang.model code for repeatable annotations
jfranck
parents: 19126
diff changeset
   175
        String annoTypeName = annoType.getName();
46cb3cd6023b 8009367: Wrong kind of name used in comparison in javax.lang.model code for repeatable annotations
jfranck
parents: 19126
diff changeset
   176
        String containerTypeName = containerType.getName();
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   177
        int directIndex = -1, containerIndex = -1;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   178
        Attribute.Compound direct = null, container = null;
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   179
        // Find directly (explicit or implicit) present annotations
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   180
        int index = -1;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   181
        for (List<Attribute.Compound> list = annotated.getAnnotationMirrors();
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   182
                !list.isEmpty();
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   183
                list = list.tail) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   184
            Attribute.Compound attribute = list.head;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   185
            index++;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   186
            if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   187
                directIndex = index;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   188
                direct = attribute;
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   189
            } else if(containerTypeName != null &&
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   190
                      attribute.type.tsym.flatName().contentEquals(containerTypeName)) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   191
                containerIndex = index;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   192
                container = attribute;
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   193
            }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   194
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   195
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   196
        // Deal with inherited annotations
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   197
        if (direct == null && container == null) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   198
            if (annotated.kind == Kinds.TYP &&
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   199
                    (annotated instanceof ClassSymbol)) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   200
                ClassSymbol s = nextSupertypeToSearch((ClassSymbol)annotated);
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   201
                if (s != null)
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   202
                    return getAnnotationsByType(s, annoType);
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   203
            }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   204
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   205
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   206
        // Pack them in an array
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   207
        Attribute[] contained0 = null;
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   208
        if (container != null)
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   209
            contained0 = unpackAttributes(container);
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19248
diff changeset
   210
        ListBuffer<Attribute.Compound> compounds = new ListBuffer<>();
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   211
        if (contained0 != null) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   212
            for (Attribute a : contained0)
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   213
                if (a instanceof Attribute.Compound)
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   214
                    compounds = compounds.append((Attribute.Compound)a);
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   215
        }
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   216
        Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[compounds.size()]);
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   217
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   218
        int size = (direct == null ? 0 : 1) + contained.length;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   219
        @SuppressWarnings("unchecked") // annoType is the Class for A
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   220
        A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   221
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   222
        // if direct && container, which is first?
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   223
        int insert = -1;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   224
        int length = arr.length;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   225
        if (directIndex >= 0 && containerIndex >= 0) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   226
            if (directIndex < containerIndex) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   227
                arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   228
                insert = 1;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   229
            } else {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   230
                arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   231
                insert = 0;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   232
                length--;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   233
            }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   234
        } else if (directIndex >= 0) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   235
            arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   236
            return arr;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   237
        } else {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   238
            // Only container
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   239
            insert = 0;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   240
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   241
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   242
        for (int i = 0; i + insert < length; i++)
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   243
            arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   244
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   245
        return arr;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   246
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   247
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   248
    // </editor-fold>
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   249
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   250
    // <editor-fold defaultstate="collapsed" desc="Types">
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   251
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   252
    /**
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   253
     * An internal-use utility that creates a runtime view of an
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   254
     * annotation. This is the implementation of
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   255
     * TypeMirror.getAnnotation(Class).
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   256
     */
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   257
    public static <A extends Annotation> A getAnnotation(AnnotatedType annotated, Class<A> annoType) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   258
        if (!annoType.isAnnotation())
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   259
            throw new IllegalArgumentException("Not an annotation type: "
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   260
                                               + annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   261
        Attribute.Compound c = getAttribute(annotated, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   262
        return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   263
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   264
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   265
    // Helper to getAnnotation[s]
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   266
    private static <A extends Annotation> Attribute.Compound getAttribute(Type annotated,
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   267
                                                                          Class<A> annoType) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   268
        String name = annoType.getName();
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   269
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   270
        for (Attribute.Compound anno : annotated.getAnnotationMirrors()) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   271
            if (name.equals(anno.type.tsym.flatName().toString()))
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   272
                return anno;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   273
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   274
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   275
        return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   276
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   277
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   278
    /**
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   279
     * An internal-use utility that creates a runtime view of
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   280
     * annotations. This is the implementation of
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   281
     * TypeMirror.getAnnotationsByType(Class).
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   282
     */
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   283
    public static <A extends Annotation> A[] getAnnotationsByType(AnnotatedType annotated, Class<A> annoType) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   284
        if (!annoType.isAnnotation())
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   285
            throw new IllegalArgumentException("Not an annotation type: "
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   286
                                               + annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   287
        // If annoType does not declare a container this is equivalent to wrapping
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   288
        // getAnnotation(...) in an array.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   289
        Class <? extends Annotation> containerType = getContainer(annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   290
        if (containerType == null) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   291
            A res = getAnnotation(annotated, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   292
            int size;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   293
            if (res == null) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   294
                size = 0;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   295
            } else {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   296
                size = 1;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   297
            }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   298
            @SuppressWarnings("unchecked") // annoType is the Class for A
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   299
            A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   300
            if (res != null)
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   301
                arr[0] = res;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   302
            return arr;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   303
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   304
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   305
        // So we have a containing type
19248
46cb3cd6023b 8009367: Wrong kind of name used in comparison in javax.lang.model code for repeatable annotations
jfranck
parents: 19126
diff changeset
   306
        String annoTypeName = annoType.getName();
46cb3cd6023b 8009367: Wrong kind of name used in comparison in javax.lang.model code for repeatable annotations
jfranck
parents: 19126
diff changeset
   307
        String containerTypeName = containerType.getName();
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   308
        int directIndex = -1, containerIndex = -1;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   309
        Attribute.Compound direct = null, container = null;
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   310
        // Find directly (explicit or implicit) present annotations
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   311
        int index = -1;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   312
        for (List<? extends Attribute.Compound> list = annotated.getAnnotationMirrors();
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   313
                !list.isEmpty();
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   314
                list = list.tail) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   315
            Attribute.Compound attribute = list.head;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   316
            index++;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   317
            if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   318
                directIndex = index;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   319
                direct = attribute;
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   320
            } else if(containerTypeName != null &&
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   321
                      attribute.type.tsym.flatName().contentEquals(containerTypeName)) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   322
                containerIndex = index;
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   323
                container = attribute;
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   324
            }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   325
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   326
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   327
        // Pack them in an array
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   328
        Attribute[] contained0 = null;
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   329
        if (container != null)
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   330
            contained0 = unpackAttributes(container);
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19248
diff changeset
   331
        ListBuffer<Attribute.Compound> compounds = new ListBuffer<>();
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   332
        if (contained0 != null) {
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   333
            for (Attribute a : contained0)
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   334
                if (a instanceof Attribute.Compound)
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   335
                    compounds = compounds.append((Attribute.Compound)a);
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   336
        }
19126
20123a84b35a 8007961: javax.lang.model tests for repeating annotations fail in getAnnotationsByType
jfranck
parents: 17278
diff changeset
   337
        Attribute.Compound[] contained = compounds.toArray(new Attribute.Compound[compounds.size()]);
16557
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   338
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   339
        int size = (direct == null ? 0 : 1) + contained.length;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   340
        @SuppressWarnings("unchecked") // annoType is the Class for A
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   341
        A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   342
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   343
        // if direct && container, which is first?
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   344
        int insert = -1;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   345
        int length = arr.length;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   346
        if (directIndex >= 0 && containerIndex >= 0) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   347
            if (directIndex < containerIndex) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   348
                arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   349
                insert = 1;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   350
            } else {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   351
                arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   352
                insert = 0;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   353
                length--;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   354
            }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   355
        } else if (directIndex >= 0) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   356
            arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   357
            return arr;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   358
        } else {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   359
            // Only container
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   360
            insert = 0;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   361
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   362
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   363
        for (int i = 0; i + insert < length; i++)
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   364
            arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   365
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   366
        return arr;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   367
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   368
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   369
    // </editor-fold>
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   370
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   371
    // <editor-fold defaultstate="collapsed" desc="Container support">
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   372
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   373
    // Needed to unpack the runtime view of containing annotations
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   374
    private static final Class<? extends Annotation> REPEATABLE_CLASS = initRepeatable();
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   375
    private static final Method VALUE_ELEMENT_METHOD = initValueElementMethod();
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   376
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   377
    private static Class<? extends Annotation> initRepeatable() {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   378
        try {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   379
            // Repeatable will not be available when bootstrapping on
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   380
            // JDK 7 so use a reflective lookup instead of a class
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   381
            // literal for Repeatable.class.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   382
            return Class.forName("java.lang.annotation.Repeatable").asSubclass(Annotation.class);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   383
        } catch (ClassNotFoundException e) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   384
            return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   385
        } catch (SecurityException e) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   386
            return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   387
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   388
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   389
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   390
    private static Method initValueElementMethod() {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   391
        if (REPEATABLE_CLASS == null)
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   392
            return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   393
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   394
        Method m = null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   395
        try {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   396
            m = REPEATABLE_CLASS.getMethod("value");
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   397
            if (m != null)
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   398
                m.setAccessible(true);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   399
            return m;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   400
        } catch (NoSuchMethodException e) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   401
            return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   402
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   403
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   404
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   405
    // Helper to getAnnotations
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   406
    private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   407
        // Since we can not refer to java.lang.annotation.Repeatable until we are
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   408
        // bootstrapping with java 8 we need to get the Repeatable annotation using
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   409
        // reflective invocations instead of just using its type and element method.
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   410
        if (REPEATABLE_CLASS != null &&
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   411
            VALUE_ELEMENT_METHOD != null) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   412
            // Get the Repeatable instance on the annotations declaration
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   413
            Annotation repeatable = (Annotation)annoType.getAnnotation(REPEATABLE_CLASS);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   414
            if (repeatable != null) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   415
                try {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   416
                    // Get the value element, it should be a class
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   417
                    // indicating the containing annotation type
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   418
                    @SuppressWarnings("unchecked")
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   419
                    Class<? extends Annotation> containerType = (Class)VALUE_ELEMENT_METHOD.invoke(repeatable);
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   420
                    if (containerType == null)
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   421
                        return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   422
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   423
                    return containerType;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   424
                } catch (ClassCastException e) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   425
                    return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   426
                } catch (IllegalAccessException e) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   427
                    return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   428
                } catch (InvocationTargetException e ) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   429
                    return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   430
                }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   431
            }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   432
        }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   433
        return null;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   434
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   435
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   436
    // Helper to getAnnotations
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   437
    private static Attribute[] unpackAttributes(Attribute.Compound container) {
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   438
        // We now have an instance of the container,
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   439
        // unpack it returning an instance of the
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   440
        // contained type or null
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   441
        return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values;
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   442
    }
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   443
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   444
    // </editor-fold>
67a3ae363f03 8007803: Implement javax.lang.model API for Type Annotations
jjg
parents:
diff changeset
   445
}