ConstantPackage.java

  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. package org.apache.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;

  22. /**
  23.  * This class is derived from the abstract {@link Constant} and represents a reference to a package.
  24.  *
  25.  * <p>
  26.  * Note: Early access Java 9 support- currently subject to change
  27.  * </p>
  28.  *
  29.  * @see Constant
  30.  * @since 6.1
  31.  */
  32. public final class ConstantPackage extends Constant implements ConstantObject {

  33.     private int nameIndex;

  34.     /**
  35.      * Initialize from another object.
  36.      *
  37.      * @param c Source to copy.
  38.      */
  39.     public ConstantPackage(final ConstantPackage c) {
  40.         this(c.getNameIndex());
  41.     }

  42.     /**
  43.      * Initialize instance from file data.
  44.      *
  45.      * @param file Input stream
  46.      * @throws IOException if an I/O error occurs.
  47.      */
  48.     ConstantPackage(final DataInput file) throws IOException {
  49.         this(file.readUnsignedShort());
  50.     }

  51.     /**
  52.      * @param nameIndex Name index in constant pool. Should refer to a ConstantUtf8.
  53.      */
  54.     public ConstantPackage(final int nameIndex) {
  55.         super(Const.CONSTANT_Package);
  56.         this.nameIndex = nameIndex;
  57.     }

  58.     /**
  59.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
  60.      * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  61.      *
  62.      * @param v Visitor object
  63.      */
  64.     @Override
  65.     public void accept(final Visitor v) {
  66.         v.visitConstantPackage(this);
  67.     }

  68.     /**
  69.      * Dump constant package to file stream in binary format.
  70.      *
  71.      * @param file Output file stream
  72.      * @throws IOException if an I/O error occurs.
  73.      */
  74.     @Override
  75.     public void dump(final DataOutputStream file) throws IOException {
  76.         file.writeByte(super.getTag());
  77.         file.writeShort(nameIndex);
  78.     }

  79.     /**
  80.      * @return dereferenced string
  81.      */
  82.     public String getBytes(final ConstantPool cp) {
  83.         return (String) getConstantValue(cp);
  84.     }

  85.     /**
  86.      * @return String object
  87.      */
  88.     @Override
  89.     public Object getConstantValue(final ConstantPool cp) {
  90.         return cp.getConstantUtf8(nameIndex).getBytes();
  91.     }

  92.     /**
  93.      * @return Name index in constant pool of package name.
  94.      */
  95.     public int getNameIndex() {
  96.         return nameIndex;
  97.     }

  98.     /**
  99.      * @param nameIndex the name index in the constant pool of this Constant Package
  100.      */
  101.     public void setNameIndex(final int nameIndex) {
  102.         this.nameIndex = nameIndex;
  103.     }

  104.     /**
  105.      * @return String representation.
  106.      */
  107.     @Override
  108.     public String toString() {
  109.         return super.toString() + "(nameIndex = " + nameIndex + ")";
  110.     }
  111. }