/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */
package org.apache.tools.ant.taskdefs.optional.depend.constantpool;

import java.io.DataInputStream;
import java.io.IOException;

/**
 * An entry in the constant pool. This class contains a represenation of the
 * constant pool entries. It is an abstract base class for all the different
 * forms of constant pool entry.
 *
 * @author Conor MacNeill
 * @see ConstantPool
 */
public abstract class ConstantPoolEntry {

    /** Tag value for UTF8 entries. */
    public final static int CONSTANT_UTF8 = 1;

    /** Tag value for Integer entries. */
    public final static int CONSTANT_INTEGER = 3;

    /** Tag value for Float entries. */
    public final static int CONSTANT_FLOAT = 4;

    /** Tag value for Long entries. */
    public final static int CONSTANT_LONG = 5;

    /** Tag value for Double entries. */
    public final static int CONSTANT_DOUBLE = 6;

    /** Tag value for Class entries. */
    public final static int CONSTANT_CLASS = 7;

    /** Tag value for String entries. */
    public final static int CONSTANT_STRING = 8;

    /** Tag value for Field Reference entries. */
    public final static int CONSTANT_FIELDREF = 9;

    /** Tag value for Method Reference entries. */
    public final static int CONSTANT_METHODREF = 10;

    /** Tag value for Interface Method Reference entries. */
    public final static int CONSTANT_INTERFACEMETHODREF = 11;

    /** Tag value for Name and Type entries. */
    public final static int CONSTANT_NAMEANDTYPE = 12;

    /**
     * This entry's tag which identifies the type of this constant pool
     * entry.
     */
    private int tag;

    /**
     * The number of slots in the constant pool, occupied by this entry.
     */
    private int numEntries;

    /**
     * A flag which indiciates if this entry has been resolved or not.
     */
    private boolean resolved;

    /**
     * Initialse the constant pool entry.
     *
     * @param tagValue the tag value which identifies which type of constant
     *      pool entry this is.
     * @param entries the number of constant pool entry slots this entry
     *      occupies.
     */
    public ConstantPoolEntry(int tagValue, int entries) {
        tag = tagValue;
        numEntries = entries;
        resolved = false;
    }

    /**
     * Read a constant pool entry from a stream. This is a factory method
     * which reads a constant pool entry form a stream and returns the
     * appropriate subclass for the entry.
     *
     * @param cpStream the stream from which the constant pool entry is to
     *      be read.
     * @return the appropriate ConstantPoolEntry subclass representing the
     *      constant pool entry from the stream.
     * @exception IOException if the constant pool entry cannot be read 
     *      from the stream
     */
    public static ConstantPoolEntry readEntry(DataInputStream cpStream)
         throws IOException {
        ConstantPoolEntry cpInfo = null;
        int cpTag = cpStream.readUnsignedByte();

        switch (cpTag) {

            case CONSTANT_UTF8:
                cpInfo = new Utf8CPInfo();

                break;
            case CONSTANT_INTEGER:
                cpInfo = new IntegerCPInfo();

                break;
            case CONSTANT_FLOAT:
                cpInfo = new FloatCPInfo();

                break;
            case CONSTANT_LONG:
                cpInfo = new LongCPInfo();

                break;
            case CONSTANT_DOUBLE:
                cpInfo = new DoubleCPInfo();

                break;
            case CONSTANT_CLASS:
                cpInfo = new ClassCPInfo();

                break;
            case CONSTANT_STRING:
                cpInfo = new StringCPInfo();

                break;
            case CONSTANT_FIELDREF:
                cpInfo = new FieldRefCPInfo();

                break;
            case CONSTANT_METHODREF:
                cpInfo = new MethodRefCPInfo();

                break;
            case CONSTANT_INTERFACEMETHODREF:
                cpInfo = new InterfaceMethodRefCPInfo();

                break;
            case CONSTANT_NAMEANDTYPE:
                cpInfo = new NameAndTypeCPInfo();

                break;
            default:
                throw new ClassFormatError("Invalid Constant Pool entry Type "
                     + cpTag);
        }

        cpInfo.read(cpStream);

        return cpInfo;
    }

    /**
     * Indicates whether this entry has been resolved. In general a constant
     * pool entry can reference another constant pool entry by its index
     * value. Resolution involves replacing this index value with the
     * constant pool entry at that index.
     *
     * @return true if this entry has been resolved.
     */
    public boolean isResolved() {
        return resolved;
    }

    /**
     * Resolve this constant pool entry with respect to its dependents in
     * the constant pool.
     *
     * @param constantPool the constant pool of which this entry is a member
     *      and against which this entry is to be resolved.
     */
    public void resolve(ConstantPool constantPool) {
        resolved = true;
    }

    /**
     * read a constant pool entry from a class stream.
     *
     * @param cpStream the DataInputStream which contains the constant pool
     *      entry to be read.
     * @exception IOException if there is a problem reading the entry from
     *      the stream.
     */
    public abstract void read(DataInputStream cpStream) throws IOException;

    /**
     * Get the Entry's type tag.
     *
     * @return The Tag value of this entry
     */
    public int getTag() {
        return tag;
    }

    /**
     * Get the number of Constant Pool Entry slots within the constant pool
     * occupied by this entry.
     *
     * @return the number of slots used.
     */
    public final int getNumEntries() {
        return numEntries;
    }

}

