KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
14 import java.lang.reflect.*;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.zip.*;
18 import java.util.Arrays JavaDoc;
19
20 import org.eclipse.swt.SWT;
21
22 public class JNIGeneratorApp {
23
24     Class JavaDoc mainClass;
25     ProgressMonitor progress;
26     String JavaDoc mainClassName, outputDir, classpath;
27     MetaData metaData;
28
29 public JNIGeneratorApp() {
30 }
31
32 public String JavaDoc getClasspath() {
33     return classpath;
34 }
35
36 public Class JavaDoc getMainClass() {
37     return mainClass;
38 }
39
40 public String JavaDoc getMainClassName() {
41     return mainClassName;
42 }
43
44 public MetaData getMetaData() {
45     return metaData;
46 }
47
48 String JavaDoc getMetaDataDir() {
49     return "./JNI Generation/org/eclipse/swt/tools/internal/";
50 }
51
52 public String JavaDoc getOutputDir() {
53     return outputDir;
54 }
55
56 void generateSTATS_C(Class JavaDoc[] classes) {
57     try {
58         StatsGenerator gen = new StatsGenerator(false);
59         gen.setMainClass(mainClass);
60         gen.setClasses(classes);
61         gen.setMetaData(metaData);
62         gen.setProgressMonitor(progress);
63         ByteArrayOutputStream out = new ByteArrayOutputStream();
64         gen.setOutput(new PrintStream(out));
65         gen.generate();
66         if (out.size() > 0) output(out.toByteArray(), outputDir + gen.getFileName());
67     } catch (Exception JavaDoc e) {
68         System.out.println("Problem");
69         e.printStackTrace(System.out);
70     }
71 }
72
73 void generateSTATS_H(Class JavaDoc[] classes) {
74     try {
75         StatsGenerator gen = new StatsGenerator(true);
76         gen.setMainClass(mainClass);
77         gen.setClasses(classes);
78         gen.setMetaData(metaData);
79         gen.setProgressMonitor(progress);
80         ByteArrayOutputStream out = new ByteArrayOutputStream();
81         gen.setOutput(new PrintStream(out));
82         gen.generate();
83         if (out.size() > 0) output(out.toByteArray(), outputDir + gen.getFileName());
84     } catch (Exception JavaDoc e) {
85         System.out.println("Problem");
86         e.printStackTrace(System.out);
87     }
88 }
89
90 void generateSTRUCTS_H(Class JavaDoc[] classes) {
91     try {
92         StructsGenerator gen = new StructsGenerator(true);
93         gen.setMainClass(mainClass);
94         gen.setClasses(classes);
95         gen.setMetaData(metaData);
96         gen.setProgressMonitor(progress);
97         ByteArrayOutputStream out = new ByteArrayOutputStream();
98         gen.setOutput(new PrintStream(out));
99         gen.generate();
100         if (out.size() > 0) output(out.toByteArray(), outputDir + gen.getFileName());
101     } catch (Exception JavaDoc e) {
102         System.out.println("Problem");
103         e.printStackTrace(System.out);
104     }
105
106 }
107
108 void generateSTRUCTS_C(Class JavaDoc[] classes) {
109     try {
110         StructsGenerator gen = new StructsGenerator(false);
111         gen.setMainClass(mainClass);
112         gen.setClasses(classes);
113         gen.setMetaData(metaData);
114         gen.setProgressMonitor(progress);
115         ByteArrayOutputStream out = new ByteArrayOutputStream();
116         gen.setOutput(new PrintStream(out));
117         gen.generate();
118         if (out.size() > 0) output(out.toByteArray(), outputDir + gen.getFileName());
119     } catch (Exception JavaDoc e) {
120         System.out.println("Problem");
121         e.printStackTrace(System.out);
122     }
123
124 }
125
126 void generateSWT_C(Class JavaDoc[] classes) {
127     try {
128         NativesGenerator gen = new NativesGenerator();
129         gen.setMainClass(mainClass);
130         gen.setClasses(classes);
131         gen.setMetaData(metaData);
132         gen.setProgressMonitor(progress);
133         ByteArrayOutputStream out = new ByteArrayOutputStream();
134         gen.setOutput(new PrintStream(out));
135         gen.generate();
136         if (out.size() > 0) output(out.toByteArray(), outputDir + gen.getFileName());
137     } catch (Exception JavaDoc e) {
138         System.out.println("Problem");
139         e.printStackTrace(System.out);
140     }
141 }
142
143
144 void generateMetaData(Class JavaDoc[] classes) {
145     try {
146         MetaDataGenerator gen = new MetaDataGenerator();
147         gen.setMainClass(mainClass);
148         gen.setClasses(classes);
149         gen.setMetaData(metaData);
150         gen.setProgressMonitor(progress);
151         ByteArrayOutputStream out = new ByteArrayOutputStream();
152         gen.setOutput(new PrintStream(out));
153         gen.generate();
154         if (!new File(getMetaDataDir()).exists()) {
155             System.out.println("Warning: Meta data output dir does not exist");
156             return;
157         }
158         if (out.size() > 0) output(out.toByteArray(), getMetaDataDir() + gen.getFileName());
159     } catch (Exception JavaDoc e) {
160         System.out.println("Problem");
161         e.printStackTrace(System.out);
162     }
163 }
164
165 public void generate() {
166     generate(null);
167 }
168
169 public void generate(ProgressMonitor progress) {
170     if (mainClassName == null) return;
171     if (progress != null) progress.setMessage("Initializing...");
172     Class JavaDoc[] classes = getClasses();
173     Class JavaDoc[] natives = getNativesClasses();
174     Class JavaDoc[] structs = getStructureClasses();
175     this.progress = progress;
176     if (progress != null) {
177         int nativeCount = 0;
178         for (int i = 0; i < natives.length; i++) {
179             Class JavaDoc clazz = natives[i];
180             Method[] methods = clazz.getDeclaredMethods();
181             for (int j = 0; j < methods.length; j++) {
182                 Method method = methods[j];
183                 if ((method.getModifiers() & Modifier.NATIVE) == 0) continue;
184                 nativeCount++;
185             }
186         }
187         int total = nativeCount * 4;
188         total += classes.length;
189         total += natives.length * 3;
190         total += structs.length * 2;
191         progress.setTotal(total);
192         progress.setMessage("Generating structs.h ...");
193     }
194     generateSTRUCTS_H(structs);
195     if (progress != null) progress.setMessage("Generating structs.c ...");
196     generateSTRUCTS_C(structs);
197     if (progress != null) progress.setMessage("Generating natives ...");
198     generateSWT_C(natives);
199     if (progress != null) progress.setMessage("Generating stats.h ...");
200     generateSTATS_H(natives);
201     if (progress != null) progress.setMessage("Generating stats.c ...");
202     generateSTATS_C(natives);
203     if (progress != null) progress.setMessage("Generating meta data ...");
204     generateMetaData(classes);
205     if (progress != null) progress.setMessage("Done.");
206     this.progress = null;
207 }
208
209 boolean compare(InputStream is1, InputStream is2) throws IOException {
210     while (true) {
211         int c1 = is1.read();
212         int c2 = is2.read();
213         if (c1 != c2) return false;
214         if (c1 == -1) break;
215     }
216     return true;
217 }
218
219 void output(byte[] bytes, String JavaDoc fileName) throws IOException {
220     FileInputStream is = null;
221     try {
222         is = new FileInputStream(fileName);
223         if (compare(new ByteArrayInputStream(bytes), new BufferedInputStream(is))) return;
224     } catch (FileNotFoundException e) {
225     } finally {
226         try {
227             if (is != null) is.close();
228         } catch (IOException e) {}
229     }
230     FileOutputStream out = new FileOutputStream(fileName);
231     out.write(bytes);
232     out.close();
233 }
234
235 String JavaDoc getPackageName(String JavaDoc className) {
236     int dot = mainClassName.lastIndexOf('.');
237     if (dot == -1) return "";
238     return mainClassName.substring(0, dot);
239 }
240
241 String JavaDoc[] getClassNames(String JavaDoc mainClassName) {
242     String JavaDoc pkgName = getPackageName(mainClassName);
243     String JavaDoc classpath = getClasspath();
244     if (classpath == null) classpath = System.getProperty("java.class.path");
245     String JavaDoc pkgPath = pkgName.replace('.', File.separatorChar);
246     String JavaDoc pkgZipPath = pkgName.replace('.', '/');
247     ArrayList JavaDoc classes = new ArrayList JavaDoc();
248     int start = 0;
249     int index = 0;
250     while (index < classpath.length()) {
251         index = classpath.indexOf(File.pathSeparatorChar, start);
252         if (index == -1) index = classpath.length();
253         String JavaDoc path = classpath.substring(start, index);
254         if (path.toLowerCase().endsWith(".jar")) {
255             ZipFile zipFile = null;
256             try {
257                 zipFile = new ZipFile(path);
258                 Enumeration JavaDoc entries = zipFile.entries();
259                 while (entries.hasMoreElements()) {
260                     ZipEntry entry = (ZipEntry)entries.nextElement();
261                     String JavaDoc name = entry.getName();
262                     if (name.startsWith(pkgZipPath) && name.endsWith(".class")) {
263                         String JavaDoc className = name.substring(pkgZipPath.length() + 1, name.length() - 6);
264                         className.replace('/', '.');
265                         classes.add(className);
266                     }
267                 }
268             } catch (IOException e) {
269             } finally {
270                 try {
271                     if (zipFile != null) zipFile.close();
272                 } catch (IOException ex) {}
273             }
274         } else {
275             File file = new File(path + File.separator + pkgPath);
276             if (file.exists()) {
277                 String JavaDoc[] entries = file.list();
278                 for (int i = 0; i < entries.length; i++) {
279                     String JavaDoc entry = entries[i];
280                     File f = new File(file, entry);
281                     if (!f.isDirectory()) {
282                         if (f.getAbsolutePath().endsWith(".class")) {
283                             String JavaDoc className = entry.substring(0, entry.length() - 6);
284                             classes.add(className);
285                         }
286                     } else {
287                         throw new Error JavaDoc("SUBDIR NOT DONE=" + f);
288                     }
289                 }
290             }
291         }
292         start = index + 1;
293     }
294     return (String JavaDoc[])classes.toArray(new String JavaDoc[classes.size()]);
295 }
296
297 public Class JavaDoc[] getClasses() {
298     if (mainClassName == null) return new Class JavaDoc[0];
299     String JavaDoc[] classNames = getClassNames(mainClassName);
300     Arrays.sort(classNames);
301     String JavaDoc packageName = getPackageName(mainClassName);
302     Class JavaDoc[] classes = new Class JavaDoc[classNames.length];
303     for (int i = 0; i < classNames.length; i++) {
304         String JavaDoc className = classNames[i];
305         try {
306             classes[i] = Class.forName(packageName + "." + className, false, getClass().getClassLoader());
307         } catch (Exception JavaDoc e) {
308             e.printStackTrace();
309         }
310     }
311     return classes;
312 }
313
314 public Class JavaDoc[] getNativesClasses() {
315     if (mainClassName == null) return new Class JavaDoc[0];
316     ArrayList JavaDoc result = new ArrayList JavaDoc();
317     Class JavaDoc[] classes = getClasses();
318     for (int i = 0; i < classes.length; i++) {
319         Class JavaDoc clazz = classes[i];
320         Method[] methods = clazz.getDeclaredMethods();
321         for (int j = 0; j < methods.length; j++) {
322             Method method = methods[j];
323             int mods = method.getModifiers();
324             if ((mods & Modifier.NATIVE) != 0) {
325                 result.add(clazz);
326                 break;
327             }
328         }
329     }
330     return (Class JavaDoc[])result.toArray(new Class JavaDoc[result.size()]);
331 }
332
333 public Class JavaDoc[] getStructureClasses() {
334     if (mainClassName == null) return new Class JavaDoc[0];
335     ArrayList JavaDoc result = new ArrayList JavaDoc();
336     Class JavaDoc[] classes = getClasses();
337     outer:
338     for (int i = 0; i < classes.length; i++) {
339         Class JavaDoc clazz = classes[i];
340         Method[] methods = clazz.getDeclaredMethods();
341         for (int j = 0; j < methods.length; j++) {
342             Method method = methods[j];
343             int mods = method.getModifiers();
344             if ((mods & Modifier.NATIVE) != 0) continue outer;
345         }
346         Field[] fields = clazz.getFields();
347         boolean hasPublicFields = false;
348         for (int j = 0; j < fields.length; j++) {
349             Field field = fields[j];
350             int mods = field.getModifiers();
351             if ((mods & Modifier.PUBLIC) != 0 && (mods & Modifier.STATIC) == 0) {
352                 hasPublicFields = true;
353                 break;
354             }
355         }
356         if (!hasPublicFields) continue;
357         result.add(clazz);
358     }
359     return (Class JavaDoc[])result.toArray(new Class JavaDoc[result.size()]);
360 }
361
362 public void setClasspath(String JavaDoc classpath) {
363     this.classpath = classpath;
364 }
365
366 public void setMainClassName(String JavaDoc str) {
367     mainClassName = str;
368     metaData = new MetaData(mainClassName);
369     String JavaDoc mainClasses = getMetaData().getMetaData("swt_main_classes", null);
370     if (mainClasses != null) {
371         String JavaDoc[] list = ItemData.split(mainClasses, ",");
372         for (int i = 0; i < list.length; i += 2) {
373             if (mainClassName.equals(list[i].trim())) {
374                 setOutputDir(list[i + 1].trim());
375             }
376         }
377     }
378     if (mainClassName != null) {
379         try {
380             mainClass = Class.forName(mainClassName, false, getClass().getClassLoader());
381         } catch (Exception JavaDoc e) {
382             e.printStackTrace();
383         }
384     }
385 }
386
387 public void setOutputDir(String JavaDoc str) {
388     if (str != null) {
389         if (!str.endsWith("\\") && !str.endsWith("/") ) {
390             str += File.separator;
391         }
392     }
393     outputDir = str;
394 }
395
396 public static String JavaDoc getDefaultMainClass() {
397     return "org.eclipse.swt.internal." + getDefaultPlatform() + ".OS";
398 }
399
400 public static String JavaDoc getDefaultPlatform() {
401     return SWT.getPlatform();
402 }
403
404 public static void main(String JavaDoc[] args) {
405     JNIGeneratorApp gen = new JNIGeneratorApp ();
406     if (args.length > 0) {
407         gen.setMainClassName(args[0]);
408         if (args.length > 1) gen.setOutputDir(args[1]);
409         if (args.length > 2) gen.setClasspath(args[2]);
410     } else {
411         gen.setMainClassName(getDefaultMainClass());
412     }
413     gen.generate();
414 }
415
416 }
417
Popular Tags