Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 385371f676892b58be53c3fce53701447b7caf9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*******************************************************************************
 * Copyright (c) 2004, 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.tools.internal;

import java.lang.reflect.*;

public class ConstantsGenerator extends JNIGenerator {

@Override
public void generate(JNIClass clazz) {
	JNIField[] fields = clazz.getDeclaredFields();
	generate(fields);
}

public void generate(JNIField[] fields) {
	sort(fields);
	outputln("int main() {");
	for (JNIField field : fields) {
		if ((field.getModifiers() & Modifier.FINAL) == 0) continue;
		generate(field);
	}
	outputln("}");
}

public void generate(JNIField field) {
	JNIType type = field.getType();
	output("\tprintf(\"public static final ");
	output(field.getType().getTypeSignature3(false));
	output(" ");
	output(field.getName());
	output(" = ");
	if (type.isType("java.lang.String") || type.isType("[B")) output("\"%s\"");
	else output("0x%x");
	output(";\\n\", ");
	output(field.getName());
	outputln(");");
}

public static void main(String[] args) {
	if (args.length < 1) {
		System.out.println("Usage: java ConstantsGenerator <className1> <className2>");
		return;
	}
	try {
		ConstantsGenerator gen = new ConstantsGenerator();
		for (String clazzName : args) {
			Class<?> clazz = Class.forName(clazzName);
			gen.generate(new ReflectClass(clazz));
		}
	} catch (Exception e) {
		System.out.println("Problem");
		e.printStackTrace(System.out);
	}
}

}

Back to the top