286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/Apackage com.sun.org.apache.bcel.internal.classfile;
286N/A
286N/A/* ====================================================================
286N/A * The Apache Software License, Version 1.1
286N/A *
286N/A * Copyright (c) 2001 The Apache Software Foundation. All rights
286N/A * reserved.
286N/A *
286N/A * Redistribution and use in source and binary forms, with or without
286N/A * modification, are permitted provided that the following conditions
286N/A * are met:
286N/A *
286N/A * 1. Redistributions of source code must retain the above copyright
286N/A * notice, this list of conditions and the following disclaimer.
286N/A *
286N/A * 2. Redistributions in binary form must reproduce the above copyright
286N/A * notice, this list of conditions and the following disclaimer in
286N/A * the documentation and/or other materials provided with the
286N/A * distribution.
286N/A *
286N/A * 3. The end-user documentation included with the redistribution,
286N/A * if any, must include the following acknowledgment:
286N/A * "This product includes software developed by the
286N/A * Apache Software Foundation (http://www.apache.org/)."
286N/A * Alternately, this acknowledgment may appear in the software itself,
286N/A * if and wherever such third-party acknowledgments normally appear.
286N/A *
286N/A * 4. The names "Apache" and "Apache Software Foundation" and
286N/A * "Apache BCEL" must not be used to endorse or promote products
286N/A * derived from this software without prior written permission. For
286N/A * written permission, please contact apache@apache.org.
286N/A *
286N/A * 5. Products derived from this software may not be called "Apache",
286N/A * "Apache BCEL", nor may "Apache" appear in their name, without
286N/A * prior written permission of the Apache Software Foundation.
286N/A *
286N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
286N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
286N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
286N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
286N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
286N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
286N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
286N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
286N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
286N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
286N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286N/A * SUCH DAMAGE.
286N/A * ====================================================================
286N/A *
286N/A * This software consists of voluntary contributions made by many
286N/A * individuals on behalf of the Apache Software Foundation. For more
286N/A * information on the Apache Software Foundation, please see
286N/A * <http://www.apache.org/>.
286N/A */
286N/A
286N/Aimport com.sun.org.apache.bcel.internal.Constants;
286N/Aimport java.io.*;
286N/A
286N/A/**
286N/A * This class is derived from <em>Attribute</em> and represents a reference
286N/A * to a <href="http://wwwipd.ira.uka.de/~pizza/gj/">GJ</a> attribute.
286N/A *
286N/A * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
286N/A * @see Attribute
286N/A */
286N/Apublic final class Signature extends Attribute {
286N/A private int signature_index;
286N/A
286N/A /**
286N/A * Initialize from another object. Note that both objects use the same
286N/A * references (shallow copy). Use clone() for a physical copy.
286N/A */
286N/A public Signature(Signature c) {
286N/A this(c.getNameIndex(), c.getLength(), c.getSignatureIndex(), c.getConstantPool());
286N/A }
286N/A
286N/A /**
286N/A * Construct object from file stream.
286N/A * @param name_index Index in constant pool to CONSTANT_Utf8
286N/A * @param length Content length in bytes
286N/A * @param file Input stream
286N/A * @param constant_pool Array of constants
286N/A * @throws IOException
286N/A */
286N/A Signature(int name_index, int length, DataInputStream file,
286N/A ConstantPool constant_pool) throws IOException
286N/A {
286N/A this(name_index, length, file.readUnsignedShort(), constant_pool);
286N/A }
286N/A
286N/A /**
286N/A * @param name_index Index in constant pool to CONSTANT_Utf8
286N/A * @param length Content length in bytes
286N/A * @param constant_pool Array of constants
286N/A * @param Signature_index Index in constant pool to CONSTANT_Utf8
286N/A */
286N/A public Signature(int name_index, int length, int signature_index,
286N/A ConstantPool constant_pool)
286N/A {
286N/A super(Constants.ATTR_SIGNATURE, name_index, length, constant_pool);
286N/A this.signature_index = signature_index;
286N/A }
286N/A
286N/A /**
286N/A * Called by objects that are traversing the nodes of the tree implicitely
286N/A * defined by the contents of a Java class. I.e., the hierarchy of methods,
286N/A * fields, attributes, etc. spawns a tree of objects.
286N/A *
286N/A * @param v Visitor object
286N/A */
286N/A public void accept(Visitor v) {
286N/A System.err.println("Visiting non-standard Signature object");
286N/A v.visitSignature(this);
286N/A }
286N/A
286N/A /**
286N/A * Dump source file attribute to file stream in binary format.
286N/A *
286N/A * @param file Output file stream
286N/A * @throws IOException
286N/A */
286N/A public final void dump(DataOutputStream file) throws IOException
286N/A {
286N/A super.dump(file);
286N/A file.writeShort(signature_index);
286N/A }
286N/A
286N/A /**
286N/A * @return Index in constant pool of source file name.
286N/A */
286N/A public final int getSignatureIndex() { return signature_index; }
286N/A
286N/A /**
286N/A * @param Signature_index.
286N/A */
286N/A public final void setSignatureIndex(int signature_index) {
286N/A this.signature_index = signature_index;
286N/A }
286N/A
286N/A /**
286N/A * @return GJ signature.
286N/A */
286N/A public final String getSignature() {
286N/A ConstantUtf8 c = (ConstantUtf8)constant_pool.getConstant(signature_index,
286N/A Constants.CONSTANT_Utf8);
286N/A return c.getBytes();
286N/A }
286N/A
286N/A /**
286N/A * Extends ByteArrayInputStream to make 'unreading' chars possible.
286N/A */
286N/A private static final class MyByteArrayInputStream extends ByteArrayInputStream {
286N/A MyByteArrayInputStream(String data) { super(data.getBytes()); }
286N/A final int mark() { return pos; }
286N/A final String getData() { return new String(buf); }
286N/A final void reset(int p) { pos = p; }
286N/A final void unread() { if(pos > 0) pos--; }
286N/A }
286N/A
286N/A private static boolean identStart(int ch) {
286N/A return ch == 'T' || ch == 'L';
286N/A }
286N/A
286N/A private static boolean identPart(int ch) {
286N/A return ch == '/' || ch == ';';
286N/A }
286N/A
286N/A private static final void matchIdent(MyByteArrayInputStream in, StringBuffer buf) {
286N/A int ch;
286N/A
286N/A if((ch = in.read()) == -1)
286N/A throw new RuntimeException("Illegal signature: " + in.getData() +
286N/A " no ident, reaching EOF");
286N/A
286N/A //System.out.println("return from ident:" + (char)ch);
286N/A
286N/A if(!identStart(ch)) {
286N/A StringBuffer buf2 = new StringBuffer();
286N/A
286N/A int count = 1;
286N/A while(Character.isJavaIdentifierPart((char)ch)) {
286N/A buf2.append((char)ch);
286N/A count++;
286N/A ch = in.read();
286N/A }
286N/A
286N/A if(ch == ':') { // Ok, formal parameter
286N/A in.skip("Ljava/lang/Object".length());
286N/A buf.append(buf2);
286N/A
286N/A ch = in.read();
286N/A in.unread();
286N/A //System.out.println("so far:" + buf2 + ":next:" +(char)ch);
286N/A } else {
286N/A for(int i=0; i < count; i++)
286N/A in.unread();
286N/A }
286N/A
286N/A return;
286N/A }
286N/A
286N/A StringBuffer buf2 = new StringBuffer();
286N/A ch = in.read();
286N/A
286N/A do {
286N/A buf2.append((char)ch);
286N/A ch = in.read();
286N/A //System.out.println("within ident:"+ (char)ch);
286N/A
286N/A } while((ch != -1) && (Character.isJavaIdentifierPart((char)ch) || (ch == '/')));
286N/A
286N/A buf.append(buf2.toString().replace('/', '.'));
286N/A
286N/A //System.out.println("regular return ident:"+ (char)ch + ":" + buf2);
286N/A
286N/A if(ch != -1)
286N/A in.unread();
286N/A }
286N/A
286N/A private static final void matchGJIdent(MyByteArrayInputStream in,
286N/A StringBuffer buf)
286N/A {
286N/A int ch;
286N/A
286N/A matchIdent(in, buf);
286N/A
286N/A ch = in.read();
286N/A if((ch == '<') || ch == '(') { // Parameterized or method
286N/A //System.out.println("Enter <");
286N/A buf.append((char)ch);
286N/A matchGJIdent(in, buf);
286N/A
286N/A while(((ch = in.read()) != '>') && (ch != ')')) { // List of parameters
286N/A if(ch == -1)
286N/A throw new RuntimeException("Illegal signature: " + in.getData() +
286N/A " reaching EOF");
286N/A
286N/A //System.out.println("Still no >");
286N/A buf.append(", ");
286N/A in.unread();
286N/A matchGJIdent(in, buf); // Recursive call
286N/A }
286N/A
286N/A //System.out.println("Exit >");
286N/A
286N/A buf.append((char)ch);
286N/A } else
286N/A in.unread();
286N/A
286N/A ch = in.read();
286N/A if(identStart(ch)) {
286N/A in.unread();
286N/A matchGJIdent(in, buf);
286N/A } else if(ch == ')') {
286N/A in.unread();
286N/A return;
286N/A } else if(ch != ';')
286N/A throw new RuntimeException("Illegal signature: " + in.getData() + " read " +
286N/A (char)ch);
286N/A }
286N/A
286N/A public static String translate(String s) {
286N/A //System.out.println("Sig:" + s);
286N/A StringBuffer buf = new StringBuffer();
286N/A
286N/A matchGJIdent(new MyByteArrayInputStream(s), buf);
286N/A
286N/A return buf.toString();
286N/A }
286N/A
286N/A public static final boolean isFormalParameterList(String s) {
286N/A return s.startsWith("<") && (s.indexOf(':') > 0);
286N/A }
286N/A
286N/A public static final boolean isActualParameterList(String s) {
286N/A return s.startsWith("L") && s.endsWith(">;");
286N/A }
286N/A
286N/A /**
286N/A * @return String representation
286N/A */
286N/A public final String toString() {
286N/A String s = getSignature();
286N/A
286N/A return "Signature(" + s + ")";
286N/A }
286N/A
286N/A /**
286N/A * @return deep copy of this attribute
286N/A */
286N/A public Attribute copy(ConstantPool constant_pool) {
286N/A return (Signature)clone();
286N/A }
286N/A}