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