Skip to content

Commit

Permalink
Fix NPE in BasicRestProjectGenerator when a resource is missing
Browse files Browse the repository at this point in the history
Throw a dedicated IOException instead.
Closes quarkusio#3331
  • Loading branch information
ia3andy authored and Dufgui committed Aug 26, 2019
1 parent e54e3cb commit ab7f053
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -111,9 +113,15 @@ private void generate(final String templateName, final Map<String, Object> conte
throws IOException {
if (!writer.exists(outputFilePath)) {
String path = templateName.startsWith("/") ? templateName : "/" + templateName;
try (final BufferedReader stream = new BufferedReader(
new InputStreamReader(getClass().getResourceAsStream(path), StandardCharsets.UTF_8))) {
String template = stream.lines().collect(Collectors.joining("\n"));
URL resource = getClass().getResource(path);
if (resource == null) {
throw new IOException("Template resource is missing: " + path);
}
try (
InputStream resourceStream = resource.openStream();
InputStreamReader streamReader = new InputStreamReader(resourceStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(streamReader)) {
String template = bufferedReader.lines().collect(Collectors.joining("\n"));
for (Entry<String, Object> e : context.entrySet()) {
if (e.getValue() != null) { // Exclude null values (classname and path can be null)
template = template.replace(format("${%s}", e.getKey()), e.getValue().toString());
Expand Down

0 comments on commit ab7f053

Please sign in to comment.