~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/LockGenerator.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.List;
 
15
import java.util.regex.Matcher;
 
16
import java.util.regex.Pattern;
 
17
 
 
18
import org.fusesource.hawtjni.generator.model.JNIClass;
 
19
import org.fusesource.hawtjni.generator.model.JNIMethod;
 
20
import org.fusesource.hawtjni.generator.model.JNIType;
 
21
import org.fusesource.hawtjni.generator.model.ReflectClass;
 
22
import org.fusesource.hawtjni.generator.model.ReflectType;
 
23
 
 
24
/**
 
25
 * 
 
26
 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
 
27
 */
 
28
public class LockGenerator extends CleanupClass {
 
29
 
 
30
    public LockGenerator() {
 
31
    }
 
32
 
 
33
    String getParams(JNIMethod method) {
 
34
        int n_args = method.getParameters().size();
 
35
        if (n_args == 0)
 
36
            return "";
 
37
        String name = method.getName();
 
38
        String params = "";
 
39
        int index = 0;
 
40
        while (true) {
 
41
            index = classSource.indexOf(name, index + 1);
 
42
            if (!Character.isWhitespace(classSource.charAt(index - 1)))
 
43
                continue;
 
44
            if (index == -1)
 
45
                return null;
 
46
            int parantesesStart = classSource.indexOf("(", index);
 
47
            if (classSource.substring(index + name.length(), parantesesStart).trim().length() == 0) {
 
48
                int parantesesEnd = classSource.indexOf(")", parantesesStart);
 
49
                params = classSource.substring(parantesesStart + 1, parantesesEnd);
 
50
                break;
 
51
            }
 
52
        }
 
53
        return params;
 
54
    }
 
55
 
 
56
    String getReturn(JNIMethod method) {
 
57
        JNIType returnType = method.getReturnType32();
 
58
        if (!returnType.isType("int"))
 
59
            return returnType.getTypeSignature3(false);
 
60
        String modifierStr = Modifier.toString(method.getModifiers());
 
61
        String name = method.getName();
 
62
        Pattern p = Pattern.compile(modifierStr + ".*" + name + ".*(.*)");
 
63
        Matcher m = p.matcher(classSource);
 
64
        if (m.find()) {
 
65
            String methodStr = classSource.substring(m.start(), m.end());
 
66
            int index = methodStr.indexOf("/*long*/");
 
67
            if (index != -1 && index < methodStr.indexOf(name)) {
 
68
                return new ReflectType(Integer.TYPE).getTypeSignature3(false) + " /*long*/";
 
69
            }
 
70
        }
 
71
        return new ReflectType(Integer.TYPE).getTypeSignature3(false);
 
72
    }
 
73
 
 
74
    public void generate(JNIClass clazz) {
 
75
        super.generate(clazz);
 
76
        generate(clazz.getDeclaredMethods());
 
77
    }
 
78
 
 
79
    public void generate(List<JNIMethod> methods) {
 
80
        sortMethods(methods);
 
81
        for (JNIMethod method : methods) {
 
82
            if ((method.getModifiers() & Modifier.NATIVE) == 0)
 
83
                continue;
 
84
            generate(method);
 
85
        }
 
86
    }
 
87
 
 
88
    public void generate(JNIMethod method) {
 
89
        int modifiers = method.getModifiers();
 
90
        boolean lock = (modifiers & Modifier.SYNCHRONIZED) != 0;
 
91
        String returnStr = getReturn(method);
 
92
        String paramsStr = getParams(method);
 
93
        if (lock) {
 
94
            String modifiersStr = Modifier.toString(modifiers & ~Modifier.SYNCHRONIZED);
 
95
            output(modifiersStr);
 
96
            if (modifiersStr.length() > 0)
 
97
                output(" ");
 
98
            output(returnStr);
 
99
            output(" _");
 
100
            output(method.getName());
 
101
            output("(");
 
102
            output(paramsStr);
 
103
            outputln(");");
 
104
        }
 
105
        String modifiersStr = Modifier.toString(modifiers & ~(Modifier.SYNCHRONIZED | (lock ? Modifier.NATIVE : 0)));
 
106
        output(modifiersStr);
 
107
        if (modifiersStr.length() > 0)
 
108
            output(" ");
 
109
        output(returnStr);
 
110
        output(" ");
 
111
        output(method.getName());
 
112
        output("(");
 
113
        output(paramsStr);
 
114
        output(")");
 
115
        if (lock) {
 
116
            outputln(" {");
 
117
            outputln("\tlock.lock();");
 
118
            outputln("\ttry {");
 
119
            output("\t\t");
 
120
            if (!method.getReturnType32().isType("void")) {
 
121
                output("return ");
 
122
            }
 
123
            output("_");
 
124
            output(method.getName());
 
125
            output("(");
 
126
            String[] paramNames = getArgNames(method);
 
127
            for (int i = 0; i < paramNames.length; i++) {
 
128
                if (i != 0)
 
129
                    output(", ");
 
130
                output(paramNames[i]);
 
131
            }
 
132
            outputln(");");
 
133
            outputln("\t} finally {");
 
134
            outputln("\t\tlock.unlock();");
 
135
            outputln("\t}");
 
136
            outputln("}");
 
137
        } else {
 
138
            outputln(";");
 
139
        }
 
140
    }
 
141
 
 
142
    public static void main(String[] args) {
 
143
        if (args.length < 2) {
 
144
            System.out.println("Usage: java LockGenerator <OS className> <OS class source>");
 
145
            return;
 
146
        }
 
147
        try {
 
148
            LockGenerator gen = new LockGenerator();
 
149
            String clazzName = args[0];
 
150
            String classSource = args[1];
 
151
            Class<?> clazz = Class.forName(clazzName);
 
152
            gen.setClassSourcePath(classSource);
 
153
            gen.generate(new ReflectClass(clazz));
 
154
        } catch (Exception e) {
 
155
            System.out.println("Problem");
 
156
            e.printStackTrace(System.out);
 
157
        }
 
158
    }
 
159
 
 
160
}