Skip to content

Commit

Permalink
Return a separate Closeable implementation when appropriate. Avoids…
Browse files Browse the repository at this point in the history
… incorrect resource leak warnings when using `ofContent()`. These started appearing after the latest Eclipse update.
  • Loading branch information
brett-smith committed Mar 21, 2024
1 parent 33a2d3f commit d7374aa
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions src/main/java/com/sshtools/tinytemplate/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,24 @@ public final static Boolean evalAsCondition(Object val) {
public final static Logger defaultStdOutLogger() {
return LazyDefaultStdOutLogger.DEFAULT;
}

public final static class CloseableTemplateModel extends TemplateModel implements Closeable {

public CloseableTemplateModel(Reader reader) {
super(reader);
}

@Override
public void close() {
try {
xtext.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

public final static class TemplateModel implements Closeable {
public static class TemplateModel {

public static TemplateModel ofPath(Path path) {
try {
Expand All @@ -364,20 +380,24 @@ public static TemplateModel ofContent(String content) {
return new TemplateModel(new StringReader(content));
}

public static TemplateModel ofResource(String resource) {
public static CloseableTemplateModel ofReader(Reader resource) {
return new CloseableTemplateModel(resource);
}

public static CloseableTemplateModel ofResource(String resource) {
return ofResource(resource, Optional.empty());
}

public static TemplateModel ofResource(String resource, ClassLoader loader) {
public static CloseableTemplateModel ofResource(String resource, ClassLoader loader) {
return ofResource(resource, Optional.of(loader));
}

public static TemplateModel ofResource(Class<?> packageClass, String childResource) {
public static CloseableTemplateModel ofResource(Class<?> packageClass, String childResource) {
return ofResource(packageClass.getPackage().getName().replace('.', '/') + "/" + childResource,
Optional.of(packageClass.getClassLoader()));
}

public static TemplateModel ofResource(String resource, Optional<ClassLoader> loader) {
public static CloseableTemplateModel ofResource(String resource, Optional<ClassLoader> loader) {
// Read resource content
var ldr = loader.orElseGet(() -> Templates.class.getClassLoader());
try {
Expand All @@ -387,7 +407,7 @@ public static TemplateModel ofResource(String resource, Optional<ClassLoader> lo
throw new FileNotFoundException(resource);
}
var in = res.openStream();
return new TemplateModel(new InputStreamReader(in));
return new CloseableTemplateModel(new InputStreamReader(in));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -405,7 +425,8 @@ public static TemplateModel ofResource(String resource, Optional<ClassLoader> lo
Optional<Supplier<Locale>> locale = Optional.empty();
Optional<TemplateModel> parent = Optional.empty();

private final Reader xtext;
protected final Reader xtext;

private StringBuilder buffer = null;
private Optional<Consumer<String>> instruction = Optional.empty();

Expand Down Expand Up @@ -459,15 +480,6 @@ public void close() throws IOException {
}
}

@Override
public void close() {
try {
xtext.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public Locale locale() {
return locale.map(l -> l.get()).orElseGet(() -> Locale.getDefault());
}
Expand Down

0 comments on commit d7374aa

Please sign in to comment.