001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     *
017     */
018    package org.apache.bcel.classfile;
019    
020    import java.io.DataInput;
021    import java.io.DataOutputStream;
022    import java.io.IOException;
023    
024    import org.apache.bcel.Constants;
025    
026    /** 
027     * This class is derived from the abstract 
028     * <A HREF="org.apache.bcel.classfile.Constant.html">Constant</A> class 
029     * and represents a reference to a long object.
030     *
031     * @version $Id: ConstantLong.java 1152072 2011-07-29 01:54:05Z dbrosius $
032     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
033     * @see     Constant
034     */
035    public final class ConstantLong extends Constant implements ConstantObject {
036    
037        private static final long serialVersionUID = 8495971186433816161L;
038        private long bytes;
039    
040    
041        /** 
042         * @param bytes Data
043         */
044        public ConstantLong(long bytes) {
045            super(Constants.CONSTANT_Long);
046            this.bytes = bytes;
047        }
048    
049    
050        /**
051         * Initialize from another object.
052         */
053        public ConstantLong(ConstantLong c) {
054            this(c.getBytes());
055        }
056    
057    
058        /** 
059         * Initialize instance from file data.
060         *
061         * @param file Input stream
062         * @throws IOException
063         */
064        ConstantLong(DataInput file) throws IOException {
065            this(file.readLong());
066        }
067    
068    
069        /**
070         * Called by objects that are traversing the nodes of the tree implicitely
071         * defined by the contents of a Java class. I.e., the hierarchy of methods,
072         * fields, attributes, etc. spawns a tree of objects.
073         *
074         * @param v Visitor object
075         */
076        @Override
077        public void accept( Visitor v ) {
078            v.visitConstantLong(this);
079        }
080    
081    
082        /**
083         * Dump constant long to file stream in binary format.
084         *
085         * @param file Output file stream
086         * @throws IOException
087         */
088        @Override
089        public final void dump( DataOutputStream file ) throws IOException {
090            file.writeByte(tag);
091            file.writeLong(bytes);
092        }
093    
094    
095        /**
096         * @return data, i.e., 8 bytes.
097         */
098        public final long getBytes() {
099            return bytes;
100        }
101    
102    
103        /**
104         * @param bytes thr raw bytes that represent this long
105         */
106        public final void setBytes( long bytes ) {
107            this.bytes = bytes;
108        }
109    
110    
111        /**
112         * @return String representation.
113         */
114        @Override
115        public final String toString() {
116            return super.toString() + "(bytes = " + bytes + ")";
117        }
118    
119    
120        /** @return Long object
121         */
122        public Object getConstantValue( ConstantPool cp ) {
123            return Long.valueOf(bytes);
124        }
125    }