45N/A/*
553N/A * Copyright (c) 2007, 2009, Oracle and/or its affiliates. All rights reserved.
45N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45N/A *
45N/A * This code is free software; you can redistribute it and/or modify it
45N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
45N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
45N/A *
45N/A * This code is distributed in the hope that it will be useful, but WITHOUT
45N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
45N/A * version 2 for more details (a copy is included in the LICENSE file that
45N/A * accompanied this code).
45N/A *
45N/A * You should have received a copy of the GNU General Public License version
45N/A * 2 along with this work; if not, write to the Free Software Foundation,
45N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
45N/A */
45N/A
45N/Apackage com.sun.tools.classfile;
45N/A
45N/Aimport java.io.IOException;
45N/A
45N/A/**
971N/A * See JVMS, section 4.8.16.
45N/A *
580N/A * <p><b>This is NOT part of any supported API.
580N/A * If you write code that depends on this, you do so at your own risk.
45N/A * This code and its internal interfaces are subject to change or
45N/A * deletion without notice.</b>
45N/A */
45N/Apublic class Annotation {
45N/A static class InvalidAnnotation extends AttributeException {
197N/A private static final long serialVersionUID = -4620480740735772708L;
45N/A InvalidAnnotation(String msg) {
45N/A super(msg);
45N/A }
45N/A }
45N/A
45N/A Annotation(ClassReader cr) throws IOException, InvalidAnnotation {
45N/A type_index = cr.readUnsignedShort();
45N/A num_element_value_pairs = cr.readUnsignedShort();
45N/A element_value_pairs = new element_value_pair[num_element_value_pairs];
45N/A for (int i = 0; i < element_value_pairs.length; i++)
45N/A element_value_pairs[i] = new element_value_pair(cr);
45N/A }
45N/A
45N/A public Annotation(ConstantPool constant_pool,
45N/A int type_index,
45N/A element_value_pair[] element_value_pairs) {
45N/A this.type_index = type_index;
45N/A num_element_value_pairs = element_value_pairs.length;
45N/A this.element_value_pairs = element_value_pairs;
45N/A }
45N/A
45N/A public int length() {
45N/A int n = 2 /*type_index*/ + 2 /*num_element_value_pairs*/;
45N/A for (element_value_pair pair: element_value_pairs)
45N/A n += pair.length();
45N/A return n;
45N/A }
45N/A
45N/A public final int type_index;
45N/A public final int num_element_value_pairs;
45N/A public final element_value_pair element_value_pairs[];
45N/A
45N/A /**
971N/A * See JVMS, section 4.8.16.1.
45N/A */
45N/A public static abstract class element_value {
45N/A public static element_value read(ClassReader cr)
45N/A throws IOException, InvalidAnnotation {
45N/A int tag = cr.readUnsignedByte();
45N/A switch (tag) {
45N/A case 'B':
45N/A case 'C':
45N/A case 'D':
45N/A case 'F':
45N/A case 'I':
45N/A case 'J':
45N/A case 'S':
45N/A case 'Z':
45N/A case 's':
45N/A return new Primitive_element_value(cr, tag);
45N/A
45N/A case 'e':
45N/A return new Enum_element_value(cr, tag);
45N/A
45N/A case 'c':
45N/A return new Class_element_value(cr, tag);
45N/A
45N/A case '@':
45N/A return new Annotation_element_value(cr, tag);
45N/A
45N/A case '[':
45N/A return new Array_element_value(cr, tag);
45N/A
45N/A default:
45N/A throw new InvalidAnnotation("unrecognized tag: " + tag);
45N/A }
45N/A }
45N/A
45N/A protected element_value(int tag) {
45N/A this.tag = tag;
45N/A }
45N/A
45N/A public abstract int length();
45N/A
45N/A public abstract <R,P> R accept(Visitor<R,P> visitor, P p);
45N/A
45N/A public interface Visitor<R,P> {
45N/A R visitPrimitive(Primitive_element_value ev, P p);
45N/A R visitEnum(Enum_element_value ev, P p);
45N/A R visitClass(Class_element_value ev, P p);
45N/A R visitAnnotation(Annotation_element_value ev, P p);
45N/A R visitArray(Array_element_value ev, P p);
45N/A }
45N/A
45N/A public final int tag;
45N/A }
45N/A
45N/A public static class Primitive_element_value extends element_value {
45N/A Primitive_element_value(ClassReader cr, int tag) throws IOException {
45N/A super(tag);
45N/A const_value_index = cr.readUnsignedShort();
45N/A }
45N/A
45N/A @Override
45N/A public int length() {
45N/A return 2;
45N/A }
45N/A
45N/A public <R,P> R accept(Visitor<R,P> visitor, P p) {
45N/A return visitor.visitPrimitive(this, p);
45N/A }
45N/A
45N/A public final int const_value_index;
45N/A
45N/A }
45N/A
45N/A public static class Enum_element_value extends element_value {
45N/A Enum_element_value(ClassReader cr, int tag) throws IOException {
45N/A super(tag);
45N/A type_name_index = cr.readUnsignedShort();
45N/A const_name_index = cr.readUnsignedShort();
45N/A }
45N/A
45N/A @Override
45N/A public int length() {
45N/A return 4;
45N/A }
45N/A
45N/A public <R,P> R accept(Visitor<R,P> visitor, P p) {
45N/A return visitor.visitEnum(this, p);
45N/A }
45N/A
45N/A public final int type_name_index;
45N/A public final int const_name_index;
45N/A }
45N/A
45N/A public static class Class_element_value extends element_value {
45N/A Class_element_value(ClassReader cr, int tag) throws IOException {
45N/A super(tag);
45N/A class_info_index = cr.readUnsignedShort();
45N/A }
45N/A
45N/A @Override
45N/A public int length() {
45N/A return 2;
45N/A }
45N/A
45N/A public <R,P> R accept(Visitor<R,P> visitor, P p) {
45N/A return visitor.visitClass(this, p);
45N/A }
45N/A
45N/A public final int class_info_index;
45N/A }
45N/A
45N/A public static class Annotation_element_value extends element_value {
45N/A Annotation_element_value(ClassReader cr, int tag)
45N/A throws IOException, InvalidAnnotation {
45N/A super(tag);
45N/A annotation_value = new Annotation(cr);
45N/A }
45N/A
45N/A @Override
45N/A public int length() {
45N/A return annotation_value.length();
45N/A }
45N/A
45N/A public <R,P> R accept(Visitor<R,P> visitor, P p) {
45N/A return visitor.visitAnnotation(this, p);
45N/A }
45N/A
45N/A public final Annotation annotation_value;
45N/A }
45N/A
45N/A public static class Array_element_value extends element_value {
45N/A Array_element_value(ClassReader cr, int tag)
45N/A throws IOException, InvalidAnnotation {
45N/A super(tag);
45N/A num_values = cr.readUnsignedShort();
45N/A values = new element_value[num_values];
45N/A for (int i = 0; i < values.length; i++)
45N/A values[i] = element_value.read(cr);
45N/A }
45N/A
45N/A @Override
45N/A public int length() {
45N/A int n = 2;
45N/A for (int i = 0; i < values.length; i++)
45N/A n += values[i].length();
45N/A return n;
45N/A }
45N/A
45N/A public <R,P> R accept(Visitor<R,P> visitor, P p) {
45N/A return visitor.visitArray(this, p);
45N/A }
45N/A
45N/A public final int num_values;
45N/A public final element_value[] values;
45N/A }
45N/A
45N/A public static class element_value_pair {
45N/A element_value_pair(ClassReader cr)
45N/A throws IOException, InvalidAnnotation {
45N/A element_name_index = cr.readUnsignedShort();
45N/A value = element_value.read(cr);
45N/A }
45N/A
45N/A public int length() {
45N/A return 2 + value.length();
45N/A }
45N/A
45N/A public final int element_name_index;
45N/A public final element_value value;
45N/A }
45N/A}