1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/classfile/Annotation.java Tue Jun 03 13:26:47 2008 -0700 1.3 @@ -0,0 +1,243 @@ 1.4 +/* 1.5 + * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.classfile; 1.30 + 1.31 +import java.io.IOException; 1.32 + 1.33 +/** 1.34 + * See JVMS3, section 4.8.16. 1.35 + * 1.36 + * <p><b>This is NOT part of any API supported by Sun Microsystems. If 1.37 + * you write code that depends on this, you do so at your own risk. 1.38 + * This code and its internal interfaces are subject to change or 1.39 + * deletion without notice.</b> 1.40 + */ 1.41 +public class Annotation { 1.42 + static class InvalidAnnotation extends AttributeException { 1.43 + InvalidAnnotation(String msg) { 1.44 + super(msg); 1.45 + } 1.46 + } 1.47 + 1.48 + Annotation(ClassReader cr) throws IOException, InvalidAnnotation { 1.49 + type_index = cr.readUnsignedShort(); 1.50 + num_element_value_pairs = cr.readUnsignedShort(); 1.51 + element_value_pairs = new element_value_pair[num_element_value_pairs]; 1.52 + for (int i = 0; i < element_value_pairs.length; i++) 1.53 + element_value_pairs[i] = new element_value_pair(cr); 1.54 + } 1.55 + 1.56 + public Annotation(ConstantPool constant_pool, 1.57 + int type_index, 1.58 + element_value_pair[] element_value_pairs) { 1.59 + this.type_index = type_index; 1.60 + num_element_value_pairs = element_value_pairs.length; 1.61 + this.element_value_pairs = element_value_pairs; 1.62 + } 1.63 + 1.64 + public int length() { 1.65 + int n = 2 /*type_index*/ + 2 /*num_element_value_pairs*/; 1.66 + for (element_value_pair pair: element_value_pairs) 1.67 + n += pair.length(); 1.68 + return n; 1.69 + } 1.70 + 1.71 + public final int type_index; 1.72 + public final int num_element_value_pairs; 1.73 + public final element_value_pair element_value_pairs[]; 1.74 + 1.75 + /** 1.76 + * See JVMS3, section 4.8.16.1. 1.77 + */ 1.78 + public static abstract class element_value { 1.79 + public static element_value read(ClassReader cr) 1.80 + throws IOException, InvalidAnnotation { 1.81 + int tag = cr.readUnsignedByte(); 1.82 + switch (tag) { 1.83 + case 'B': 1.84 + case 'C': 1.85 + case 'D': 1.86 + case 'F': 1.87 + case 'I': 1.88 + case 'J': 1.89 + case 'S': 1.90 + case 'Z': 1.91 + case 's': 1.92 + return new Primitive_element_value(cr, tag); 1.93 + 1.94 + case 'e': 1.95 + return new Enum_element_value(cr, tag); 1.96 + 1.97 + case 'c': 1.98 + return new Class_element_value(cr, tag); 1.99 + 1.100 + case '@': 1.101 + return new Annotation_element_value(cr, tag); 1.102 + 1.103 + case '[': 1.104 + return new Array_element_value(cr, tag); 1.105 + 1.106 + default: 1.107 + throw new InvalidAnnotation("unrecognized tag: " + tag); 1.108 + } 1.109 + } 1.110 + 1.111 + protected element_value(int tag) { 1.112 + this.tag = tag; 1.113 + } 1.114 + 1.115 + public abstract int length(); 1.116 + 1.117 + public abstract <R,P> R accept(Visitor<R,P> visitor, P p); 1.118 + 1.119 + public interface Visitor<R,P> { 1.120 + R visitPrimitive(Primitive_element_value ev, P p); 1.121 + R visitEnum(Enum_element_value ev, P p); 1.122 + R visitClass(Class_element_value ev, P p); 1.123 + R visitAnnotation(Annotation_element_value ev, P p); 1.124 + R visitArray(Array_element_value ev, P p); 1.125 + } 1.126 + 1.127 + public final int tag; 1.128 + } 1.129 + 1.130 + public static class Primitive_element_value extends element_value { 1.131 + Primitive_element_value(ClassReader cr, int tag) throws IOException { 1.132 + super(tag); 1.133 + const_value_index = cr.readUnsignedShort(); 1.134 + } 1.135 + 1.136 + @Override 1.137 + public int length() { 1.138 + return 2; 1.139 + } 1.140 + 1.141 + public <R,P> R accept(Visitor<R,P> visitor, P p) { 1.142 + return visitor.visitPrimitive(this, p); 1.143 + } 1.144 + 1.145 + public final int const_value_index; 1.146 + 1.147 + } 1.148 + 1.149 + public static class Enum_element_value extends element_value { 1.150 + Enum_element_value(ClassReader cr, int tag) throws IOException { 1.151 + super(tag); 1.152 + type_name_index = cr.readUnsignedShort(); 1.153 + const_name_index = cr.readUnsignedShort(); 1.154 + } 1.155 + 1.156 + @Override 1.157 + public int length() { 1.158 + return 4; 1.159 + } 1.160 + 1.161 + public <R,P> R accept(Visitor<R,P> visitor, P p) { 1.162 + return visitor.visitEnum(this, p); 1.163 + } 1.164 + 1.165 + public final int type_name_index; 1.166 + public final int const_name_index; 1.167 + } 1.168 + 1.169 + public static class Class_element_value extends element_value { 1.170 + Class_element_value(ClassReader cr, int tag) throws IOException { 1.171 + super(tag); 1.172 + class_info_index = cr.readUnsignedShort(); 1.173 + } 1.174 + 1.175 + @Override 1.176 + public int length() { 1.177 + return 2; 1.178 + } 1.179 + 1.180 + public <R,P> R accept(Visitor<R,P> visitor, P p) { 1.181 + return visitor.visitClass(this, p); 1.182 + } 1.183 + 1.184 + public final int class_info_index; 1.185 + } 1.186 + 1.187 + public static class Annotation_element_value extends element_value { 1.188 + Annotation_element_value(ClassReader cr, int tag) 1.189 + throws IOException, InvalidAnnotation { 1.190 + super(tag); 1.191 + annotation_value = new Annotation(cr); 1.192 + } 1.193 + 1.194 + @Override 1.195 + public int length() { 1.196 + return annotation_value.length(); 1.197 + } 1.198 + 1.199 + public <R,P> R accept(Visitor<R,P> visitor, P p) { 1.200 + return visitor.visitAnnotation(this, p); 1.201 + } 1.202 + 1.203 + public final Annotation annotation_value; 1.204 + } 1.205 + 1.206 + public static class Array_element_value extends element_value { 1.207 + Array_element_value(ClassReader cr, int tag) 1.208 + throws IOException, InvalidAnnotation { 1.209 + super(tag); 1.210 + num_values = cr.readUnsignedShort(); 1.211 + values = new element_value[num_values]; 1.212 + for (int i = 0; i < values.length; i++) 1.213 + values[i] = element_value.read(cr); 1.214 + } 1.215 + 1.216 + @Override 1.217 + public int length() { 1.218 + int n = 2; 1.219 + for (int i = 0; i < values.length; i++) 1.220 + n += values[i].length(); 1.221 + return n; 1.222 + } 1.223 + 1.224 + public <R,P> R accept(Visitor<R,P> visitor, P p) { 1.225 + return visitor.visitArray(this, p); 1.226 + } 1.227 + 1.228 + public final int num_values; 1.229 + public final element_value[] values; 1.230 + } 1.231 + 1.232 + public static class element_value_pair { 1.233 + element_value_pair(ClassReader cr) 1.234 + throws IOException, InvalidAnnotation { 1.235 + element_name_index = cr.readUnsignedShort(); 1.236 + value = element_value.read(cr); 1.237 + } 1.238 + 1.239 + public int length() { 1.240 + return 2 + value.length(); 1.241 + } 1.242 + 1.243 + public final int element_name_index; 1.244 + public final element_value value; 1.245 + } 1.246 +}