Skip to content

Commit 843648d

Browse files
committed
🚧 Use Record
1 parent 84fc415 commit 843648d

File tree

1 file changed

+23
-38
lines changed

1 file changed

+23
-38
lines changed

‎core-codemods/src/main/java/io/codemodder/codemods/SensitiveDataLoggingCodemod.java‎

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public CodemodFileScanningResult visit(
7373
if (analysis.isSensitiveAndDirectlyLogged()) {
7474
// remove the log statement altogether
7575
statement.get().remove();
76-
String analysisText = analysis.isSensitiveAnalysisText();
76+
String analysisText = analysis.sensitiveAnalysisText();
7777
CodemodChange change = CodemodChange.from(startLine, analysisText);
7878
changes.add(change);
7979
}
@@ -119,56 +119,22 @@ private SensitivityAndFixAnalysis performSensitivityAnalysis(
119119
* We can fix if there's only one statement on the given line (meaning, it may span multiple
120120
* lines, but only one statement is started on the line).
121121
*/
122-
private Optional<Statement> getSingleStatement(final CompilationUnit cu, final Integer line) {
122+
private static Optional<Statement> getSingleStatement(
123+
final CompilationUnit cu, final Integer line) {
123124
return cu.findAll(Statement.class).stream()
124125
.filter(s -> s.getRange().isPresent())
125126
.filter(s -> s.getRange().get().begin.line == line)
126127
.findFirst();
127128
}
128129

129-
/** The results of the sensitivity analysis and, optionally, the fix to apply. */
130-
private interface SensitivityAndFixAnalysis {
131-
132-
/**
133-
* A detailed analysis of whether the data is sensitive, like a password, security token, etc.
134-
* and its directly logged.
135-
*/
136-
String isSensitiveAnalysisText();
137-
138-
/** Whether the statement logs sensitive data. */
139-
boolean isSensitiveAndDirectlyLogged();
140-
}
141-
142-
private static class SensitivityAndFixAnalysisDTO implements SensitivityAndFixAnalysis {
143-
144-
@JsonProperty("sensitive_analysis_text")
145-
private String sensitiveAnalysisText;
146-
147-
@JsonProperty("is_data_directly_logged")
148-
private String isDataDirectlyLogged;
149-
150-
@JsonProperty("is_it_sensitive_and_directly_logged")
151-
private boolean isSensitiveAndDirectlyLogged;
152-
153-
@Override
154-
public String isSensitiveAnalysisText() {
155-
return sensitiveAnalysisText;
156-
}
157-
158-
@Override
159-
public boolean isSensitiveAndDirectlyLogged() {
160-
return isSensitiveAndDirectlyLogged;
161-
}
162-
}
163-
164130
@Override
165131
public boolean shouldRun() {
166132
List<Run> runs = sarif.rawDocument().getRuns();
167133
return runs != null && !runs.isEmpty() && !runs.get(0).getResults().isEmpty();
168134
}
169135

170136
/** Reads the source code from the given file and numbers each line. */
171-
private List<String> readNumberedLines(final Path source) throws IOException {
137+
private static List<String> readNumberedLines(final Path source) throws IOException {
172138
final var counter = new AtomicInteger();
173139
try (final var lines = Files.lines(source)) {
174140
return lines.map(line -> counter.incrementAndGet() + ": " + line).toList();
@@ -194,4 +160,23 @@ private static String snippet(final List<String> lines, final int line) {
194160
* the code snippet sent to OpenAI.
195161
*/
196162
private static final int CONTEXT = 10;
163+
164+
/** The results of the sensitivity analysis. */
165+
private interface SensitivityAndFixAnalysis {
166+
167+
/**
168+
* A detailed analysis of whether the data is sensitive, like a password, security token, etc.
169+
* and its directly logged.
170+
*/
171+
String sensitiveAnalysisText();
172+
173+
/** Whether the statement logs sensitive data. */
174+
boolean isSensitiveAndDirectlyLogged();
175+
}
176+
177+
private record SensitivityAndFixAnalysisDTO(
178+
@JsonProperty("sensitive_analysis_text") String sensitiveAnalysisText,
179+
@JsonProperty("is_data_directly_logged") String isDataDirectlyLogged,
180+
@JsonProperty("is_it_sensitive_and_directly_logged") boolean isSensitiveAndDirectlyLogged)
181+
implements SensitivityAndFixAnalysis {}
197182
}

0 commit comments

Comments
 (0)