javap/src/main/java/org/apidesign/javap/AnnotationParser.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 12 Dec 2012 20:35:01 +0100
branchbenchmarks
changeset 313 398110b3eee6
parent 249 001389026dbf
child 405 e41809be6106
child 652 f095ea52f417
permissions -rw-r--r--
Backed out changeset 5a053b8f40ad: Now when the test finally passed, we can increase the time out again.wq
     1 /*
     2  * Copyright (c) 2002, Oracle and/or its affiliates. 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 package org.apidesign.javap;
    26 
    27 import java.io.ByteArrayInputStream;
    28 import java.io.DataInputStream;
    29 import java.io.IOException;
    30 
    31 /** An abstract parser for annotation definitions. Analyses the bytes and
    32  * performs some callbacks to the overriden parser methods.
    33  *
    34  * @author Jaroslav Tulach <jtulach@netbeans.org>
    35  */
    36 public class AnnotationParser {
    37     private final boolean textual;
    38     
    39     protected AnnotationParser(boolean textual) {
    40         this.textual = textual;
    41     }
    42 
    43     protected void visitAnnotationStart(String type) throws IOException {
    44     }
    45 
    46     protected void visitAnnotationEnd(String type) throws IOException {
    47     }
    48     
    49     protected void visitAttr(
    50         String annoType, String attr, String attrType, String value
    51     ) throws IOException {
    52     }
    53     
    54     /** Initialize the parsing with constant pool from <code>cd</code>.
    55      * 
    56      * @param attr the attribute defining annotations
    57      * @param cd constant pool
    58      * @throws IOException in case I/O fails
    59      */
    60     public final void parse(byte[] attr, ClassData cd) throws IOException {
    61         ByteArrayInputStream is = new ByteArrayInputStream(attr);
    62         DataInputStream dis = new DataInputStream(is);
    63         try {
    64             read(dis, cd);
    65         } finally {
    66             is.close();
    67         }
    68     }
    69     
    70     private void read(DataInputStream dis, ClassData cd) throws IOException {
    71     	int cnt = dis.readUnsignedShort();
    72         for (int i = 0; i < cnt; i++) {
    73             readAnno(dis, cd);
    74         }
    75     }
    76 
    77     private void readAnno(DataInputStream dis, ClassData cd) throws IOException {
    78         int type = dis.readUnsignedShort();
    79         String typeName = cd.StringValue(type);
    80         visitAnnotationStart(typeName);
    81     	int cnt = dis.readUnsignedShort();
    82     	for (int i = 0; i < cnt; i++) {
    83             String attrName = cd.StringValue(dis.readUnsignedShort());
    84             readValue(dis, cd, typeName, attrName);
    85         }
    86         visitAnnotationEnd(typeName);
    87         if (cnt == 0) {
    88             visitAttr(typeName, null, null, null);
    89         }
    90     }
    91 
    92     private void readValue(DataInputStream dis, ClassData cd, String typeName, String attrName) 
    93     throws IOException {
    94         char type = (char)dis.readByte();
    95         if (type == '@') {
    96             readAnno(dis, cd);
    97         } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N
    98             int primitive = dis.readUnsignedShort();
    99             String val = cd.stringValue(primitive, textual);
   100             String attrType;
   101             if (type == 's') {
   102                 attrType = "Ljava_lang_String_2";
   103                 if (textual) {
   104                     val = '"' + val + '"';
   105                 }
   106             } else {
   107                 attrType = "" + type;
   108             }
   109             visitAttr(typeName, attrName, attrType, val);
   110         } else if (type == 'c') {
   111             int cls = dis.readUnsignedShort();
   112         } else if (type == '[') {
   113             int cnt = dis.readUnsignedShort();
   114             for (int i = 0; i < cnt; i++) {
   115                 readValue(dis, cd, typeName, attrName);
   116             }
   117         } else if (type == 'e') {
   118             int enumT = dis.readUnsignedShort();
   119             int enumN = dis.readUnsignedShort();
   120         } else {
   121             throw new IOException("Unknown type " + type);
   122         }
   123     }
   124 }