View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * This class is derived from the abstract {@link Constant}
28   * and represents a reference to a long object.
29   *
30   * @see     Constant
31   */
32  public final class ConstantLong extends Constant implements ConstantObject {
33  
34      private long bytes;
35  
36  
37      /**
38       * @param bytes Data
39       */
40      public ConstantLong(final long bytes) {
41          super(Const.CONSTANT_Long);
42          this.bytes = bytes;
43      }
44  
45  
46      /**
47       * Initialize from another object.
48       */
49      public ConstantLongonstantLong.html#ConstantLong">ConstantLong(final ConstantLong c) {
50          this(c.getBytes());
51      }
52  
53  
54      /**
55       * Initialize instance from file data.
56       *
57       * @param file Input stream
58       * @throws IOException
59       */
60      ConstantLong(final DataInput file) throws IOException {
61          this(file.readLong());
62      }
63  
64  
65      /**
66       * Called by objects that are traversing the nodes of the tree implicitely
67       * defined by the contents of a Java class. I.e., the hierarchy of methods,
68       * fields, attributes, etc. spawns a tree of objects.
69       *
70       * @param v Visitor object
71       */
72      @Override
73      public void accept( final Visitor v ) {
74          v.visitConstantLong(this);
75      }
76  
77  
78      /**
79       * Dump constant long to file stream in binary format.
80       *
81       * @param file Output file stream
82       * @throws IOException
83       */
84      @Override
85      public void dump( final DataOutputStream file ) throws IOException {
86          file.writeByte(super.getTag());
87          file.writeLong(bytes);
88      }
89  
90  
91      /**
92       * @return data, i.e., 8 bytes.
93       */
94      public long getBytes() {
95          return bytes;
96      }
97  
98  
99      /**
100      * @param bytes the raw bytes that represent this long
101      */
102     public void setBytes( final long bytes ) {
103         this.bytes = bytes;
104     }
105 
106 
107     /**
108      * @return String representation.
109      */
110     @Override
111     public String toString() {
112         return super.toString() + "(bytes = " + bytes + ")";
113     }
114 
115 
116     /** @return Long object
117      */
118     @Override
119     public Object getConstantValue( final ConstantPool cp ) {
120         return Long.valueOf(bytes);
121     }
122 }