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

Fri, 26 Jun 2009 19:12:41 -0700

author
jjg
date
Fri, 26 Jun 2009 19:12:41 -0700
changeset 309
664edca41e34
parent 229
03bcd66bd8e7
child 554
9d9f26857129
permissions
-rw-r--r--

6855544: add missing files
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.edu

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

mercurial