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