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 static String getToModuleNameAtIndex(final ConstantPool constantPool, final int index) {
36          return constantPool.getConstantString(index, Const.CONSTANT_Module);
37      }
38      private final int exportsIndex; // points to CONSTANT_Package_info
39      private final int exportsFlags;
40      private final int exportsToCount;
41  
42      private final int[] exportsToIndex; // points to CONSTANT_Module_info
43  
44      /**
45       * Constructs object from file stream.
46       *
47       * @param file Input stream
48       * @throws IOException if an I/O Exception occurs in readUnsignedShort
49       */
50      ModuleExports(final DataInput file) throws IOException {
51          exportsIndex = file.readUnsignedShort();
52          exportsFlags = file.readUnsignedShort();
53          exportsToCount = file.readUnsignedShort();
54          exportsToIndex = new int[exportsToCount];
55          for (int i = 0; i < exportsToCount; i++) {
56              exportsToIndex[i] = file.readUnsignedShort();
57          }
58      }
59  
60      /**
61       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
62       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
63       *
64       * @param v Visitor object
65       */
66      @Override
67      public void accept(final Visitor v) {
68          v.visitModuleExports(this);
69      }
70  
71      /**
72       * @return deep copy of this object
73       */
74      public ModuleExports copy() {
75          try {
76              return (ModuleExports) clone();
77          } catch (final CloneNotSupportedException e) {
78              // TODO should this throw?
79          }
80          return null;
81      }
82  
83      /**
84       * Dump table entry to file stream in binary format.
85       *
86       * @param file Output file stream
87       * @throws IOException if an I/O Exception occurs in writeShort
88       */
89      public void dump(final DataOutputStream file) throws IOException {
90          file.writeShort(exportsIndex);
91          file.writeShort(exportsFlags);
92          file.writeShort(exportsToCount);
93          for (final int entry : exportsToIndex) {
94              file.writeShort(entry);
95          }
96      }
97  
98      /**
99       * Gets the flags for this ModuleExports.
100      * @return the exportsFlags
101      * @since 6.10.0
102      */
103     public int getExportsFlags() {
104         return exportsFlags;
105     }
106 
107     /**
108      * Gets the exported package name.
109      * @param constantPool the constant pool from the ClassFile
110      * @return the exported package name
111      * @since 6.10.0
112      */
113     public String getPackageName(final ConstantPool constantPool) {
114         return constantPool.constantToString(exportsIndex, Const.CONSTANT_Package);
115     }
116 
117     /**
118      * Gets an array of module names for this ModuleExports.
119      * @param constantPool Array of constants usually obtained from the ClassFile object
120      * @return array of module names following 'exports to'
121      * @since 6.10.0
122      */
123     public String[] getToModuleNames(final ConstantPool constantPool) {
124         final String[] toModuleNames = new String[exportsToCount];
125         for (int i = 0; i < exportsToCount; i++) {
126             toModuleNames[i] = getToModuleNameAtIndex(constantPool, exportsToIndex[i]);
127         }
128         return toModuleNames;
129     }
130 
131     /**
132      * @return String representation
133      */
134     @Override
135     public String toString() {
136         return "exports(" + exportsIndex + ", " + exportsFlags + ", " + exportsToCount + ", ...)";
137     }
138 
139     /**
140      * @return Resolved string representation
141      */
142     public String toString(final ConstantPool constantPool) {
143         final StringBuilder buf = new StringBuilder();
144         final String packageName = getPackageName(constantPool);
145         buf.append(packageName);
146         buf.append(", ").append(String.format("%04x", exportsFlags));
147         buf.append(", to(").append(exportsToCount).append("):\n");
148         for (final int index : exportsToIndex) {
149             final String moduleName = getToModuleNameAtIndex(constantPool, index);
150             buf.append("      ").append(moduleName).append("\n");
151         }
152         return buf.substring(0, buf.length() - 1); // remove the last newline
153     }
154 }