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