Skip to content

Commit

Permalink
* Make Builder fail on ClassNotFoundException or `NoClassDefFoun…
Browse files Browse the repository at this point in the history
…dError` instead of logging warnings
  • Loading branch information
saudet committed Feb 27, 2016
1 parent 485081f commit 4083b8a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/bytedeco/javacpp/tools/BuildMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/bytedeco/javacpp/tools/Builder.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -703,7 +703,7 @@ public static void printHelp() {
}
System.out.println(
"JavaCPP version " + version + "\n" +
"Copyright (C) 2011-2015 Samuel Audet <samuel.audet@gmail.com>\n" +
"Copyright (C) 2011-2016 Samuel Audet <samuel.audet@gmail.com>\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 .**)]");
Expand Down
30 changes: 12 additions & 18 deletions src/main/java/org/bytedeco/javacpp/tools/ClassScanner.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -41,9 +41,9 @@ class ClassScanner {
this.loader = loader;
}

private Logger logger;
private Collection<Class> classes;
private UserClassLoader loader;
final Logger logger;
final Collection<Class> classes;
final UserClassLoader loader;

public Collection<Class> getClasses() {
return classes;
Expand All @@ -52,33 +52,27 @@ 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('/'))))) {
addClass(filename.replace('/', '.'));
}
}

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) {
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down

0 comments on commit 4083b8a

Please sign in to comment.