    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 package.
    029 *
    030 * <p>
    031 * Note: Early access Java 9 support- currently subject to change
    032 * </p>
    033 *
    034 * @see Constant
    035 * @since 6.1
    036 */
    037public final class ConstantPackage extends Constant implements ConstantObject {
    038
    039    private int nameIndex;
    040
    041    /**
    042     * Initialize from another object.
    043     *
    044     * @param c Source to copy.
    045     */
    046    public ConstantPackage(final ConstantPackage c) {
    047        this(c.getNameIndex());
    048    }
    049
    050    /**
    051     * Initialize instance from file data.
    052     *
    053     * @param file Input stream
    054     * @throws IOException if an I/O error occurs.
    055     */
    056    ConstantPackage(final DataInput file) throws IOException {
    057        this(file.readUnsignedShort());
    058    }
    059
    060    /**
    061     * @param nameIndex Name index in constant pool. Should refer to a ConstantUtf8.
    062     */
    063    public ConstantPackage(final int nameIndex) {
    064        super(Const.CONSTANT_Package);
    065        this.nameIndex = nameIndex;
    066    }
    067
    068    /**
    069     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
    070     * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    071     *
    072     * @param v Visitor object
    073     */
    074    @Override
    075    public void accept(final Visitor v) {
    076        v.visitConstantPackage(this);
    077    }
    078
    079    /**
    080     * Dump constant package to file stream in binary format.
    081     *
    082     * @param file Output file stream
    083     * @throws IOException if an I/O error occurs.
    084     */
    085    @Override
    086    public void dump(final DataOutputStream file) throws IOException {
    087        file.writeByte(super.getTag());
    088        file.writeShort(nameIndex);
    089    }
    090
    091    /**
    092     * @return dereferenced string
    093     */
    094    public String getBytes(final ConstantPool cp) {
    095        return (String) getConstantValue(cp);
    096    }
    097
    098    /**
    099     * @return String object
    100     */
    101    @Override
    102    public Object getConstantValue(final ConstantPool cp) {
    103        return cp.getConstantUtf8(nameIndex).getBytes();
    104    }
    105
    106    /**
    107     * @return Name index in constant pool of package name.
    108     */
    109    public int getNameIndex() {
    110        return nameIndex;
    111    }
    112
    113    /**
    114     * @param nameIndex the name index in the constant pool of this Constant Package
    115     */
    116    public void setNameIndex(final int nameIndex) {
    117        this.nameIndex = nameIndex;
    118    }
    119
    120    /**
    121     * @return String representation.
    122     */
    123    @Override
    124    public String toString() {
    125        return super.toString() + "(nameIndex = " + nameIndex + ")";
    126    }
    127}