Skip to content

Commit 7563fa3

Browse files
committed
applying errorprone and nullaway for jst
1 parent ac76c69 commit 7563fa3

23 files changed

+144
-110
lines changed

jst/build.gradle

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ plugins {
22
id 'buildlogic.java-library-conventions'
33
alias(libs.plugins.versions)
44
id "org.xcommand.javacc"
5+
alias(libs.plugins.errorprone)
56
}
67

78
javacc {
@@ -10,6 +11,24 @@ javacc {
1011
}
1112
compileJava.dependsOn(jcc)
1213

14+
import net.ltgt.gradle.errorprone.CheckSeverity
15+
tasks.withType(JavaCompile) {
16+
options.errorprone {
17+
//disableAllChecks
18+
check("NullAway", CheckSeverity.ERROR)
19+
option("NullAway:AnnotatedPackages", "org.xcommand")
20+
option("NullAway:ExcludedClassAnnotations", "javax.annotation.processing.Generated")
21+
option("NullAway:KnownInitializers", "org.xcommand.web.WebCVInitializationFilter.init")
22+
excludedPaths.set(".*/build/.*")
23+
}
24+
// Include to disable NullAway on test code
25+
if (name.toLowerCase().contains("test")) {
26+
options.errorprone {
27+
disable("NullAway")
28+
}
29+
}
30+
}
31+
1332
sourceSets {
1433
main {
1534
java {
@@ -18,6 +37,11 @@ sourceSets {
1837
}
1938
}
2039
dependencies {
40+
annotationProcessor libs.errorprone.core
41+
errorprone libs.nullaway
42+
api libs.jspecify
43+
errorprone libs.errorprone.core
44+
2145
implementation project(':xc-core')
2246
implementation libs.javacc
2347
annotationProcessor libs.recordbuilder.processor

jst/src/main/java/org/xcommand/template/jst/CachingFilesSystemScanner.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
import org.xcommand.util.FilesUnchecked;
99

1010
import java.util.HashMap;
11+
import java.util.List;
1112

1213
public class CachingFilesSystemScanner implements ICommand {
1314
@Override
1415
public void execute() {
15-
var rootPaths = fileSystemScannerCV.getRootPath();
16-
17-
var fssc = FileSystemScanner.newInstance();
18-
fssc.setRootPaths(rootPaths);
16+
var fssc = FileSystemScanner.newInstance(List.of(fileSystemScannerCV.getRootPath()));
1917
fssc.getFileFoundNotifier().registerObserver(new FileFoundHandler());
2018
var changedFiles = new HashMap<String, FileMapEntry>();
2119
cachingFilesSystemScannerCV.setChangedFiles(changedFiles);

jst/src/main/java/org/xcommand/template/jst/DefaultJSTParserProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public void execute() {
7676
code.append(v);
7777
}
7878
};
79+
@SuppressWarnings("unused")
7980
private final ICommand eolObserver = new ICommand() {
8081
@Override
8182
public void execute() {

jst/src/main/java/org/xcommand/template/jst/DefaultJSTRoutines.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55

66
import java.io.OutputStreamWriter;
77
import java.io.Writer;
8+
import java.nio.charset.StandardCharsets;
89
import java.util.Objects;
910

1011
public class DefaultJSTRoutines implements IJSTRoutines {
1112
public void ensureExistenceOfWriter() {
12-
TCP.getContext().computeIfAbsent("writer", s -> new OutputStreamWriter(System.out));
13+
TCP.getContext().computeIfAbsent("writer", s -> new OutputStreamWriter(System.out, StandardCharsets.UTF_8));
1314
}
1415

16+
@Override
1517
public void $s(String aString) {
1618
Objects.requireNonNull(aString, "aString");
1719
writeToWriter(aString);
1820
}
1921

22+
@Override
2023
public void $v(String aName) {
2124
Objects.requireNonNull(aName, "aName");
2225
var value = (String) TCP.getContext().get(aName);
@@ -25,6 +28,8 @@ public void ensureExistenceOfWriter() {
2528
}
2629

2730
private void writeToWriter(String aString) {
28-
Sneaky.runnable(() -> ((Writer) TCP.getContext().get("writer")).write(aString)).run();
31+
if (TCP.getContext().get("writer") instanceof Writer w) {
32+
Sneaky.runnable(() -> w.write(aString)).run();
33+
}
2934
}
3035
}

jst/src/main/java/org/xcommand/template/jst/FileSystemBasedJSTProvider.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
import java.util.HashMap;
77
import java.util.List;
88
import java.util.Map;
9+
import java.util.Optional;
910

1011
public class FileSystemBasedJSTProvider implements IJSTProvider {
1112

1213
@Override
13-
public ClassMapEntry getClassMapEntry(Map aCtx, String aClassname) {
14-
return classMap.get(aClassname);
14+
public Optional<ClassMapEntry> getClassMapEntry(Map aCtx, String aClassname) {
15+
if (!classMap.containsKey(aClassname)) {
16+
return Optional.empty();
17+
}
18+
return Optional.ofNullable(classMap.get(aClassname));
1519
}
1620

1721
@Override
1822
public Map<String, ClassMapEntry> getClassMap() {
19-
return null;
23+
return Map.of();
2024
}
2125

2226
public void setGenSourceDir(String aGenSourceDir) {
@@ -28,10 +32,12 @@ public INotifier getChangeNotifier() {
2832
return changeNotifier;
2933
}
3034

31-
private List srcDirs;
35+
@SuppressWarnings("unused")
36+
private List<String> srcDirs = List.of();
3237
private final Map<String, ClassMapEntry> classMap = new HashMap<>();
3338
private final INotifier changeNotifier = new BasicNotifier();
34-
private String genSourceDir;
39+
@SuppressWarnings("unused")
40+
private String genSourceDir = "build";
3541
}
3642

3743

jst/src/main/java/org/xcommand/template/jst/FileSystemBasedJSTScanner.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.xcommand.template.jst;
22

3+
import org.jspecify.annotations.Nullable;
34
import org.xcommand.core.DynaBeanProvider;
45
import org.xcommand.core.ICommand;
56
import org.xcommand.core.IDynaBeanProvider;
@@ -94,7 +95,7 @@ private String getClassnameFromFilename(Path aSrcDir, Path aAbsolutePath) {
9495
}
9596
}
9697

97-
private String genSourceDir;
98+
private @Nullable String genSourceDir;
9899
private final INotifier changeNotifier = new BasicNotifier();
99100

100101
private final IDynaBeanProvider dbp = DynaBeanProvider.newThreadClassMethodInstance();

jst/src/main/java/org/xcommand/template/jst/FileSystemScanner.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
public final class FileSystemScanner implements ICommand, FileSystemScannerExt {
1818

19-
private FileSystemScanner(FileSystemScannerExt ext) {
20-
this.ext = ext == null ? this : ext;
19+
private FileSystemScanner(List<Path> rootPaths, FileSystemScannerExt ext) {
20+
this.rootPaths = rootPaths;
21+
this.ext = ext;
22+
}
23+
private FileSystemScanner(List<Path> rootPaths) {
24+
this.rootPaths = rootPaths;
25+
this.ext = this;
2126
}
2227

2328
private final FileSystemScannerExt ext;
@@ -27,18 +32,17 @@ private FileSystemScanner(FileSystemScannerExt ext) {
2732
private final IFileSystemScannerCV fileSystemScannerCV = dbp.newBeanForInterface(IFileSystemScannerCV.class);
2833

2934

30-
public static FileSystemScanner newInstance() {
31-
return new FileSystemScanner(null);
35+
public static FileSystemScanner newInstance(List<Path> rootPaths) {
36+
return new FileSystemScanner(rootPaths);
3237
}
33-
public static FileSystemScanner newInstance(FileSystemScannerExt relations) {
34-
return new FileSystemScanner(relations);
38+
public static FileSystemScanner newInstance(List<Path> rootPaths, FileSystemScannerExt relations) {
39+
return new FileSystemScanner(rootPaths, relations);
3540
}
3641

3742
@Override
3843
public void execute() {
3944
rootPaths.forEach(rootPath -> {
4045
fileSystemScannerCV.setRootPath(rootPath);
41-
boolean ex = Files.exists(rootPath);
4246
FilesUnchecked.walkFileTree(rootPath, new SimpleFileVisitor<>() {
4347
@Override
4448
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
@@ -59,10 +63,6 @@ public INotifier getFileFoundNotifier() {
5963
return fileFoundNotifier;
6064
}
6165

62-
public void setRootPaths(Path... aRootPaths) {
63-
setRootPaths(List.of(aRootPaths));
64-
}
65-
6666
public void setRootPaths(List<Path> aRootDirs) {
6767
rootPaths = aRootDirs;
6868
}

jst/src/main/java/org/xcommand/template/jst/FileSystemScannerExt.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.nio.file.Path;
44

5+
@FunctionalInterface
56
public interface FileSystemScannerExt {
67
void handlePath(Path path);
78
}

jst/src/main/java/org/xcommand/template/jst/IJSTProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import org.xcommand.pattern.observer.INotifier;
44

55
import java.util.Map;
6+
import java.util.Optional;
67

78
public interface IJSTProvider {
8-
ClassMapEntry getClassMapEntry(Map<String, Object> aCtx, String aClassname);
9+
Optional<ClassMapEntry> getClassMapEntry(Map<String, Object> aCtx, String aClassname);
910

1011
Map<String, ClassMapEntry> getClassMap();
1112

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.xcommand.template.jst;
22

3+
@FunctionalInterface
34
public interface IUriToClassnameMapper {
45
String getClassnameForUri(String aURI);
56
}

0 commit comments

Comments
 (0)