Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/classfile/Annotation.java
38899 views
/*1* Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.classfile;2627import java.io.IOException;2829/**30* See JVMS, section 4.8.16.31*32* <p><b>This is NOT part of any supported API.33* If you write code that depends on this, you do so at your own risk.34* This code and its internal interfaces are subject to change or35* deletion without notice.</b>36*/37public class Annotation {38static class InvalidAnnotation extends AttributeException {39private static final long serialVersionUID = -4620480740735772708L;40InvalidAnnotation(String msg) {41super(msg);42}43}4445Annotation(ClassReader cr) throws IOException, InvalidAnnotation {46type_index = cr.readUnsignedShort();47num_element_value_pairs = cr.readUnsignedShort();48element_value_pairs = new element_value_pair[num_element_value_pairs];49for (int i = 0; i < element_value_pairs.length; i++)50element_value_pairs[i] = new element_value_pair(cr);51}5253public Annotation(ConstantPool constant_pool,54int type_index,55element_value_pair[] element_value_pairs) {56this.type_index = type_index;57num_element_value_pairs = element_value_pairs.length;58this.element_value_pairs = element_value_pairs;59}6061public int length() {62int n = 2 /*type_index*/ + 2 /*num_element_value_pairs*/;63for (element_value_pair pair: element_value_pairs)64n += pair.length();65return n;66}6768public final int type_index;69public final int num_element_value_pairs;70public final element_value_pair element_value_pairs[];7172/**73* See JVMS, section 4.8.16.1.74*/75public static abstract class element_value {76public static element_value read(ClassReader cr)77throws IOException, InvalidAnnotation {78int tag = cr.readUnsignedByte();79switch (tag) {80case 'B':81case 'C':82case 'D':83case 'F':84case 'I':85case 'J':86case 'S':87case 'Z':88case 's':89return new Primitive_element_value(cr, tag);9091case 'e':92return new Enum_element_value(cr, tag);9394case 'c':95return new Class_element_value(cr, tag);9697case '@':98return new Annotation_element_value(cr, tag);99100case '[':101return new Array_element_value(cr, tag);102103default:104throw new InvalidAnnotation("unrecognized tag: " + tag);105}106}107108protected element_value(int tag) {109this.tag = tag;110}111112public abstract int length();113114public abstract <R,P> R accept(Visitor<R,P> visitor, P p);115116public interface Visitor<R,P> {117R visitPrimitive(Primitive_element_value ev, P p);118R visitEnum(Enum_element_value ev, P p);119R visitClass(Class_element_value ev, P p);120R visitAnnotation(Annotation_element_value ev, P p);121R visitArray(Array_element_value ev, P p);122}123124public final int tag;125}126127public static class Primitive_element_value extends element_value {128Primitive_element_value(ClassReader cr, int tag) throws IOException {129super(tag);130const_value_index = cr.readUnsignedShort();131}132133@Override134public int length() {135return 2;136}137138public <R,P> R accept(Visitor<R,P> visitor, P p) {139return visitor.visitPrimitive(this, p);140}141142public final int const_value_index;143144}145146public static class Enum_element_value extends element_value {147Enum_element_value(ClassReader cr, int tag) throws IOException {148super(tag);149type_name_index = cr.readUnsignedShort();150const_name_index = cr.readUnsignedShort();151}152153@Override154public int length() {155return 4;156}157158public <R,P> R accept(Visitor<R,P> visitor, P p) {159return visitor.visitEnum(this, p);160}161162public final int type_name_index;163public final int const_name_index;164}165166public static class Class_element_value extends element_value {167Class_element_value(ClassReader cr, int tag) throws IOException {168super(tag);169class_info_index = cr.readUnsignedShort();170}171172@Override173public int length() {174return 2;175}176177public <R,P> R accept(Visitor<R,P> visitor, P p) {178return visitor.visitClass(this, p);179}180181public final int class_info_index;182}183184public static class Annotation_element_value extends element_value {185Annotation_element_value(ClassReader cr, int tag)186throws IOException, InvalidAnnotation {187super(tag);188annotation_value = new Annotation(cr);189}190191@Override192public int length() {193return annotation_value.length();194}195196public <R,P> R accept(Visitor<R,P> visitor, P p) {197return visitor.visitAnnotation(this, p);198}199200public final Annotation annotation_value;201}202203public static class Array_element_value extends element_value {204Array_element_value(ClassReader cr, int tag)205throws IOException, InvalidAnnotation {206super(tag);207num_values = cr.readUnsignedShort();208values = new element_value[num_values];209for (int i = 0; i < values.length; i++)210values[i] = element_value.read(cr);211}212213@Override214public int length() {215int n = 2;216for (int i = 0; i < values.length; i++)217n += values[i].length();218return n;219}220221public <R,P> R accept(Visitor<R,P> visitor, P p) {222return visitor.visitArray(this, p);223}224225public final int num_values;226public final element_value[] values;227}228229public static class element_value_pair {230element_value_pair(ClassReader cr)231throws IOException, InvalidAnnotation {232element_name_index = cr.readUnsignedShort();233value = element_value.read(cr);234}235236public int length() {237return 2 + value.length();238}239240public final int element_name_index;241public final element_value value;242}243}244245246