Permalink
Browse files

FIX java.lang.NullPointerException when no java compiler available (f…

…ixes #107)

Didn't manage to actually test it fixes the problem, since I didn't reproduce it.

At the minimum we will get a better error message, but we should even auto disable
 auto compile in that case.
  • Loading branch information...
xhanin committed Dec 31, 2014
1 parent a23cd73 commit b22dd96a9de91d262cfc2c8b73fb9814e5694242
@@ -119,6 +119,11 @@ public CompilationManager(EventBus eventBus, Iterable<Path> sourceRoots, Path de
this.settings = settings;
javaCompiler = ToolProvider.getSystemJavaCompiler();
if (javaCompiler == null) {
throw new IllegalStateException(
"trying to setup a compilation manager while no system compiler is available." +
" This should be prevented by checking the system java compiler first.");
}
fileManager = javaCompiler.getStandardFileManager(new DiagnosticCollector<JavaFileObject>(), Locale.ENGLISH, Charsets.UTF_8);
try {
if (!destination.toFile().exists()) {
@@ -43,6 +43,12 @@ public Apps(AppSettings settings) {
}
public CompilationManager newAppCompilationManager(EventBus eventBus, CompilationSettings compilationSettings) {
if (!hasSystemJavaCompiler()) {
throw new IllegalStateException(
"trying to setup a compilation manager while no system compiler is available. " +
"This should be prevented by checking the system java compiler first. " +
"Use hasSystemJavaCompiler() to check that before calling this method.");
}
if (hasJavadocTools()) {
eventBus.register(new Object() {
@Subscribe
@@ -144,12 +150,22 @@ public void run() {
return process;
}
private boolean hasJavadocTools() {
public static boolean hasJavadocTools() {
try {
Class.forName("com.sun.tools.javadoc.Main");
return true;
} catch (Exception e) {
return false;
}
}
public static boolean hasSystemJavaCompiler() {
try {
Class<?> tp = Class.forName("javax.tools.ToolProvider");
return tp.getMethod("getSystemJavaCompiler").invoke(null) != null;
} catch (Exception e) {
return false;
}
}
}
@@ -1,6 +1,11 @@
package restx;
import com.google.common.base.*;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Stopwatch;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
@@ -13,7 +18,11 @@
import restx.classloader.HotReloadingClassLoader;
import restx.common.RestxConfig;
import restx.common.metrics.api.MetricRegistry;
import restx.factory.*;
import restx.factory.Factory;
import restx.factory.Name;
import restx.factory.NamedComponent;
import restx.factory.SingletonFactoryMachine;
import restx.factory.Warehouse;
import restx.http.HttpStatus;
import restx.security.RestxSessionCookieFilter;
import restx.server.WebServer;
@@ -578,7 +587,7 @@ private boolean useHotCompile() {
logger.info("can't enable hot compile: restx.app.package is not set.\n" +
"Run your app with -Drestx.app.package=<app.base.package> to enable hot compile.");
return false;
} else if (!hasToolsJar()) {
} else if (!hasSystemJavaCompiler()) {
logger.info("can't enable hot compile: tools.jar is not in classpath.\n" +
"Run your app with a JDK rather than a JRE to enable hot compile.");
return false;
@@ -590,7 +599,7 @@ private boolean useHotCompile() {
&& !getMode().equals(RestxContext.Modes.PROD)
&& !getMode().equals(RestxContext.Modes.TEST)
&& appSettings.appPackage().isPresent()
&& hasToolsJar();
&& hasSystemJavaCompiler();
}
}
@@ -602,13 +611,8 @@ private String getMode(RestxRequest restxRequest) {
return restxRequest.getHeader("RestxMode").or(getMode());
}
private boolean hasToolsJar() {
try {
Class.forName("javax.tools.ToolProvider");
return true;
} catch (Exception e) {
return false;
}
private boolean hasSystemJavaCompiler() {
return Apps.hasSystemJavaCompiler();
}
private boolean useAutoCompile() {

0 comments on commit b22dd96

Please sign in to comment.