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