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  
26  /**
27   * This class represents an entry in the exports table of the Module attribute. Each entry describes a package which may
28   * open the parent module.
29   *
30   * @see Module
31   * @since 6.4.0
32   */
33  public final class ModuleExports implements Cloneable, Node {
34  
35      private final int exportsIndex; // points to CONSTANT_Package_info
36      private final int exportsFlags;
37      private final int exportsToCount;
38      private final int[] exportsToIndex; // points to CONSTANT_Module_info
39  
40      /**
41       * Constructs object from file stream.
42       *
43       * @param file Input stream
44       * @throws IOException if an I/O Exception occurs in readUnsignedShort
45       */
46      ModuleExports(final DataInput file) throws IOException {
47          exportsIndex = file.readUnsignedShort();
48          exportsFlags = file.readUnsignedShort();
49          exportsToCount = file.readUnsignedShort();
50          exportsToIndex = new int[exportsToCount];
51          for (int i = 0; i < exportsToCount; i++) {
52              exportsToIndex[i] = file.readUnsignedShort();
53          }
54      }
55  
56      /**
57       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
58       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
59       *
60       * @param v Visitor object
61       */
62      @Override
63      public void accept(final Visitor v) {
64          v.visitModuleExports(this);
65      }
66  
67      // TODO add more getters and setters?
68  
69      /**
70       * @return deep copy of this object
71       */
72      public ModuleExports copy() {
73          try {
74              return (ModuleExports) clone();
75          } catch (final CloneNotSupportedException e) {
76              // TODO should this throw?
77          }
78          return null;
79      }
80  
81      /**
82       * Dump table entry to file stream in binary format.
83       *
84       * @param file Output file stream
85       * @throws IOException if an I/O Exception occurs in writeShort
86       */
87      public void dump(final DataOutputStream file) throws IOException {
88          file.writeShort(exportsIndex);
89          file.writeShort(exportsFlags);
90          file.writeShort(exportsToCount);
91          for (final int entry : exportsToIndex) {
92              file.writeShort(entry);
93          }
94      }
95  
96      /**
97       * @return String representation
98       */
99      @Override
100     public String toString() {
101         return "exports(" + exportsIndex + ", " + exportsFlags + ", " + exportsToCount + ", ...)";
102     }
103 
104     /**
105      * @return Resolved string representation
106      */
107     public String toString(final ConstantPool constantPool) {
108         final StringBuilder buf = new StringBuilder();
109         final String packageName = constantPool.constantToString(exportsIndex, Const.CONSTANT_Package);
110         buf.append(Utility.compactClassName(packageName, false));
111         buf.append(", ").append(String.format("%04x", exportsFlags));
112         buf.append(", to(").append(exportsToCount).append("):\n");
113         for (final int index : exportsToIndex) {
114             final String moduleName = constantPool.getConstantString(index, Const.CONSTANT_Module);
115             buf.append("      ").append(Utility.compactClassName(moduleName, false)).append("\n");
116         }
117         return buf.substring(0, buf.length() - 1); // remove the last newline
118     }
119 }