KickJava   Java API By Example, From Geeks To Geeks.

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


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
15 public class ConstantsGenerator extends JNIGenerator {
16
17 public void generate(Class JavaDoc clazz) {
18     Field[] fields = clazz.getDeclaredFields();
19     generate(fields);
20 }
21
22 public void generate(Field[] fields) {
23     sort(fields);
24     outputln("int main() {");
25     for (int i = 0; i < fields.length; i++) {
26         Field field = fields[i];
27         if ((field.getModifiers() & Modifier.FINAL) == 0) continue;
28         generate(field);
29     }
30     outputln("}");
31 }
32
33 public void generate(Field field) {
34     Class JavaDoc type = field.getType();
35     output("\tprintf(\"public static final ");
36     output(getTypeSignature3(field.getType()));
37     output(" ");
38     output(field.getName());
39     output(" = ");
40     if (type == String JavaDoc.class || type == byte[].class) output("\"%s\"");
41     else output("0x%x");
42     output(";\\n\", ");
43     output(field.getName());
44     outputln(");");
45 }
46
47 public static void main(String JavaDoc[] args) {
48     if (args.length < 1) {
49         System.out.println("Usage: java ConstantsGenerator <className1> <className2>");
50         return;
51     }
52     try {
53         ConstantsGenerator gen = new ConstantsGenerator();
54         for (int i = 0; i < args.length; i++) {
55             String JavaDoc clazzName = args[i];
56             Class JavaDoc clazz = Class.forName(clazzName);
57             gen.generate(clazz);
58         }
59     } catch (Exception JavaDoc e) {
60         System.out.println("Problem");
61         e.printStackTrace(System.out);
62     }
63 }
64
65 }
66
Popular Tags