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