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