|
| 1 | +/* |
| 2 | + * reserved comment block |
| 3 | + * DO NOT REMOVE OR ALTER! |
| 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 | +import com.sun.org.apache.bcel.internal.util.Args; |
| 30 | + |
| 31 | +/** |
| 32 | + * Extends {@link Attribute} and records the classes and |
| 33 | + * interfaces that are authorized to claim membership in the nest hosted by the |
| 34 | + * current class or interface. There may be at most one Record attribute in a |
| 35 | + * ClassFile structure. |
| 36 | + * |
| 37 | + * @see Attribute |
| 38 | + * @since 6.9.0 |
| 39 | + */ |
| 40 | +public final class Record extends Attribute { |
| 41 | + |
| 42 | + private static final RecordComponentInfo[] EMPTY_RCI_ARRAY = {}; |
| 43 | + |
| 44 | + private static RecordComponentInfo[] readComponents(final DataInput input, final ConstantPool constantPool) |
| 45 | + throws IOException { |
| 46 | + final int classCount = input.readUnsignedShort(); |
| 47 | + final RecordComponentInfo[] components = new RecordComponentInfo[classCount]; |
| 48 | + for (int i = 0; i < classCount; i++) { |
| 49 | + components[i] = new RecordComponentInfo(input, constantPool); |
| 50 | + } |
| 51 | + return components; |
| 52 | + } |
| 53 | + |
| 54 | + private RecordComponentInfo[] components; |
| 55 | + |
| 56 | + /** |
| 57 | + * Constructs object from input stream. |
| 58 | + * |
| 59 | + * @param nameIndex Index in constant pool |
| 60 | + * @param length Content length in bytes |
| 61 | + * @param input Input stream |
| 62 | + * @param constantPool Array of constants |
| 63 | + * @throws IOException if an I/O error occurs. |
| 64 | + */ |
| 65 | + Record(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) |
| 66 | + throws IOException { |
| 67 | + this(nameIndex, length, readComponents(input, constantPool), constantPool); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Constructs a new instance using components. |
| 72 | + * |
| 73 | + * @param nameIndex Index in constant pool |
| 74 | + * @param length Content length in bytes |
| 75 | + * @param classes Array of Record Component Info elements |
| 76 | + * @param constantPool Array of constants |
| 77 | + */ |
| 78 | + public Record(final int nameIndex, final int length, final RecordComponentInfo[] classes, |
| 79 | + final ConstantPool constantPool) { |
| 80 | + super(Const.ATTR_RECORD, nameIndex, length, constantPool); |
| 81 | + this.components = classes != null ? classes : EMPTY_RCI_ARRAY; |
| 82 | + Args.requireU2(this.components.length, "attributes.length"); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Called by objects that are traversing the nodes of the tree implicitly |
| 87 | + * defined by the contents of a Java class. For example, the hierarchy of methods, |
| 88 | + * fields, attributes, etc. spawns a tree of objects. |
| 89 | + * |
| 90 | + * @param v Visitor object |
| 91 | + */ |
| 92 | + @Override |
| 93 | + public void accept(final Visitor v) { |
| 94 | + v.visitRecord(this); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Copies this instance and its components. |
| 99 | + * |
| 100 | + * @return a deep copy of this instance and its components. |
| 101 | + */ |
| 102 | + @Override |
| 103 | + public Attribute copy(final ConstantPool constantPool) { |
| 104 | + final Record c = (Record) clone(); |
| 105 | + if (components.length > 0) { |
| 106 | + c.components = components.clone(); |
| 107 | + } |
| 108 | + c.setConstantPool(constantPool); |
| 109 | + return c; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Dumps this instance into a file stream in binary format. |
| 114 | + * |
| 115 | + * @param file output stream. |
| 116 | + * @throws IOException if an I/O error occurs. |
| 117 | + */ |
| 118 | + @Override |
| 119 | + public void dump(final DataOutputStream file) throws IOException { |
| 120 | + super.dump(file); |
| 121 | + file.writeShort(components.length); |
| 122 | + for (final RecordComponentInfo component : components) { |
| 123 | + component.dump(file); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Gets all the record components. |
| 129 | + * |
| 130 | + * @return array of Record Component Info elements. |
| 131 | + */ |
| 132 | + public RecordComponentInfo[] getComponents() { |
| 133 | + return components; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Converts this instance to a String suitable for debugging. |
| 138 | + * |
| 139 | + * @return String a String suitable for debugging. |
| 140 | + */ |
| 141 | + @Override |
| 142 | + public String toString() { |
| 143 | + final StringBuilder buf = new StringBuilder(); |
| 144 | + buf.append("Record("); |
| 145 | + buf.append(components.length); |
| 146 | + buf.append("):\n"); |
| 147 | + for (final RecordComponentInfo component : components) { |
| 148 | + buf.append(" ").append(component.toString()).append("\n"); |
| 149 | + } |
| 150 | + return buf.substring(0, buf.length() - 1); // remove the last newline |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments