2 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25 package org.apidesign.javap;
27 import java.io.ByteArrayInputStream;
28 import java.io.DataInputStream;
29 import java.io.IOException;
31 /** An abstract parser for annotation definitions. Analyses the bytes and
32 * performs some callbacks to the overriden parser methods.
34 * @author Jaroslav Tulach <jtulach@netbeans.org>
36 public class AnnotationParser {
37 protected AnnotationParser() {
40 protected void visitAttr(String type, String attr, String value) {
43 /** Initialize the parsing with constant pool from <code>cd</code>.
45 * @param attr the attribute defining annotations
46 * @param cd constant pool
47 * @throws IOException in case I/O fails
49 public final void parse(byte[] attr, ClassData cd) throws IOException {
50 ByteArrayInputStream is = new ByteArrayInputStream(attr);
51 DataInputStream dis = new DataInputStream(is);
59 private void read(DataInputStream dis, ClassData cd) throws IOException {
60 int cnt = dis.readUnsignedShort();
61 for (int i = 0; i < cnt; i++) {
66 private void readAnno(DataInputStream dis, ClassData cd) throws IOException {
67 int type = dis.readUnsignedShort();
68 String typeName = cd.StringValue(type);
69 int cnt = dis.readUnsignedShort();
70 for (int i = 0; i < cnt; i++) {
71 String attrName = cd.StringValue(dis.readUnsignedShort());
72 readValue(dis, cd, typeName, attrName);
76 private void readValue(DataInputStream dis, ClassData cd, String typeName, String attrName)
78 char type = (char)dis.readByte();
81 } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N
82 int primitive = dis.readUnsignedShort();
83 visitAttr(typeName, attrName, cd.StringValue(primitive));
84 } else if (type == 'c') {
85 int cls = dis.readUnsignedShort();
86 } else if (type == '[') {
87 int cnt = dis.readUnsignedShort();
88 for (int i = 0; i < cnt; i++) {
89 readValue(dis, cd, typeName, attrName);
91 } else if (type == 'e') {
92 int enumT = dis.readUnsignedShort();
93 int enumN = dis.readUnsignedShort();
95 throw new IOException("Unknown type " + type);