1 11 package org.eclipse.swt.tools.internal; 12 13 import java.lang.reflect.*; 14 15 public class StatsGenerator extends JNIGenerator { 16 17 boolean header; 18 19 public StatsGenerator(boolean header) { 20 this.header = header; 21 } 22 23 public void generateCopyright() { 24 generateMetaData("swt_copyright"); 25 } 26 27 public void generateIncludes() { 28 if (!header) { 29 outputln("#include \"swt.h\""); 30 output("#include \""); 31 output(getOutputName()); 32 outputln("_stats.h\""); 33 outputln(); 34 } 35 } 36 37 public void generate(Class clazz) { 38 if (header) { 39 generateHeaderFile(clazz); 40 } else { 41 generateSourceFile(clazz); 42 } 43 } 44 45 public String getExtension() { 46 return header ? ".h" : super.getExtension(); 47 } 48 49 public String getSuffix() { 50 return "_stats"; 51 } 52 53 void generateHeaderFile(Class clazz){ 54 generateNATIVEMacros(clazz); 55 Method[] methods = clazz.getDeclaredMethods(); 56 sort(methods); 57 generateFunctionEnum(methods); 58 } 59 60 void generateNATIVEMacros(Class clazz) { 61 String className = getClassName(clazz); 62 outputln("#ifdef NATIVE_STATS"); 63 output("extern int "); 64 output(className); 65 outputln("_nativeFunctionCount;"); 66 output("extern int "); 67 output(className); 68 outputln("_nativeFunctionCallCount[];"); 69 output("extern char* "); 70 output(className); 71 outputln("_nativeFunctionNames[];"); 72 output("#define "); 73 output(className); 74 output("_NATIVE_ENTER(env, that, func) "); 75 output(className); 76 outputln("_nativeFunctionCallCount[func]++;"); 77 output("#define "); 78 output(className); 79 outputln("_NATIVE_EXIT(env, that, func) "); 80 outputln("#else"); 81 output("#define "); 82 output(className); 83 outputln("_NATIVE_ENTER(env, that, func) "); 84 output("#define "); 85 output(className); 86 outputln("_NATIVE_EXIT(env, that, func) "); 87 outputln("#endif"); 88 outputln(); 89 } 90 91 void generateSourceFile(Class clazz) { 92 outputln("#ifdef NATIVE_STATS"); 93 outputln(); 94 Method[] methods = clazz.getDeclaredMethods(); 95 int methodCount = 0; 96 for (int i = 0; i < methods.length; i++) { 97 Method method = methods[i]; 98 if ((method.getModifiers() & Modifier.NATIVE) == 0) continue; 99 methodCount++; 100 } 101 String className = getClassName(clazz); 102 output("int "); 103 output(className); 104 output("_nativeFunctionCount = "); 105 output(String.valueOf(methodCount)); 106 outputln(";"); 107 output("int "); 108 output(className); 109 output("_nativeFunctionCallCount["); 110 output(String.valueOf(methodCount)); 111 outputln("];"); 112 output("char * "); 113 output(className); 114 outputln("_nativeFunctionNames[] = {"); 115 sort(methods); 116 for (int i = 0; i < methods.length; i++) { 117 Method method = methods[i]; 118 if ((method.getModifiers() & Modifier.NATIVE) == 0) continue; 119 output("\t\""); 120 output(getFunctionName(method)); 121 outputln("\","); 122 if (progress != null) progress.step(); 123 } 124 outputln("};"); 125 outputln(); 126 generateStatsNatives(className); 127 outputln(); 128 outputln("#endif"); 129 } 130 131 void generateStatsNatives(String className) { 132 outputln("#define STATS_NATIVE(func) Java_org_eclipse_swt_tools_internal_NativeStats_##func"); 133 outputln(); 134 135 output("JNIEXPORT jint JNICALL STATS_NATIVE("); 136 output(toC(className + "_GetFunctionCount")); 137 outputln(")"); 138 outputln("\t(JNIEnv *env, jclass that)"); 139 outputln("{"); 140 output("\treturn "); 141 output(className); 142 outputln("_nativeFunctionCount;"); 143 outputln("}"); 144 outputln(); 145 146 output("JNIEXPORT jstring JNICALL STATS_NATIVE("); 147 output(toC(className + "_GetFunctionName")); 148 outputln(")"); 149 outputln("\t(JNIEnv *env, jclass that, jint index)"); 150 outputln("{"); 151 output("\treturn "); 152 if (isCPP) { 153 output("env->NewStringUTF("); 154 } else { 155 output("(*env)->NewStringUTF(env, "); 156 } 157 output(className); 158 outputln("_nativeFunctionNames[index]);"); 159 outputln("}"); 160 outputln(); 161 162 output("JNIEXPORT jint JNICALL STATS_NATIVE("); 163 output(toC(className + "_GetFunctionCallCount")); 164 outputln(")"); 165 outputln("\t(JNIEnv *env, jclass that, jint index)"); 166 outputln("{"); 167 output("\treturn "); 168 output(className); 169 outputln("_nativeFunctionCallCount[index];"); 170 outputln("}"); 171 } 172 173 void generateFunctionEnum(Method[] methods) { 174 if (methods.length == 0) return; 175 outputln("typedef enum {"); 176 for (int i = 0; i < methods.length; i++) { 177 Method method = methods[i]; 178 if ((method.getModifiers() & Modifier.NATIVE) == 0) continue; 179 output("\t"); 180 output(getFunctionName(method)); 181 outputln("_FUNC,"); 182 if (progress != null) progress.step(); 183 } 184 Class clazz = methods[0].getDeclaringClass(); 185 output("} "); 186 output(getClassName(clazz)); 187 outputln("_FUNCS;"); 188 } 189 190 } 191 | Popular Tags |