javap/src/main/java/org/apidesign/javap/AnnotationParser.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 03 Feb 2013 18:58:09 +0100
branchreflection
changeset 652 f095ea52f417
parent 252 55d2676f82c9
child 653 bcdfc29fd004
permissions -rw-r--r--
Supporting annotations with arrays
     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     private final boolean iterateArray;
    39     
    40     protected AnnotationParser(boolean textual, boolean iterateArray) {
    41         this.textual = textual;
    42         this.iterateArray = iterateArray;
    43     }
    44 
    45     protected void visitAnnotationStart(String type) throws IOException {
    46     }
    47 
    48     protected void visitAnnotationEnd(String type) throws IOException {
    49     }
    50 
    51     protected void visitValueStart(String attrName, char type) throws IOException {
    52     }
    53 
    54     protected void visitValueEnd(String attrName, char type) throws IOException {
    55     }
    56 
    57     
    58     protected void visitAttr(
    59         String annoType, String attr, String attrType, String value
    60     ) throws IOException {
    61     }
    62     
    63     /** Initialize the parsing with constant pool from <code>cd</code>.
    64      * 
    65      * @param attr the attribute defining annotations
    66      * @param cd constant pool
    67      * @throws IOException in case I/O fails
    68      */
    69     public final void parse(byte[] attr, ClassData cd) throws IOException {
    70         ByteArrayInputStream is = new ByteArrayInputStream(attr);
    71         DataInputStream dis = new DataInputStream(is);
    72         try {
    73             read(dis, cd);
    74         } finally {
    75             is.close();
    76         }
    77     }
    78     
    79     private void read(DataInputStream dis, ClassData cd) throws IOException {
    80     	int cnt = dis.readUnsignedShort();
    81         for (int i = 0; i < cnt; i++) {
    82             readAnno(dis, cd);
    83         }
    84     }
    85 
    86     private void readAnno(DataInputStream dis, ClassData cd) throws IOException {
    87         int type = dis.readUnsignedShort();
    88         String typeName = cd.StringValue(type);
    89         visitAnnotationStart(typeName);
    90     	int cnt = dis.readUnsignedShort();
    91     	for (int i = 0; i < cnt; i++) {
    92             String attrName = cd.StringValue(dis.readUnsignedShort());
    93             readValue(dis, cd, typeName, attrName);
    94         }
    95         visitAnnotationEnd(typeName);
    96         if (cnt == 0) {
    97             visitAttr(typeName, null, null, null);
    98         }
    99     }
   100 
   101     private void readValue(
   102         DataInputStream dis, ClassData cd, String typeName, String attrName
   103     ) throws IOException {
   104         char type = (char)dis.readByte();
   105         visitValueStart(attrName, type);
   106         if (type == '@') {
   107             readAnno(dis, cd);
   108         } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N
   109             int primitive = dis.readUnsignedShort();
   110             String val = cd.stringValue(primitive, textual);
   111             String attrType;
   112             if (type == 's') {
   113                 attrType = "Ljava_lang_String_2";
   114                 if (textual) {
   115                     val = '"' + val + '"';
   116                 }
   117             } else {
   118                 attrType = "" + type;
   119             }
   120             visitAttr(typeName, attrName, attrType, val);
   121         } else if (type == 'c') {
   122             int cls = dis.readUnsignedShort();
   123         } else if (type == '[') {
   124             int cnt = dis.readUnsignedShort();
   125             for (int i = 0; i < cnt; i++) {
   126                 readValue(dis, cd, typeName, iterateArray ? attrName : null);
   127             }
   128         } else if (type == 'e') {
   129             int enumT = dis.readUnsignedShort();
   130             String attrType = cd.stringValue(enumT, textual);
   131             int enumN = dis.readUnsignedShort();
   132             String val = cd.stringValue(enumN, textual);
   133             if (textual) {
   134                 val = '"' + val + '"';
   135             }
   136             visitAttr(typeName, attrName, attrType, val);
   137         } else {
   138             throw new IOException("Unknown type " + type);
   139         }
   140         visitValueEnd(attrName, type);
   141     }
   142 }