summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt.tools/JNI Generation/org/eclipse/swt/tools/internal/CleanupNatives.java
blob: 0e908795b124b6bf4bbcda487fa90e62460557e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.tools.internal;

import java.util.*;
import java.lang.reflect.*;

public class CleanupNatives extends CleanupClass {
	
public CleanupNatives() {
}

String[] getArgNames(Method method) {
	int n_args = method.getParameterTypes().length;
	if (n_args == 0) return new String[0];
	String name = method.getName();
	String params = "";
	int index = 0;
	while (true) {
		index = classSource.indexOf(name, index + 1);
		if (index == -1) return null;
		int parantesesStart = classSource.indexOf("(", index);
		if (classSource.substring(index + name.length(), parantesesStart).trim().length() == 0) {
			int parantesesEnd = classSource.indexOf(")", parantesesStart);
 			params = classSource.substring(parantesesStart + 1, parantesesEnd);
 			break;
		}
	}
	String[] names = new String[n_args];
	StringTokenizer tk = new StringTokenizer(params, ",");
	for (int i = 0; i < names.length; i++) {
		String s = tk.nextToken().trim();
		StringTokenizer tk1 = new StringTokenizer(s, " ");
		String s1 = null;
		while (tk1.hasMoreTokens()) {
			s1 = tk1.nextToken();
		}
		names[i] = s1.trim();
	}
	return names;	
}

public void generate(Class clazz) {
	unusedCount = usedCount = 0;
	super.generate(clazz);
	Method[] methods = clazz.getDeclaredMethods();
	generate(methods);
	output("used=" + usedCount + " unused=" + unusedCount + " total=" + (unusedCount + usedCount));
}

public void generate(Method[] methods) {
	Arrays.sort(methods, new Comparator() {
		public int compare(Object a, Object b) {
			return ((Method)a).getName().compareTo(((Method)b).getName());
		}
	});
	
	for (int i = 0; i < methods.length; i++) {
		Method method = methods[i];
		if ((method.getModifiers() & Modifier.NATIVE) == 0) continue;
		generate(method);
	}
}

public void generate(Method method) {
	String name = method.getName();
	Enumeration keys = files.keys();
	while (keys.hasMoreElements()) {
		Object key = keys.nextElement();
		String str = (String)files.get(key);
		if (str.indexOf(name) != -1) {
			int modifiers = method.getModifiers();
			Class clazz = method.getDeclaringClass();
			String modifiersStr = Modifier.toString(modifiers);
			output(modifiersStr);
			if (modifiersStr.length() > 0) output(" ");
			output(getTypeSignature3(method.getReturnType()));
			output(" " );
			output(method.getName());
			output("(");
			Class[] paramTypes = method.getParameterTypes();
			String[] paramNames = getArgNames(method);
			for (int i = 0; i < paramTypes.length; i++) {
				Class paramType = paramTypes[i];
				if (i != 0) output(", ");
				String sig = getTypeSignature3(paramType);
				if (clazz.getPackage().equals(paramType.getPackage())) sig = getClassName(paramType);
				output(sig);
				output(" ");
				output(paramNames[i]);
			}
			output(");");
			outputDelimiter();
			usedCount++;
			return;
		}
	}
	unusedCount++;
//	output("NOT USED=" + method.toString() + "\n");
}

public static void main(String[] args) {
//	args = new String[]{"a", "b", "c"};
	if (args.length < 2) {
		System.out.println("Usage: java CleanupNatives <OS className> <src path> <class source>");
		return;
	}
	try {
		CleanupNatives gen = new CleanupNatives();
		String clazzName = args[0];
		String[] sourcePath = new String[]{args[1]};
		String classSource = args[2]; 
//		clazzName = "org.eclipse.swt.internal.gtk.OS";
//		sourcePath = new String[]{
//			"/bluebird/teamswt/ssq/eclipse-gtk/workspace/org.eclipse.swt/Eclipse SWT/gtk/",
//			"/bluebird/teamswt/ssq/eclipse-gtk/workspace/org.eclipse.swt/Eclipse SWT Drag and Drop/gtk/",
//		};
//		classSource = "/bluebird/teamswt/ssq/eclipse-gtk/workspace/org.eclipse.swt/Eclipse SWT PI/gtk/org/eclipse/swt/internal/gtk/OS.java";
		Class clazz = Class.forName(clazzName);
		gen.setSourcePath(sourcePath);
		gen.setClassSourcePath(classSource);
		gen.generate(clazz);
	} catch (Exception e) {
		System.out.println("Problem");
		e.printStackTrace(System.out);
	}
}

}