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