jdk/src/java.base/share/classes/sun/reflect/annotation/TypeAnnotationParser.java
author darcy
Tue, 23 May 2017 14:34:45 -0700
changeset 45332 9c9e51c44638
parent 39495 80833370f1b8
permissions -rw-r--r--
8074977: Constructor.getAnnotatedParameterTypes returns wrong value Summary: Additional comments from plevart and forax Reviewed-by: mchung, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
     1
/*
45332
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
     2
 * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
     4
 *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    10
 *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    15
 * accompanied this code).
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    16
 *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    20
 *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    23
 * questions.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    24
 */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    25
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    26
package sun.reflect.annotation;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    27
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    28
import java.lang.annotation.*;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    29
import java.lang.reflect.*;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    30
import java.nio.ByteBuffer;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    31
import java.nio.BufferUnderflowException;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    32
import java.util.ArrayList;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    33
import java.util.Arrays;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    34
import java.util.List;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    35
import java.util.LinkedHashMap;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    36
import java.util.Map;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31061
diff changeset
    37
import jdk.internal.misc.SharedSecrets;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31061
diff changeset
    38
import jdk.internal.misc.JavaLangAccess;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34708
diff changeset
    39
import jdk.internal.reflect.ConstantPool;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    40
import static sun.reflect.annotation.TypeAnnotation.*;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    41
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    42
/**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    43
 * TypeAnnotationParser implements the logic needed to parse
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    44
 * TypeAnnotations from an array of bytes.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    45
 */
21622
643cd3c6f72d 8027796: Refactor Core Reflection for Type Annotations
jfranck
parents: 21361
diff changeset
    46
