1 32 package net.sf.retrotranslator.runtime.impl; 33 34 import java.io.*; 35 import java.lang.reflect.*; 36 import java.util.MissingResourceException ; 37 import net.sf.retrotranslator.runtime.asm.Type; 38 39 42 public class RuntimeTools { 43 44 public static final String CONSTRUCTOR_NAME = "<init>"; 45 public static final String STATIC_NAME = "<clinit>"; 46 public static final String CLASS_EXTENSION = ".class"; 47 48 public static Class getBaseClass(char type) { 49 return getBaseClass(Type.getType(new String (new char[]{type}))); 50 } 51 52 public static Class getBaseClass(Type type) { 53 switch (type.getSort()) { 54 case Type.VOID: 55 return void.class; 56 case Type.BOOLEAN: 57 return boolean.class; 58 case Type.CHAR: 59 return char.class; 60 case Type.BYTE: 61 return byte.class; 62 case Type.SHORT: 63 return short.class; 64 case Type.INT: 65 return int.class; 66 case Type.FLOAT: 67 return float.class; 68 case Type.LONG: 69 return long.class; 70 case Type.DOUBLE: 71 return double.class; 72 } 73 return null; 74 } 75 76 public static String getConstructorDescriptor(final Constructor c) { 77 Class [] parameters = c.getParameterTypes(); 78 StringBuffer buf = new StringBuffer ("("); 79 for (Class parameter : parameters) { 80 buf.append(Type.getDescriptor(parameter)); 81 } 82 return buf.append(")V").toString(); 83 } 84 85 public static Object cloneNonEmptyArray(Object value) { 86 if (!value.getClass().isArray() || Array.getLength(value) == 0) return value; 87 if (value instanceof Object []) return ((Object []) value).clone(); 88 if (value instanceof boolean[]) return ((boolean[]) value).clone(); 89 if (value instanceof byte[]) return ((byte[]) value).clone(); 90 if (value instanceof char[]) return ((char[]) value).clone(); 91 if (value instanceof double[]) return ((double[]) value).clone(); 92 if (value instanceof float[]) return ((float[]) value).clone(); 93 if (value instanceof int[]) return ((int[]) value).clone(); 94 if (value instanceof long[]) return ((long[]) value).clone(); 95 if (value instanceof short[]) return ((short[]) value).clone(); 96 throw new IllegalStateException (); 97 } 98 99 public static String getString(java.lang.reflect.Type type) { 100 if (!(type instanceof Class )) return type.toString(); 101 Class aClass = (Class ) type; 102 int dimensionCount = 0; 103 for (; aClass.isArray(); dimensionCount++) { 104 aClass = aClass.getComponentType(); 105 } 106 if (dimensionCount == 0) return aClass.getName(); 107 StringBuilder builder = new StringBuilder (); 108 builder.append(aClass.getName()); 109 for (; dimensionCount > 0; dimensionCount--) { 110 builder.append("[]"); 111 } 112 return builder.toString(); 113 } 114 115 public static StringBuilder append(StringBuilder builder, java.lang.reflect.Type [] types) { 116 for (int i = 0; i < types.length; i++) { 117 if (i > 0) builder.append(','); 118 builder.append(getString(types[i])); 119 } 120 return builder; 121 } 122 123 public static byte[] readResourceToByteArray(Class loader, String resourceName) throws MissingResourceException { 124 InputStream inputStream = loader.getResourceAsStream(resourceName); 125 if (inputStream == null) return null; 126 try { 127 try { 128 byte[] buffer = new byte[0x1000]; 129 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 130 int count; 131 while((count = inputStream.read(buffer)) > 0) { 132 outputStream.write(buffer, 0, count); 133 } 134 return outputStream.toByteArray(); 135 } finally { 136 inputStream.close(); 137 } 138 } catch (IOException e) { 139 throw new RuntimeException (e); 140 } 141 } 142 143 public static byte[] getBytecode(Class target) { 144 if (target.isPrimitive() || target.isArray()) return null; 145 String targetName = target.getName(); 146 int index = targetName.lastIndexOf('.'); 147 String simpleName = index < 0 ? targetName : targetName.substring(index + 1); 148 return readResourceToByteArray(target, simpleName + CLASS_EXTENSION); 149 } 150 151 } 152 | Popular Tags |