001    /*
002     * @(#)APoolInfo.java 1.5 95/08/16 Chuck McManis
003     *
004     * Copyright (c) 1996 Chuck McManis, All Rights Reserved.
005     *
006     * Permission to use, copy, modify, and distribute this software
007     * and its documentation for NON-COMMERCIAL purposes and without
008     * fee is hereby granted provided that this copyright notice
009     * appears in all copies.
010     *
011     * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
012     * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
013     * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
014     * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE
015     * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
016     * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
017     */
018    
019    package edu.rice.cs.cunit.classFile.constantPool;
020    
021    import edu.rice.cs.cunit.classFile.constantPool.visitors.IPoolInfoVisitor;
022    
023    import edu.rice.cs.cunit.util.PositionDataInputStream;
024    import java.io.DataOutputStream;
025    import java.io.IOException;
026    import java.util.ArrayList;
027    
028    /**
029     * Represents an abstract constant pool object.
030     *
031     * @author Mathias Ricken
032     */
033    public abstract class APoolInfo {
034        // Constant pool object types
035        public static final int CONSTANT_Class = 7;
036        public static final int CONSTANT_Fieldref = 9;
037        public static final int CONSTANT_Methodref = 10;
038        public static final int CONSTANT_InterfaceMethodref = 11;
039        public static final int CONSTANT_String = 8;
040        public static final int CONSTANT_Integer = 3;
041        public static final int CONSTANT_Float = 4;
042        public static final int CONSTANT_Long = 5;
043        public static final int CONSTANT_Double = 6;
044        public static final int CONSTANT_NameAndType = 12;
045        public static final int CONSTANT_Utf8_ASCII = 1;
046        public static final int CONSTANT_Utf8_Unicode = 2;
047    
048        /**
049         * Object type.
050         */
051        protected int _type;
052    
053        /**
054         * Reference to the constant pool.
055         */
056        protected ConstantPool _constantPool;
057    
058        /**
059         * Constructor
060         *
061         * @param type object type
062         */
063        public APoolInfo(int type, ConstantPool cp) {
064            _type = type;
065            _constantPool = cp;
066        }
067    
068        /**
069         * Read from stream and return unresolved constant pool object.
070         *
071         * @param dis stream
072         *
073         * @return unresolved constant pool object.
074         *
075         * @throws IOException
076         * @throws ClassFormatError
077         */
078        public static APoolInfo read(PositionDataInputStream dis, ConstantPool cp) throws IOException, ClassFormatError {
079            int type = dis.readByte();
080            APoolInfo item;
081            switch(type) {
082                case CONSTANT_Class:
083                    item = new ClassPoolInfo(dis, cp);
084                    break;
085                case CONSTANT_Fieldref:
086                    item = new FieldPoolInfo(dis, cp);
087                    break;
088                case CONSTANT_Methodref:
089                    item = new MethodPoolInfo(dis, cp);
090                    break;
091                case CONSTANT_InterfaceMethodref:
092                    item = new InterfaceMethodPoolInfo(dis, cp);
093                    break;
094                case CONSTANT_NameAndType:
095                    item = new NameAndTypePoolInfo(dis, cp);
096                    break;
097                case CONSTANT_String:
098                    item = new StringPoolInfo(dis, cp);
099                    break;
100                case CONSTANT_Integer:
101                    item = new IntegerPoolInfo(dis, cp);
102                    break;
103                case CONSTANT_Float:
104                    item = new FloatPoolInfo(dis, cp);
105                    break;
106                case CONSTANT_Long:
107                    item = new LongPoolInfo(dis, cp);
108                    break;
109                case CONSTANT_Double:
110                    item = new DoublePoolInfo(dis, cp);
111                    break;
112                case CONSTANT_Utf8_ASCII:
113                    item = new ASCIIPoolInfo(dis, cp);
114                    break;
115                case CONSTANT_Utf8_Unicode:
116                    item = new UnicodePoolInfo(dis, cp);
117                    break;
118                default:
119                    throw new ClassFormatError("Unknown constant pool item type: "+type+", offset: "+(dis.getPosition()-1));
120            }
121            return item;
122        }
123    
124        /**
125         * Return the reference to the constant pool item that is already in pool, that matches this one.
126         *
127         * @param pool constant pool
128         *
129         * @return matching object or null if not found
130         */
131        public APoolInfo inPool(ConstantPool pool) {
132            for(APoolInfo cpi : pool) {
133                if (this.equals(cpi)) {
134                    return cpi;
135                }
136            }
137            return null;
138        }
139    
140        /**
141         * Write this constant pool object into the stream, including the type byte.
142         *
143         * @param dos stream
144         *
145         * @throws IOException
146         */
147        public abstract void write(DataOutputStream dos) throws IOException;
148    
149        /**
150         * Resolve constant pool objects. This makes sure that the object links match the index links.
151         */
152        public abstract void resolve();
153    
154        /**
155         * Reindex constant pool indices. This makes sure the index links match the object links.
156         */
157        public abstract void reindex();
158    
159        /**
160         * Return a human-readable version of this constant pool object.
161         *
162         * @return string
163         */
164        public abstract String toStringVerbose();
165    
166        /**
167         * Return a human-readable version of this constant pool object.
168         *
169         * @return string
170         */
171        public abstract String toString();
172    
173        /**
174         * Return a hash code.
175         *
176         * @return hash code
177         */
178        public abstract int hashCode();
179    
180        /**
181         * Compare this object and another one.
182         *
183         * @param obj other object
184         *
185         * @return true if the same
186         */
187        public abstract boolean equals(Object obj);
188    
189        /**
190         * Execute a visitor.
191         *
192         * @param visitor visitor
193         * @param data    visitor-specific parameter
194         *
195         * @return visitor-specific return value
196         */
197        public abstract <R, D> R execute(IPoolInfoVisitor<R, D> visitor, D data);
198    }