From 138cbfd82b61567c7ef8009725ac64d88214798c Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 21 May 2013 18:29:33 -0500 Subject: [PATCH 1/5] Fix a couple of compiler warnings Admittedly, we shut up a few instead of fixing them. Signed-off-by: Johannes Schindelin --- src/main/java/org/scijava/InstantiableException.java | 2 ++ src/main/java/org/scijava/MenuPath.java | 2 ++ src/main/java/org/scijava/NoSuchServiceException.java | 2 ++ src/main/java/org/scijava/object/ObjectIndex.java | 4 ++-- src/main/java/org/scijava/util/SizableArrayList.java | 2 ++ 5 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/scijava/InstantiableException.java b/src/main/java/org/scijava/InstantiableException.java index 1f2558305..98973208a 100644 --- a/src/main/java/org/scijava/InstantiableException.java +++ b/src/main/java/org/scijava/InstantiableException.java @@ -42,6 +42,8 @@ */ public class InstantiableException extends Exception { + private static final long serialVersionUID = 1L; + public InstantiableException() { super(); } diff --git a/src/main/java/org/scijava/MenuPath.java b/src/main/java/org/scijava/MenuPath.java index 843a81931..0491b0201 100644 --- a/src/main/java/org/scijava/MenuPath.java +++ b/src/main/java/org/scijava/MenuPath.java @@ -45,6 +45,8 @@ */ public class MenuPath extends ArrayList { + private static final long serialVersionUID = 1L; + /** The separator between elements of a menu path string. */ public static final String PATH_SEPARATOR = ">"; diff --git a/src/main/java/org/scijava/NoSuchServiceException.java b/src/main/java/org/scijava/NoSuchServiceException.java index 4ed42102e..6a0e6d4c2 100644 --- a/src/main/java/org/scijava/NoSuchServiceException.java +++ b/src/main/java/org/scijava/NoSuchServiceException.java @@ -44,6 +44,8 @@ */ public class NoSuchServiceException extends RuntimeException { + private static final long serialVersionUID = 1L; + public NoSuchServiceException() { super(); } diff --git a/src/main/java/org/scijava/object/ObjectIndex.java b/src/main/java/org/scijava/object/ObjectIndex.java index 16d3a4e16..e95419b44 100644 --- a/src/main/java/org/scijava/object/ObjectIndex.java +++ b/src/main/java/org/scijava/object/ObjectIndex.java @@ -275,13 +275,13 @@ protected boolean remove(final Object o, final Class type, } protected boolean addToList(final E obj, final List list, - @SuppressWarnings("unused") final boolean batch) + final boolean batch) { return list.add(obj); } protected boolean removeFromList(final Object obj, final List list, - @SuppressWarnings("unused") final boolean batch) + final boolean batch) { return list.remove(obj); } diff --git a/src/main/java/org/scijava/util/SizableArrayList.java b/src/main/java/org/scijava/util/SizableArrayList.java index ebbef7906..e15a7dd5a 100644 --- a/src/main/java/org/scijava/util/SizableArrayList.java +++ b/src/main/java/org/scijava/util/SizableArrayList.java @@ -51,6 +51,8 @@ */ public class SizableArrayList extends ArrayList implements Sizable { + private static final long serialVersionUID = 1L; + // -- Constructors -- public SizableArrayList(final int initialCapacity) { From a7a43f0451dfc653646fcea0d60102cfeb89e485 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 21 May 2013 18:31:45 -0500 Subject: [PATCH 2/5] CheckSezpoz: Use the ToolProvider to get the 'apt' class This is more robust than expecting the 'apt' main class to be available in the default class path. Signed-off-by: Johannes Schindelin --- src/main/java/org/scijava/util/CheckSezpoz.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/scijava/util/CheckSezpoz.java b/src/main/java/org/scijava/util/CheckSezpoz.java index a22a5610c..830a5cf99 100644 --- a/src/main/java/org/scijava/util/CheckSezpoz.java +++ b/src/main/java/org/scijava/util/CheckSezpoz.java @@ -57,6 +57,7 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; +import javax.tools.ToolProvider; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -366,7 +367,7 @@ public static boolean fix(final File classes, final File sources) { final Method aptProcess; try { final Class aptClass = - CheckSezpoz.class.getClassLoader().loadClass("com.sun.tools.apt.Main"); + ToolProvider.getSystemToolClassLoader().loadClass("com.sun.tools.apt.Main"); aptProcess = aptClass.getMethod("process", new Class[] { String[].class }); } From 3e8f482943d87d9bb9958f71b4e3f21958fecd3f Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 14 Jul 2013 13:14:14 -0500 Subject: [PATCH 3/5] CheckSezpoz: use ToolProvider.getSystemJavaCompiler() ... but fall back to calling the apt class directly if we're not running inside a JDK. Signed-off-by: Johannes Schindelin --- .../java/org/scijava/util/CheckSezpoz.java | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/scijava/util/CheckSezpoz.java b/src/main/java/org/scijava/util/CheckSezpoz.java index 830a5cf99..e0bdc721c 100644 --- a/src/main/java/org/scijava/util/CheckSezpoz.java +++ b/src/main/java/org/scijava/util/CheckSezpoz.java @@ -43,6 +43,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.security.DigestInputStream; import java.security.MessageDigest; @@ -57,6 +58,7 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; +import javax.tools.JavaCompiler; import javax.tools.ToolProvider; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -364,19 +366,6 @@ protected static boolean hasAnnotation(final File file) { * @return whether anything in {@code META-INF/annotations/*} changed */ public static boolean fix(final File classes, final File sources) { - final Method aptProcess; - try { - final Class aptClass = - ToolProvider.getSystemToolClassLoader().loadClass("com.sun.tools.apt.Main"); - aptProcess = - aptClass.getMethod("process", new Class[] { String[].class }); - } - catch (final Exception e) { - e.printStackTrace(); - System.err - .println("ERROR: Could not fix " + sources + ": apt not found"); - return false; - } if (!sources.exists()) { System.err.println("ERROR: Sources are not in the expected place: " + sources); @@ -384,10 +373,7 @@ public static boolean fix(final File classes, final File sources) { } final List aptArgs = new ArrayList(); - aptArgs.add("-nocompile"); if (verbose) aptArgs.add("-verbose"); - aptArgs.add("-factory"); - aptArgs.add("net.java.sezpoz.impl.IndexerFactory"); aptArgs.add("-d"); aptArgs.add(classes.getPath()); final int count = aptArgs.size(); @@ -413,7 +399,7 @@ public static boolean fix(final File classes, final File sources) { final String[] args = aptArgs.toArray(new String[aptArgs.size()]); try { System.err.println("WARN: Updating the annotation index in " + classes); - aptProcess.invoke(null, new Object[] { args }); + runApt(args); } catch (final Exception e) { e.printStackTrace(); @@ -441,6 +427,36 @@ public static boolean fix(final File classes, final File sources) { return result; } + private static void runApt(String[] args) throws ClassNotFoundException, + SecurityException, NoSuchMethodException, IllegalArgumentException, + IllegalAccessException, InvocationTargetException + { + final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + if (compiler != null) { + final List aptArgs = new ArrayList(); + aptArgs.add("-proc:only"); + aptArgs.add("-processor"); + aptArgs.add("net.java.sezpoz.impl.Indexer6"); + aptArgs.addAll(Arrays.asList(args)); + compiler.run(null, null, null, args); + return; + } + + System.err.println("WARN: falling back to calling the 'apt' Main class directly"); + final List aptArgs = new ArrayList(); + aptArgs.add("-nocompile"); + aptArgs.add("-factory"); + aptArgs.add("net.java.sezpoz.impl.IndexerFactory"); + aptArgs.addAll(Arrays.asList(args)); + + final Method aptProcess; + final Class aptClass = + ToolProvider.getSystemToolClassLoader().loadClass("com.sun.tools.apt.Main"); + aptProcess = + aptClass.getMethod("process", new Class[] { String[].class }); + aptProcess.invoke(null, aptArgs.toArray()); + } + private static MessageDigest digest; /** From 37f1fe9b7e6d500e7e41f65159eedb77fc03bd39 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 14 Jul 2013 13:15:47 -0500 Subject: [PATCH 4/5] CheckSezpozTest: use ToolProvider.getSystemJavaCompiler() ... instead of calling 'javac' directly. This is more robust than expecting 'javac' to be in the PATH as long as we are assuming that the regression tests are run within a JDK -- which is usually the case because they are run in the context of building scijava-common. This fixes http://trac.imagej.net/ticket/1560. Signed-off-by: Johannes Schindelin --- src/test/java/org/scijava/util/CheckSezpozTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/scijava/util/CheckSezpozTest.java b/src/test/java/org/scijava/util/CheckSezpozTest.java index 0546c0ccb..aefdb3457 100644 --- a/src/test/java/org/scijava/util/CheckSezpozTest.java +++ b/src/test/java/org/scijava/util/CheckSezpozTest.java @@ -50,6 +50,9 @@ import java.net.URL; import java.net.URLClassLoader; +import javax.tools.JavaCompiler; +import javax.tools.ToolProvider; + import net.java.sezpoz.Index; import net.java.sezpoz.IndexItem; @@ -115,8 +118,9 @@ public void testBasic() throws Exception { + "}\n"); writer.close(); - ProcessUtils.exec(sources, System.err, System.out, "javac", "-classpath", - System.getProperty("java.class.path"), "Annotated.java"); + final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + compiler.run(null, null, null, "-classpath", + System.getProperty("java.class.path"), new File(sources, "Annotated.java").getAbsolutePath()); // to make sure the annotation processor "has not run", // we need to copy the .class file From defcc5f40764cb8b0ebff9c8de91bd3538ed00ca Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 14 Jul 2013 15:06:54 -0500 Subject: [PATCH 5/5] Skip CheckSezpozTest when running in a JRE It is really a shame, but it seems that all of the following strategies fail in Eclipse: - try to figure out where a JDK might be (there might be none) - try to figure out where Eclipse is and use its APT processing (first, we cannot really figure out where Eclipse is when running or debugging regular Java classes with a main() method -- with JUnit tests, we could at least figure out where Eclipse's JUnit support classes are and hence where the current Eclipse is -- and there is no platform-independent Java way to enquire the executable path of the parent process of the current JVM; second, even then we would have to deal with tons of Eclipse-specific things we would have to provide to call Eclipse's APT) - run Sezpoz' Indexer6 directly (it cannot be run directly, you have to have a RoundEnvironment and provide scanners for annotations and classes and mirror classes) The most promising solution to the problem that Eclipse is still unable to conform to the Java Compiler specification, at this time and age, would have been to call the Indexer6 directly. But this developer thinks that the effort required to basically replicate the complete APT functionality just to run Sezpoz is not worth it: we could instead tell the developer to get their act together and install a JDK already (which they need anyway because Eclipse is *not* the gold standard, the JDK is, so if things compile/run correctly in Eclipse, you *still* have to test with plain JDK). Signed-off-by: Johannes Schindelin --- src/test/java/org/scijava/util/CheckSezpozTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/org/scijava/util/CheckSezpozTest.java b/src/test/java/org/scijava/util/CheckSezpozTest.java index aefdb3457..b1b460cba 100644 --- a/src/test/java/org/scijava/util/CheckSezpozTest.java +++ b/src/test/java/org/scijava/util/CheckSezpozTest.java @@ -38,6 +38,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; import java.io.File; import java.io.FileInputStream; @@ -119,6 +120,8 @@ public void testBasic() throws Exception { writer.close(); final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + if (compiler == null) System.err.println("WARN: running in a JRE; Skipping CheckSezpozTest!"); + assumeTrue(compiler != null); compiler.run(null, null, null, "-classpath", System.getProperty("java.class.path"), new File(sources, "Annotated.java").getAbsolutePath());