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  import java.util.Arrays;
24  
25  import org.apache.bcel.Const;
26  import org.apache.bcel.util.Args;
27  import org.apache.commons.lang3.ArrayUtils;
28  
29  /**
30   * This class is derived from <em>Attribute</em> and represents the list of packages that are exported or opened by the
31   * Module attribute. There may be at most one ModulePackages attribute in a ClassFile structure.
32   *
33   * @see Attribute
34   */
35  public final class ModulePackages extends Attribute {
36  
37      private int[] packageIndexTable;
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 if an I/O error occurs.
47       */
48      ModulePackages(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
49          this(nameIndex, length, (int[]) null, constantPool);
50          final int packageCount = input.readUnsignedShort();
51          packageIndexTable = new int[packageCount];
52          for (int i = 0; i < packageCount; i++) {
53              packageIndexTable[i] = input.readUnsignedShort();
54          }
55      }
56  
57      /**
58       * @param nameIndex Index in constant pool
59       * @param length Content length in bytes
60       * @param packageIndexTable Table of indices in constant pool
61       * @param constantPool Array of constants
62       */
63      public ModulePackages(final int nameIndex, final int length, final int[] packageIndexTable, final ConstantPool constantPool) {
64          super(Const.ATTR_MODULE_PACKAGES, nameIndex, length, constantPool);
65          this.packageIndexTable = packageIndexTable != null ? packageIndexTable : ArrayUtils.EMPTY_INT_ARRAY;
66          Args.requireU2(this.packageIndexTable.length, "packageIndexTable.length");
67      }
68  
69      /**
70       * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
71       * physical copy.
72       *
73       * @param c Source to copy.
74       */
75      public ModulePackages(final ModulePackages c) {
76          this(c.getNameIndex(), c.getLength(), c.getPackageIndexTable(), c.getConstantPool());
77      }
78  
79      /**
80       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
81       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
82       *
83       * @param v Visitor object
84       */
85      @Override
86      public void accept(final Visitor v) {
87          v.visitModulePackages(this);
88      }
89  
90      /**
91       * @return deep copy of this attribute
92       */
93      @Override
94      public Attribute copy(final ConstantPool constantPool) {
95          final ModulePackages c = (ModulePackages) clone();
96          if (packageIndexTable != null) {
97              c.packageIndexTable = packageIndexTable.clone();
98          }
99          c.setConstantPool(constantPool);
100         return c;
101     }
102 
103     /**
104      * Dump ModulePackages attribute to file stream in binary format.
105      *
106      * @param file Output file stream
107      * @throws IOException if an I/O error occurs.
108      */
109     @Override
110     public void dump(final DataOutputStream file) throws IOException {
111         super.dump(file);
112         file.writeShort(packageIndexTable.length);
113         for (final int index : packageIndexTable) {
114             file.writeShort(index);
115         }
116     }
117 
118     /**
119      * @return Length of package table.
120      */
121     public int getNumberOfPackages() {
122         return packageIndexTable == null ? 0 : packageIndexTable.length;
123     }
124 
125     /**
126      * @return array of indices into constant pool of package names.
127      */
128     public int[] getPackageIndexTable() {
129         return packageIndexTable;
130     }
131 
132     /**
133      * @return string array of package names
134      */
135     public String[] getPackageNames() {
136         final String[] names = new String[packageIndexTable.length];
137         Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(packageIndexTable[i], Const.CONSTANT_Package)));
138         return names;
139     }
140 
141     /**
142      * @param packageIndexTable the list of package indexes Also redefines number_of_packages according to table length.
143      */
144     public void setPackageIndexTable(final int[] packageIndexTable) {
145         this.packageIndexTable = packageIndexTable != null ? packageIndexTable : ArrayUtils.EMPTY_INT_ARRAY;
146     }
147 
148     /**
149      * @return String representation, i.e., a list of packages.
150      */
151     @Override
152     public String toString() {
153         final StringBuilder buf = new StringBuilder();
154         buf.append("ModulePackages(");
155         buf.append(packageIndexTable.length);
156         buf.append("):\n");
157         for (final int index : packageIndexTable) {
158             final String packageName = super.getConstantPool().getConstantString(index, Const.CONSTANT_Package);
159             buf.append("  ").append(Utility.compactClassName(packageName, false)).append("\n");
160         }
161         return buf.substring(0, buf.length() - 1); // remove the last newline
162     }
163 }