jaroslav@152: /* jaroslav@152: * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. jaroslav@152: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@152: * jaroslav@152: * This code is free software; you can redistribute it and/or modify it jaroslav@152: * under the terms of the GNU General Public License version 2 only, as jaroslav@152: * published by the Free Software Foundation. Oracle designates this jaroslav@152: * particular file as subject to the "Classpath" exception as provided jaroslav@152: * by Oracle in the LICENSE file that accompanied this code. jaroslav@152: * jaroslav@152: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@152: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@152: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@152: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@152: * accompanied this code). jaroslav@152: * jaroslav@152: * You should have received a copy of the GNU General Public License version jaroslav@152: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@152: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@152: * jaroslav@152: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@152: * or visit www.oracle.com if you need additional information or have any jaroslav@152: * questions. jaroslav@152: */ jtulach@167: package org.apidesign.javap; jaroslav@152: jaroslav@152: import java.io.ByteArrayInputStream; jaroslav@152: import java.io.DataInputStream; jaroslav@152: import java.io.IOException; jaroslav@152: jaroslav@152: /** An abstract parser for annotation definitions. Analyses the bytes and jaroslav@152: * performs some callbacks to the overriden parser methods. jaroslav@152: * jaroslav@152: * @author Jaroslav Tulach jaroslav@152: */ jaroslav@152: public class AnnotationParser { jaroslav@237: private final boolean textual; jaroslav@237: jaroslav@237: protected AnnotationParser(boolean textual) { jaroslav@237: this.textual = textual; jaroslav@152: } jaroslav@152: jaroslav@235: protected void visitAnnotationStart(String type) throws IOException { jaroslav@235: } jaroslav@235: jaroslav@235: protected void visitAnnotationEnd(String type) throws IOException { jaroslav@235: } jaroslav@235: jaroslav@235: protected void visitAttr( jaroslav@236: String annoType, String attr, String attrType, String value jaroslav@235: ) throws IOException { jaroslav@152: } jaroslav@152: jaroslav@152: /** Initialize the parsing with constant pool from cd. jaroslav@152: * jaroslav@152: * @param attr the attribute defining annotations jaroslav@152: * @param cd constant pool jaroslav@152: * @throws IOException in case I/O fails jaroslav@152: */ jaroslav@152: public final void parse(byte[] attr, ClassData cd) throws IOException { jaroslav@152: ByteArrayInputStream is = new ByteArrayInputStream(attr); jaroslav@152: DataInputStream dis = new DataInputStream(is); jaroslav@152: try { jaroslav@152: read(dis, cd); jaroslav@152: } finally { jaroslav@152: is.close(); jaroslav@152: } jaroslav@152: } jaroslav@152: jaroslav@152: private void read(DataInputStream dis, ClassData cd) throws IOException { jaroslav@152: int cnt = dis.readUnsignedShort(); jaroslav@152: for (int i = 0; i < cnt; i++) { jaroslav@152: readAnno(dis, cd); jaroslav@152: } jaroslav@152: } jaroslav@152: jaroslav@152: private void readAnno(DataInputStream dis, ClassData cd) throws IOException { jaroslav@152: int type = dis.readUnsignedShort(); jaroslav@152: String typeName = cd.StringValue(type); jaroslav@235: visitAnnotationStart(typeName); jaroslav@152: int cnt = dis.readUnsignedShort(); jaroslav@152: for (int i = 0; i < cnt; i++) { jaroslav@153: String attrName = cd.StringValue(dis.readUnsignedShort()); jaroslav@153: readValue(dis, cd, typeName, attrName); jaroslav@152: } jaroslav@235: visitAnnotationEnd(typeName); jaroslav@240: if (cnt == 0) { jaroslav@249: visitAttr(typeName, null, null, null); jaroslav@240: } jaroslav@152: } jaroslav@152: jaroslav@153: private void readValue(DataInputStream dis, ClassData cd, String typeName, String attrName) jaroslav@152: throws IOException { jaroslav@152: char type = (char)dis.readByte(); jaroslav@152: if (type == '@') { jaroslav@152: readAnno(dis, cd); jaroslav@152: } else if ("CFJZsSIDB".indexOf(type) >= 0) { // NOI18N jaroslav@152: int primitive = dis.readUnsignedShort(); jaroslav@237: String val = cd.stringValue(primitive, textual); jaroslav@236: String attrType; jaroslav@236: if (type == 's') { jaroslav@252: attrType = "Ljava_lang_String_2"; jaroslav@237: if (textual) { jaroslav@237: val = '"' + val + '"'; jaroslav@237: } jaroslav@236: } else { jaroslav@236: attrType = "" + type; jaroslav@236: } jaroslav@237: visitAttr(typeName, attrName, attrType, val); jaroslav@152: } else if (type == 'c') { jaroslav@152: int cls = dis.readUnsignedShort(); jaroslav@152: } else if (type == '[') { jaroslav@152: int cnt = dis.readUnsignedShort(); jaroslav@152: for (int i = 0; i < cnt; i++) { jaroslav@153: readValue(dis, cd, typeName, attrName); jaroslav@152: } jaroslav@152: } else if (type == 'e') { jaroslav@152: int enumT = dis.readUnsignedShort(); jaroslav@152: int enumN = dis.readUnsignedShort(); jaroslav@152: } else { jaroslav@152: throw new IOException("Unknown type " + type); jaroslav@152: } jaroslav@152: } jaroslav@152: }