4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 25 import com.sun.org.apache.bcel.internal.Constants; 26 import java.io.*; 27 28 /** 29 * This class is derived from <em>Attribute</em> and denotes that this class 30 * is an Inner class of another. 31 * to the source file of this class. 32 * It is instantiated from the <em>Attribute.readAttribute()</em> method. 33 * 34 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 35 * @see Attribute 36 */ 37 public final class InnerClasses extends Attribute { 38 private InnerClass[] inner_classes; 39 private int number_of_classes; 40 41 /** 42 * Initialize from another object. Note that both objects use the same 43 * references (shallow copy). Use clone() for a physical copy. 44 */ 45 public InnerClasses(InnerClasses c) { 46 this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), 47 c.getConstantPool()); 48 } 49 50 /** 51 * @param name_index Index in constant pool to CONSTANT_Utf8 52 * @param length Content length in bytes 53 * @param inner_classes array of inner classes attributes 54 * @param constant_pool Array of constants 55 * @param sourcefile_index Index in constant pool to CONSTANT_Utf8 56 */ 57 public InnerClasses(int name_index, int length, 58 InnerClass[] inner_classes, 59 ConstantPool constant_pool) 60 { 61 super(Constants.ATTR_INNER_CLASSES, name_index, length, constant_pool); 62 setInnerClasses(inner_classes); 63 } 64 65 /** 66 * Construct object from file stream. 67 * 68 * @param name_index Index in constant pool to CONSTANT_Utf8 69 * @param length Content length in bytes 70 * @param file Input stream 71 * @param constant_pool Array of constants 72 * @throws IOException 73 */ 74 InnerClasses(int name_index, int length, DataInputStream file, 75 ConstantPool constant_pool) throws IOException 76 { 77 this(name_index, length, (InnerClass[])null, constant_pool); 78 79 number_of_classes = file.readUnsignedShort(); 80 inner_classes = new InnerClass[number_of_classes]; 81 82 for(int i=0; i < number_of_classes; i++) 83 inner_classes[i] = new InnerClass(file); 84 } 85 /** 86 * Called by objects that are traversing the nodes of the tree implicitely 87 * defined by the contents of a Java class. I.e., the hierarchy of methods, 88 * fields, attributes, etc. spawns a tree of objects. 89 * 90 * @param v Visitor object 91 */ 92 public void accept(Visitor v) { 93 v.visitInnerClasses(this); 94 } 95 /** 96 * Dump source file attribute to file stream in binary format. 97 * 98 * @param file Output file stream 99 * @throws IOException 100 */ 101 public final void dump(DataOutputStream file) throws IOException 102 { 103 super.dump(file); 104 file.writeShort(number_of_classes); 105 106 for(int i=0; i < number_of_classes; i++) 107 inner_classes[i].dump(file); 108 } 109 110 /** 111 * @return array of inner class "records" 112 */ 113 public final InnerClass[] getInnerClasses() { return inner_classes; } 114 115 /** 116 * @param inner_classes. 117 */ 118 public final void setInnerClasses(InnerClass[] inner_classes) { 119 this.inner_classes = inner_classes; 120 number_of_classes = (inner_classes == null)? 0 : inner_classes.length; 121 } 122 123 /** 124 * @return String representation. 125 */ 126 public final String toString() { 127 StringBuffer buf = new StringBuffer(); 128 129 for(int i=0; i < number_of_classes; i++) 130 buf.append(inner_classes[i].toString(constant_pool) + "\n"); 131 132 return buf.toString(); 133 } 134 135 /** 136 * @return deep copy of this attribute 137 */ 138 public Attribute copy(ConstantPool constant_pool) { 139 InnerClasses c = (InnerClasses)clone(); 140 141 c.inner_classes = new InnerClass[number_of_classes]; 142 for(int i=0; i < number_of_classes; i++) 143 c.inner_classes[i] = inner_classes[i].copy(); 144 145 c.constant_pool = constant_pool; 146 return c; 147 } 148 } | 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.bcel.internal.classfile; 23 24 import java.io.DataInput; 25 import java.io.DataOutputStream; 26 import java.io.IOException; 27 28 import com.sun.org.apache.bcel.internal.Const; 29 30 /** 31 * This class is derived from <em>Attribute</em> and denotes that this class 32 * is an Inner class of another. 33 * to the source file of this class. 34 * It is instantiated from the <em>Attribute.readAttribute()</em> method. 35 * 36 * @version $Id: InnerClasses.java 1749603 2016-06-21 20:50:19Z ggregory $ 37 * @see Attribute 38 */ 39 public final class InnerClasses extends Attribute { 40 41 private InnerClass[] inner_classes; 42 43 44 /** 45 * Initialize from another object. Note that both objects use the same 46 * references (shallow copy). Use clone() for a physical copy. 47 */ 48 public InnerClasses(final InnerClasses c) { 49 this(c.getNameIndex(), c.getLength(), c.getInnerClasses(), c.getConstantPool()); 50 } 51 52 53 /** 54 * @param name_index Index in constant pool to CONSTANT_Utf8 55 * @param length Content length in bytes 56 * @param inner_classes array of inner classes attributes 57 * @param constant_pool Array of constants 58 */ 59 public InnerClasses(final int name_index, final int length, final InnerClass[] inner_classes, 60 final ConstantPool constant_pool) { 61 super(Const.ATTR_INNER_CLASSES, name_index, length, constant_pool); 62 this.inner_classes = inner_classes != null ? inner_classes : new InnerClass[0]; 63 } 64 65 66 /** 67 * Construct object from input stream. 68 * 69 * @param name_index Index in constant pool to CONSTANT_Utf8 70 * @param length Content length in bytes 71 * @param input Input stream 72 * @param constant_pool Array of constants 73 * @throws IOException 74 */ 75 InnerClasses(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) 76 throws IOException { 77 this(name_index, length, (InnerClass[]) null, constant_pool); 78 final int number_of_classes = input.readUnsignedShort(); 79 inner_classes = new InnerClass[number_of_classes]; 80 for (int i = 0; i < number_of_classes; i++) { 81 inner_classes[i] = new InnerClass(input); 82 } 83 } 84 85 86 /** 87 * Called by objects that are traversing the nodes of the tree implicitely 88 * defined by the contents of a Java class. I.e., the hierarchy of methods, 89 * fields, attributes, etc. spawns a tree of objects. 90 * 91 * @param v Visitor object 92 */ 93 @Override 94 public void accept( final Visitor v ) { 95 v.visitInnerClasses(this); 96 } 97 98 99 /** 100 * Dump source file attribute to file stream in binary format. 101 * 102 * @param file Output file stream 103 * @throws IOException 104 */ 105 @Override 106 public final void dump( final DataOutputStream file ) throws IOException { 107 super.dump(file); 108 file.writeShort(inner_classes.length); 109 for (final InnerClass inner_class : inner_classes) { 110 inner_class.dump(file); 111 } 112 } 113 114 115 /** 116 * @return array of inner class "records" 117 */ 118 public final InnerClass[] getInnerClasses() { 119 return inner_classes; 120 } 121 122 123 /** 124 * @param inner_classes the array of inner classes 125 */ 126 public final void setInnerClasses( final InnerClass[] inner_classes ) { 127 this.inner_classes = inner_classes != null ? inner_classes : new InnerClass[0]; 128 } 129 130 131 /** 132 * @return String representation. 133 */ 134 @Override 135 public final String toString() { 136 final StringBuilder buf = new StringBuilder(); 137 buf.append("InnerClasses("); 138 buf.append(inner_classes.length); 139 buf.append("):\n"); 140 for (final InnerClass inner_class : inner_classes) { 141 buf.append(inner_class.toString(super.getConstantPool())).append("\n"); 142 } 143 return buf.toString(); 144 } 145 146 147 /** 148 * @return deep copy of this attribute 149 */ 150 @Override 151 public Attribute copy( final ConstantPool _constant_pool ) { 152 // TODO this could be recoded to use a lower level constructor after creating a copy of the inner classes 153 final InnerClasses c = (InnerClasses) clone(); 154 c.inner_classes = new InnerClass[inner_classes.length]; 155 for (int i = 0; i < inner_classes.length; i++) { 156 c.inner_classes[i] = inner_classes[i].copy(); 157 } 158 c.setConstantPool(_constant_pool); 159 return c; 160 } 161 } |