1 25 26 package org.netbeans.modules.classfile; 27 28 import java.io.*; 29 import java.util.*; 30 31 36 public abstract class Field { 37 38 private int iName; 39 private int iType; 40 private String _name; 41 private String _type; 42 43 int access; 44 ClassFile classFile; 45 Map<ClassName,Annotation> annotations; 46 String typeSignature; 47 AttributeMap attributes; 48 49 50 Field(DataInputStream in, ConstantPool pool, ClassFile classFile, 51 boolean includeCode) throws IOException { 52 access = in.readUnsignedShort(); 53 iName = in.readUnsignedShort(); 54 iType = in.readUnsignedShort(); 55 this.classFile = classFile; 56 attributes = AttributeMap.load(in, pool, includeCode); 57 } 58 59 Field(String name, String type, ClassFile classFile) { 60 access = 0; 61 _name = name; 62 _type = type; 63 this.classFile = classFile; 64 attributes = new AttributeMap(new HashMap<String ,byte[]>(1)); 65 } 66 67 public final String getName() { 68 if (_name == null && iName != 0) { 69 CPUTF8Info utfName = (CPUTF8Info)classFile.constantPool.get(iName); 70 _name = utfName.getName(); 71 } 72 return _name; 73 } 74 75 public final String getDescriptor() { 76 if (_type == null && iType != 0) { 77 CPUTF8Info utfType = (CPUTF8Info)classFile.constantPool.get(iType); 78 _type = utfType.getName(); 79 } 80 return _type; 81 } 82 83 public abstract String getDeclaration(); 84 85 public final int getAccess() { 86 return access; 87 } 88 89 public final boolean isStatic() { 90 return Access.isStatic(access); 91 } 92 93 public final boolean isPublic() { 94 return Access.isPublic(access); 95 } 96 97 public final boolean isProtected() { 98 return Access.isProtected(access); 99 } 100 101 public final boolean isPackagePrivate() { 102 return Access.isPackagePrivate(access); 103 } 104 105 public final boolean isPrivate() { 106 return Access.isPrivate(access); 107 } 108 109 public final boolean isDeprecated() { 110 return attributes.get("Deprecated") != null; 111 } 112 113 public final boolean isSynthetic() { 114 return attributes.get("Synthetic") != null; 115 } 116 117 121 public final ClassFile getClassFile() { 122 return classFile; 123 } 124 125 130 public String getTypeSignature() { 131 if (typeSignature == null) { 132 DataInputStream in = attributes.getStream("Signature"); if (in != null) { 134 try { 135 int index = in.readUnsignedShort(); 136 CPUTF8Info entry = 137 (CPUTF8Info)classFile.constantPool.get(index); 138 typeSignature = entry.getName(); 139 in.close(); 140 } catch (IOException e) { 141 throw new InvalidClassFileAttributeException("invalid Signature attribute", e); 142 } 143 } 144 } 145 return typeSignature; 146 } 147 148 void setTypeSignature(String sig) { 149 typeSignature = sig; 150 } 151 152 156 public final Collection<Annotation> getAnnotations() { 157 loadAnnotations(); 158 return annotations.values(); 159 } 160 161 165 public final Annotation getAnnotation(final ClassName annotationClass) { 166 loadAnnotations(); 167 return annotations.get(annotationClass); 168 } 169 170 174 public final boolean isAnnotationPresent(final ClassName annotationClass) { 175 loadAnnotations(); 176 return annotations.get(annotationClass) != null; 177 } 178 179 189 public final AttributeMap getAttributes(){ 190 return attributes; 191 } 192 193 void loadAnnotations() { 194 if (annotations == null) 195 annotations = ClassFile.buildAnnotationMap(classFile.constantPool, 196 attributes); 197 } 198 199 public String toString() { 200 StringBuffer sb = new StringBuffer (); 201 String name = getName(); 202 if (name != null) { 203 sb.append(getName()); 204 sb.append(' '); 205 } 206 if (isSynthetic()) 207 sb.append("(synthetic)"); if (isDeprecated()) 209 sb.append("(deprecated)"); sb.append("type="); sb.append(getDescriptor()); 212 if (getTypeSignature() != null) { 213 sb.append(", signature="); sb.append(typeSignature); 215 } 216 sb.append(", access="); sb.append(Access.toString(access)); 218 loadAnnotations(); 219 if (annotations.size() > 0) { 220 Iterator iter = annotations.values().iterator(); 221 sb.append(", annotations={ "); 222 while (iter.hasNext()) { 223 sb.append(iter.next().toString()); 224 if (iter.hasNext()) 225 sb.append(", "); 226 } 227 sb.append(" }"); 228 } 229 return sb.toString(); 230 } 231 } 232 | Popular Tags |