1 25 26 package org.netbeans.modules.classfile; 27 28 import java.io.*; 29 30 37 public final class Variable extends Field { 38 39 private Object constValue = notLoadedConstValue; 40 41 private static final Object notLoadedConstValue = new Object (); 42 43 static Variable[] loadFields(DataInputStream in, ConstantPool pool, 44 ClassFile cls) 45 throws IOException { 46 int count = in.readUnsignedShort(); 47 Variable[] variables = new Variable[count]; 48 for (int i = 0; i < count; i++) 49 variables[i] = new Variable(in, pool, cls); 50 return variables; 51 } 52 53 54 Variable(DataInputStream in, ConstantPool pool, ClassFile cls) 55 throws IOException { 56 super(in, pool, cls, false); 57 } 58 59 64 public final boolean isConstant() { 65 return attributes.get("ConstantValue") != null; } 67 68 73 @Deprecated 74 public final Object getValue() { 75 return getConstantValue(); 76 } 77 78 83 public final Object getConstantValue() { 84 if (constValue == notLoadedConstValue) { 85 DataInputStream in = attributes.getStream("ConstantValue"); if (in != null) { 87 try { 88 int index = in.readUnsignedShort(); 89 CPEntry cpe = classFile.constantPool.get(index); 90 constValue = cpe.getValue(); 91 } catch (IOException e) { 92 throw new InvalidClassFileAttributeException("invalid ConstantValue attribute", e); 93 } 94 } 95 } 96 return constValue; 97 } 98 99 106 public final String getDeclaration() { 107 StringBuffer sb = new StringBuffer (); 108 sb.append(CPFieldMethodInfo.getSignature(getDescriptor(), false)); 109 sb.append(' '); 110 sb.append(getName()); 111 return sb.toString(); 112 } 113 114 117 public final boolean isEnumConstant() { 118 return (access & Access.ENUM) == Access.ENUM; 119 } 120 121 public String toString() { 122 StringBuffer sb = new StringBuffer (super.toString()); 123 if (isConstant()) { 124 sb.append(", const value="); sb.append(getValue()); 126 } 127 return sb.toString(); 128 } 129 } 130 | Popular Tags |