1 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 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 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 .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 [] 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 clazzName = args[i]; 56 Class clazz = Class.forName(clazzName); 57 gen.generate(clazz); 58 } 59 } catch (Exception e) { 60 System.out.println("Problem"); 61 e.printStackTrace(System.out); 62 } 63 } 64 65 } 66 | Popular Tags |