1
/*******************************************************************************
2
* Copyright (c) 2009 Progress Software, Inc.
3
* Copyright (c) 2004, 2006 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.io.BufferedReader;
14
import java.io.FileReader;
15
import java.io.IOException;
16
import java.io.PrintStream;
17
import java.lang.reflect.Modifier;
18
import java.util.ArrayList;
19
import java.util.Collections;
20
import java.util.Comparator;
21
import java.util.List;
23
import org.fusesource.hawtjni.generator.model.JNIClass;
24
import org.fusesource.hawtjni.generator.model.JNIField;
25
import org.fusesource.hawtjni.generator.model.JNIMethod;
26
import org.fusesource.hawtjni.generator.model.JNIType;
27
import org.fusesource.hawtjni.runtime.ClassFlag;
31
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
33
public abstract class JNIGenerator {
35
static final String delimiter = System.getProperty("line.separator");
36
static final String JNI64 = "JNI64";
38
ArrayList<JNIClass> classes;
39
String copyright = "";
41
PrintStream output = System.out;
42
ProgressMonitor progress;
43
private String outputName;
45
static String fixDelimiter(String str) {
46
if (delimiter.equals("\n")) {
49
return str.replaceAll("\n", delimiter);
52
static String getFunctionName(JNIMethod method) {
53
return getFunctionName(method, method.getParameterTypes());
56
static String getFunctionName(JNIMethod method, List<JNIType> paramTypes) {
57
if ((method.getModifiers() & Modifier.NATIVE) == 0)
58
return method.getName();
59
String function = toC(method.getName());
60
if (!method.isNativeUnique()) {
61
StringBuffer buffer = new StringBuffer();
62
buffer.append(function);
64
for (JNIType paramType : paramTypes) {
65
buffer.append(toC(paramType.getTypeSignature(false)));
67
return buffer.toString();
72
static String loadFile(String file) {
74
FileReader fr = new FileReader(file);
75
BufferedReader br = new BufferedReader(fr);
76
StringBuffer str = new StringBuffer();
77
char[] buffer = new char[1024];
79
while ((read = br.read(buffer)) != -1) {
80
str.append(buffer, 0, read);
83
return str.toString();
84
} catch (IOException e) {
85
throw new RuntimeException("File not found:" + file, e);
89
public static void sortMethods(List<JNIMethod> methods) {
90
Collections.sort(methods, new Comparator<JNIMethod>() {
91
public int compare(JNIMethod mth1, JNIMethod mth2) {
92
int result = mth1.getName().compareTo(mth2.getName());
93
return result != 0 ? result : getFunctionName(mth1).compareTo(getFunctionName(mth2));
98
static void sortFields(List<JNIField> fields) {
99
Collections.sort(fields, new Comparator<JNIField>() {
100
public int compare(JNIField a, JNIField b) {
101
return a.getName().compareTo(b.getName());
106
static void sortClasses(ArrayList<JNIClass> classes) {
107
Collections.sort(classes, new Comparator<JNIClass>() {
108
public int compare(JNIClass a, JNIClass b) {
109
return a.getName().compareTo(b.getName());
114
static String toC(String str) {
115
int length = str.length();
116
StringBuffer buffer = new StringBuffer(length * 2);
117
for (int i = 0; i < length; i++) {
118
char c = str.charAt(i);
137
('a' <= c && c <= 'z')
138
|| ('A' <= c && c <= 'Z')
139
|| ('0' <= c && c <= '9')
143
buffer.append(String.format("_0%04x",(int)c));
147
return buffer.toString();
150
public abstract void generate(JNIClass clazz);
152
public void generateCopyright() {
155
public void generateIncludes() {
158
public void generate() {
163
sortClasses(classes);
164
for (JNIClass clazz : classes) {
165
if (clazz.getFlag(ClassFlag.CPP)) {
174
protected void generate(ArrayList<JNIClass> classes) {
175
for (JNIClass clazz : classes) {
176
if (clazz.getGenerate())
178
if (progress != null)
183
public boolean getCPP() {
187
public String getDelimiter() {
191
public PrintStream getOutput() {
195
public String getOutputName() {
199
public void setOutputName(String outputName) {
200
this.outputName = outputName;
203
public ProgressMonitor getProgressMonitor() {
207
public void output(String str) {
211
public void outputln() {
212
output(getDelimiter());
215
public void outputln(String str) {
217
output(getDelimiter());
220
public void setClasses(ArrayList<JNIClass> classes) {
221
this.classes = classes;
224
public void setOutput(PrintStream output) {
225
this.output = output;
228
public void setProgressMonitor(ProgressMonitor progress) {
229
this.progress = progress;
231
public String getCopyright() {
235
public void setCopyright(String copyright) {
236
this.copyright = copyright;