javap/src/main/java/org/apidesign/javap/AnnotationParser.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 02 Dec 2012 12:39:51 +0100
branchreflection
changeset 236 d97770281580
parent 235 bf0a77f029c4
child 237 84ffc347412d
permissions -rw-r--r--
Using the attribute type to in the attribute method name
     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     protected AnnotationParser() {
    38     }
    39 
    40     protected void visitAnnotationStart(String type) throws IOException {
    41     }
    42 
    43     protected void visitAnnotationEnd(String type) throws IOException {
    44     }
    45     
    46     protected void visitAttr(
    47         String annoType, String attr, String attrType, String value
    48     ) throws IOException {
    49     }
    50     
    51     /** Initialize the parsing with constant pool from <code>cd</code>.
    52      * 
    53      * @param attr the attribute defining annotations
    54      * @param cd constant pool
    55      * @throws IOException in case I/O fails
    56      */
    57     public final void parse(byte[] attr, ClassData cd) throws IOException {
    58         ByteArrayInputStream is = new ByteArrayInputStream(attr);
    59         DataInputStream dis = new DataInputStream(is);
    60         try {
    61             read(dis, cd);
    62         } finally {
    63             is.close();
    64         }
    65     }
    66     
    67     private void read(DataInputStream dis, ClassData cd) throws IOException {
    68     	int cnt = dis.readUnsignedShort();
    69         for (int i = 0; i < cnt; i++) {
    70             readAnno(dis, cd);
    71         }
    72     }
    73 
    74     private void readAnno(DataInputStream dis, ClassData cd) throws IOException {
    75         int type = dis.readUnsignedShort();
    76         String typeName = cd.StringValue(type);
    77         visitAnnotationStart(typeName);
    78     	int cnt = dis.readUnsignedShort();
    79     	for (int i = 0; i < cnt; i++) {
    80             String attrName = cd.StringValue(dis.readUnsignedShort());
    81             readValue(dis, cd, typeName, attrName);
    82         }
    83         visitAnnotationEnd(typeName);
    84     }
    85 
    86     private void readValue(DataInputStream dis, ClassData cd, String typeName, String attrName) 
    87     throws IOException {
    88         char type = (char)dis.readByte();
    89         if (type == '@') {
    90             readAnno(dis, cd);
    91         } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N
    92             int primitive = dis.readUnsignedShort();
    93             String attrType;
    94             if (type == 's') {
    95                 attrType = "Ljava_lang_String";
    96             } else {
    97                 attrType = "" + type;
    98             }
    99             visitAttr(typeName, attrName, attrType, cd.StringValue(primitive));
   100         } else if (type == 'c') {
   101             int cls = dis.readUnsignedShort();
   102         } else if (type == '[') {
   103             int cnt = dis.readUnsignedShort();
   104             for (int i = 0; i < cnt; i++) {
   105                 readValue(dis, cd, typeName, attrName);
   106             }
   107         } else if (type == 'e') {
   108             int enumT = dis.readUnsignedShort();
   109             int enumN = dis.readUnsignedShort();
   110         } else {
   111             throw new IOException("Unknown type " + type);
   112         }
   113     }
   114 }