Source code

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