1 23 24 25 package com.sun.jdo.api.persistence.enhancer.classfile; 26 27 import java.io.*; 28 import java.util.Vector ; 29 import java.util.Enumeration ; 30 import java.util.NoSuchElementException ; 31 32 40 41 public class AttributeVector { 42 43 44 private ClassAttribute attributes[] = null; 45 46 49 private ClassAttribute attrAt(int i) { 50 return attributes[i]; 51 } 52 53 56 public AttributeVector() { } 57 58 61 public void addElement(ClassAttribute attr) { 62 if (attributes == null) 63 attributes = new ClassAttribute[1]; 64 else { 65 ClassAttribute newAttributes[] = new ClassAttribute[attributes.length+1]; 66 System.arraycopy(attributes, 0, newAttributes, 0, attributes.length); 67 attributes = newAttributes; 68 } 69 attributes[attributes.length-1] = attr; 70 } 71 72 public Enumeration elements() { 73 class AttributeVectorEnumeration implements Enumeration { 74 private ClassAttribute[] attributes; 75 private int current = 0; 76 77 AttributeVectorEnumeration(ClassAttribute attrs[]) { 78 attributes = attrs; 79 } 80 81 public boolean hasMoreElements() { 82 return attributes != null && current < attributes.length; 83 } 84 public Object nextElement() { 85 if (!hasMoreElements()) 86 throw new NoSuchElementException (); 87 return attributes[current++]; 88 } 89 } 90 91 return new AttributeVectorEnumeration(attributes); 92 } 93 94 97 public ClassAttribute findAttribute(String attrName) { 98 Enumeration e = elements(); 99 while (e.hasMoreElements()) { 100 ClassAttribute attr = (ClassAttribute) e.nextElement(); 101 if (attr.attrName().asString().equals(attrName)) 102 return attr; 103 } 104 return null; 105 } 106 107 110 static AttributeVector readAttributes( 111 DataInputStream data, ConstantPool constantPool) 112 throws IOException { 113 AttributeVector attribs = new AttributeVector(); 114 int n_attrs = data.readUnsignedShort(); 115 while (n_attrs-- > 0) { 116 attribs.addElement(ClassAttribute.read(data, constantPool)); 117 } 118 return attribs; 119 } 120 121 124 static AttributeVector readAttributes( 125 DataInputStream data, CodeEnv codeEnv) 126 throws IOException { 127 AttributeVector attribs = new AttributeVector(); 128 int n_attrs = data.readUnsignedShort(); 129 while (n_attrs-- > 0) { 130 attribs.addElement(ClassAttribute.read(data, codeEnv)); 131 } 132 return attribs; 133 } 134 135 138 void write(DataOutputStream out) throws IOException { 139 if (attributes == null) { 140 out.writeShort(0); 141 } else { 142 out.writeShort(attributes.length); 143 for (int i=0; i<attributes.length; i++) 144 attributes[i].write(out); 145 } 146 } 147 148 151 void print(PrintStream out, int indent) { 152 if (attributes != null) { 153 for (int i=0; i<attributes.length; i++) 154 attributes[i].print(out, indent); 155 } 156 } 157 158 161 void summarize() { 162 System.out.println((attributes == null ? 0 : attributes.length) + 163 " attributes"); } 165 166 } 167 168 | Popular Tags |