1 /* 2 * Javassist, a Java-bytecode translator toolkit. 3 * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved. 4 * 5 * The contents of this file are subject to the Mozilla Public License Version 6 * 1.1 (the "License"); you may not use this file except in compliance with 7 * the License. Alternatively, the contents of this file may be used under 8 * the terms of the GNU Lesser General Public License Version 2.1 or later, 9 * or the Apache License Version 2.0. 10 * 11 * Software distributed under the License is distributed on an "AS IS" basis, 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 13 * for the specific language governing rights and limitations under the 14 * License. 15 */ 16 17 package javassist.bytecode; 18 19 import java.io.DataInputStream; 20 import java.io.IOException; 21 import java.util.Map; 22 23 /** 24 * <code>NestMembers_attribute</code>. 25 * It was introduced by JEP-181. See JVMS 4.7.29 for the specification. 26 * 27 * @since 3.24 28 */ 29 public class NestMembersAttribute extends AttributeInfo { 30 /** 31 * The name of this attribute <code>"NestMembers"</code>. 32 */ 33 public static final String tag = "NestMembers"; 34 NestMembersAttribute(ConstPool cp, int n, DataInputStream in)35 NestMembersAttribute(ConstPool cp, int n, DataInputStream in) throws IOException { 36 super(cp, n, in); 37 } 38 NestMembersAttribute(ConstPool cp, byte[] info)39 private NestMembersAttribute(ConstPool cp, byte[] info) { 40 super(cp, tag, info); 41 } 42 43 /** 44 * Makes a copy. Class names are replaced according to the 45 * given <code>Map</code> object. 46 * 47 * @param newCp the constant pool table used by the new copy. 48 * @param classnames pairs of replaced and substituted 49 * class names. 50 */ 51 @Override copy(ConstPool newCp, Map<String, String> classnames)52 public AttributeInfo copy(ConstPool newCp, Map<String, String> classnames) { 53 byte[] src = get(); 54 byte[] dest = new byte[src.length]; 55 ConstPool cp = getConstPool(); 56 57 int n = ByteArray.readU16bit(src, 0); 58 ByteArray.write16bit(n, dest, 0); 59 60 for (int i = 0, j = 2; i < n; ++i, j += 2) { 61 int index = ByteArray.readU16bit(src, j); 62 int newIndex = cp.copy(index, newCp, classnames); 63 ByteArray.write16bit(newIndex, dest, j); 64 } 65 66 return new NestMembersAttribute(newCp, dest); 67 } 68 69 /** 70 * Returns <code>number_of_classes</code>. 71 * @return the number of the classes recorded in this attribute. 72 */ numberOfClasses()73 public int numberOfClasses() { 74 return ByteArray.readU16bit(info, 0); 75 } 76 77 /** Returns <code>classes[index]</code>. 78 * 79 * @param index the index into <code>classes</code>. 80 * @return the value at the given index in the <code>classes</code> array. 81 * It is an index into the constant pool. 82 * The constant pool entry at the returned index is a 83 * <code>CONSTANT_Class_info</code> structure. 84 */ memberClass(int index)85 public int memberClass(int index) { 86 return ByteArray.readU16bit(info, index * 2 + 2); 87 } 88 } 89