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 <em>Attribute</em> and indicates the main class of a module.
    028 * There may be at most one ModuleMainClass attribute in a ClassFile structure.
    029 *
    030 * @see     Attribute
    031 */
    032public final class ModuleMainClass extends Attribute {
    033
    034    private int main_class_index;
    035
    036
    037    /**
    038     * Initialize from another object. Note that both objects use the same
    039     * references (shallow copy). Use copy() for a physical copy.
    040     */
    041    public ModuleMainClass(final ModuleMainClass c) {
    042        this(c.getNameIndex(), c.getLength(), c.getHostClassIndex(), c.getConstantPool());
    043    }
    044
    045
    046    /**
    047     * @param name_index Index in constant pool
    048     * @param length Content length in bytes
    049     * @param main_class_index Host class index
    050     * @param constant_pool Array of constants
    051     */
    052    public ModuleMainClass(final int name_index, final int length, final int main_class_index,
    053            final ConstantPool constant_pool) {
    054        super(Const.ATTR_NEST_MEMBERS, name_index, length, constant_pool);
    055        this.main_class_index = main_class_index;
    056    }
    057
    058
    059    /**
    060     * Construct object from input stream.
    061     * @param name_index Index in constant pool
    062     * @param length Content length in bytes
    063     * @param input Input stream
    064     * @param constant_pool Array of constants
    065     * @throws IOException
    066     */
    067    ModuleMainClass(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException {
    068        this(name_index, length, 0, constant_pool);
    069        main_class_index = input.readUnsignedShort();
    070    }
    071
    072
    073    /**
    074     * Called by objects that are traversing the nodes of the tree implicitly
    075     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    076     * fields, attributes, etc. spawns a tree of objects.
    077     *
    078     * @param v Visitor object
    079     */
    080    @Override
    081    public void accept( final Visitor v ) {
    082        v.visitModuleMainClass(this);
    083    }
    084
    085
    086    /**
    087     * Dump ModuleMainClass attribute to file stream in binary format.
    088     *
    089     * @param file Output file stream
    090     * @throws IOException if an I/O error occurs.
    091     */
    092    @Override
    093    public void dump( final DataOutputStream file ) throws IOException {
    094        super.dump(file);
    095        file.writeShort(main_class_index);
    096    }
    097
    098
    099    /**
    100     * @return index into constant pool of host class name.
    101     */
    102    public int getHostClassIndex() {
    103        return main_class_index;
    104    }
    105
    106
    107    /**
    108     * @param main_class_index the host class index
    109     */
    110    public void setHostClassIndex( final int main_class_index ) {
    111        this.main_class_index = main_class_index;
    112    }
    113
    114
    115    /**
    116     * @return String representation
    117     */
    118    @Override
    119    public String toString() {
    120        final StringBuilder buf = new StringBuilder();
    121        buf.append("ModuleMainClass: ");
    122        final String class_name = super.getConstantPool().getConstantString(main_class_index, Const.CONSTANT_Class);
    123        buf.append(Utility.compactClassName(class_name, false));
    124        return buf.toString();
    125    }
    126
    127
    128    /**
    129     * @return deep copy of this attribute
    130     */
    131    @Override
    132    public Attribute copy( final ConstantPool _constant_pool ) {
    133        final ModuleMainClass c = (ModuleMainClass) clone();
    134        c.setConstantPool(_constant_pool);
    135        return c;
    136    }
    137}