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