Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 83557aabbebbf64c54e779a8931bc0371f76ed4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*******************************************************************************
 * Copyright (c) 2004, 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.tools.internal;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;

public abstract class JNIGenerator implements Flags {

	JNIClass mainClass;
	JNIClass[] classes;
	MetaData metaData;
	String delimiter;
	PrintStream output;
	ProgressMonitor progress;
	
	static final String JNI64 = "JNI64";

public JNIGenerator() {
	delimiter = System.getProperty("line.separator");
	output = System.out;
	metaData = new MetaData(new Properties());
}

public static String skipCopyrights(InputStream is) throws IOException {
	int state = 0;
	StringBuilder copyrights = new StringBuilder();
	while (state != 5) {
		int c = is.read();
		if (c == -1) return null;
		switch (state) {
			case 0:
				if (!Character.isWhitespace((char)c)) state = 1;
			case 1:
				if (c == '/') state = 2;
				else return null;
				break;
			case 2:
				if (c == '*') state = 3;
				else return null;
				break;
			case 3:
				if (c == '*') state = 4;
				break;
			case 4:
				if (c == '/') state = 5;
				else state = 3;
				break;
		}
		if (state > 0) copyrights.append((char)c);
	}
	return copyrights.toString();
}

public static boolean compare(InputStream is1, InputStream is2) throws IOException {
	skipCopyrights(is1);
	skipCopyrights(is2);
	while (true) {
		int c1 = is1.read();
		int c2 = is2.read();
		if (c1 != c2) return false;
		if (c1 == -1) break;
	}
	return true;
}

public static void output(byte[] bytes, String fileName) throws IOException {
	try (FileInputStream is = new FileInputStream(fileName)){
		if (compare(new ByteArrayInputStream(bytes), new BufferedInputStream(is))) return;
	} catch (FileNotFoundException e) {
	}
	try (FileOutputStream out = new FileOutputStream(fileName)) {
		out.write(bytes);
	}
}

public static String getDelimiter(String fileName) {
	
	try (InputStream is = new BufferedInputStream(new FileInputStream(fileName))){
		int c;
		while ((c = is.read()) != -1) {
			if (c == '\n') return "\n";
			if (c == '\r') {
				int c1 = is.read();
				if (c1 == '\n') {
					return "\r\n";
				}
				return "\r";
			}
		}
	} catch (IOException e) {
	}
	return System.getProperty("line.separator");
}

String fixDelimiter(String str) {
	if (delimiter.equals("\n")) return str;
	int index = 0, length = str.length();
	StringBuilder buffer = new StringBuilder();
	while (index != -1) {
		int start = index;
		index = str.indexOf('\n', start);
		if (index == -1) {
			buffer.append(str.substring(start, length));
		} else {
			buffer.append(str.substring(start, index));
			buffer.append(delimiter);
			index++;
		}
	}
	return buffer.toString();
}

static String getFunctionName(JNIMethod method) {
	return getFunctionName(method, method.getParameterTypes());
}

static String getFunctionName(JNIMethod method, JNIType[] paramTypes) {
	if ((method.getModifiers() & Modifier.NATIVE) == 0) return method.getName();
	String function = toC(method.getName());
	if (!method.isNativeUnique()) {
		StringBuilder buffer = new StringBuilder();
		buffer.append(function);
		buffer.append("__");
		for (JNIType paramType : paramTypes) {
			buffer.append(toC(paramType.getTypeSignature(false)));
		}
		return buffer.toString();
	}
	return function;
}

static String loadFile (String file) {
	try (FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr)){
		StringBuilder str = new StringBuilder();
		char[] buffer = new char[1024];
		int read;
		while ((read = br.read(buffer)) != -1) {
			str.append(buffer, 0, read);
		}
		fr.close();
		return str.toString();
	} catch (IOException e) {
		throw new RuntimeException("File not found:" + file, e);
	}
}

