1 11 package org.eclipse.swt.tools.internal; 12 13 import java.util.*; 14 import java.lang.reflect.*; 15 16 public class CleanupNatives extends CleanupClass { 17 18 public CleanupNatives() { 19 } 20 21 String [] getArgNames(Method method) { 22 int n_args = method.getParameterTypes().length; 23 if (n_args == 0) return new String [0]; 24 String name = method.getName(); 25 String params = ""; 26 int index = 0; 27 while (true) { 28 index = classSource.indexOf(name, index + 1); 29 if (index == -1) return null; 30 int parantesesStart = classSource.indexOf("(", index); 31 if (classSource.substring(index + name.length(), parantesesStart).trim().length() == 0) { 32 int parantesesEnd = classSource.indexOf(")", parantesesStart); 33 params = classSource.substring(parantesesStart + 1, parantesesEnd); 34 break; 35 } 36 } 37 String [] names = new String [n_args]; 38 StringTokenizer tk = new StringTokenizer(params, ","); 39 for (int i = 0; i < names.length; i++) { 40 String s = tk.nextToken().trim(); 41 StringTokenizer tk1 = new StringTokenizer(s, " "); 42 String s1 = null; 43 while (tk1.hasMoreTokens()) { 44 s1 = tk1.nextToken(); 45 } 46 names[i] = s1.trim(); 47 } 48 return names; 49 } 50 51 public void generate(Class clazz) { 52 unusedCount = usedCount = 0; 53 super.generate(clazz); 54 Method[] methods = clazz.getDeclaredMethods(); 55 generate(methods); 56 output("used=" + usedCount + " unused=" + unusedCount + " total=" + (unusedCount + usedCount)); 57 } 58 59 public void generate(Method[] methods) { 60 sort(methods); 61 for (int i = 0; i < methods.length; i++) { 62 Method method = methods[i]; 63 if ((method.getModifiers() & Modifier.NATIVE) == 0) continue; 64 generate(method); 65 } 66 } 67 68 public void generate(Method method) { 69 String name = method.getName(); 70 Enumeration keys = files.keys(); 71 while (keys.hasMoreElements()) { 72 Object key = keys.nextElement(); 73 String str = (String )files.get(key); 74 if (str.indexOf(name) != -1) { 75 int modifiers = method.getModifiers(); 76 Class clazz = method.getDeclaringClass(); 77 String modifiersStr = Modifier.toString(modifiers); 78 output(modifiersStr); 79 if (modifiersStr.length() > 0) output(" "); 80 output(getTypeSignature3(method.getReturnType())); 81 output(" " ); 82 output(method.getName()); 83 output("("); 84 Class [] paramTypes = method.getParameterTypes(); 85 String [] paramNames = getArgNames(method); 86 for (int i = 0; i < paramTypes.length; i++) { 87 Class paramType = paramTypes[i]; 88 if (i != 0) output(", "); 89 String sig = getTypeSignature3(paramType); 90 if (clazz.getPackage().equals(paramType.getPackage())) sig = getClassName(paramType); 91 output(sig); 92 output(" "); 93 output(paramNames[i]); 94 } 95 outputln(");"); 96 usedCount++; 97 return; 98 } 99 } 100 unusedCount++; 101 } 103 104 public static void main(String [] args) { 105 if (args.length < 2) { 106 System.out.println("Usage: java CleanupNatives <OS className> <src path> <class source>"); 107 return; 108 } 109 try { 110 CleanupNatives gen = new CleanupNatives(); 111 String clazzName = args[0]; 112 String [] sourcePath = new String []{args[1]}; 113 String classSource = args[2]; 114 Class clazz = Class.forName(clazzName); 115 gen.setSourcePath(sourcePath); 116 gen.setClassSourcePath(classSource); 117 gen.generate(clazz); 118 } catch (Exception e) { 119 System.out.println("Problem"); 120 e.printStackTrace(System.out); 121 } 122 } 123 124 } 125 | Popular Tags |