001    /*******************************************************************************
002     * Copyright (c) 2009 Progress Software, Inc.
003     * Copyright (c) 2004 IBM Corporation and others.
004     *
005     * All rights reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     *
010     *******************************************************************************/
011    package org.fusesource.hawtjni.generator;
012    
013    import java.lang.reflect.Modifier;
014    import java.util.List;
015    
016    import org.fusesource.hawtjni.generator.model.JNIClass;
017    import org.fusesource.hawtjni.generator.model.JNIField;
018    import org.fusesource.hawtjni.generator.model.ReflectClass;
019    
020    /**
021     * 
022     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
023     */
024    public class SizeofGenerator extends JNIGenerator {
025    
026        public void generate(JNIClass clazz) {
027            String className = clazz.getSimpleName();
028            output("\tprintf(\"");
029            output(className);
030            output("=%d\\n\", sizeof(");
031            output(className);
032            outputln("));");
033        }
034    
035        public void generate() {
036            outputln("int main() {");
037            super.generate();
038            outputln("}");
039        }
040    
041        public void generate(List<JNIField> fields) {
042            sortFields(fields);
043            for (JNIField field : fields) {
044                if ((field.getModifiers() & Modifier.FINAL) == 0)
045                    continue;
046                generate(field);
047            }
048        }
049    
050        public void generate(JNIField field) {
051            output("\tprintf(\"");
052            output(field.getName());
053            output("=%d\\n\", sizeof(");
054            output(field.getName());
055            outputln("));");
056        }
057    
058        public static void main(String[] args) {
059            if (args.length < 1) {
060                System.out.println("Usage: java SizeofGenerator <className1> <className2>");
061                return;
062            }
063            try {
064                SizeofGenerator gen = new SizeofGenerator();
065                for (int i = 0; i < args.length; i++) {
066                    String clazzName = args[i];
067                    Class<?> clazz = Class.forName(clazzName);
068                    gen.generate(new ReflectClass(clazz));
069                }
070            } catch (Exception e) {
071                System.out.println("Problem");
072                e.printStackTrace(System.out);
073            }
074        }
075    
076    }