~ubuntu-branches/ubuntu/quantal/hawtjni/quantal

« back to all changes in this revision

Viewing changes to hawtjni-generator/src/main/java/org/fusesource/hawtjni/generator/SizeofGenerator.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-08-05 19:40:25 UTC
  • Revision ID: james.westby@ubuntu.com-20100805194025-3004hn889accwu2i
Tags: upstream-1.0~+git0c502e20c4
ImportĀ upstreamĀ versionĀ 1.0~+git0c502e20c4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2009 Progress Software, Inc.
 
3
 * Copyright (c) 2004 IBM Corporation and others.
 
4
 *
 
5
 * All rights reserved. This program and the accompanying materials
 
6
 * are made available under the terms of the Eclipse Public License v1.0
 
7
 * which accompanies this distribution, and is available at
 
8
 * http://www.eclipse.org/legal/epl-v10.html
 
9
 *
 
10
 *******************************************************************************/
 
11
package org.fusesource.hawtjni.generator;
 
12
 
 
13
import java.lang.reflect.Modifier;
 
14
import java.util.List;
 
15
 
 
16
import org.fusesource.hawtjni.generator.model.JNIClass;
 
17
import org.fusesource.hawtjni.generator.model.JNIField;
 
18
import org.fusesource.hawtjni.generator.model.ReflectClass;
 
19
 
 
20
/**
 
21
 * 
 
22
 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
 
23
 */
 
24
public class SizeofGenerator extends JNIGenerator {
 
25
 
 
26
    public void generate(JNIClass clazz) {
 
27
        String className = clazz.getSimpleName();
 
28
        output("\tprintf(\"");
 
29
        output(className);
 
30
        output("=%d\\n\", sizeof(");
 
31
        output(className);
 
32
        outputln("));");
 
33
    }
 
34
 
 
35
    public void generate() {
 
36
        outputln("int main() {");
 
37
        super.generate();
 
38
        outputln("}");
 
39
    }
 
40
 
 
41
    public void generate(List<JNIField> fields) {
 
42
        sortFields(fields);
 
43
        for (JNIField field : fields) {
 
44
            if ((field.getModifiers() & Modifier.FINAL) == 0)
 
45
                continue;
 
46
            generate(field);
 
47
        }
 
48
    }
 
49
 
 
50
    public void generate(JNIField field) {
 
51
        output("\tprintf(\"");
 
52
        output(field.getName());
 
53
        output("=%d\\n\", sizeof(");
 
54
        output(field.getName());
 
55
        outputln("));");
 
56
    }
 
57
 
 
58
    public static void main(String[] args) {
 
59
        if (args.length < 1) {
 
60
            System.out.println("Usage: java SizeofGenerator <className1> <className2>");
 
61
            return;
 
62
        }
 
63
        try {
 
64
            SizeofGenerator gen = new SizeofGenerator();
 
65
            for (int i = 0; i < args.length; i++) {
 
66
                String clazzName = args[i];
 
67
                Class<?> clazz = Class.forName(clazzName);
 
68
                gen.generate(new ReflectClass(clazz));
 
69
            }
 
70
        } catch (Exception e) {
 
71
            System.out.println("Problem");
 
72
            e.printStackTrace(System.out);
 
73
        }
 
74
    }
 
75
 
 
76
}