src/share/classes/com/sun/tools/classfile/Annotation.java

Tue, 16 Sep 2008 18:35:18 -0700

author
jjg
date
Tue, 16 Sep 2008 18:35:18 -0700
changeset 113
eff38cc97183
parent 54
eaf608c64fec
child 198
b4b1f7732289
permissions
-rw-r--r--

6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
Reviewed-by: darcy, mcimadamore

jjg@46 1 /*
xdono@54 2 * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
jjg@46 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@46 4 *
jjg@46 5 * This code is free software; you can redistribute it and/or modify it
jjg@46 6 * under the terms of the GNU General Public License version 2 only, as
jjg@46 7 * published by the Free Software Foundation. Sun designates this
jjg@46 8 * particular file as subject to the "Classpath" exception as provided
jjg@46 9 * by Sun in the LICENSE file that accompanied this code.
jjg@46 10 *
jjg@46 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@46 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@46 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@46 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@46 15 * accompanied this code).
jjg@46 16 *
jjg@46 17 * You should have received a copy of the GNU General Public License version
jjg@46 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@46 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@46 20 *
jjg@46 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@46 22 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@46 23 * have any questions.
jjg@46 24 */
jjg@46 25
jjg@46 26 package com.sun.tools.classfile;
jjg@46 27
jjg@46 28 import java.io.IOException;
jjg@46 29
jjg@46 30 /**
jjg@46 31 * See JVMS3, section 4.8.16.
jjg@46 32 *
jjg@46 33 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
jjg@46 34 * you write code that depends on this, you do so at your own risk.
jjg@46 35 * This code and its internal interfaces are subject to change or
jjg@46 36 * deletion without notice.</b>
jjg@46 37 */
jjg@46 38 public class Annotation {
jjg@46 39 static class InvalidAnnotation extends AttributeException {
jjg@46 40 InvalidAnnotation(String msg) {
jjg@46 41 super(msg);
jjg@46 42 }
jjg@46 43 }
jjg@46 44
jjg@46 45 Annotation(ClassReader cr) throws IOException, InvalidAnnotation {
jjg@46 46 type_index = cr.readUnsignedShort();
jjg@46 47 num_element_value_pairs = cr.readUnsignedShort();
jjg@46 48 element_value_pairs = new element_value_pair[num_element_value_pairs];
jjg@46 49 for (int i = 0; i < element_value_pairs.length; i++)
jjg@46 50 element_value_pairs[i] = new element_value_pair(cr);
jjg@46 51 }
jjg@46 52
jjg@46 53 public Annotation(ConstantPool constant_pool,
jjg@46 54 int type_index,
jjg@46 55 element_value_pair[] element_value_pairs) {
jjg@46 56 this.type_index = type_index;
jjg@46 57 num_element_value_pairs = element_value_pairs.length;
jjg@46 58 this.element_value_pairs = element_value_pairs;
jjg@46 59 }
jjg@46 60
jjg@46 61 public int length() {
jjg@46 62 int n = 2 /*type_index*/ + 2 /*num_element_value_pairs*/;
jjg@46 63 for (element_value_pair pair: element_value_pairs)
jjg@46 64 n += pair.length();
jjg@46 65 return n;
jjg@46 66 }
jjg@46 67
jjg@46 68 public final int type_index;
jjg@46 69 public final int num_element_value_pairs;
jjg@46 70 public final element_value_pair element_value_pairs[];
jjg@46 71
jjg@46 72 /**
jjg@46 73 * See JVMS3, section 4.8.16.1.
jjg@46 74 */
jjg@46 75 public static abstract class element_value {
jjg@46 76 public static element_value read(ClassReader cr)
jjg@46 77 throws IOException, InvalidAnnotation {
jjg@46 78 int tag = cr.readUnsignedByte();
jjg@46 79 switch (tag) {
jjg@46 80 case 'B':
jjg@46 81 case 'C':
jjg@46 82 case 'D':
jjg@46 83 case 'F':
jjg@46 84 case 'I':
jjg@46 85 case 'J':
jjg@46 86 case 'S':
jjg@46 87 case 'Z':
jjg@46 88 case 's':
jjg@46 89 return new Primitive_element_value(cr, tag);
jjg@46 90
jjg@46 91 case 'e':
jjg@46 92 return new Enum_element_value(cr, tag);
jjg@46 93
jjg@46 94 case 'c':
jjg@46 95 return new Class_element_value(cr, tag);
jjg@46 96
jjg@46 97 case '@':
jjg@46 98 return new Annotation_element_value(cr, tag);
jjg@46 99
jjg@46 100 case '[':
jjg@46 101 return new Array_element_value(cr, tag);
jjg@46 102
jjg@46 103 default:
jjg@46 104 throw new InvalidAnnotation("unrecognized tag: " + tag);
jjg@46 105 }
jjg@46 106 }
jjg@46 107
jjg@46 108 protected element_value(int tag) {
jjg@46 109 this.tag = tag;
jjg@46 110 }
jjg@46 111
jjg@46 112 public abstract int length();
jjg@46 113
jjg@46 114 public abstract <R,P> R accept(Visitor<R,P> visitor, P p);
jjg@46 115
jjg@46 116 public interface Visitor<R,P> {
jjg@46 117 R visitPrimitive(Primitive_element_value ev, P p);
jjg@46 118 R visitEnum(Enum_element_value ev, P p);
jjg@46 119 R visitClass(Class_element_value ev, P p);
jjg@46 120 R visitAnnotation(Annotation_element_value ev, P p);
jjg@46 121 R visitArray(Array_element_value ev, P p);
jjg@46 122 }
jjg@46 123
jjg@46 124 public final int tag;
jjg@46 125 }
jjg@46 126
jjg@46 127 public static class Primitive_element_value extends element_value {
jjg@46 128 Primitive_element_value(ClassReader cr, int tag) throws IOException {
jjg@46 129 super(tag);
jjg@46 130 const_value_index = cr.readUnsignedShort();
jjg@46 131 }
jjg@46 132
jjg@46 133 @Override
jjg@46 134 public int length() {
jjg@46 135 return 2;
jjg@46 136 }
jjg@46 137
jjg@46 138 public <R,P> R accept(Visitor<R,P> visitor, P p) {
jjg@46 139 return visitor.visitPrimitive(this, p);
jjg@46 140 }
jjg@46 141
jjg@46 142 public final int const_value_index;
jjg@46 143
jjg@46 144 }
jjg@46 145
jjg@46 146 public static class Enum_element_value extends element_value {
jjg@46 147 Enum_element_value(ClassReader cr, int tag) throws IOException {
jjg@46 148 super(tag);
jjg@46 149 type_name_index = cr.readUnsignedShort();
jjg@46 150 const_name_index = cr.readUnsignedShort();
jjg@46 151 }
jjg@46 152
jjg@46 153 @Override
jjg@46 154 public int length() {
jjg@46 155 return 4;
jjg@46 156 }
jjg@46 157
jjg@46 158 public <R,P> R accept(Visitor<R,P> visitor, P p) {
jjg@46 159 return visitor.visitEnum(this, p);
jjg@46 160 }
jjg@46 161
jjg@46 162 public final int type_name_index;
jjg@46 163 public final int const_name_index;
jjg@46 164 }
jjg@46 165
jjg@46 166 public static class Class_element_value extends element_value {
jjg@46 167 Class_element_value(ClassReader cr, int tag) throws IOException {
jjg@46 168 super(tag);
jjg@46 169 class_info_index = cr.readUnsignedShort();
jjg@46 170 }
jjg@46 171
jjg@46 172 @Override
jjg@46 173 public int length() {
jjg@46 174 return 2;
jjg@46 175 }
jjg@46 176
jjg@46 177 public <R,P> R accept(Visitor<R,P> visitor, P p) {
jjg@46 178 return visitor.visitClass(this, p);
jjg@46 179 }
jjg@46 180
jjg@46 181 public final int class_info_index;
jjg@46 182 }
jjg@46 183
jjg@46 184 public static class Annotation_element_value extends element_value {
jjg@46 185 Annotation_element_value(ClassReader cr, int tag)
jjg@46 186 throws IOException, InvalidAnnotation {
jjg@46 187 super(tag);
jjg@46 188 annotation_value = new Annotation(cr);
jjg@46 189 }
jjg@46 190
jjg@46 191 @Override
jjg@46 192 public int length() {
jjg@46 193 return annotation_value.length();
jjg@46 194 }
jjg@46 195
jjg@46 196 public <R,P> R accept(Visitor<R,P> visitor, P p) {
jjg@46 197 return visitor.visitAnnotation(this, p);
jjg@46 198 }
jjg@46 199
jjg@46 200 public final Annotation annotation_value;
jjg@46 201 }
jjg@46 202
jjg@46 203 public static class Array_element_value extends element_value {
jjg@46 204 Array_element_value(ClassReader cr, int tag)
jjg@46 205 throws IOException, InvalidAnnotation {
jjg@46 206 super(tag);
jjg@46 207 num_values = cr.readUnsignedShort();
jjg@46 208 values = new element_value[num_values];
jjg@46 209 for (int i = 0; i < values.length; i++)
jjg@46 210 values[i] = element_value.read(cr);
jjg@46 211 }
jjg@46 212
jjg@46 213 @Override
jjg@46 214 public int length() {
jjg@46 215 int n = 2;
jjg@46 216 for (int i = 0; i < values.length; i++)
jjg@46 217 n += values[i].length();
jjg@46 218 return n;
jjg@46 219 }
jjg@46 220
jjg@46 221 public <R,P> R accept(Visitor<R,P> visitor, P p) {
jjg@46 222 return visitor.visitArray(this, p);
jjg@46 223 }
jjg@46 224
jjg@46 225 public final int num_values;
jjg@46 226 public final element_value[] values;
jjg@46 227 }
jjg@46 228
jjg@46 229 public static class element_value_pair {
jjg@46 230 element_value_pair(ClassReader cr)
jjg@46 231 throws IOException, InvalidAnnotation {
jjg@46 232 element_name_index = cr.readUnsignedShort();
jjg@46 233 value = element_value.read(cr);
jjg@46 234 }
jjg@46 235
jjg@46 236 public int length() {
jjg@46 237 return 2 + value.length();
jjg@46 238 }
jjg@46 239
jjg@46 240 public final int element_name_index;
jjg@46 241 public final element_value value;
jjg@46 242 }
jjg@46 243 }

mercurial