~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/ConstantsGenerator.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.JNIType;
 
19
import org.fusesource.hawtjni.generator.model.ReflectClass;
 
20
 
 
21
/**
 
22
 * 
 
23
 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
 
24
 */
 
25
public class ConstantsGenerator extends JNIGenerator {
 
26
 
 
27
    public void generate(JNIClass clazz) {
 
28
        List<JNIField> fields = clazz.getDeclaredFields();
 
29
        generate(fields);
 
30
    }
 
31
 
 
32
    public void generate(List<JNIField> fields) {
 
33
        sortFields(fields);
 
34
        outputln("int main() {");
 
35
        for (JNIField field : fields) {
 
36
            if ((field.getModifiers() & Modifier.FINAL) == 0)
 
37
                continue;
 
38
            generate(field);
 
39
        }
 
40
        outputln("}");
 
41
    }
 
42
 
 
43
    public void generate(JNIField field) {
 
44
        JNIType type = field.getType();
 
45
        output("\tprintf(\"public static final ");
 
46
        output(field.getType().getTypeSignature3(false));
 
47
        output(" ");
 
48
        output(field.getName());
 
49
        output(" = ");
 
50
        if (type.isType("java.lang.String") || type.isType("[B"))
 
51
            output("\"%s\"");
 
52
        else
 
53
            output("0x%x");
 
54
        output(";\\n\", ");
 
55
        output(field.getName());
 
56
        outputln(");");
 
57
    }
 
58
 
 
59
    public static void main(String[] args) {
 
60
        if (args.length < 1) {
 
61
            System.out.println("Usage: java ConstantsGenerator <className1> <className2>");
 
62
            return;
 
63
        }
 
64
        try {
 
65
            ConstantsGenerator gen = new ConstantsGenerator();
 
66
            for (String clazzName : args) {
 
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
}