KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > tools > internal > CleanupConstants


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 JavaDoc getFieldValue(Field field) {
19     String JavaDoc 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 JavaDoc 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 JavaDoc name = field.getName();
51     Enumeration keys = files.keys();
52     while (keys.hasMoreElements()) {
53         Object JavaDoc key = keys.nextElement();
54         String JavaDoc str = (String JavaDoc)files.get(key);
55         if (str.indexOf(name) != -1) {
56             int modifiers = field.getModifiers();
57             String JavaDoc 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 // output("NOT USED=" + field.toString() + " \n");
73
}
74
75 public static void main(String JavaDoc[] 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 JavaDoc clazzName = args[0];
83         String JavaDoc[] sourcePath = new String JavaDoc[]{args[1]};
84         String JavaDoc classSource = args[2];
85         Class JavaDoc clazz = Class.forName(clazzName);
86         gen.setSourcePath(sourcePath);
87         gen.setClassSourcePath(classSource);
88         gen.generate(clazz);
89     } catch (Exception JavaDoc e) {
90         System.out.println("Problem");
91         e.printStackTrace(System.out);
92     }
93 }
94
95 }
96
Popular Tags