1
/*******************************************************************************
2
* Copyright (c) 2009 Progress Software, Inc.
3
* Copyright (c) 2004, 2007 IBM Corporation and others.
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
10
*******************************************************************************/
11
package org.fusesource.hawtjni.generator;
13
import java.lang.reflect.Modifier;
14
import java.util.List;
15
import java.util.regex.Matcher;
16
import java.util.regex.Pattern;
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;
26
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
28
public class LockGenerator extends CleanupClass {
30
public LockGenerator() {
33
String getParams(JNIMethod method) {
34
int n_args = method.getParameters().size();
37
String name = method.getName();
41
index = classSource.indexOf(name, index + 1);
42
if (!Character.isWhitespace(classSource.charAt(index - 1)))
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);
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);
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*/";
71
return new ReflectType(Integer.TYPE).getTypeSignature3(false);
74
public void generate(JNIClass clazz) {
75
super.generate(clazz);
76
generate(clazz.getDeclaredMethods());
79
public void generate(List<JNIMethod> methods) {
81
for (JNIMethod method : methods) {
82
if ((method.getModifiers() & Modifier.NATIVE) == 0)
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);
94
String modifiersStr = Modifier.toString(modifiers & ~Modifier.SYNCHRONIZED);
96
if (modifiersStr.length() > 0)
100
output(method.getName());
105
String modifiersStr = Modifier.toString(modifiers & ~(Modifier.SYNCHRONIZED | (lock ? Modifier.NATIVE : 0)));
106
output(modifiersStr);
107
if (modifiersStr.length() > 0)
111
output(method.getName());
117
outputln("\tlock.lock();");
120
if (!method.getReturnType32().isType("void")) {
124
output(method.getName());
126
String[] paramNames = getArgNames(method);
127
for (int i = 0; i < paramNames.length; i++) {
130
output(paramNames[i]);
133
outputln("\t} finally {");
134
outputln("\t\tlock.unlock();");
142
public static void main(String[] args) {
143
if (args.length < 2) {
144
System.out.println("Usage: java LockGenerator <OS className> <OS class source>");
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);