001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.bcel.classfile; 019 020import java.io.DataInput; 021import java.io.DataOutputStream; 022import java.io.IOException; 023import java.util.Arrays; 024 025import org.apache.bcel.Const; 026import org.apache.commons.lang3.ArrayUtils; 027 028/** 029 * This class is derived from <em>Attribute</em> and records the classes and interfaces that are authorized to claim 030 * membership in the nest hosted by the current class or interface. There may be at most one NestMembers attribute in a 031 * ClassFile structure. 032 * 033 * @see Attribute 034 */ 035public final class NestMembers extends Attribute { 036 037 private int[] classes; 038 039 /** 040 * Construct object from input stream. 041 * 042 * @param name_index Index in constant pool 043 * @param length Content length in bytes 044 * @param input Input stream 045 * @param constant_pool Array of constants 046 * @throws IOException if an I/O error occurs. 047 */ 048 NestMembers(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException { 049 this(name_index, length, (int[]) null, constant_pool); 050 final int number_of_classes = input.readUnsignedShort(); 051 classes = new int[number_of_classes]; 052 for (int i = 0; i < number_of_classes; i++) { 053 classes[i] = input.readUnsignedShort(); 054 } 055 } 056 057 /** 058 * @param name_index Index in constant pool 059 * @param length Content length in bytes 060 * @param classes Table of indices in constant pool 061 * @param constant_pool Array of constants 062 */ 063 public NestMembers(final int name_index, final int length, final int[] classes, final ConstantPool constant_pool) { 064 super(Const.ATTR_NEST_MEMBERS, name_index, length, constant_pool); 065 this.classes = classes != null ? classes : ArrayUtils.EMPTY_INT_ARRAY; 066 } 067 068 /** 069 * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a 070 * physical copy. 071 */ 072 public NestMembers(final NestMembers c) { 073 this(c.getNameIndex(), c.getLength(), c.getClasses(), c.getConstantPool()); 074 } 075 076 /** 077 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 078 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 079 * 080 * @param v Visitor object 081 */ 082 @Override 083 public void accept(final Visitor v) { 084 v.visitNestMembers(this); 085 } 086 087 /** 088 * @return deep copy of this attribute 089 */ 090 @Override 091 public Attribute copy(final ConstantPool constantPool) { 092 final NestMembers c = (NestMembers) clone(); 093 if (classes != null) { 094 c.classes = new int[classes.length]; 095 System.arraycopy(classes, 0, c.classes, 0, classes.length); 096 } 097 c.setConstantPool(constantPool); 098 return c; 099 } 100 101 /** 102 * Dump NestMembers attribute to file stream in binary format. 103 * 104 * @param file Output file stream 105 * @throws IOException if an I/O error occurs. 106 */ 107 @Override 108 public void dump(final DataOutputStream file) throws IOException { 109 super.dump(file); 110 file.writeShort(classes.length); 111 for (final int index : classes) { 112 file.writeShort(index); 113 } 114 } 115 116 /** 117 * @return array of indices into constant pool of class names. 118 */ 119 public int[] getClasses() { 120 return classes; 121 } 122 123 /** 124 * @return string array of class names 125 */ 126 public String[] getClassNames() { 127 final String[] names = new String[classes.length]; 128 Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(classes[i], Const.CONSTANT_Class))); 129 return names; 130 } 131 132 /** 133 * @return Length of classes table. 134 */ 135 public int getNumberClasses() { 136 return classes == null ? 0 : classes.length; 137 } 138 139 /** 140 * @param classes the list of class indexes Also redefines number_of_classes according to table length. 141 */ 142 public void setClasses(final int[] classes) { 143 this.classes = classes != null ? classes : ArrayUtils.EMPTY_INT_ARRAY; 144 } 145 146 /** 147 * @return String representation, i.e., a list of classes. 148 */ 149 @Override 150 public String toString() { 151 final StringBuilder buf = new StringBuilder(); 152 buf.append("NestMembers("); 153 buf.append(classes.length); 154 buf.append("):\n"); 155 for (final int index : classes) { 156 final String className = super.getConstantPool().getConstantString(index, Const.CONSTANT_Class); 157 buf.append(" ").append(Utility.compactClassName(className, false)).append("\n"); 158 } 159 return buf.substring(0, buf.length() - 1); // remove the last newline 160 } 161}