public final class TypeAnnotationParser {
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    47
    private static final TypeAnnotation[] EMPTY_TYPE_ANNOTATION_ARRAY = new TypeAnnotation[0];
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    48
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    49
    /**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    50
     * Build an AnnotatedType from the parameters supplied.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    51
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    52
     * This method and {@code buildAnnotatedTypes} are probably
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    53
     * the entry points you are looking for.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    54
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    55
     * @param rawAnnotations the byte[] encoding of all type annotations on this declaration
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    56
     * @param cp the ConstantPool needed to parse the embedded Annotation
21361
6e4ef4e0097f 8023651: j.l.r.Constructor.getAnnotatedReceiverType() and j.l.r.Constructor.getAnnotatedReturnType() for inner classes return incorrect result
jfranck
parents: 20496
diff changeset
    57
     * @param decl the declaration this type annotation is on
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    58
     * @param container the Class this type annotation is on (may be the same as decl)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    59
     * @param type the type the AnnotatedType corresponds to
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    60
     * @param filter the type annotation targets included in this AnnotatedType
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    61
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    62
    public static AnnotatedType buildAnnotatedType(byte[] rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    63
            ConstantPool cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    64
            AnnotatedElement decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    65
            Class<?> container,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    66
            Type type,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    67
            TypeAnnotationTarget filter) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    68
        TypeAnnotation[] tas = parseTypeAnnotations(rawAnnotations,
34708
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
    69
                cp, decl, container);
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
    70
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    71
        List<TypeAnnotation> l = new ArrayList<>(tas.length);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    72
        for (TypeAnnotation t : tas) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    73
            TypeAnnotationTargetInfo ti = t.getTargetInfo();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    74
            if (ti.getTarget() == filter)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    75
                l.add(t);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    76
        }
24493
9a793dfeb09d 8038994: AnnotatedType.getType() of a TypeVariable boundary without annotations return null
jfranck
parents: 21845
diff changeset
    77
        TypeAnnotation[] typeAnnotations = l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    78
        return AnnotatedTypeFactory.buildAnnotatedType(type,
34708
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
    79
                AnnotatedTypeFactory.nestingForType(type, LocationInfo.BASE_LOCATION),
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
    80
                typeAnnotations,
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
    81
                typeAnnotations,
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
    82
                decl);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    83
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    84
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    85
    /**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    86
     * Build an array of AnnotatedTypes from the parameters supplied.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    87
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    88
     * This method and {@code buildAnnotatedType} are probably
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    89
     * the entry points you are looking for.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    90
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    91
     * @param rawAnnotations the byte[] encoding of all type annotations on this declaration
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    92
     * @param cp the ConstantPool needed to parse the embedded Annotation
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    93
     * @param decl the declaration this type annotation is on
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    94
     * @param container the Class this type annotation is on (may be the same as decl)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    95
     * @param types the Types the AnnotatedTypes corresponds to
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    96
     * @param filter the type annotation targets that included in this AnnotatedType
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    97
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    98
    public static AnnotatedType[] buildAnnotatedTypes(byte[] rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
    99
            ConstantPool cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   100
            AnnotatedElement decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   101
            Class<?> container,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   102
            Type[] types,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   103
            TypeAnnotationTarget filter) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   104
        int size = types.length;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   105
        AnnotatedType[] result = new AnnotatedType[size];
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   106
        Arrays.fill(result, AnnotatedTypeFactory.EMPTY_ANNOTATED_TYPE);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   107
        @SuppressWarnings("rawtypes")
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   108
        ArrayList[] l = new ArrayList[size]; // array of ArrayList<TypeAnnotation>
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   109
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   110
        TypeAnnotation[] tas = parseTypeAnnotations(rawAnnotations,
34708
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
   111
                cp, decl, container);
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
   112
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   113
        for (TypeAnnotation t : tas) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   114
            TypeAnnotationTargetInfo ti = t.getTargetInfo();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   115
            if (ti.getTarget() == filter) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   116
                int pos = ti.getCount();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   117
                if (l[pos] == null) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   118
                    ArrayList<TypeAnnotation> tmp = new ArrayList<>(tas.length);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   119
                    l[pos] = tmp;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   120
                }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   121
                @SuppressWarnings("unchecked")
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   122
                ArrayList<TypeAnnotation> tmp = l[pos];
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   123
                tmp.add(t);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   124
            }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   125
        }
45332
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   126
        // If a constructor has a mandated outer this, that parameter
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   127
        // has no annotations and the annotations to parameter mapping
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   128
        // should be offset by 1.
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   129
        boolean offset = false;
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   130
        if (decl instanceof Constructor) {
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   131
            Constructor<?> ctor = (Constructor<?>) decl;
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   132
            Class<?> declaringClass = ctor.getDeclaringClass();
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   133
            if (!declaringClass.isEnum() &&
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   134
                (declaringClass.isMemberClass() &&
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   135
                 (declaringClass.getModifiers() & Modifier.STATIC) == 0) ) {
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   136
                offset = true;
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   137
            }
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   138
        }
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   139
        for (int i = 0; i < size; i++) {
45332
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   140
            ArrayList<TypeAnnotation> list;
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   141
            if (offset) {
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   142
                @SuppressWarnings("unchecked")
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   143
                ArrayList<TypeAnnotation> tmp = (i == 0) ? null : l[i - 1];
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   144
                list = tmp;
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   145
            } else {
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   146
                @SuppressWarnings("unchecked")
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   147
                ArrayList<TypeAnnotation> tmp = l[i];
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   148
                list = tmp;
9c9e51c44638 8074977: Constructor.getAnnotatedParameterTypes returns wrong value
darcy
parents: 39495
diff changeset
   149
            }
20496
430c1bccd943 8021398: j.l.r.Parameter.getAnnotatedType().getType() for not annotated use of type returns null
emc
parents: 20481
diff changeset
   150
            TypeAnnotation[] typeAnnotations;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   151
            if (list != null) {
20496
430c1bccd943 8021398: j.l.r.Parameter.getAnnotatedType().getType() for not annotated use of type returns null
emc
parents: 20481
diff changeset
   152
                typeAnnotations = list.toArray(new TypeAnnotation[list.size()]);
430c1bccd943 8021398: j.l.r.Parameter.getAnnotatedType().getType() for not annotated use of type returns null
emc
parents: 20481
diff changeset
   153
            } else {
430c1bccd943 8021398: j.l.r.Parameter.getAnnotatedType().getType() for not annotated use of type returns null
emc
parents: 20481
diff changeset
   154
                typeAnnotations = EMPTY_TYPE_ANNOTATION_ARRAY;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   155
            }
20496
430c1bccd943 8021398: j.l.r.Parameter.getAnnotatedType().getType() for not annotated use of type returns null
emc
parents: 20481
diff changeset
   156
            result[i] = AnnotatedTypeFactory.buildAnnotatedType(types[i],
34708
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
   157
                    AnnotatedTypeFactory.nestingForType(types[i], LocationInfo.BASE_LOCATION),
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
   158
                    typeAnnotations,
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
   159
                    typeAnnotations,
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
   160
                    decl);
20496
430c1bccd943 8021398: j.l.r.Parameter.getAnnotatedType().getType() for not annotated use of type returns null
emc
parents: 20481
diff changeset
   161
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   162
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   163
        return result;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   164
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   165
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   166
    // Class helpers
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   167
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   168
    /**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   169
     * Build an AnnotatedType for the class decl's supertype.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   170
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   171
     * @param rawAnnotations the byte[] encoding of all type annotations on this declaration
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   172
     * @param cp the ConstantPool needed to parse the embedded Annotation
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   173
     * @param decl the Class which annotated supertype is being built
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   174
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   175
    public static AnnotatedType buildAnnotatedSuperclass(byte[] rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   176
            ConstantPool cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   177
            Class<?> decl) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   178
        Type supertype = decl.getGenericSuperclass();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   179
        if (supertype == null)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   180
            return AnnotatedTypeFactory.EMPTY_ANNOTATED_TYPE;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   181
        return buildAnnotatedType(rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   182
                                  cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   183
                                  decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   184
                                  decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   185
                                  supertype,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   186
                                  TypeAnnotationTarget.CLASS_EXTENDS);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   187
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   188
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   189
    /**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   190
     * Build an array of AnnotatedTypes for the class decl's implemented
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   191
     * interfaces.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   192
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   193
     * @param rawAnnotations the byte[] encoding of all type annotations on this declaration
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   194
     * @param cp the ConstantPool needed to parse the embedded Annotation
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   195
     * @param decl the Class whose annotated implemented interfaces is being built
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   196
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   197
    public static AnnotatedType[] buildAnnotatedInterfaces(byte[] rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   198
            ConstantPool cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   199
            Class<?> decl) {
20481
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 17450
diff changeset
   200
        if (decl == Object.class ||
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 17450
diff changeset
   201
                decl.isArray() ||
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 17450
diff changeset
   202
                decl.isPrimitive() ||
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 17450
diff changeset
   203
                decl == Void.TYPE)
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 17450
diff changeset
   204
            return AnnotatedTypeFactory.EMPTY_ANNOTATED_TYPE_ARRAY;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   205
        return buildAnnotatedTypes(rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   206
                                   cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   207
                                   decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   208
                                   decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   209
                                   decl.getGenericInterfaces(),
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   210
                                   TypeAnnotationTarget.CLASS_IMPLEMENTS);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   211
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   212
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   213
    // TypeVariable helpers
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   214
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   215
    /**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   216
     * Parse regular annotations on a TypeVariable declared on genericDecl.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   217
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   218
     * Regular Annotations on TypeVariables are stored in the type
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   219
     * annotation byte[] in the class file.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   220
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   221
     * @param genericDecl the declaration declaring the type variable
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   222
     * @param typeVarIndex the 0-based index of this type variable in the declaration
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   223
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   224
    public static <D extends GenericDeclaration> Annotation[] parseTypeVariableAnnotations(D genericDecl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   225
            int typeVarIndex) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   226
        AnnotatedElement decl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   227
        TypeAnnotationTarget predicate;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   228
        if (genericDecl instanceof Class) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   229
            decl = (Class<?>)genericDecl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   230
            predicate = TypeAnnotationTarget.CLASS_TYPE_PARAMETER;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   231
        } else if (genericDecl instanceof Executable) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   232
            decl = (Executable)genericDecl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   233
            predicate = TypeAnnotationTarget.METHOD_TYPE_PARAMETER;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   234
        } else {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   235
            throw new AssertionError("Unknown GenericDeclaration " + genericDecl + "\nthis should not happen.");
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   236
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   237
        List<TypeAnnotation> typeVarAnnos = TypeAnnotation.filter(parseAllTypeAnnotations(decl),
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   238
                                                                  predicate);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   239
        List<Annotation> res = new ArrayList<>(typeVarAnnos.size());
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   240
        for (TypeAnnotation t : typeVarAnnos)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   241
            if (t.getTargetInfo().getCount() == typeVarIndex)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   242
                res.add(t.getAnnotation());
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   243
        return res.toArray(new Annotation[0]);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   244
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   245
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   246
    /**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   247
     * Build an array of AnnotatedTypes for the declaration decl's bounds.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   248
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   249
     * @param bounds the bounds corresponding to the annotated bounds
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   250
     * @param decl the declaration whose annotated bounds is being built
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   251
     * @param typeVarIndex the index of this type variable on the decl
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   252
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   253
    public static <D extends GenericDeclaration> AnnotatedType[] parseAnnotatedBounds(Type[] bounds,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   254
            D decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   255
            int typeVarIndex) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   256
        return parseAnnotatedBounds(bounds, decl, typeVarIndex, LocationInfo.BASE_LOCATION);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   257
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   258
    //helper for above
21622
643cd3c6f72d 8027796: Refactor Core Reflection for Type Annotations
jfranck
parents: 21361
diff changeset
   259
    private static <D extends GenericDeclaration> AnnotatedType[] parseAnnotatedBounds(Type[] bounds,
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   260
            D decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   261
            int typeVarIndex,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   262
            LocationInfo loc) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   263
        List<TypeAnnotation> candidates = fetchBounds(decl);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   264
        if (bounds != null) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   265
            int startIndex = 0;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   266
            AnnotatedType[] res = new AnnotatedType[bounds.length];
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   267
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   268
            // Adjust bounds index
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   269
            //
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   270
            // Figure out if the type annotations for this bound starts with 0
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   271
            // or 1. The spec says within a bound the 0:th type annotation will
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   272
            // always be on an bound of a Class type (not Interface type). So
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   273
            // if the programmer starts with an Interface type for the first
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   274
            // (and following) bound(s) the implicit Object bound is considered
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   275
            // the first (that is 0:th) bound and type annotations start on
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   276
            // index 1.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   277
            if (bounds.length > 0) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   278
                Type b0 = bounds[0];
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   279
                if (!(b0 instanceof Class<?>)) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   280
                    startIndex = 1;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   281
                } else {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   282
                    Class<?> c = (Class<?>)b0;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   283
                    if (c.isInterface()) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   284
                        startIndex = 1;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   285
                    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   286
                }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   287
            }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   288
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   289
            for (int i = 0; i < bounds.length; i++) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   290
                List<TypeAnnotation> l = new ArrayList<>(candidates.size());
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   291
                for (TypeAnnotation t : candidates) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   292
                    TypeAnnotationTargetInfo tInfo = t.getTargetInfo();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   293
                    if (tInfo.getSecondaryIndex() == i + startIndex &&
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   294
                            tInfo.getCount() == typeVarIndex) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   295
                        l.add(t);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   296
                    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   297
                }
24493
9a793dfeb09d 8038994: AnnotatedType.getType() of a TypeVariable boundary without annotations return null
jfranck
parents: 21845
diff changeset
   298
                res[i] = AnnotatedTypeFactory.buildAnnotatedType(bounds[i],
34708
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
   299
                        AnnotatedTypeFactory.nestingForType(bounds[i], loc),
24493
9a793dfeb09d 8038994: AnnotatedType.getType() of a TypeVariable boundary without annotations return null
jfranck
parents: 21845
diff changeset
   300
                        l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY),
9a793dfeb09d 8038994: AnnotatedType.getType() of a TypeVariable boundary without annotations return null
jfranck
parents: 21845
diff changeset
   301
                        candidates.toArray(EMPTY_TYPE_ANNOTATION_ARRAY),
9a793dfeb09d 8038994: AnnotatedType.getType() of a TypeVariable boundary without annotations return null
jfranck
parents: 21845
diff changeset
   302
                        (AnnotatedElement)decl);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   303
            }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   304
            return res;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   305
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   306
        return new AnnotatedType[0];
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   307
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   308
    private static <D extends GenericDeclaration> List<TypeAnnotation> fetchBounds(D decl) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   309
        AnnotatedElement boundsDecl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   310
        TypeAnnotationTarget target;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   311
        if (decl instanceof Class) {
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   312
            target = TypeAnnotationTarget.CLASS_TYPE_PARAMETER_BOUND;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   313
            boundsDecl = (Class)decl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   314
        } else {
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   315
            target = TypeAnnotationTarget.METHOD_TYPE_PARAMETER_BOUND;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   316
            boundsDecl = (Executable)decl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   317
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   318
        return TypeAnnotation.filter(TypeAnnotationParser.parseAllTypeAnnotations(boundsDecl), target);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   319
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   320
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   321
    /*
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   322
     * Parse all type annotations on the declaration supplied. This is needed
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   323
     * when you go from for example an annotated return type on a method that
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   324
     * is a type variable declared on the class. In this case you need to
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   325
     * 'jump' to the decl of the class and parse all type annotations there to
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   326
     * find the ones that are applicable to the type variable.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   327
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   328
    static TypeAnnotation[] parseAllTypeAnnotations(AnnotatedElement decl) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   329
        Class<?> container;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   330
        byte[] rawBytes;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31061
diff changeset
   331
        JavaLangAccess javaLangAccess = SharedSecrets.getJavaLangAccess();
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   332
        if (decl instanceof Class) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   333
            container = (Class<?>)decl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   334
            rawBytes = javaLangAccess.getRawClassTypeAnnotations(container);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   335
        } else if (decl instanceof Executable) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   336
            container = ((Executable)decl).getDeclaringClass();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   337
            rawBytes = javaLangAccess.getRawExecutableTypeAnnotations((Executable)decl);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   338
        } else {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   339
            // Should not reach here. Assert?
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   340
            return EMPTY_TYPE_ANNOTATION_ARRAY;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   341
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   342
        return parseTypeAnnotations(rawBytes, javaLangAccess.getConstantPool(container),
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   343
                                    decl, container);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   344
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   345
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   346
    /* Parse type annotations encoded as an array of bytes */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   347
    private static TypeAnnotation[] parseTypeAnnotations(byte[] rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   348
            ConstantPool cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   349
            AnnotatedElement baseDecl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   350
            Class<?> container) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   351
        if (rawAnnotations == null)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   352
            return EMPTY_TYPE_ANNOTATION_ARRAY;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   353
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   354
        ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   355
        int annotationCount = buf.getShort() & 0xFFFF;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   356
        List<TypeAnnotation> typeAnnotations = new ArrayList<>(annotationCount);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   357
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   358
        // Parse each TypeAnnotation
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   359
        for (int i = 0; i < annotationCount; i++) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   360
             TypeAnnotation ta = parseTypeAnnotation(buf, cp, baseDecl, container);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   361
             if (ta != null)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   362
                 typeAnnotations.add(ta);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   363
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   364
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   365
        return typeAnnotations.toArray(EMPTY_TYPE_ANNOTATION_ARRAY);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   366
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   367
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   368
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   369
    // Helper
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   370
    static Map<Class<? extends Annotation>, Annotation> mapTypeAnnotations(TypeAnnotation[] typeAnnos) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   371
        Map<Class<? extends Annotation>, Annotation> result =
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   372
            new LinkedHashMap<>();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   373
        for (TypeAnnotation t : typeAnnos) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   374
            Annotation a = t.getAnnotation();
39495
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   375
            if (a != null) {
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   376
                Class<? extends Annotation> klass = a.annotationType();
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   377
                AnnotationType type = AnnotationType.getInstance(klass);
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   378
                if (type.retention() == RetentionPolicy.RUNTIME &&
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   379
                    result.put(klass, a) != null) {
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   380
                    throw new AnnotationFormatError("Duplicate annotation for class: "+klass+": " + a);
39495
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   381
                }
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   382
            }
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   383
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   384
        return result;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   385
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   386
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   387
    // Position codes
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   388
    // Regular type parameter annotations
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   389
    private static final byte CLASS_TYPE_PARAMETER = 0x00;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   390
    private static final byte METHOD_TYPE_PARAMETER = 0x01;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   391
    // Type Annotations outside method bodies
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   392
    private static final byte CLASS_EXTENDS = 0x10;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   393
    private static final byte CLASS_TYPE_PARAMETER_BOUND = 0x11;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   394
    private static final byte METHOD_TYPE_PARAMETER_BOUND = 0x12;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   395
    private static final byte FIELD = 0x13;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   396
    private static final byte METHOD_RETURN = 0x14;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   397
    private static final byte METHOD_RECEIVER = 0x15;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   398
    private static final byte METHOD_FORMAL_PARAMETER = 0x16;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   399
    private static final byte THROWS = 0x17;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   400
    // Type Annotations inside method bodies
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   401
    private static final byte LOCAL_VARIABLE = (byte)0x40;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   402
    private static final byte RESOURCE_VARIABLE = (byte)0x41;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   403
    private static final byte EXCEPTION_PARAMETER = (byte)0x42;
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   404
    private static final byte INSTANCEOF = (byte)0x43;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   405
    private static final byte NEW = (byte)0x44;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   406
    private static final byte CONSTRUCTOR_REFERENCE = (byte)0x45;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   407
    private static final byte METHOD_REFERENCE = (byte)0x46;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   408
    private static final byte CAST = (byte)0x47;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   409
    private static final byte CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT = (byte)0x48;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   410
    private static final byte METHOD_INVOCATION_TYPE_ARGUMENT = (byte)0x49;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   411
    private static final byte CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT = (byte)0x4A;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   412
    private static final byte METHOD_REFERENCE_TYPE_ARGUMENT = (byte)0x4B;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   413
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   414
    private static TypeAnnotation parseTypeAnnotation(ByteBuffer buf,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   415
            ConstantPool cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   416
            AnnotatedElement baseDecl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   417
            Class<?> container) {
21845
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   418
        try {
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   419
            TypeAnnotationTargetInfo ti = parseTargetInfo(buf);
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   420
            LocationInfo locationInfo = LocationInfo.parseLocationInfo(buf);
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   421
            Annotation a = AnnotationParser.parseAnnotation(buf, cp, container, false);
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   422
            if (ti == null) // Inside a method for example
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   423
                return null;
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   424
            return new TypeAnnotation(ti, locationInfo, a, baseDecl);
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   425
        } catch (IllegalArgumentException | // Bad type in const pool at specified index
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   426
                BufferUnderflowException e) {
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   427
            throw new AnnotationFormatError(e);
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   428
        }
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   429
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   430
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   431
    private static TypeAnnotationTargetInfo parseTargetInfo(ByteBuffer buf) {
21845
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   432
        int posCode = buf.get() & 0xFF;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   433
        switch(posCode) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   434
        case CLASS_TYPE_PARAMETER:
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   435
        case METHOD_TYPE_PARAMETER: {
21845
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   436
            int index = buf.get() & 0xFF;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   437
            TypeAnnotationTargetInfo res;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   438
            if (posCode == CLASS_TYPE_PARAMETER)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   439
                res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_TYPE_PARAMETER,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   440
                        index);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   441
            else
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   442
                res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_TYPE_PARAMETER,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   443
                        index);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   444
            return res;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   445
            } // unreachable break;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   446
        case CLASS_EXTENDS: {
21845
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   447
            short index = buf.getShort(); //needs to be signed
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   448
            if (index == -1) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   449
                return new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_EXTENDS);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   450
            } else if (index >= 0) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   451
                TypeAnnotationTargetInfo res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_IMPLEMENTS,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   452
                        index);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   453
                return res;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   454
            }} break;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   455
        case CLASS_TYPE_PARAMETER_BOUND:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   456
            return parse2ByteTarget(TypeAnnotationTarget.CLASS_TYPE_PARAMETER_BOUND, buf);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   457
        case METHOD_TYPE_PARAMETER_BOUND:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   458
            return parse2ByteTarget(TypeAnnotationTarget.METHOD_TYPE_PARAMETER_BOUND, buf);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   459
        case FIELD:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   460
            return new TypeAnnotationTargetInfo(TypeAnnotationTarget.FIELD);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   461
        case METHOD_RETURN:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   462
            return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_RETURN);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   463
        case METHOD_RECEIVER:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   464
            return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_RECEIVER);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   465
        case METHOD_FORMAL_PARAMETER: {
21845
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   466
            int index = buf.get() & 0xFF;
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   467
            return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_FORMAL_PARAMETER,
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   468
                    index);
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   469
            } //unreachable break;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   470
        case THROWS:
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   471
            return parseShortTarget(TypeAnnotationTarget.THROWS, buf);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   472
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   473
        /*
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   474
         * The ones below are inside method bodies, we don't care about them for core reflection
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   475
         * other than adjusting for them in the byte stream.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   476
         */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   477
        case LOCAL_VARIABLE:
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   478
        case RESOURCE_VARIABLE:
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   479
            short length = buf.getShort();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   480
            for (int i = 0; i < length; ++i) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   481
                short offset = buf.getShort();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   482
                short varLength = buf.getShort();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   483
                short index = buf.getShort();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   484
            }
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   485
            return null;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   486
        case EXCEPTION_PARAMETER: {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   487
            byte index = buf.get();
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   488
            }
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   489
            return null;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   490
        case INSTANCEOF:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   491
        case NEW:
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   492
        case CONSTRUCTOR_REFERENCE:
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   493
        case METHOD_REFERENCE: {
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   494
            short offset = buf.getShort();
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   495
            }
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   496
            return null;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   497
        case CAST:
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   498
        case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   499
        case METHOD_INVOCATION_TYPE_ARGUMENT:
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   500
        case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   501
        case METHOD_REFERENCE_TYPE_ARGUMENT: {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   502
            short offset = buf.getShort();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   503
            byte index = buf.get();
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   504
            }
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   505
            return null;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   506
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   507
        default:
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   508
            // will throw error below
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   509
            break;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   510
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   511
        throw new AnnotationFormatError("Could not parse bytes for type annotations");
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   512
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   513
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   514
    private static TypeAnnotationTargetInfo parseShortTarget(TypeAnnotationTarget target, ByteBuffer buf) {
21845
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   515
        int index = buf.getShort() & 0xFFFF;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   516
        return new TypeAnnotationTargetInfo(target, index);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   517
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   518
    private static TypeAnnotationTargetInfo parse2ByteTarget(TypeAnnotationTarget target, ByteBuffer buf) {
21845
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   519
        int count = buf.get() & 0xFF;
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   520
        int secondaryIndex = buf.get() & 0xFF;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   521
        return new TypeAnnotationTargetInfo(target,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   522
                                            count,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   523
                                            secondaryIndex);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   524
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   525
}