1 7 8 package org.gjt.jclasslib.structures; 9 10 import org.gjt.jclasslib.io.Log; 11 12 import java.io.*; 13 import java.lang.reflect.Array ; 14 15 22 public abstract class AbstractStructure { 23 24 28 public static final String SYSTEM_PROPERTY_DEBUG = "jclasslib.io.debug"; 29 30 34 protected ClassFile classFile; 35 36 39 protected boolean debug; 40 41 44 protected AbstractStructure() { 45 debug = Boolean.getBoolean(SYSTEM_PROPERTY_DEBUG); 46 } 47 48 53 public ClassFile getClassFile() { 54 return classFile; 55 } 56 57 64 public void setClassFile(ClassFile classFile) { 65 this.classFile = classFile; 66 } 67 68 79 public void read(DataInput in) 80 throws InvalidByteCodeException, IOException { 81 } 82 83 92 public void write(DataOutput out) 93 throws InvalidByteCodeException, IOException { 94 } 95 96 101 public boolean getDebug() { 102 return debug; 103 } 104 105 110 public void setDebug(boolean debug) { 111 this.debug = debug; 112 } 113 114 122 protected int getLength(Object array) { 123 if (array == null || !array.getClass().isArray()) { 124 return 0; 125 } else { 126 return Array.getLength(array); 127 } 128 } 129 130 135 protected void debug(String message) { 136 if (debug) { 137 Log.debug(message); 138 } 139 } 140 141 147 protected String printBytes(int bytes) { 148 return padHexString(Integer.toHexString(bytes), 8); 149 } 150 151 158 protected String printAccessFlags(int accessFlags) { 159 return padHexString(Integer.toHexString(accessFlags), 4); 160 } 161 162 private String padHexString(String hexString, int length) { 163 StringBuffer buffer = new StringBuffer ("0x"); 164 165 for (int i = hexString.length(); i < length; i++) { 166 buffer.append('0'); 167 } 168 buffer.append(hexString); 169 170 return buffer.toString(); 171 } 172 173 180 abstract protected String printAccessFlagsVerbose(int accessFlags); 181 182 193 protected String printAccessFlagsVerbose(int[] availableAccessFlags, String [] availableAccessFlagsVerbose, 194 int accessFlags) { 195 StringBuffer accessFlagsVerbose = new StringBuffer (); 196 197 198 int all = 0; 199 for (int i = 0; i < availableAccessFlags.length; i++) { 200 all |= availableAccessFlags[i]; 201 if ((accessFlags & availableAccessFlags[i]) != 0) { 202 accessFlagsVerbose.append(availableAccessFlagsVerbose[i]).append(' '); 203 } 204 } 205 206 if ((all | accessFlags) != all) { 208 accessFlagsVerbose.append("? "); 209 } 210 if (accessFlagsVerbose.length() > 0) { 211 accessFlagsVerbose.setLength(accessFlagsVerbose.length() - 1); 212 } 213 return accessFlagsVerbose.toString(); 214 } 215 216 } 217 | Popular Tags |