Skip to content

Commit b22dd96

Browse files
committed
FIX java.lang.NullPointerException when no java compiler available (fixes #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.
1 parent a23cd73 commit b22dd96

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

restx-classloader/src/main/java/restx/classloader/CompilationManager.java

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ public CompilationManager(EventBus eventBus, Iterable<Path> sourceRoots, Path de
119119
this.settings = settings;
120120

121121
javaCompiler = ToolProvider.getSystemJavaCompiler();
122+
if (javaCompiler == null) {
123+
throw new IllegalStateException(
124+
"trying to setup a compilation manager while no system compiler is available." +
125+
" This should be prevented by checking the system java compiler first.");
126+
}
122127
fileManager = javaCompiler.getStandardFileManager(new DiagnosticCollector<JavaFileObject>(), Locale.ENGLISH, Charsets.UTF_8);
123128
try {
124129
if (!destination.toFile().exists()) {

restx-core/src/main/java/restx/Apps.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public Apps(AppSettings settings) {
4343
}
4444

4545
public CompilationManager newAppCompilationManager(EventBus eventBus, CompilationSettings compilationSettings) {
46+
if (!hasSystemJavaCompiler()) {
47+
throw new IllegalStateException(
48+
"trying to setup a compilation manager while no system compiler is available. " +
49+
"This should be prevented by checking the system java compiler first. " +
50+
"Use hasSystemJavaCompiler() to check that before calling this method.");
51+
}
4652
if (hasJavadocTools()) {
4753
eventBus.register(new Object() {
4854
@Subscribe
@@ -144,12 +150,22 @@ public void run() {
144150
return process;
145151
}
146152

147-
private boolean hasJavadocTools() {
153+
public static boolean hasJavadocTools() {
148154
try {
149155
Class.forName("com.sun.tools.javadoc.Main");
150156
return true;
151157
} catch (Exception e) {
152158
return false;
153159
}
154160
}
161+
162+
public static boolean hasSystemJavaCompiler() {
163+
try {
164+
Class<?> tp = Class.forName("javax.tools.ToolProvider");
165+
return tp.getMethod("getSystemJavaCompiler").invoke(null) != null;
166+
} catch (Exception e) {
167+
return false;
168+
}
169+
}
170+
155171
}

restx-core/src/main/java/restx/RestxMainRouterFactory.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package restx;
22

3-
import com.google.common.base.*;
3+
import com.google.common.base.Optional;
4+
import com.google.common.base.Predicate;
5+
import com.google.common.base.Stopwatch;
6+
import com.google.common.base.Supplier;
7+
import com.google.common.base.Suppliers;
8+
import com.google.common.base.Throwables;
49
import com.google.common.collect.ImmutableSet;
510
import com.google.common.eventbus.EventBus;
611
import com.google.common.eventbus.Subscribe;
@@ -13,7 +18,11 @@
1318
import restx.classloader.HotReloadingClassLoader;
1419
import restx.common.RestxConfig;
1520
import restx.common.metrics.api.MetricRegistry;
16-
import restx.factory.*;
21+
import restx.factory.Factory;
22+
import restx.factory.Name;
23+
import restx.factory.NamedComponent;
24+
import restx.factory.SingletonFactoryMachine;
25+
import restx.factory.Warehouse;
1726
import restx.http.HttpStatus;
1827
import restx.security.RestxSessionCookieFilter;
1928
import restx.server.WebServer;
@@ -578,7 +587,7 @@ private boolean useHotCompile() {
578587
logger.info("can't enable hot compile: restx.app.package is not set.\n" +
579588
"Run your app with -Drestx.app.package=<app.base.package> to enable hot compile.");
580589
return false;
581-
} else if (!hasToolsJar()) {
590+
} else if (!hasSystemJavaCompiler()) {
582591
logger.info("can't enable hot compile: tools.jar is not in classpath.\n" +
583592
"Run your app with a JDK rather than a JRE to enable hot compile.");
584593
return false;
@@ -590,7 +599,7 @@ private boolean useHotCompile() {
590599
&& !getMode().equals(RestxContext.Modes.PROD)
591600
&& !getMode().equals(RestxContext.Modes.TEST)
592601
&& appSettings.appPackage().isPresent()
593-
&& hasToolsJar();
602+
&& hasSystemJavaCompiler();
594603
}
595604
}
596605

@@ -602,13 +611,8 @@ private String getMode(RestxRequest restxRequest) {
602611
return restxRequest.getHeader("RestxMode").or(getMode());
603612
}
604613

605-
private boolean hasToolsJar() {
606-
try {
607-
Class.forName("javax.tools.ToolProvider");
608-
return true;
609-
} catch (Exception e) {
610-
return false;
611-
}
614+
private boolean hasSystemJavaCompiler() {
615+
return Apps.hasSystemJavaCompiler();
612616
}
613617

614618
private boolean useAutoCompile() {

0 commit comments

Comments
 (0)