KickJava   Java API By Example, From Geeks To Geeks.

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


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