Skip to content

Commit bf11703

Browse files
authored
Merge pull request #1 from Seclous/feature/nvd-exception-handling
Feature/nvd exception handling
2 parents aeaad31 + 8689d4a commit bf11703

File tree

6 files changed

+498
-360
lines changed

6 files changed

+498
-360
lines changed

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

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,13 @@
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;
25+
import org.bytedeco.javacpp.annotation.ExceptionMapper;
3326
import org.bytedeco.javacpp.annotation.Platform;
3427
import org.bytedeco.javacpp.tools.Logger;
3528

29+
import java.io.File;
30+
import java.util.*;
31+
3632
/**
3733
* Does the heavy lifting of collecting values off Properties annotations found
3834
* on enclosing classes. Operates for the desired "platform" value specified
@@ -41,14 +37,15 @@
4137
*
4238
* @see Loader#loadProperties(Class, java.util.Properties, boolean)
4339
*/
44-
public class ClassProperties extends HashMap<String,List<String>> {
40+
public class ClassProperties extends HashMap<String, List<String>> {
4541
private static final Logger logger = Logger.create(ClassProperties.class);
4642

4743
public ClassProperties() { }
44+
4845
public ClassProperties(Properties properties) {
49-
platform = properties.getProperty("platform");
46+
platform = properties.getProperty("platform");
5047
platformExtension = properties.getProperty("platform.extension");
51-
platformRoot = properties.getProperty("platform.root");
48+
platformRoot = properties.getProperty("platform.root");
5249
pathSeparator = properties.getProperty("platform.path.separator");
5350
if (platformRoot == null || platformRoot.length() == 0) {
5451
platformRoot = ".";
@@ -57,17 +54,11 @@ public ClassProperties(Properties properties) {
5754
platformRoot += File.separator;
5855
}
5956
for (Map.Entry e : properties.entrySet()) {
60-
String k = (String)e.getKey(), v = (String)e.getValue();
57+
String k = (String) e.getKey(), v = (String) e.getValue();
6158
if (v == null || v.length() == 0) {
6259
continue;
6360
}
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")) {
61+
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")) {
7162
addAll(k, v.split(pathSeparator));
7263
} else {
7364
setProperty(k, v);
@@ -84,21 +75,21 @@ public ClassProperties(Properties properties) {
8475
public List<String> get(String key) {
8576
List<String> list = super.get(key);
8677
if (list == null) {
87-
put((String)key, list = new ArrayList<String>());
78+
put((String) key, list = new ArrayList<String>());
8879
}
8980
return list;
9081
}
9182

92-
public void addAll(String key, String ... values) {
83+
public void addAll(String key, String... values) {
9384
if (values != null) {
9485
addAll(key, Arrays.asList(values));
9586
}
9687
}
88+
9789
public void addAll(String key, Collection<String> values) {
9890
if (values != null) {
9991
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")) {
92+
if (key.equals("platform.compiler") || key.equals("platform.sysroot") || key.equals("platform.toolchain") || key.equals("platform.includepath") || key.equals("platform.linkpath")) {
10293
root = platformRoot;
10394
}
10495

@@ -107,8 +98,7 @@ public void addAll(String key, Collection<String> values) {
10798
if (value == null) {
10899
continue;
109100
}
110-
if (root != null && !new File(value).isAbsolute() &&
111-
new File(root + value).exists()) {
101+
if (root != null && !new File(value).isAbsolute() && new File(root + value).exists()) {
112102
value = root + value;
113103
}
114104
if (!values2.contains(value)) {
@@ -121,10 +111,12 @@ public void addAll(String key, Collection<String> values) {
121111
public String getProperty(String key) {
122112
return getProperty(key, null);
123113
}
114+
124115
public String getProperty(String key, String defaultValue) {
125116
List<String> values = get(key);
126117
return values.isEmpty() ? defaultValue : values.get(0);
127118
}
119+
128120
public String setProperty(String key, String value) {
129121
List<String> values = get(key);
130122
String oldValue = values.isEmpty() ? null : values.get(0);
@@ -137,17 +129,14 @@ public void load(Class cls, boolean inherit) {
137129
Class<?> c = Loader.getEnclosingClass(cls);
138130
List<Class> classList = new ArrayList<Class>();
139131
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) {
132+
while (!c.isAnnotationPresent(org.bytedeco.javacpp.annotation.Properties.class) && !c.isAnnotationPresent(Platform.class) && c.getSuperclass() != null && c.getSuperclass() != Object.class && c.getSuperclass() != Pointer.class) {
143133
// accumulate superclasses to process native methods from those as well
144134
classList.add(0, c = c.getSuperclass());
145135
}
146136
if (effectiveClasses == null) {
147137
effectiveClasses = classList;
148138
}
149-
org.bytedeco.javacpp.annotation.Properties classProperties =
150-
c.getAnnotation(org.bytedeco.javacpp.annotation.Properties.class);
139+
org.bytedeco.javacpp.annotation.Properties classProperties = c.getAnnotation(org.bytedeco.javacpp.annotation.Properties.class);
151140
Platform classPlatform = c.getAnnotation(Platform.class);
152141
Platform[] platforms = null;
153142
String ourTarget = null;
@@ -195,17 +184,15 @@ public void load(Class cls, boolean inherit) {
195184
}
196185
if (classPlatform != null) {
197186
if (platforms == null) {
198-
platforms = new Platform[] { classPlatform };
187+
platforms = new Platform[]{classPlatform};
199188
} else {
200189
platforms = Arrays.copyOf(platforms, platforms.length + 1);
201190
platforms[platforms.length - 1] = classPlatform;
202191
}
203192
}
204193
boolean hasPlatformProperties = platforms != null && platforms.length > (classProperties != null && classPlatform != null ? 1 : 0);
205194

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 = {};
195+
String[] exceptionMappings = {}, pragma = {}, define = {}, exclude = {}, include = {}, cinclude = {}, includepath = {}, includeresource = {}, compiler = {}, linkpath = {}, linkresource = {}, link = {}, frameworkpath = {}, framework = {}, preloadpath = {}, preloadresource = {}, preload = {}, resourcepath = {}, resource = {}, extension = {}, executablepath = {}, executable = {};
209196
String library = "jni" + c.getSimpleName();
210197
if (hasPlatformProperties) {
211198
if (ourTarget != null && ourTarget.length() > 0) {
@@ -219,8 +206,8 @@ public void load(Class cls, boolean inherit) {
219206
}
220207
}
221208
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 };
209+
String[][] names = {p.value().length > 0 ? p.value() : defaultNames, p.not(), p.pattern()};
210+
boolean[] matches = {false, false, false};
224211
for (int i = 0; i < names.length; i++) {
225212
for (String s : names[i]) {
226213
if ((i < 2 && platform.startsWith(s)) || (s.length() > 0 && platform.matches(s))) {
@@ -241,28 +228,41 @@ public void load(Class cls, boolean inherit) {
241228
if (!match) {
242229
continue;
243230
}
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(); }
231+
232+
if (p.exceptionMappings().length > 0) {
233+
List<String> exceptionMappingsList = new LinkedList<>();
234+
for (ExceptionMapper mapper : p.exceptionMappings()) {
235+
final String cppException = mapper.cppException();
236+
final String javaException = mapper.javaExceptionClass().getName().replace('.', '/');
237+
238+
exceptionMappingsList.add(cppException);
239+
exceptionMappingsList.add(javaException);
240+
}
241+
exceptionMappings = exceptionMappingsList.toArray(new String[0]);
242+
}
243+
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(); }
249249
if (p.includepath().length > 0) { includepath = p.includepath(); }
250250
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(); }
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(); }
255255
if (p.frameworkpath().length > 0) { frameworkpath = p.frameworkpath(); }
256-
if (p.framework() .length > 0) { framework = p.framework(); }
256+
if (p.framework().length > 0) { framework = p.framework(); }
257257
if (p.preloadresource().length > 0) { preloadresource = p.preloadresource(); }
258258
if (p.preloadpath().length > 0) { preloadpath = p.preloadpath(); }
259-
if (p.preload() .length > 0) { preload = p.preload(); }
259+
if (p.preload().length > 0) { preload = p.preload(); }
260260
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(); }
261+
if (p.resource().length > 0) { resource = p.resource(); }
262+
if (p.extension().length > 0) { extension = p.extension(); }
263263
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(); }
264+
if (p.executable().length > 0) { executable = p.executable(); }
265+
if (p.library().length() > 0) { library = p.library(); }
266266
}
267267
}
268268
for (int i = 0; i < includeresource.length; i++) {
@@ -289,6 +289,8 @@ public void load(Class cls, boolean inherit) {
289289
linkresource[i] = "/" + name;
290290
}
291291
}
292+
293+
addAll("platform.exceptionMappings", exceptionMappings);
292294
addAll("platform.pragma", pragma);
293295
addAll("platform.define", define);
294296
addAll("platform.exclude", exclude);
@@ -319,7 +321,7 @@ public void load(Class cls, boolean inherit) {
319321

320322
if (LoadEnabled.class.isAssignableFrom(c)) {
321323
try {
322-
((LoadEnabled)c.newInstance()).init(this);
324+
((LoadEnabled) c.newInstance()).init(this);
323325
} catch (ClassCastException | InstantiationException | IllegalAccessException e) {
324326
logger.warn("Could not create an instance of " + c + ": " + e);
325327
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.bytedeco.javacpp.annotation;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
import java.lang.annotation.Target;
6+
7+
/**
8+
* Exception Mapper maps a C/C++ exception to the given java exception.
9+
* Will overwrite any existing exception-mappings.
10+
*/
11+
@Retention(RetentionPolicy.RUNTIME)
12+
@Target({})
13+
public @interface ExceptionMapper {
14+
15+
/**
16+
* The C/C++ exception to be mapped (e.g.: std::runtime_error).
17+
*
18+
* @return A String representation of the C/C++ exception to be mapped.
19+
*/
20+
String cppException();
21+
22+
/**
23+
* The corresponding java-exception.
24+
*
25+
* @return The corresponding java-exception.
26+
*/
27+
Class<? extends Throwable> javaExceptionClass();
28+
}
29+

0 commit comments

Comments
 (0)