From 4083b8a1488c394665db713cf8683e2576c769dc Mon Sep 17 00:00:00 2001 From: Samuel Audet Date: Sat, 27 Feb 2016 11:44:38 +0900 Subject: [PATCH] * Make `Builder` fail on `ClassNotFoundException` or `NoClassDefFoundError` instead of logging warnings --- CHANGELOG.md | 1 + .../org/bytedeco/javacpp/tools/BuildMojo.java | 3 +- .../org/bytedeco/javacpp/tools/Builder.java | 6 ++-- .../bytedeco/javacpp/tools/ClassScanner.java | 30 ++++++++----------- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f2056a4c..d963b3a5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ + * Make `Builder` fail on `ClassNotFoundException` or `NoClassDefFoundError` instead of logging warnings * Allow `Builder` to generate native libraries with empty `@Platform` annotation even without user defined `native` methods * Enhance `Parser` to support a bit better `&&` tokens and C++11 rvalue references ([issue bytedeco/javacpp-presets#160](https://github.com/bytedeco/javacpp-presets/issues/160)) * Add properties for the `linux-armhf` platform, and pick up `macosx` when `os.name` is `darwin` diff --git a/src/main/java/org/bytedeco/javacpp/tools/BuildMojo.java b/src/main/java/org/bytedeco/javacpp/tools/BuildMojo.java index 43e36aa13..cce68fffc 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/BuildMojo.java +++ b/src/main/java/org/bytedeco/javacpp/tools/BuildMojo.java @@ -23,6 +23,7 @@ package org.bytedeco.javacpp.tools; import java.io.File; +import java.io.IOException; import java.util.Arrays; import java.util.Map; import java.util.Properties; @@ -226,7 +227,7 @@ String[] merge(String[] ss, String s) { if (log.isDebugEnabled()) { log.debug("outputFiles: " + Arrays.deepToString(outputFiles)); } - } catch (Exception e) { + } catch (IOException | ClassNotFoundException | NoClassDefFoundError | InterruptedException | ParserException e) { log.error("Failed to execute JavaCPP Builder: " + e.getMessage()); throw new MojoExecutionException("Failed to execute JavaCPP Builder", e); } diff --git a/src/main/java/org/bytedeco/javacpp/tools/Builder.java b/src/main/java/org/bytedeco/javacpp/tools/Builder.java index 36d7fbe54..738886b16 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Builder.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Builder.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011-2014 Samuel Audet + * Copyright (C) 2011-2016 Samuel Audet * * Licensed either under the Apache License, Version 2.0, or (at your option) * under the terms of the GNU General Public License as published by @@ -575,7 +575,7 @@ public Builder property(String key, String value) { } /** Requests the {@link #classScanner} to add a class or all classes from a package. * A {@code null} argument indicates the unnamed package. */ - public Builder classesOrPackages(String ... classesOrPackages) throws IOException { + public Builder classesOrPackages(String ... classesOrPackages) throws IOException, ClassNotFoundException, NoClassDefFoundError { if (classesOrPackages == null) { classScanner.addPackage(null, true); } else for (String s : classesOrPackages) { @@ -703,7 +703,7 @@ public static void printHelp() { } System.out.println( "JavaCPP version " + version + "\n" + - "Copyright (C) 2011-2015 Samuel Audet \n" + + "Copyright (C) 2011-2016 Samuel Audet \n" + "Project site: https://github.com/bytedeco/javacpp"); System.out.println(); System.out.println("Usage: java -jar javacpp.jar [options] [class or package (suffixed with .* or .**)]"); diff --git a/src/main/java/org/bytedeco/javacpp/tools/ClassScanner.java b/src/main/java/org/bytedeco/javacpp/tools/ClassScanner.java index 1f93252ba..6e0b61e46 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/ClassScanner.java +++ b/src/main/java/org/bytedeco/javacpp/tools/ClassScanner.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014 Samuel Audet + * Copyright (C) 2014-2016 Samuel Audet * * Licensed either under the Apache License, Version 2.0, or (at your option) * under the terms of the GNU General Public License as published by @@ -41,9 +41,9 @@ class ClassScanner { this.loader = loader; } - private Logger logger; - private Collection classes; - private UserClassLoader loader; + final Logger logger; + final Collection classes; + final UserClassLoader loader; public Collection getClasses() { return classes; @@ -52,25 +52,19 @@ public UserClassLoader getClassLoader() { return loader; } - public void addClass(String className) { + public void addClass(String className) throws ClassNotFoundException, NoClassDefFoundError { if (className == null) { return; } else if (className.endsWith(".class")) { className = className.substring(0, className.length()-6); } - try { - Class c = Class.forName(className, false, loader); - if (!classes.contains(c)) { - classes.add(c); - } - } catch (ClassNotFoundException e) { - logger.warn("Could not find class " + className + ": " + e); - } catch (NoClassDefFoundError e) { - logger.warn("Could not load class " + className + ": " + e); + Class c = Class.forName(className, false, loader); + if (!classes.contains(c)) { + classes.add(c); } } - public void addMatchingFile(String filename, String packagePath, boolean recursive) { + public void addMatchingFile(String filename, String packagePath, boolean recursive) throws ClassNotFoundException, NoClassDefFoundError { if (filename != null && filename.endsWith(".class") && (packagePath == null || (recursive && filename.startsWith(packagePath)) || filename.regionMatches(0, packagePath, 0, Math.max(filename.lastIndexOf('/'), packagePath.lastIndexOf('/'))))) { @@ -78,7 +72,7 @@ public void addMatchingFile(String filename, String packagePath, boolean recursi } } - public void addMatchingDir(String parentName, File dir, String packagePath, boolean recursive) { + public void addMatchingDir(String parentName, File dir, String packagePath, boolean recursive) throws ClassNotFoundException, NoClassDefFoundError { File[] files = dir.listFiles(); Arrays.sort(files); for (File f : files) { @@ -91,7 +85,7 @@ public void addMatchingDir(String parentName, File dir, String packagePath, bool } } - public void addPackage(String packageName, boolean recursive) throws IOException { + public void addPackage(String packageName, boolean recursive) throws IOException, ClassNotFoundException, NoClassDefFoundError { String[] paths = loader.getPaths(); final String packagePath = packageName == null ? null : (packageName.replace('.', '/') + "/"); int prevSize = classes.size(); @@ -118,7 +112,7 @@ public void addPackage(String packageName, boolean recursive) throws IOException } } - public void addClassOrPackage(String name) throws IOException { + public void addClassOrPackage(String name) throws IOException, ClassNotFoundException, NoClassDefFoundError { if (name == null) { return; }