1 11 package org.eclipse.swt.tools.internal; 12 13 import java.lang.reflect.*; 14 import java.util.*; 15 16 public class CleanupConstants extends CleanupClass { 17 18 String getFieldValue(Field field) { 19 String name = field.getName(); 20 int index = 0; 21 while (true) { 22 index = classSource.indexOf(name, index + 1); 23 if (index == -1) return null; 24 int equalsIndex = classSource.indexOf("=", index); 25 if (classSource.substring(index + name.length(), equalsIndex).trim().length() == 0) { 26 int semiIndex = classSource.indexOf(";", equalsIndex); 27 return classSource.substring(equalsIndex + 1, semiIndex).trim(); 28 } 29 } 30 } 31 32 public void generate(Class clazz) { 33 unusedCount = usedCount = 0; 34 super.generate(clazz); 35 Field[] fields = clazz.getDeclaredFields(); 36 generate(fields); 37 output("used=" + usedCount + " unused=" + unusedCount + " total=" + (unusedCount + usedCount)); 38 } 39 40 public void generate(Field[] fields) { 41 sort(fields); 42 for (int i = 0; i < fields.length; i++) { 43 Field field = fields[i]; 44 if ((field.getModifiers() & Modifier.FINAL) == 0) continue; 45 generate(field); 46 } 47 } 48 49 public void generate(Field field) { 50 String name = field.getName(); 51 Enumeration keys = files.keys(); 52 while (keys.hasMoreElements()) { 53 Object key = keys.nextElement(); 54 String str = (String )files.get(key); 55 if (str.indexOf(name) != -1) { 56 int modifiers = field.getModifiers(); 57 String modifiersStr = Modifier.toString(modifiers); 58 output("\t"); 59 output(modifiersStr); 60 if (modifiersStr.length() > 0) output(" "); 61 output(getTypeSignature3(field.getType())); 62 output(" " ); 63 output(field.getName()); 64 output(" = "); 65 output(getFieldValue(field)); 66 outputln(";"); 67 usedCount++; 68 return; 69 } 70 } 71 unusedCount++; 72 } 74 75 public static void main(String [] args) { 76 if (args.length < 2) { 77 System.out.println("Usage: java CleanupConstants <OS className> <src path> <class source>"); 78 return; 79 } 80 try { 81 CleanupConstants gen = new CleanupConstants(); 82 String clazzName = args[0]; 83 String [] sourcePath = new String []{args[1]}; 84 String classSource = args[2]; 85 Class clazz = Class.forName(clazzName); 86 gen.setSourcePath(sourcePath); 87 gen.setClassSourcePath(classSource); 88 gen.generate(clazz); 89 } catch (Exception e) { 90 System.out.println("Problem"); 91 e.printStackTrace(System.out); 92 } 93 } 94 95 } 96 | Popular Tags |