langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/AnnoConstruct.java
author mcimadamore
Thu, 09 Mar 2017 12:08:02 +0000
changeset 44186 fe848a208b20
parent 34752 9c262a013456
permissions -rw-r--r--
8176265: Method overload resolution on a covariant base type doesn't work in 9 Summary: Some type mappings should not be recursive Reviewed-by: vromero, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
     1
/*
28341
8f015bb3a75f 8006469: Cleanup reflective access of java.lang.annotation.Repeatable
darcy
parents: 26266
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
     4
 *
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    10
 *
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    15
 * accompanied this code).
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    16
 *
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    20
 *
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    23
 * questions.
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    24
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 28341
diff changeset
    25
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    26
package com.sun.tools.javac.code;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    27
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    28
import java.lang.annotation.Annotation;
21485
2759750a8cdf 8026855: AnnoConstruct.getAnnotationsByType includes inherited indirectly present annotations even when containee type is not inheritable
jfranck
parents: 21007
diff changeset
    29
import java.lang.annotation.Inherited;
28341
8f015bb3a75f 8006469: Cleanup reflective access of java.lang.annotation.Repeatable
darcy
parents: 26266
diff changeset
    30
import java.lang.annotation.Repeatable;
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    31
import java.lang.reflect.InvocationTargetException;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    32
import java.lang.reflect.Method;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    33
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    34
import javax.lang.model.AnnotatedConstruct;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    35
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    36
import com.sun.tools.javac.model.AnnotationProxyMaker;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    37
import com.sun.tools.javac.util.DefinedBy;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    38
import com.sun.tools.javac.util.DefinedBy.Api;
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    39
import com.sun.tools.javac.util.List;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    40
import com.sun.tools.javac.util.ListBuffer;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    41
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    42
/**
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    43
 * Common super type for annotated constructs such as Types and Symbols.
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    44
 *
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    45
 * This class should *not* contain any fields since it would have a significant
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    46
 * impact on the javac memory footprint.
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    47
 *
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    48
 * <p><b>This is NOT part of any supported API.
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    49
 * If you write code that depends on this, you do so at your own
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    50
 * risk.  This code and its internal interfaces are subject to change
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    51
 * or deletion without notice.</b></p>
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    52
 */
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    53
public abstract class AnnoConstruct implements AnnotatedConstruct {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    54
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    55
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    56
    // Override to enforce a narrower return type.
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    57
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    58
    public abstract List<? extends Attribute.Compound> getAnnotationMirrors();
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    59
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    60
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    61
    // This method is part of the javax.lang.model API, do not use this in javac code.
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    62
    protected <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    63
        String name = annoType.getName();
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    64
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    65
        for (Attribute.Compound anno : getAnnotationMirrors()) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    66
            if (name.equals(anno.type.tsym.flatName().toString()))
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    67
                return anno;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    68
        }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    69
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    70
        return null;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    71
    }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    72
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    73
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    74
    @SuppressWarnings("unchecked")
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    75
    protected <A extends Annotation> A[] getInheritedAnnotations(Class<A> annoType) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    76
        return (A[]) java.lang.reflect.Array.newInstance(annoType, 0);  // annoType is the Class for A
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    77
    }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    78
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    79
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    80
    // This method is part of the javax.lang.model API, do not use this in javac code.
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    81
    @DefinedBy(Api.LANGUAGE_MODEL)
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    82
    public <A extends Annotation> A[] getAnnotationsByType(Class<A> annoType) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    83
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    84
        if (!annoType.isAnnotation())
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    85
            throw new IllegalArgumentException("Not an annotation type: "
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    86
                                               + annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    87
        // If annoType does not declare a container this is equivalent to wrapping
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    88
        // getAnnotation(...) in an array.
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    89
        Class <? extends Annotation> containerType = getContainer(annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    90
        if (containerType == null) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    91
            A res = getAnnotation(annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    92
            int size = res == null ? 0 : 1;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    93
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    94
            @SuppressWarnings("unchecked") // annoType is the Class for A
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    95
            A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    96
            if (res != null)
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    97
                arr[0] = res;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    98
            return arr;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
    99
        }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   100
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   101
        // So we have a containing type
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   102
        String annoTypeName = annoType.getName();
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   103
        String containerTypeName = containerType.getName();
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   104
        int directIndex = -1, containerIndex = -1;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   105
        Attribute.Compound direct = null, container = null;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   106
        // Find directly (explicit or implicit) present annotations
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   107
        int index = -1;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   108
        for (Attribute.Compound attribute : getAnnotationMirrors()) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   109
            index++;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   110
            if (attribute.type.tsym.flatName().contentEquals(annoTypeName)) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   111
                directIndex = index;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   112
                direct = attribute;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   113
            } else if(containerTypeName != null &&
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   114
                      attribute.type.tsym.flatName().contentEquals(containerTypeName)) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   115
                containerIndex = index;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   116
                container = attribute;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   117
            }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   118
        }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   119
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   120
        // Deal with inherited annotations
