Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/classfile/Annotation.java
41161 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}132133public Primitive_element_value(int const_value_index, int tag) {134super(tag);135this.const_value_index = const_value_index;136}137138@Override139public int length() {140return 2;141}142143public <R,P> R accept(Visitor<R,P> visitor, P p) {144return visitor.visitPrimitive(this, p);145}146147public final int const_value_index;148149}150151public static class Enum_element_value extends element_value {152Enum_element_value(ClassReader cr, int tag) throws IOException {153super(tag);154type_name_index = cr.readUnsignedShort();155const_name_index = cr.readUnsignedShort();156}157158public Enum_element_value(int type_name_index, int const_name_index, int tag) {159super(tag);160this.type_name_index = type_name_index;161this.const_name_index = const_name_index;162}163164@Override165public int length() {166return 4;167}168169public <R,P> R accept(Visitor<R,P> visitor, P p) {170return visitor.visitEnum(this, p);171}172173public final int type_name_index;174public final int const_name_index;175}176177public static class Class_element_value extends element_value {178Class_element_value(ClassReader cr, int tag) throws IOException {179super(tag);180class_info_index = cr.readUnsignedShort();181}182183public Class_element_value(int class_info_index, int tag) {184super(tag);185this.class_info_index = class_info_index;186}187188@Override189public int length() {190return 2;191}192193public <R,P> R accept(Visitor<R,P> visitor, P p) {194return visitor.visitClass(this, p);195}196197public final int class_info_index;198}199200public static class Annotation_element_value extends element_value {201Annotation_element_value(ClassReader cr, int tag)202throws IOException, InvalidAnnotation {203super(tag);204annotation_value = new Annotation(cr);205}206207public Annotation_element_value(Annotation annotation_value, int tag) {208super(tag);209this.annotation_value = annotation_value;210}211212@Override213public int length() {214return annotation_value.length();215}216217public <R,P> R accept(Visitor<R,P> visitor, P p) {218return visitor.visitAnnotation(this, p);219}220221public final Annotation annotation_value;222}223224public static class Array_element_value extends element_value {225Array_element_value(ClassReader cr, int tag)226throws IOException, InvalidAnnotation {227super(tag);228num_values = cr.readUnsignedShort();229values = new element_value[num_values];230for (int i = 0; i < values.length; i++)231values[i] = element_value.read(cr);232}233234public Array_element_value(element_value[] values, int tag) {235super(tag);236this.num_values = values.length;237this.values = values;238}239240@Override241public int length() {242int n = 2;243for (int i = 0; i < values.length; i++)244n += values[i].length();245return n;246}247248public <R,P> R accept(Visitor<R,P> visitor, P p) {249return visitor.visitArray(this, p);250}251252public final int num_values;253public final element_value[] values;254}255256public static class element_value_pair {257element_value_pair(ClassReader cr)258throws IOException, InvalidAnnotation {259element_name_index = cr.readUnsignedShort();260value = element_value.read(cr);261}262263public element_value_pair(int element_name_index, element_value value) {264this.element_name_index = element_name_index;265this.value = value;266}267268public int length() {269return 2 + value.length();270}271272public final int element_name_index;273public final element_value value;274}275}276277278