Skip to content

Commit 0d068e4

Browse files
author
FITOR
committed
[UNFINISHED] prepared dynamic exceptionMapping
1 parent 22fc1e4 commit 0d068e4

File tree

3 files changed

+192
-120
lines changed

3 files changed

+192
-120
lines changed

src/main/java/org/bytedeco/javacpp/ClassProperties.java

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,12 @@
2222

2323
package org.bytedeco.javacpp;
2424

25-
import java.io.File;
26-
import java.util.ArrayList;
27-
import java.util.Arrays;
28-
import java.util.Collection;
29-
import java.util.HashMap;
30-
import java.util.List;
31-
import java.util.Map;
32-
import java.util.Properties;
3325
import org.bytedeco.javacpp.annotation.Platform;
3426
import org.bytedeco.javacpp.tools.Logger;
3527

28+
import java.io.File;
29+
import java.util.*;
30+
3631
/**
3732
* Does the heavy lifting of collecting values off Properties annotations found
3833
* on enclosing classes. Operates for the desired "platform" value specified
@@ -41,14 +36,15 @@
4136
*
4237
* @see Loader#loadProperties(Class, java.util.Properties, boolean)
4338
*/
44-
public class ClassProperties extends HashMap<String,List<String>> {
39+
public class ClassProperties extends HashMap<String, List<String>> {
4540
private static final Logger logger = Logger.create(ClassProperties.class);
4641

4742
public ClassProperties() { }
43+
4844
public ClassProperties(Properties properties) {
49-
platform = properties.getProperty("platform");
45+
platform = properties.getProperty("platform");
5046
platformExtension = properties.getProperty("platform.extension");
51-
platformRoot = properties.getProperty("platform.root");
47+
platformRoot = properties.getProperty("platform.root");
5248
pathSeparator = properties.getProperty("platform.path.separator");
5349
if (platformRoot == null || platformRoot.length() == 0) {
5450
platformRoot = ".";
@@ -57,17 +53,11 @@ public ClassProperties(Properties properties) {
5753
platformRoot += File.separator;
5854
}
5955
for (Map.Entry e : properties.entrySet()) {
60-
String k = (String)e.getKey(), v = (String)e.getValue();
56+
String k = (String) e.getKey(), v = (String) e.getValue();
6157
if (v == null || v.length() == 0) {
6258
continue;
6359
}
64-
if (k.equals("platform.includepath") || k.equals("platform.includeresource") || k.equals("platform.include")
65-
|| k.equals("platform.linkpath") || k.equals("platform.linkresource") || k.equals("platform.link")
66-
|| k.equals("platform.preloadpath") || k.equals("platform.preloadresource") || k.equals("platform.preload")
67-
|| k.equals("platform.resourcepath") || k.equals("platform.resource")
68-
|| k.equals("platform.frameworkpath") || k.equals("platform.framework")
69-
|| k.equals("platform.executablepath") || k.equals("platform.executable")
70-
|| k.equals("platform.compiler.*") || k.equals("platform.library.suffix") || k.equals("platform.extension")) {
60+
if (k.equals("platform.includepath") || k.equals("platform.includeresource") || k.equals("platform.include") || k.equals("platform.linkpath") || k.equals("platform.linkresource") || k.equals("platform.link") || k.equals("platform.preloadpath") || k.equals("platform.preloadresource") || k.equals("platform.preload") || k.equals("platform.resourcepath") || k.equals("platform.resource") || k.equals("platform.frameworkpath") || k.equals("platform.framework") || k.equals("platform.executablepath") || k.equals("platform.executable") || k.equals("platform.compiler.*") || k.equals("platform.library.suffix") || k.equals("platform.extension")) {
7161
addAll(k, v.split(pathSeparator));
7262
} else {
7363
setProperty(k, v);
@@ -84,21 +74,21 @@ public ClassProperties(Properties properties) {
8474
public List<String> get(String key) {
8575
List<String> list = super.get(key);
8676
if (list == null) {
87-
put((String)key, list = new ArrayList<String>());
77+
put((String) key, list = new ArrayList<String>());
8878
}
8979
return list;
9080
}
9181

92-
public void addAll(String key, String ... values) {
82+
public void addAll(String key, String... values) {
9383
if (values != null) {
9484
addAll(key, Arrays.asList(values));
9585
}
9686
}
87+
9788
public void addAll(String key, Collection<String> values) {
9889
if (values != null) {
9990
String root = null;
100-
if (key.equals("platform.compiler") || key.equals("platform.sysroot") || key.equals("platform.toolchain") ||
101-
key.equals("platform.includepath") || key.equals("platform.linkpath")) {
91+
if (key.equals("platform.compiler") || key.equals("platform.sysroot") || key.equals("platform.toolchain") || key.equals("platform.includepath") || key.equals("platform.linkpath")) {
10292
root = platformRoot;
10393
}
10494

@@ -107,8 +97,7 @@ public void addAll(String key, Collection<String> values) {
10797
if (value == null) {
10898
continue;
10999
}
110-
if (root != null && !new File(value).isAbsolute() &&
111-
new File(root + value).exists()) {
100+
if (root != null && !new File(value).isAbsolute() && new File(root + value).exists()) {
112101
value = root + value;
113102
}
114103
if (!values2.contains(value)) {
@@ -121,10 +110,12 @@ public void addAll(String key, Collection<String> values) {
121110
public String getProperty(String key) {
122111
return getProperty(key, null);
123112
}
113+
124114
public String getProperty(String key, String defaultValue) {
125115
List<String> values = get(key);
126116
return values.isEmpty() ? defaultValue : values.get(0);
127117
}
118+
128119
public String setProperty(String key, String value) {
129120
List<String> values = get(key);
130121
String oldValue = values.isEmpty() ? null : values.get(0);
@@ -137,17 +128,14 @@ public void load(Class cls, boolean inherit) {
137128
Class<?> c = Loader.getEnclosingClass(cls);
138129
List<Class> classList = new ArrayList<Class>();
139130
classList.add(0, c);
140-
while (!c.isAnnotationPresent(org.bytedeco.javacpp.annotation.Properties.class)
141-
&& !c.isAnnotationPresent(Platform.class) && c.getSuperclass() != null
142-
&& c.getSuperclass() != Object.class && c.getSuperclass() != Pointer.class) {
131+
while (!c.isAnnotationPresent(org.bytedeco.javacpp.annotation.Properties.class) && !c.isAnnotationPresent(Platform.class) && c.getSuperclass() != null && c.getSuperclass() != Object.class && c.getSuperclass() != Pointer.class) {
143132
// accumulate superclasses to process native methods from those as well
144133
classList.add(0, c = c.getSuperclass());
145134
}
146135
if (effectiveClasses == null) {
147136
effectiveClasses = classList;
148137
}
149-
org.bytedeco.javacpp.annotation.Properties classProperties =
150-
c.getAnnotation(org.bytedeco.javacpp.annotation.Properties.class);
138+
org.bytedeco.javacpp.annotation.Properties classProperties = c.getAnnotation(org.bytedeco.javacpp.annotation.Properties.class);
151139
Platform classPlatform = c.getAnnotation(Platform.class);
152140
Platform[] platforms = null;
153141
String ourTarget = null;
@@ -195,17 +183,15 @@ public void load(Class cls, boolean inherit) {
195183
}
196184
if (classPlatform != null) {
197185
if (platforms == null) {
198-
platforms = new Platform[] { classPlatform };
186+
platforms = new Platform[]{classPlatform};
199187
} else {
200188
platforms = Arrays.copyOf(platforms, platforms.length + 1);
201189
platforms[platforms.length - 1] = classPlatform;
202190
}
203191
}
204192
boolean hasPlatformProperties = platforms != null && platforms.length > (classProperties != null && classPlatform != null ? 1 : 0);
205193

206-
String[] pragma = {}, define = {}, exclude = {}, include = {}, cinclude = {}, includepath = {}, includeresource = {}, compiler = {},
207-
linkpath = {}, linkresource = {}, link = {}, frameworkpath = {}, framework = {}, preloadpath = {}, preloadresource = {}, preload = {},
208-
resourcepath = {}, resource = {}, extension = {}, executablepath = {}, executable = {};
194+
String[] exceptionMappings = {}, pragma = {}, define = {}, exclude = {}, include = {}, cinclude = {}, includepath = {}, includeresource = {}, compiler = {}, linkpath = {}, linkresource = {}, link = {}, frameworkpath = {}, framework = {}, preloadpath = {}, preloadresource = {}, preload = {}, resourcepath = {}, resource = {}, extension = {}, executablepath = {}, executable = {};
209195
String library = "jni" + c.getSimpleName();
210196
if (hasPlatformProperties) {
211197
if (ourTarget != null && ourTarget.length() > 0) {
@@ -219,8 +205,8 @@ public void load(Class cls, boolean inherit) {
219205
}
220206
}
221207
for (Platform p : platforms != null ? platforms : new Platform[0]) {
222-
String[][] names = { p.value().length > 0 ? p.value() : defaultNames, p.not(), p.pattern() };
223-
boolean[] matches = { false, false, false };
208+
String[][] names = {p.value().length > 0 ? p.value() : defaultNames, p.not(), p.pattern()};
209+
boolean[] matches = {false, false, false};
224210
for (int i = 0; i < names.length; i++) {
225211
for (String s : names[i]) {
226212
if ((i < 2 && platform.startsWith(s)) || (s.length() > 0 && platform.matches(s))) {
@@ -241,28 +227,29 @@ public void load(Class cls, boolean inherit) {
241227
if (!match) {
242228
continue;
243229
}
244-
if (p.pragma() .length > 0) { pragma = p.pragma(); }
245-
if (p.define() .length > 0) { define = p.define(); }
246-
if (p.exclude() .length > 0) { exclude = p.exclude(); }
247-
if (p.include() .length > 0) { include = p.include(); }
248-
if (p.cinclude() .length > 0) { cinclude = p.cinclude(); }
230+
if (p.exceptionMappings().length > 0) { exceptionMappings = p.exceptionMappings(); }
231+
if (p.pragma().length > 0) { pragma = p.pragma(); }
232+
if (p.define().length > 0) { define = p.define(); }
233+
if (p.exclude().length > 0) { exclude = p.exclude(); }
234+
if (p.include().length > 0) { include = p.include(); }
235+
if (p.cinclude().length > 0) { cinclude = p.cinclude(); }
249236
if (p.includepath().length > 0) { includepath = p.includepath(); }
250237
if (p.includeresource().length > 0) { includeresource = p.includeresource(); }
251-
if (p.compiler() .length > 0) { compiler = p.compiler(); }
252-
if (p.linkpath() .length > 0) { linkpath = p.linkpath(); }
253-
if (p.linkresource() .length > 0) { linkresource = p.linkresource(); }
254-
if (p.link() .length > 0) { link = p.link(); }
238+
if (p.compiler().length > 0) { compiler = p.compiler(); }
239+
if (p.linkpath().length > 0) { linkpath = p.linkpath(); }
240+
if (p.linkresource().length > 0) { linkresource = p.linkresource(); }
241+
if (p.link().length > 0) { link = p.link(); }
255242
if (p.frameworkpath().length > 0) { frameworkpath = p.frameworkpath(); }
256-
if (p.framework() .length > 0) { framework = p.framework(); }
243+
if (p.framework().length > 0) { framework = p.framework(); }
257244
if (p.preloadresource().length > 0) { preloadresource = p.preloadresource(); }
258245
if (p.preloadpath().length > 0) { preloadpath = p.preloadpath(); }
259-
if (p.preload() .length > 0) { preload = p.preload(); }
246+
if (p.preload().length > 0) { preload = p.preload(); }
260247
if (p.resourcepath().length > 0) { resourcepath = p.resourcepath(); }
261-
if (p.resource() .length > 0) { resource = p.resource(); }
262-
if (p.extension() .length > 0) { extension = p.extension(); }
248+
if (p.resource().length > 0) { resource = p.resource(); }
249+
if (p.extension().length > 0) { extension = p.extension(); }
263250
if (p.executablepath().length > 0) { executablepath = p.executablepath(); }
264-
if (p.executable() .length > 0) { executable = p.executable(); }
265-
if (p.library().length() > 0) { library = p.library(); }
251+
if (p.executable().length > 0) { executable = p.executable(); }
252+
if (p.library().length() > 0) { library = p.library(); }
266253
}
267254
}
268255
for (int i = 0; i < includeresource.length; i++) {
@@ -289,6 +276,8 @@ public void load(Class cls, boolean inherit) {
289276
linkresource[i] = "/" + name;
290277
}
291278
}
279+
280+
addAll("platform.exceptionMappings", exceptionMappings);
292281
addAll("platform.pragma", pragma);
293282
addAll("platform.define", define);
294283
addAll("platform.exclude", exclude);
@@ -319,7 +308,7 @@ public void load(Class cls, boolean inherit) {
319308

320309
if (LoadEnabled.class.isAssignableFrom(c)) {
321310
try {
322-
((LoadEnabled)c.newInstance()).init(this);
311+
((LoadEnabled) c.newInstance()).init(this);
323312
} catch (ClassCastException | InstantiationException | IllegalAccessException e) {
324313
logger.warn("Could not create an instance of " + c + ": " + e);
325314
}

0 commit comments

Comments
 (0)