21485
2759750a8cdf 8026855: AnnoConstruct.getAnnotationsByType includes inherited indirectly present annotations even when containee type is not inheritable
jfranck
parents: 21007
diff changeset
   121
        if (direct == null && container == null &&
2759750a8cdf 8026855: AnnoConstruct.getAnnotationsByType includes inherited indirectly present annotations even when containee type is not inheritable
jfranck
parents: 21007
diff changeset
   122
                annoType.isAnnotationPresent(Inherited.class))
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   123
            return getInheritedAnnotations(annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   124
21487
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   125
        Attribute.Compound[] contained = unpackContained(container);
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   126
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   127
        // In case of an empty legacy container we might need to look for
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   128
        // inherited annos as well
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   129
        if (direct == null && contained.length == 0 &&
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   130
                annoType.isAnnotationPresent(Inherited.class))
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   131
            return getInheritedAnnotations(annoType);
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   132
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   133
        int size = (direct == null ? 0 : 1) + contained.length;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   134
        @SuppressWarnings("unchecked") // annoType is the Class for A
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   135
        A[] arr = (A[])java.lang.reflect.Array.newInstance(annoType, size);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   136
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   137
        // if direct && container, which is first?
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   138
        int insert = -1;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   139
        int length = arr.length;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   140
        if (directIndex >= 0 && containerIndex >= 0) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   141
            if (directIndex < containerIndex) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   142
                arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   143
                insert = 1;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   144
            } else {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   145
                arr[arr.length - 1] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   146
                insert = 0;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   147
                length--;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   148
            }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   149
        } else if (directIndex >= 0) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   150
            arr[0] = AnnotationProxyMaker.generateAnnotation(direct, annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   151
            return arr;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   152
        } else {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   153
            // Only container
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   154
            insert = 0;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   155
        }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   156
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   157
        for (int i = 0; i + insert < length; i++)
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   158
            arr[insert + i] = AnnotationProxyMaker.generateAnnotation(contained[i], annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   159
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   160
        return arr;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   161
    }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   162
21487
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   163
    private Attribute.Compound[] unpackContained(Attribute.Compound container) {
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   164
        // Pack them in an array
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   165
        Attribute[] contained0 = null;
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   166
        if (container != null)
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   167
            contained0 = unpackAttributes(container);
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   168
        ListBuffer<Attribute.Compound> compounds = new ListBuffer<>();
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   169
        if (contained0 != null) {
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   170
            for (Attribute a : contained0)
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   171
                if (a instanceof Attribute.Compound)
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   172
                    compounds = compounds.append((Attribute.Compound)a);
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   173
        }
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   174
        return compounds.toArray(new Attribute.Compound[compounds.size()]);
c00623074ad0 8026857: AnnoConstruct.getAnnotationsByType does not search supertype for inherited annotations if @SomeContainer({}) is present
jfranck
parents: 21485
diff changeset
   175
    }
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   176
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   177
    // This method is part of the javax.lang.model API, do not use this in javac code.
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   178
    @DefinedBy(Api.LANGUAGE_MODEL)
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   179
    public <A extends Annotation> A getAnnotation(Class<A> annoType) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   180
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   181
        if (!annoType.isAnnotation())
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   182
            throw new IllegalArgumentException("Not an annotation type: " + annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   183
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   184
        Attribute.Compound c = getAttribute(annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   185
        return c == null ? null : AnnotationProxyMaker.generateAnnotation(c, annoType);
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   186
    }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   187
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   188
    // Helper to getAnnotationsByType
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   189
    private static Class<? extends Annotation> getContainer(Class<? extends Annotation> annoType) {
28341
8f015bb3a75f 8006469: Cleanup reflective access of java.lang.annotation.Repeatable
darcy
parents: 26266
diff changeset
   190
        Repeatable repeatable = annoType.getAnnotation(Repeatable.class);
8f015bb3a75f 8006469: Cleanup reflective access of java.lang.annotation.Repeatable
darcy
parents: 26266
diff changeset
   191
        return (repeatable == null) ? null : repeatable.value();
21007
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   192
    }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   193
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   194
    // Helper to getAnnotationsByType
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   195
    private static Attribute[] unpackAttributes(Attribute.Compound container) {
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   196
        // We now have an instance of the container,
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   197
        // unpack it returning an instance of the
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   198
        // contained type or null
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   199
        return ((Attribute.Array)container.member(container.type.tsym.name.table.names.value)).values;
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   200
    }
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   201
484c9d902861 8021237: clean up JavacAnnotatedConstruct
alundblad
parents:
diff changeset
   202
}