static void sort(JNIMethod[] methods) {
	Arrays.sort(methods, (mth1, mth2) -> {
		int result = mth1.getName().compareTo(mth2.getName());
		return result != 0 ? result : getFunctionName(mth1).compareTo(getFunctionName(mth2));
	});
}

static void sort(JNIField[] fields) {
	Arrays.sort(fields, (a, b) -> a.getName().compareTo(b.getName()));
}

static void sort(JNIClass[] classes) {
	Arrays.sort(classes, (a, b) -> a.getName().compareTo(b.getName()));	
}

static String[] split(String str, String separator) {
	StringTokenizer tk = new StringTokenizer(str, separator);
	List<String> result = new ArrayList<>();
	while (tk.hasMoreTokens()) {
		result.add(tk.nextToken());
	}
	return result.toArray(new String[result.size()]);
}

static String toC(String str) {
	int length = str.length();
	StringBuilder buffer = new StringBuilder(length * 2);
	for (int i = 0; i < length; i++) {
		char c = str.charAt(i);
		switch (c) {
			case '_': buffer.append("_1"); break;
			case ';': buffer.append("_2"); break;
			case '[': buffer.append("_3"); break;
			case '.': buffer.append("_"); break;
			case '/': buffer.append("_"); break;
			default: buffer.append(c);
		}
	}
	return buffer.toString();
}

public abstract void generate(JNIClass clazz);

public void generateCopyright() {
}

public void generateAutoGenNote() {
	outputln("/* Note: This file was auto-generated by " + JNIGenerator.class.getName() + " */");
	outputln("/* DO NOT EDIT - your changes will be lost. */");
	outputln();
}

public void generateIncludes() {
}

public void generate() {
	if (classes == null) return;
	generateCopyright();
	generateAutoGenNote();
	generateIncludes();
	sort(classes);
	for (JNIClass clazz : classes) {
		if (getGenerate(clazz)) generate(clazz);
		if (progress != null) progress.step();
	}
	output.flush();
}

public void generateMetaData(String key) {
	MetaData mt = getMetaData();
	String data = mt.getMetaData(key, null);
	if (data == null) return;
	if (data.length() == 0) return;
	outputln(fixDelimiter(data));
}

public JNIClass[] getClasses() {
	return classes;
}

public boolean getCPP() {
	for (JNIClass clazz : classes) {
		if (clazz.getFlag(FLAG_CPP)) {
			return true;
		}
	}
	return false;
}

public String getDelimiter() {
	return delimiter;
}

public String getExtension() {
	return getCPP() ? ".cpp" : getM() ? ".m" : ".c";
}

public String getFileName() {
	return getOutputName() + getSuffix() + getExtension();
}

protected boolean getGenerate(JNIItem item) {
	return item.getGenerate();
}

public PrintStream getOutput() {
	return output;
}

public String getOutputName() {
	return getMainClass().getSimpleName().toLowerCase();
}

public boolean getM() {
	for (JNIClass clazz : classes) {
		if (clazz.getFlag(FLAG_M)) {
			return true;
		}
	}
	return false;
}

public JNIClass getMainClass() {
	return mainClass;
}

public MetaData getMetaData() {
	return metaData;
}

public ProgressMonitor getProgressMonitor() {
	return progress;
}

public String getSuffix() {
	return "";
}

public void output(String str) {
	output.print(str);
}

public void outputln() {
	output(getDelimiter());
}

public void outputln(String str) {
	output(str);
	output(getDelimiter());
}

public void setClasses(JNIClass[] classes) {
	this.classes = classes;
}

public void setDelimiter(String delimiter) {
	this.delimiter = delimiter;
}

public void setMainClass(JNIClass mainClass) {
	this.mainClass = mainClass;
}

public void setMetaData(MetaData data) {
	metaData = data;
}

public void setOutput(PrintStream output) {
	this.output = output;
}

public void setProgressMonitor(ProgressMonitor progress) {
	this.progress = progress;
}

}

Back to the top