jdk/src/java.base/share/classes/sun/reflect/annotation/TypeAnnotationParser.java
author darcy
Thu, 07 Jul 2016 10:16:47 -0700
changeset 39495 80833370f1b8
parent 37363 329dba26ffd2
child 45332 9c9e51c44638
permissions -rw-r--r--
8152174: Type annotations with a missing type throw NullPointerException Reviewed-by: jfranck
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
     1
/*
39495
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
     2
 * Copyright (c) 2013, 2016, 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
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   126
        for (int i = 0; i < size; i++) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   127
            @SuppressWarnings("unchecked")
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   128
            ArrayList<TypeAnnotation> list = l[i];
20496
430c1bccd943 8021398: j.l.r.Parameter.getAnnotatedType().getType() for not annotated use of type returns null
emc
parents: 20481
diff changeset
   129
            TypeAnnotation[] typeAnnotations;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   130
            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
   131
                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
   132
            } else {
430c1bccd943 8021398: j.l.r.Parameter.getAnnotatedType().getType() for not annotated use of type returns null
emc
parents: 20481
diff changeset
   133
                typeAnnotations = EMPTY_TYPE_ANNOTATION_ARRAY;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   134
            }
20496
430c1bccd943 8021398: j.l.r.Parameter.getAnnotatedType().getType() for not annotated use of type returns null
emc
parents: 20481
diff changeset
   135
            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
   136
                    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
   137
                    typeAnnotations,
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
   138
                    typeAnnotations,
4a1e3728135c 8057804: AnnotatedType interfaces provide no way to get annotations on owner type
jfranck
parents: 32834
diff changeset
   139
                    decl);
20496
430c1bccd943 8021398: j.l.r.Parameter.getAnnotatedType().getType() for not annotated use of type returns null
emc
parents: 20481
diff changeset
   140
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   141
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   142
        return result;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   143
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   144
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   145
    // Class helpers
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   146
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   147
    /**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   148
     * Build an AnnotatedType for the class decl's supertype.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   149
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   150
     * @param rawAnnotations the byte[] encoding of all type annotations on this declaration
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   151
     * @param cp the ConstantPool needed to parse the embedded Annotation
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   152
     * @param decl the Class which annotated supertype is being built
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   153
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   154
    public static AnnotatedType buildAnnotatedSuperclass(byte[] rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   155
            ConstantPool cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   156
            Class<?> decl) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   157
        Type supertype = decl.getGenericSuperclass();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   158
        if (supertype == null)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   159
            return AnnotatedTypeFactory.EMPTY_ANNOTATED_TYPE;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   160
        return buildAnnotatedType(rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   161
                                  cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   162
                                  decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   163
                                  decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   164
                                  supertype,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   165
                                  TypeAnnotationTarget.CLASS_EXTENDS);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   166
    }
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 array of AnnotatedTypes for the class decl's implemented
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   170
     * interfaces.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   171
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   172
     * @param rawAnnotations the byte[] encoding of all type annotations on this declaration
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   173
     * @param cp the ConstantPool needed to parse the embedded Annotation
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   174
     * @param decl the Class whose annotated implemented interfaces is being built
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   175
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   176
    public static AnnotatedType[] buildAnnotatedInterfaces(byte[] rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   177
            ConstantPool cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   178
            Class<?> decl) {
20481
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 17450
diff changeset
   179
        if (decl == Object.class ||
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 17450
diff changeset
   180
                decl.isArray() ||
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 17450
diff changeset
   181
                decl.isPrimitive() ||
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 17450
diff changeset
   182
                decl == Void.TYPE)
2735b307d256 8007072: Update Core Reflection for Type Annotations to match latest spec
jfranck
parents: 17450
diff changeset
   183
            return AnnotatedTypeFactory.EMPTY_ANNOTATED_TYPE_ARRAY;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   184
        return buildAnnotatedTypes(rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   185
                                   cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   186
                                   decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   187
                                   decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   188
                                   decl.getGenericInterfaces(),
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   189
                                   TypeAnnotationTarget.CLASS_IMPLEMENTS);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   190
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   191
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   192
    // TypeVariable helpers
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   193
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   194
    /**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   195
     * Parse regular annotations on a TypeVariable declared on genericDecl.
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
     * Regular Annotations on TypeVariables are stored in the type
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   198
     * annotation byte[] in the class file.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   199
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   200
     * @param genericDecl the declaration declaring the type variable
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   201
     * @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
   202
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   203
    public static <D extends GenericDeclaration> Annotation[] parseTypeVariableAnnotations(D genericDecl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   204
            int typeVarIndex) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   205
        AnnotatedElement decl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   206
        TypeAnnotationTarget predicate;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   207
        if (genericDecl instanceof Class) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   208
            decl = (Class<?>)genericDecl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   209
            predicate = TypeAnnotationTarget.CLASS_TYPE_PARAMETER;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   210
        } else if (genericDecl instanceof Executable) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   211
            decl = (Executable)genericDecl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   212
            predicate = TypeAnnotationTarget.METHOD_TYPE_PARAMETER;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   213
        } else {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   214
            throw new AssertionError("Unknown GenericDeclaration " + genericDecl + "\nthis should not happen.");
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
        List<TypeAnnotation> typeVarAnnos = TypeAnnotation.filter(parseAllTypeAnnotations(decl),
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   217
                                                                  predicate);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   218
        List<Annotation> res = new ArrayList<>(typeVarAnnos.size());
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   219
        for (TypeAnnotation t : typeVarAnnos)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   220
            if (t.getTargetInfo().getCount() == typeVarIndex)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   221
                res.add(t.getAnnotation());
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   222
        return res.toArray(new Annotation[0]);
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
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   225
    /**
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   226
     * Build an array of AnnotatedTypes for the declaration decl's bounds.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   227
     *
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   228
     * @param bounds the bounds corresponding to the annotated bounds
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   229
     * @param decl the declaration whose annotated bounds is being built
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   230
     * @param typeVarIndex the index of this type variable on the decl
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   231
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   232
    public static <D extends GenericDeclaration> AnnotatedType[] parseAnnotatedBounds(Type[] bounds,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   233
            D decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   234
            int typeVarIndex) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   235
        return parseAnnotatedBounds(bounds, decl, typeVarIndex, LocationInfo.BASE_LOCATION);
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
    //helper for above
21622
643cd3c6f72d 8027796: Refactor Core Reflection for Type Annotations
jfranck
parents: 21361
diff changeset
   238
    private static <D extends GenericDeclaration> AnnotatedType[] parseAnnotatedBounds(Type[] bounds,
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   239
            D decl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   240
            int typeVarIndex,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   241
            LocationInfo loc) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   242
        List<TypeAnnotation> candidates = fetchBounds(decl);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   243
        if (bounds != null) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   244
            int startIndex = 0;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   245
            AnnotatedType[] res = new AnnotatedType[bounds.length];
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
            // Adjust bounds index
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
            // Figure out if the type annotations for this bound starts with 0
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   250
            // 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
   251
            // 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
   252
            // if the programmer starts with an Interface type for the first
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   253
            // (and following) bound(s) the implicit Object bound is considered
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   254
            // the first (that is 0:th) bound and type annotations start on
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   255
            // index 1.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   256
            if (bounds.length > 0) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   257
                Type b0 = bounds[0];
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   258
                if (!(b0 instanceof Class<?>)) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   259
                    startIndex = 1;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   260
                } else {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   261
                    Class<?> c = (Class<?>)b0;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   262
                    if (c.isInterface()) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   263
                        startIndex = 1;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   264
                    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   265
                }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   266
            }
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
            for (int i = 0; i < bounds.length; i++) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   269
                List<TypeAnnotation> l = new ArrayList<>(candidates.size());
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   270
                for (TypeAnnotation t : candidates) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   271
                    TypeAnnotationTargetInfo tInfo = t.getTargetInfo();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   272
                    if (tInfo.getSecondaryIndex() == i + startIndex &&
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   273
                            tInfo.getCount() == typeVarIndex) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   274
                        l.add(t);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   275
                    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   276
                }
24493
9a793dfeb09d 8038994: AnnotatedType.getType() of a TypeVariable boundary without annotations return null
jfranck
parents: 21845
diff changeset
   277
                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
   278
                        AnnotatedTypeFactory.nestingForType(bounds[i], loc),
24493
9a793dfeb09d 8038994: AnnotatedType.getType() of a TypeVariable boundary without annotations return null
jfranck
parents: 21845
diff changeset
   279
                        l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY),
9a793dfeb09d 8038994: AnnotatedType.getType() of a TypeVariable boundary without annotations return null
jfranck
parents: 21845
diff changeset
   280
                        candidates.toArray(EMPTY_TYPE_ANNOTATION_ARRAY),
9a793dfeb09d 8038994: AnnotatedType.getType() of a TypeVariable boundary without annotations return null
jfranck
parents: 21845
diff changeset
   281
                        (AnnotatedElement)decl);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   282
            }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   283
            return res;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   284
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   285
        return new AnnotatedType[0];
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
    private static <D extends GenericDeclaration> List<TypeAnnotation> fetchBounds(D decl) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   288
        AnnotatedElement boundsDecl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   289
        TypeAnnotationTarget target;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   290
        if (decl instanceof Class) {
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   291
            target = TypeAnnotationTarget.CLASS_TYPE_PARAMETER_BOUND;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   292
            boundsDecl = (Class)decl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   293
        } else {
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   294
            target = TypeAnnotationTarget.METHOD_TYPE_PARAMETER_BOUND;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   295
            boundsDecl = (Executable)decl;
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
        return TypeAnnotation.filter(TypeAnnotationParser.parseAllTypeAnnotations(boundsDecl), target);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   298
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   299
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   300
    /*
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   301
     * Parse all type annotations on the declaration supplied. This is needed
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   302
     * 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
   303
     * 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
   304
     * '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
   305
     * find the ones that are applicable to the type variable.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   306
     */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   307
    static TypeAnnotation[] parseAllTypeAnnotations(AnnotatedElement decl) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   308
        Class<?> container;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   309
        byte[] rawBytes;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 31061
