~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/CleanupConstants.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, 2007 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.Collection;
 
15
import java.util.List;
 
16
 
 
17
import org.fusesource.hawtjni.generator.model.JNIClass;
 
18
import org.fusesource.hawtjni.generator.model.JNIField;
 
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 CleanupConstants extends CleanupClass {
 
26
 
 
27
    String getFieldValue(JNIField field) {
 
28
        String name = field.getName();
 
29
        int index = 0;
 
30
        while (true) {
 
31
            index = classSource.indexOf(name, index + 1);
 
32
            if (index == -1)
 
33
                return null;
 
34
            int equalsIndex = classSource.indexOf("=", index);
 
35
            if (classSource.substring(index + name.length(), equalsIndex).trim().length() == 0) {
 
36
                int semiIndex = classSource.indexOf(";", equalsIndex);
 
37
                return classSource.substring(equalsIndex + 1, semiIndex).trim();
 
38
            }
 
39
        }
 
40
    }
 
41
 
 
42
    public void generate(JNIClass clazz) {
 
43
        unusedCount = usedCount = 0;
 
44
        super.generate(clazz);
 
45
        List<JNIField> fields = clazz.getDeclaredFields();
 
46
        generate(fields);
 
47
        output("used=" + usedCount + " unused=" + unusedCount + " total=" + (unusedCount + usedCount));
 
48
    }
 
49
 
 
50
    public void generate(List<JNIField> fields) {
 
51
        sortFields(fields);
 
52
        for (JNIField field : fields) {
 
53
            if ((field.getModifiers() & Modifier.FINAL) == 0)
 
54
                continue;
 
55
            generate(field);
 
56
        }
 
57
    }
 
58
 
 
59
    public void generate(JNIField field) {
 
60
        String name = field.getName();
 
61
        Collection<String> values = files.values();
 
62
        for (String str : values) {
 
63
            if (str.indexOf(name) != -1) {
 
64
                int modifiers = field.getModifiers();
 
65
                String modifiersStr = Modifier.toString(modifiers);
 
66
                output("\t");
 
67
                output(modifiersStr);
 
68
                if (modifiersStr.length() > 0)
 
69
                    output(" ");
 
70
                output(field.getType().getTypeSignature3(false));
 
71
                output(" ");
 
72
                output(field.getName());
 
73
                output(" = ");
 
74
                output(getFieldValue(field));
 
75
                outputln(";");
 
76
                usedCount++;
 
77
                return;
 
78
            }
 
79
        }
 
80
        unusedCount++;
 
81
        // output("NOT USED=" + field.toString() + " \n");
 
82
    }
 
83
 
 
84
    public static void main(String[] args) {
 
85
        if (args.length < 3) {
 
86
            System.out.println("Usage: java CleanupConstants <OS className> <class source> <src path1> <src path2>");
 
87
            return;
 
88
        }
 
89
        try {
 
90
            CleanupConstants gen = new CleanupConstants();
 
91
            String clazzName = args[0];
 
92
            String classSource = args[1];
 
93
            String[] sourcePath = new String[args.length - 2];
 
94
            System.arraycopy(args, 2, sourcePath, 0, sourcePath.length);
 
95
            Class<?> clazz = Class.forName(clazzName);
 
96
            gen.setSourcePath(sourcePath);
 
97
            gen.setClassSourcePath(classSource);
 
98
            gen.generate(new ReflectClass(clazz));
 
99
        } catch (Exception e) {
 
100
            System.out.println("Problem");
 
101
            e.printStackTrace(System.out);
 
102
        }
 
103
    }
 
104
 
 
105
}