You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2016/04/14 22:30:13 UTC

[03/18] incubator-geode git commit: GEODE-1214: Better error handling for compilation failures

GEODE-1214: Better error handling for compilation failures


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/90ab09cc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/90ab09cc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/90ab09cc

Branch: refs/heads/feature/GEODE-1162
Commit: 90ab09cc429643e3b7d9ec336fccbc6b2c0402c4
Parents: c67a1c9
Author: Jens Deppe <jd...@pivotal.io>
Authored: Tue Apr 12 09:01:00 2016 -0700
Committer: Jens Deppe <jd...@pivotal.io>
Committed: Tue Apr 12 12:44:41 2016 -0700

----------------------------------------------------------------------
 .../gemstone/gemfire/internal/ClassBuilder.java    | 13 ++++++++++++-
 .../gemfire/internal/JarClassLoaderJUnitTest.java  | 17 +++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/90ab09cc/geode-core/src/test/java/com/gemstone/gemfire/internal/ClassBuilder.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/ClassBuilder.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/ClassBuilder.java
index 118ad86..780ed4f 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/ClassBuilder.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/ClassBuilder.java
@@ -29,6 +29,8 @@ import java.util.List;
 import java.util.jar.JarEntry;
 import java.util.jar.JarOutputStream;
 
+import javax.tools.Diagnostic;
+import javax.tools.DiagnosticCollector;
 import javax.tools.FileObject;
 import javax.tools.ForwardingJavaFileManager;
 import javax.tools.JavaCompiler;
@@ -214,7 +216,16 @@ public class ClassBuilder implements Serializable {
     fileObjects.add(new JavaSourceFromString(className, classCode));
 
     List<String> options = Arrays.asList("-classpath", this.classPath);
-    javaCompiler.getTask(null, fileManager, null, options, null, fileObjects).call();
+    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
+    if (! javaCompiler.getTask(null, fileManager, diagnostics, options, null, fileObjects).call()) {
+      StringBuilder errorMsg = new StringBuilder();
+      for (Diagnostic d : diagnostics.getDiagnostics()) {
+        String err = String.format("Compilation error: Line %d - %s%n", d.getLineNumber(), d.getMessage(null));
+        errorMsg.append(err);
+        System.err.print(err);
+      }
+      throw new IOException(errorMsg.toString());
+    }
     return byteArrayOutputStream.toByteArray();
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/90ab09cc/geode-core/src/test/java/com/gemstone/gemfire/internal/JarClassLoaderJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/JarClassLoaderJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/JarClassLoaderJUnitTest.java
index 20c7990..56b715f 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/JarClassLoaderJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/JarClassLoaderJUnitTest.java
@@ -171,6 +171,23 @@ public class JarClassLoaderJUnitTest {
   }
 
   @Test
+  public void testFailingCompilation() throws Exception {
+    StringBuffer stringBuffer = new StringBuffer();
+    stringBuffer.append("import com.gemstone.gemfire.cache.Declarable;");
+    stringBuffer.append("import com.gemstone.gemfire.cache.execute.Function;");
+    stringBuffer.append("import com.gemstone.gemfire.cache.execute.FunctionContext;");
+    stringBuffer.append("public class JarClassLoaderJUnitFunction implements Function {}");
+    String functionString = stringBuffer.toString();
+
+    try {
+      this.classBuilder.createJarFromClassContent("JarClassLoaderJUnitFunction", functionString);
+      fail("This code should have failed to compile and thrown an exception");
+    } catch (Exception ex) {
+      // All good
+    }
+  }
+
+  @Test
   public void testFunctions() throws IOException, ClassNotFoundException {
     final File jarFile1 = new File(JAR_PREFIX + "JarClassLoaderJUnit.jar#1");
     final File jarFile2 = new File(JAR_PREFIX + "JarClassLoaderJUnit.jar#2");