001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020package org.apache.bcel.classfile; 021 022import java.io.DataInput; 023import java.io.DataOutputStream; 024import java.io.IOException; 025 026import org.apache.bcel.Const; 027import org.apache.bcel.util.Args; 028 029/** 030 * This class is derived from <em>Attribute</em> and indicates the main class of a module. There may be at most one 031 * ModuleMainClass attribute in a ClassFile structure. 032 * 033 * @see Attribute 034 */ 035public final class ModuleMainClass extends Attribute { 036 037 private int mainClassIndex; 038 039 /** 040 * Constructs object from input stream. 041 * 042 * @param nameIndex Index in constant pool 043 * @param length Content length in bytes 044 * @param input Input stream 045 * @param constantPool Array of constants 046 * @throws IOException if an I/O error occurs. 047 */ 048 ModuleMainClass(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException { 049 this(nameIndex, length, 0, constantPool); 050 mainClassIndex = input.readUnsignedShort(); 051 } 052 053 /** 054 * @param nameIndex Index in constant pool 055 * @param length Content length in bytes 056 * @param mainClassIndex Host class index 057 * @param constantPool Array of constants 058 */ 059 public ModuleMainClass(final int nameIndex, final int length, final int mainClassIndex, final ConstantPool constantPool) { 060 super(Const.ATTR_NEST_MEMBERS, nameIndex, length, constantPool); 061 this.mainClassIndex = Args.requireU2(mainClassIndex, "mainClassIndex"); 062 } 063 064 /** 065 * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a 066 * physical copy. 067 * 068 * @param c Source to copy. 069 */ 070 public ModuleMainClass(final ModuleMainClass c) { 071 this(c.getNameIndex(), c.getLength(), c.getHostClassIndex(), c.getConstantPool()); 072 } 073 074 /** 075 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e., 076 * the hierarchy of methods, 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 * @return deep copy of this attribute 087 */ 088 @Override 089 public Attribute copy(final ConstantPool constantPool) { 090 final ModuleMainClass c = (ModuleMainClass) clone(); 091 c.setConstantPool(constantPool); 092 return c; 093 } 094 095 /** 096 * Dump ModuleMainClass attribute to file stream in binary format. 097 * 098 * @param file Output file stream 099 * @throws IOException if an I/O error occurs. 100 */ 101 @Override 102 public void dump(final DataOutputStream file) throws IOException { 103 super.dump(file); 104 file.writeShort(mainClassIndex); 105 } 106 107 /** 108 * @return index into constant pool of host class name. 109 */ 110 public int getHostClassIndex() { 111 return mainClassIndex; 112 } 113 114 /** 115 * @param mainClassIndex the host class index 116 */ 117 public void setHostClassIndex(final int mainClassIndex) { 118 this.mainClassIndex = mainClassIndex; 119 } 120 121 /** 122 * @return String representation 123 */ 124 @Override 125 public String toString() { 126 final StringBuilder buf = new StringBuilder(); 127 buf.append("ModuleMainClass: "); 128 final String className = super.getConstantPool().getConstantString(mainClassIndex, Const.CONSTANT_Class); 129 buf.append(Utility.compactClassName(className, false)); 130 return buf.toString(); 131 } 132}