diff changeset
   310
        JavaLangAccess javaLangAccess = SharedSecrets.getJavaLangAccess();
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   311
        if (decl instanceof Class) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   312
            container = (Class<?>)decl;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   313
            rawBytes = javaLangAccess.getRawClassTypeAnnotations(container);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   314
        } else if (decl instanceof Executable) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   315
            container = ((Executable)decl).getDeclaringClass();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   316
            rawBytes = javaLangAccess.getRawExecutableTypeAnnotations((Executable)decl);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   317
        } else {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   318
            // Should not reach here. Assert?
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   319
            return EMPTY_TYPE_ANNOTATION_ARRAY;
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
        return parseTypeAnnotations(rawBytes, javaLangAccess.getConstantPool(container),
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   322
                                    decl, container);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   323
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   324
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   325
    /* Parse type annotations encoded as an array of bytes */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   326
    private static TypeAnnotation[] parseTypeAnnotations(byte[] rawAnnotations,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   327
            ConstantPool cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   328
            AnnotatedElement baseDecl,
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
        if (rawAnnotations == null)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   331
            return EMPTY_TYPE_ANNOTATION_ARRAY;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   332
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   333
        ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   334
        int annotationCount = buf.getShort() & 0xFFFF;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   335
        List<TypeAnnotation> typeAnnotations = new ArrayList<>(annotationCount);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   336
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   337
        // Parse each TypeAnnotation
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   338
        for (int i = 0; i < annotationCount; i++) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   339
             TypeAnnotation ta = parseTypeAnnotation(buf, cp, baseDecl, container);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   340
             if (ta != null)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   341
                 typeAnnotations.add(ta);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   342
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   343
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   344
        return typeAnnotations.toArray(EMPTY_TYPE_ANNOTATION_ARRAY);
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
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   347
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   348
    // Helper
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   349
    static Map<Class<? extends Annotation>, Annotation> mapTypeAnnotations(TypeAnnotation[] typeAnnos) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   350
        Map<Class<? extends Annotation>, Annotation> result =
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   351
            new LinkedHashMap<>();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   352
        for (TypeAnnotation t : typeAnnos) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   353
            Annotation a = t.getAnnotation();
39495
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   354
            if (a != null) {
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   355
                Class<? extends Annotation> klass = a.annotationType();
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   356
                AnnotationType type = AnnotationType.getInstance(klass);
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   357
                if (type.retention() == RetentionPolicy.RUNTIME &&
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   358
                    result.put(klass, a) != null) {
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   359
                    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
   360
                }
80833370f1b8 8152174: Type annotations with a missing type throw NullPointerException
darcy
parents: 37363
diff changeset
   361
            }
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   362
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   363
        return result;
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
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   366
    // Position codes
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   367
    // Regular type parameter annotations
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   368
    private static final byte CLASS_TYPE_PARAMETER = 0x00;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   369
    private static final byte METHOD_TYPE_PARAMETER = 0x01;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   370
    // Type Annotations outside method bodies
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   371
    private static final byte CLASS_EXTENDS = 0x10;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   372
    private static final byte CLASS_TYPE_PARAMETER_BOUND = 0x11;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   373
    private static final byte METHOD_TYPE_PARAMETER_BOUND = 0x12;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   374
    private static final byte FIELD = 0x13;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   375
    private static final byte METHOD_RETURN = 0x14;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   376
    private static final byte METHOD_RECEIVER = 0x15;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   377
    private static final byte METHOD_FORMAL_PARAMETER = 0x16;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   378
    private static final byte THROWS = 0x17;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   379
    // Type Annotations inside method bodies
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   380
    private static final byte LOCAL_VARIABLE = (byte)0x40;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   381
    private static final byte RESOURCE_VARIABLE = (byte)0x41;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   382
    private static final byte EXCEPTION_PARAMETER = (byte)0x42;
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   383
    private static final byte INSTANCEOF = (byte)0x43;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   384
    private static final byte NEW = (byte)0x44;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   385
    private static final byte CONSTRUCTOR_REFERENCE = (byte)0x45;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   386
    private static final byte METHOD_REFERENCE = (byte)0x46;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   387
    private static final byte CAST = (byte)0x47;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   388
    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
   389
    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
   390
    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
   391
    private static final byte METHOD_REFERENCE_TYPE_ARGUMENT = (byte)0x4B;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   392
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   393
    private static TypeAnnotation parseTypeAnnotation(ByteBuffer buf,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   394
            ConstantPool cp,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   395
            AnnotatedElement baseDecl,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   396
            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
   397
        try {
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   398
            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
   399
            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
   400
            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
   401
            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
   402
                return null;
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   403
            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
   404
        } 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
   405
                BufferUnderflowException e) {
35695321f5a5 8023278: Reflection API methods do not throw AnnotationFormatError in case of malformed Runtime[In]VisibleTypeAnnotations attribute
jfranck
parents: 21622
diff changeset
   406
            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
   407
        }
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   408
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   409
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   410
    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
   411
        int posCode = buf.get() & 0xFF;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   412
        switch(posCode) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   413
        case CLASS_TYPE_PARAMETER:
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   414
        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
   415
            int index = buf.get() & 0xFF;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   416
            TypeAnnotationTargetInfo res;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   417
            if (posCode == CLASS_TYPE_PARAMETER)
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   418
                res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_TYPE_PARAMETER,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   419
                        index);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   420
            else
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   421
                res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_TYPE_PARAMETER,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   422
                        index);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   423
            return res;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   424
            } // unreachable break;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   425
        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
   426
            short index = buf.getShort(); //needs to be signed
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   427
            if (index == -1) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   428
                return new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_EXTENDS);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   429
            } else if (index >= 0) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   430
                TypeAnnotationTargetInfo res = new TypeAnnotationTargetInfo(TypeAnnotationTarget.CLASS_IMPLEMENTS,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   431
                        index);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   432
                return res;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   433
            }} break;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   434
        case CLASS_TYPE_PARAMETER_BOUND:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   435
            return parse2ByteTarget(TypeAnnotationTarget.CLASS_TYPE_PARAMETER_BOUND, buf);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   436
        case METHOD_TYPE_PARAMETER_BOUND:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   437
            return parse2ByteTarget(TypeAnnotationTarget.METHOD_TYPE_PARAMETER_BOUND, buf);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   438
        case FIELD:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   439
            return new TypeAnnotationTargetInfo(TypeAnnotationTarget.FIELD);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   440
        case METHOD_RETURN:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   441
            return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_RETURN);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   442
        case METHOD_RECEIVER:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   443
            return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_RECEIVER);
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   444
        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
   445
            int index = buf.get() & 0xFF;
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   446
            return new TypeAnnotationTargetInfo(TypeAnnotationTarget.METHOD_FORMAL_PARAMETER,
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   447
                    index);
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   448
            } //unreachable break;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   449
        case THROWS:
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   450
            return parseShortTarget(TypeAnnotationTarget.THROWS, buf);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   451
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   452
        /*
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   453
         * 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
   454
         * other than adjusting for them in the byte stream.
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   455
         */
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   456
        case LOCAL_VARIABLE:
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   457
        case RESOURCE_VARIABLE:
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   458
            short length = buf.getShort();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   459
            for (int i = 0; i < length; ++i) {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   460
                short offset = buf.getShort();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   461
                short varLength = buf.getShort();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   462
                short index = buf.getShort();
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   463
            }
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   464
            return null;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   465
        case EXCEPTION_PARAMETER: {
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   466
            byte index = buf.get();
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   467
            }
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   468
            return null;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   469
        case INSTANCEOF:
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   470
        case NEW:
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   471
        case CONSTRUCTOR_REFERENCE:
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   472
        case METHOD_REFERENCE: {
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   473
            short offset = buf.getShort();
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   474
            }
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   475
            return null;
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   476
        case CAST:
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   477
        case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   478
        case METHOD_INVOCATION_TYPE_ARGUMENT:
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   479
        case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   480
        case METHOD_REFERENCE_TYPE_ARGUMENT: {
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
            byte index = buf.get();
17450
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   483
            }
0f704861915e 8007073: Implement Core Reflection for Type Annotations on parameters
jfranck
parents: 15510
diff changeset
   484
            return null;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   485
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   486
        default:
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   487
            // will throw error below
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   488
            break;
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   489
        }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   490
        throw new AnnotationFormatError("Could not parse bytes for type annotations");
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   491
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   492
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   493
    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
   494
        int index = buf.getShort() & 0xFFFF;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   495
        return new TypeAnnotationTargetInfo(target, index);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   496
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   497
    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
   498
        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
   499
        int secondaryIndex = buf.get() & 0xFF;
15510
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   500
        return new TypeAnnotationTargetInfo(target,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   501
                                            count,
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   502
                                            secondaryIndex);
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   503
    }
898d924a7efd 8004698: Implement Core Reflection for Type Annotations
jfranck
parents:
diff changeset
   504
}