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 records the classes and interfaces that are authorized to claim
31   * membership in the nest hosted by the current class or interface. There may be at most one NestMembers attribute in a
32   * ClassFile structure.
33   *
34   * @see Attribute
35   */
36  public final class NestMembers extends Attribute {
37  
38      private int[] classes;
39  
40      /**
41       * Constructs object from input stream.
42       *
43       * @param nameIndex Index in constant pool
44       * @param length Content length in bytes
45       * @param input Input stream
46       * @param constantPool Array of constants
47       * @throws IOException if an I/O error occurs.
48       */
49      NestMembers(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
50          this(nameIndex, length, (int[]) null, constantPool);
51          final int classCount = input.readUnsignedShort();
52          classes = new int[classCount];
53          for (int i = 0; i < classCount; i++) {
54              classes[i] = input.readUnsignedShort();
55          }
56      }
57  
58      /**
59       * @param nameIndex Index in constant pool
60       * @param length Content length in bytes
61       * @param classes Table of indices in constant pool
62       * @param constantPool Array of constants
63       */
64      public NestMembers(final int nameIndex, final int length, final int[] classes, final ConstantPool constantPool) {
65          super(Const.ATTR_NEST_MEMBERS, nameIndex, length, constantPool);
66          this.classes = ArrayUtils.nullToEmpty(classes);
67          Args.requireU2(this.classes.length, "classes.length");
68      }
69  
70      /**
71       * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
72       * physical copy.
73       *
74       * @param c Source to copy.
75       */
76      public NestMembers(final NestMembers c) {
77          this(c.getNameIndex(), c.getLength(), c.getClasses(), c.getConstantPool());
78      }
79  
80      /**
81       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
82       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
83       *
84       * @param v Visitor object
85       */
86      @Override
87      public void accept(final Visitor v) {
88          v.visitNestMembers(this);
89      }
90  
91      /**
92       * @return deep copy of this attribute
93       */
94      @Override
95      public Attribute copy(final ConstantPool constantPool) {
96          final NestMembers c = (NestMembers) clone();
97          if (classes.length > 0) {
98              c.classes = classes.clone();
99          }
100         c.setConstantPool(constantPool);
101         return c;
102     }
103 
104     /**
105      * Dump NestMembers attribute to file stream in binary format.
106      *
107      * @param file Output file stream
108      * @throws IOException if an I/O error occurs.
109      */
110     @Override
111     public void dump(final DataOutputStream file) throws IOException {
112         super.dump(file);
113         file.writeShort(classes.length);
114         for (final int index : classes) {
115             file.writeShort(index);
116         }
117     }
118 
119     /**
120      * @return array of indices into constant pool of class names.
121      */
122     public int[] getClasses() {
123         return classes;
124     }
125 
126     /**
127      * @return string array of class names
128      */
129     public String[] getClassNames() {
130         final String[] names = new String[classes.length];
131         Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(classes[i], Const.CONSTANT_Class)));
132         return names;
133     }
134 
135     /**
136      * @return Length of classes table.
137      */
138     public int getNumberClasses() {
139         return classes.length;
140     }
141 
142     /**
143      * @param classes the list of class indexes Also redefines number_of_classes according to table length.
144      */
145     public void setClasses(final int[] classes) {
146         this.classes = ArrayUtils.nullToEmpty(classes);
147     }
148 
149     /**
150      * @return String representation, i.e., a list of classes.
151      */
152     @Override
153     public String toString() {
154         final StringBuilder buf = new StringBuilder();
155         buf.append("NestMembers(");
156         buf.append(classes.length);
157         buf.append("):\n");
158         for (final int index : classes) {
159             final String className = super.getConstantPool().getConstantString(index, Const.CONSTANT_Class);
160             buf.append("  ").append(Utility.compactClassName(className, false)).append("\n");
161         }
162         return buf.substring(0, buf.length() - 1); // remove the last newline
163     }
164 }