001    /*******************************************************************************
002     * Copyright (c) 2009 Progress Software, Inc.
003     * Copyright (c) 2004, 2007 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.Collection;
015    import java.util.List;
016    
017    import org.fusesource.hawtjni.generator.model.JNIClass;
018    import org.fusesource.hawtjni.generator.model.JNIField;
019    import org.fusesource.hawtjni.generator.model.ReflectClass;
020    
021    /**
022     * 
023     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
024     */
025    public class CleanupConstants extends CleanupClass {
026    
027        String getFieldValue(JNIField field) {
028            String name = field.getName();
029            int index = 0;
030            while (true) {
031                index = classSource.indexOf(name, index + 1);
032                if (index == -1)
033                    return null;
034                int equalsIndex = classSource.indexOf("=", index);
035                if (classSource.substring(index + name.length(), equalsIndex).trim().length() == 0) {
036                    int semiIndex = classSource.indexOf(";", equalsIndex);
037                    return classSource.substring(equalsIndex + 1, semiIndex).trim();
038                }
039            }
040        }
041    
042        public void generate(JNIClass clazz) {
043            unusedCount = usedCount = 0;
044            super.generate(clazz);
045            List<JNIField> fields = clazz.getDeclaredFields();
046            generate(fields);
047            output("used=" + usedCount + " unused=" + unusedCount + " total=" + (unusedCount + usedCount));
048        }
049    
050        public void generate(List<JNIField> fields) {
051            sortFields(fields);
052            for (JNIField field : fields) {
053                if ((field.getModifiers() & Modifier.FINAL) == 0)
054                    continue;
055                generate(field);
056            }
057        }
058    
059        public void generate(JNIField field) {
060            String name = field.getName();
061            Collection<String> values = files.values();
062            for (String str : values) {
063                if (str.indexOf(name) != -1) {
064                    int modifiers = field.getModifiers();
065                    String modifiersStr = Modifier.toString(modifiers);
066                    output("\t");
067                    output(modifiersStr);
068                    if (modifiersStr.length() > 0)
069                        output(" ");
070                    output(field.getType().getTypeSignature3(false));
071                    output(" ");
072                    output(field.getName());
073                    output(" = ");
074                    output(getFieldValue(field));
075                    outputln(";");
076                    usedCount++;
077                    return;
078                }
079            }
080            unusedCount++;
081            // output("NOT USED=" + field.toString() + " \n");
082        }
083    
084        public static void main(String[] args) {
085            if (args.length < 3) {
086                System.out.println("Usage: java CleanupConstants <OS className> <class source> <src path1> <src path2>");
087                return;
088            }
089            try {
090                CleanupConstants gen = new CleanupConstants();
091                String clazzName = args[0];
092                String classSource = args[1];
093                String[] sourcePath = new String[args.length - 2];
094                System.arraycopy(args, 2, sourcePath, 0, sourcePath.length);
095                Class<?> clazz = Class.forName(clazzName);
096                gen.setSourcePath(sourcePath);
097                gen.setClassSourcePath(classSource);
098                gen.generate(new ReflectClass(clazz));
099            } catch (Exception e) {
100                System.out.println("Problem");
101                e.printStackTrace(System.out);
102            }
103        }
104    
105    }