1 7 8 package org.gjt.jclasslib.structures; 9 10 import java.io.*; 11 12 18 public abstract class AbstractStructureWithAttributes extends AbstractStructure { 19 20 21 protected AttributeInfo[] attributes; 22 23 27 public AttributeInfo[] getAttributes() { 28 return attributes; 29 } 30 31 35 public void setAttributes(AttributeInfo[] attributes) { 36 this.attributes = attributes; 37 } 38 39 44 public AttributeInfo findAttribute(Class attributeClass) { 45 AttributeInfo foundAttribute = null; 46 for (int i = 0; i < attributes.length; i++) { 47 if (attributes[i].getClass() == attributeClass) { 48 foundAttribute = attributes[i]; 49 break; 50 } 51 } 52 return foundAttribute; 53 } 54 55 64 protected void readAttributes(DataInput in) 65 throws InvalidByteCodeException, IOException { 66 67 int attributesCount = in.readUnsignedShort(); 68 attributes = new AttributeInfo[attributesCount]; 69 70 for (int i = 0; i < attributesCount; i++) { 71 attributes[i] = AttributeInfo.createOrSkip(in, classFile); 72 } 73 } 74 75 83 protected void writeAttributes(DataOutput out) 84 throws InvalidByteCodeException, IOException { 85 86 int attributesCount = getLength(attributes); 87 88 out.writeShort(attributesCount); 89 90 for (int i = 0; i < attributesCount; i++) { 91 if (attributes[i] == null) { 92 throw new InvalidByteCodeException("attribute " + i + " is null"); 93 } 94 attributes[i].write(out); 95 } 96 } 97 98 102 protected int getTotalAttributesLength() { 103 int totalLength = 0; 104 int attributesCount = getLength(attributes); 105 for (int i = 0; i < attributesCount; i++) { 106 if (attributes[i] != null) { 107 totalLength += attributes[i].getAttributeLength(); 108 } 109 } 110 return totalLength; 111 } 112 } 113 | Popular Tags |