KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
14 import java.lang.reflect.*;
15
16 public class CleanupNatives extends CleanupClass {
17     
18 public CleanupNatives() {
19 }
20
21 String JavaDoc[] getArgNames(Method method) {
22     int n_args = method.getParameterTypes().length;
23     if (n_args == 0) return new String JavaDoc[0];
24     String JavaDoc name = method.getName();
25     String JavaDoc 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 JavaDoc[] names = new String JavaDoc[n_args];
38     StringTokenizer tk = new StringTokenizer(params, ",");
39     for (int i = 0; i < names.length; i++) {
40         String JavaDoc s = tk.nextToken().trim();
41         StringTokenizer tk1 = new StringTokenizer(s, " ");
42         String JavaDoc 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 JavaDoc 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 JavaDoc name = method.getName();
70     Enumeration keys = files.keys();
71     while (keys.hasMoreElements()) {
72         Object JavaDoc key = keys.nextElement();
73         String JavaDoc str = (String JavaDoc)files.get(key);
74         if (str.indexOf(name) != -1) {
75             int modifiers = method.getModifiers();
76             Class JavaDoc clazz = method.getDeclaringClass();
77             String JavaDoc 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 JavaDoc[] paramTypes = method.getParameterTypes();
85             String JavaDoc[] paramNames = getArgNames(method);
86             for (int i = 0; i < paramTypes.length; i++) {
87                 Class JavaDoc paramType = paramTypes[i];
88                 if (i != 0) output(", ");
89                 String JavaDoc 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 // output("NOT USED=" + method.toString() + "\n");
102
}
103
104 public static void main(String JavaDoc[] 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 JavaDoc clazzName = args[0];
112         String JavaDoc[] sourcePath = new String JavaDoc[]{args[1]};
113         String JavaDoc classSource = args[2];
114         Class JavaDoc clazz = Class.forName(clazzName);
115         gen.setSourcePath(sourcePath);
116         gen.setClassSourcePath(classSource);
117         gen.generate(clazz);
118     } catch (Exception JavaDoc e) {
119         System.out.println("Problem");
120         e.printStackTrace(System.out);
121     }
122 }
123
124 }
125
Popular Tags