View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.classfile;
21  
22  import java.io.DataInput;
23  import java.io.DataOutputStream;
24  import java.io.IOException;
25  
26  import org.apache.bcel.Const;
27  import org.apache.bcel.util.Args;
28  
29  /**
30   * This class is derived from <em>Attribute</em> and indicates the main class of a module. There may be at most one
31   * ModuleMainClass attribute in a ClassFile structure.
32   *
33   * @see Attribute
34   */
35  public final class ModuleMainClass extends Attribute {
36  
37      private int mainClassIndex;
38  
39      /**
40       * Constructs object from input stream.
41       *
42       * @param nameIndex Index in constant pool.
43       * @param length Content length in bytes.
44       * @param input Input stream.
45       * @param constantPool Array of constants.
46       * @throws IOException Thrown if an I/O error occurs.
47       */
48      ModuleMainClass(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
49          this(nameIndex, length, 0, constantPool);
50          mainClassIndex = input.readUnsignedShort();
51      }
52  
53      /**
54       * Constructs a new ModuleMainClass.
55       *
56       * @param nameIndex Index in constant pool.
57       * @param length Content length in bytes.
58       * @param mainClassIndex Host class index.
59       * @param constantPool Array of constants.
60       */
61      public ModuleMainClass(final int nameIndex, final int length, final int mainClassIndex, final ConstantPool constantPool) {
62          super(Const.ATTR_MODULE_MAIN_CLASS, nameIndex, length, constantPool);
63          this.mainClassIndex = Args.requireU2(mainClassIndex, "mainClassIndex");
64      }
65  
66      /**
67       * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
68       * physical copy.
69       *
70       * @param c Source to copy.
71       */
72      public ModuleMainClass(final ModuleMainClass c) {
73          this(c.getNameIndex(), c.getLength(), c.getHostClassIndex(), c.getConstantPool());
74      }
75  
76      /**
77       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e.,
78       * the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
79       *
80       * @param v Visitor object.
81       */
82      @Override
83      public void accept(final Visitor v) {
84          v.visitModuleMainClass(this);
85      }
86  
87      /**
88       * @return deep copy of this attribute.
89       */
90      @Override
91      public Attribute copy(final ConstantPool constantPool) {
92          final ModuleMainClass c = (ModuleMainClass) clone();
93          c.setConstantPool(constantPool);
94          return c;
95      }
96  
97      /**
98       * Dumps ModuleMainClass attribute to file stream in binary format.
99       *
100      * @param file Output file stream.
101      * @throws IOException Thrown if an I/O error occurs.
102      */
103     @Override
104     public void dump(final DataOutputStream file) throws IOException {
105         super.dump(file);
106         file.writeShort(mainClassIndex);
107     }
108 
109     /**
110      * Gets the host class index.
111      *
112      * @return index into constant pool of host class name.
113      */
114     public int getHostClassIndex() {
115         return mainClassIndex;
116     }
117 
118     /**
119      * Sets the host class index.
120      *
121      * @param mainClassIndex The host class index.
122      */
123     public void setHostClassIndex(final int mainClassIndex) {
124         this.mainClassIndex = mainClassIndex;
125     }
126 
127     /**
128      * @return String representation.
129      */
130     @Override
131     public String toString() {
132         final StringBuilder buf = new StringBuilder();
133         buf.append("ModuleMainClass: ");
134         final String className = super.getConstantPool().getConstantString(mainClassIndex, Const.CONSTANT_Class);
135         buf.append(Utility.compactClassName(className, false));
136         return buf.toString();
137     }
138 }