1 11 package org.eclipse.swt.tools.internal; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.lang.reflect.Field ; 16 import java.lang.reflect.Method ; 17 import java.util.Properties ; 18 19 public class MetaData { 20 21 Properties data; 22 23 public MetaData(String mainClass) { 24 data = new Properties (); 25 int index = 0; 26 Class clazz = getClass(); 27 int length = mainClass.length(); 28 while (index < length) { 29 index = mainClass.indexOf('.', index); 30 if (index == -1) index = length; 31 InputStream is = clazz.getResourceAsStream(mainClass.substring(0, index) + ".properties"); 32 if (is != null) { 33 try { 34 data.load(is); 35 } catch (IOException e) { 36 } finally { 37 try { 38 is.close(); 39 } catch (IOException e) {} 40 } 41 42 } 43 index++; 44 } 45 } 46 47 public MetaData(Properties data) { 48 this.data = data; 49 } 50 51 public ClassData getMetaData(Class clazz) { 52 String key = JNIGenerator.toC(clazz.getName()); 53 String value = getMetaData(key, ""); 54 return new ClassData(clazz, value); 55 } 56 57 public FieldData getMetaData(Field field) { 58 String className = JNIGenerator.getClassName(field.getDeclaringClass()); 59 String key = className + "_" + field.getName(); 60 String value = getMetaData(key, ""); 61 return new FieldData(field, value); 62 } 63 64 boolean convertTo32Bit(Class [] paramTypes) { 65 boolean changed = false; 66 for (int i = 0; i < paramTypes.length; i++) { 67 Class paramType = paramTypes[i]; 68 if (paramType == Long.TYPE) { 69 paramTypes[i] = Integer.TYPE; 70 changed = true; 71 } 72 if (paramType == long[].class) { 73 paramTypes[i] = int[].class; 74 changed = true; 75 } 76 } 77 return changed; 78 } 79 80 public MethodData getMetaData(Method method) { 81 String className = JNIGenerator.getClassName(method.getDeclaringClass()); 82 String key = className + "_" + JNIGenerator.getFunctionName(method); 83 String value = getMetaData(key, null); 84 if (value == null) { 85 key = className + "_" + method.getName(); 86 value = getMetaData(key, null); 87 } 88 91 if (value == null) { 92 Class [] paramTypes = method.getParameterTypes(); 93 if (convertTo32Bit(paramTypes)) { 94 key = className + "_" + JNIGenerator.getFunctionName(method, paramTypes); 95 value = getMetaData(key, null); 96 } 97 } 98 if (value == null) value = ""; 99 return new MethodData(method, value); 100 } 101 102 public ParameterData getMetaData(Method method, int parameter) { 103 String className = JNIGenerator.getClassName(method.getDeclaringClass()); 104 String key = className + "_" + JNIGenerator.getFunctionName(method) + "_" + parameter; 105 String value = getMetaData(key, null); 106 if (value == null) { 107 key = className + "_" + method.getName() + "_" + parameter; 108 value = getMetaData(key, null); 109 } 110 113 if (value == null) { 114 Class [] paramTypes = method.getParameterTypes(); 115 if (convertTo32Bit(paramTypes)) { 116 key = className + "_" + JNIGenerator.getFunctionName(method, paramTypes) + "_" + parameter; 117 value = getMetaData(key, null); 118 } 119 } 120 if (value == null) value = ""; 121 return new ParameterData(method, parameter, value); 122 } 123 124 public String getMetaData(String key, String defaultValue) { 125 return data.getProperty(key, defaultValue); 126 } 127 128 public void setMetaData(Class clazz, ClassData value) { 129 String key = JNIGenerator.toC(clazz.getName()); 130 setMetaData(key, value.toString()); 131 } 132 133 public void setMetaData(Field field, FieldData value) { 134 String className = JNIGenerator.getClassName(field.getDeclaringClass()); 135 String key = className + "_" + field.getName(); 136 setMetaData(key, value.toString()); 137 } 138 139 public void setMetaData(Method method, MethodData value) { 140 String key; 141 String className = JNIGenerator.getClassName(method.getDeclaringClass()); 142 if (JNIGenerator.isNativeUnique(method)) { 143 key = className + "_" + method.getName (); 144 } else { 145 key = className + "_" + JNIGenerator.getFunctionName(method); 146 } 147 setMetaData(key, value.toString()); 148 } 149 150 public void setMetaData(Method method, int arg, ParameterData value) { 151 String key; 152 String className = JNIGenerator.getClassName(method.getDeclaringClass()); 153 if (JNIGenerator.isNativeUnique(method)) { 154 key = className + "_" + method.getName () + "_" + arg; 155 } else { 156 key = className + "_" + JNIGenerator.getFunctionName(method) + "_" + arg; 157 } 158 setMetaData(key, value.toString()); 159 } 160 161 public void setMetaData(String key, String value) { 162 data.setProperty(key, value); 163 } 164 165 } 166 | Popular Tags |