Skip to content

Commit ae89f24

Browse files
committed
Complete nullability info in org.firebirdsql.jaybird.parser
1 parent bf0265f commit ae89f24

9 files changed

Lines changed: 53 additions & 39 deletions

File tree

src/main/org/firebirdsql/jaybird/parser/AbstractToken.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: Copyright 2021-2022 Mark Rotteveel
1+
// SPDX-FileCopyrightText: Copyright 2021-2026 Mark Rotteveel
22
// SPDX-License-Identifier: LGPL-2.1-or-later
33
package org.firebirdsql.jaybird.parser;
44

@@ -18,7 +18,7 @@ abstract class AbstractToken implements Token {
1818

1919
private final int pos;
2020
private final CharSequence src;
21-
private String cachedText;
21+
private @Nullable String cachedText;
2222

2323
AbstractToken(int pos, CharSequence src, int start, int end) {
2424
this.pos = pos;

src/main/org/firebirdsql/jaybird/parser/ObjectReferenceExtractor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
// SPDX-FileCopyrightText: Copyright 2025 Mark Rotteveel
1+
// SPDX-FileCopyrightText: Copyright 2025-2026 Mark Rotteveel
22
// SPDX-License-Identifier: LGPL-2.1-or-later
33
package org.firebirdsql.jaybird.parser;
44

55
import org.firebirdsql.jaybird.util.Identifier;
66
import org.firebirdsql.jaybird.util.ObjectReference;
7+
import org.jspecify.annotations.Nullable;
78

89
import java.util.ArrayList;
910
import java.util.List;
@@ -28,9 +29,9 @@ public final class ObjectReferenceExtractor implements TokenVisitor {
2829

2930
// pre-sizing at 3, as for current usages we expect at most 3 identifiers (i.e. <schema>.<package>.<procedure>)
3031
private final List<Identifier> identifiers = new ArrayList<>(3);
31-
private Token previousToken;
32+
private @Nullable Token previousToken;
3233
private boolean previousTokenWasScopeSpecifierValue;
33-
private UnexpectedTokenException unexpectedTokenException;
34+
private @Nullable UnexpectedTokenException unexpectedTokenException;
3435

3536
@Override
3637
public void visitToken(Token token, VisitorRegistrar visitorRegistrar) {
@@ -94,7 +95,7 @@ public void reset() {
9495
unexpectedTokenException = null;
9596
}
9697

97-
private boolean isScopeSpecifier(Token token) {
98+
private boolean isScopeSpecifier(@Nullable Token token) {
9899
return token instanceof OperatorToken operatorToken && operatorToken.charAt(0) == '%';
99100
}
100101

src/main/org/firebirdsql/jaybird/parser/ReturningClauseDetector.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// SPDX-FileCopyrightText: Copyright 2021-2024 Mark Rotteveel
1+
// SPDX-FileCopyrightText: Copyright 2021-2026 Mark Rotteveel
22
// SPDX-License-Identifier: LGPL-2.1-or-later
33
package org.firebirdsql.jaybird.parser;
44

5+
import org.jspecify.annotations.Nullable;
6+
57
import java.util.ArrayDeque;
68
import java.util.Arrays;
79
import java.util.Set;
@@ -75,7 +77,7 @@ public int length() {
7577
// An alternative would be to collect all tokens after RETURNING and see if they match the syntax of
7678
// a RETURNING clause; considered too much effort for little gain for now
7779
private int returningClauseTokenCount;
78-
private Boolean returningClauseFound;
80+
private @Nullable Boolean returningClauseFound;
7981

8082
@Override
8183
public void visitToken(Token token) {

src/main/org/firebirdsql/jaybird/parser/SqlParser.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: Copyright 2021-2023 Mark Rotteveel
1+
// SPDX-FileCopyrightText: Copyright 2021-2026 Mark Rotteveel
22
// SPDX-License-Identifier: LGPL-2.1-or-later
33
package org.firebirdsql.jaybird.parser;
44

@@ -168,33 +168,26 @@ public void removeVisitor(TokenVisitor tokenVisitor) {
168168
public static class Builder {
169169

170170
private final SqlTokenizer.Builder tokenizerBuilder;
171-
private List<TokenVisitor> visitors;
171+
private final List<TokenVisitor> visitors = new ArrayList<>();
172172

173173
private Builder(SqlTokenizer.Builder tokenizerBuilder) {
174174
this.tokenizerBuilder = tokenizerBuilder;
175175
}
176176

177177
public Builder withVisitor(TokenVisitor visitor) {
178-
if (visitors == null) {
179-
visitors = new ArrayList<>();
180-
}
181178
visitors.add(visitor);
182179
return this;
183180
}
184181

182+
@SuppressWarnings("unused")
185183
public Builder withVisitors(TokenVisitor... visitors) {
186-
if (this.visitors == null) {
187-
this.visitors = new ArrayList<>();
188-
}
189184
this.visitors.addAll(Arrays.asList(visitors));
190185
return this;
191186
}
192187

193188
public SqlParser of(String statementText) {
194189
SqlParser parser = new SqlParser(tokenizerBuilder.of(statementText));
195-
if (visitors != null) {
196-
visitors.forEach(parser::addVisitor);
197-
}
190+
visitors.forEach(parser::addVisitor);
198191
return parser;
199192
}
200193

src/main/org/firebirdsql/jaybird/parser/SqlTokenizer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// SPDX-FileCopyrightText: Copyright 2021-2025 Mark Rotteveel
1+
// SPDX-FileCopyrightText: Copyright 2021-2026 Mark Rotteveel
22
// SPDX-License-Identifier: LGPL-2.1-or-later
33
package org.firebirdsql.jaybird.parser;
44

55
import org.firebirdsql.util.InternalApi;
6+
import org.jspecify.annotations.Nullable;
67

78
import java.nio.CharBuffer;
89
import java.util.Iterator;
@@ -26,7 +27,7 @@ public final class SqlTokenizer implements Iterator<Token>, AutoCloseable {
2627
private final String src;
2728
private final ReservedWords reservedWords;
2829
private int pos = 0;
29-
private Token next;
30+
private @Nullable Token next;
3031

3132
private SqlTokenizer(String src, ReservedWords reservedWords) {
3233
this.src = src;
@@ -136,7 +137,7 @@ private int peek() {
136137
}
137138

138139
@SuppressWarnings({ "java:S1479", "java:S3776" })
139-
private Token nextToken() {
140+
private @Nullable Token nextToken() {
140141
if (isClosed()) return null;
141142
int start = pos;
142143
int c = read();

src/main/org/firebirdsql/jaybird/parser/StatementDetector.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// SPDX-FileCopyrightText: Copyright 2021-2025 Mark Rotteveel
1+
// SPDX-FileCopyrightText: Copyright 2021-2026 Mark Rotteveel
22
// SPDX-License-Identifier: LGPL-2.1-or-later
33
package org.firebirdsql.jaybird.parser;
44

55
import org.firebirdsql.util.InternalApi;
6+
import org.jspecify.annotations.Nullable;
67

78
import java.util.Map;
89
import java.util.TreeMap;
@@ -53,9 +54,9 @@ public final class StatementDetector implements TokenVisitor {
5354
private final boolean detectReturning;
5455
private LocalStatementType statementType = LocalStatementType.UNKNOWN;
5556
private ParserState parserState = ParserState.START;
56-
private Token schemaToken;
57-
private Token tableNameToken;
58-
private ReturningClauseDetector returningClauseDetector;
57+
private @Nullable Token schemaToken;
58+
private @Nullable Token tableNameToken;
59+
private @Nullable ReturningClauseDetector returningClauseDetector;
5960

6061
/**
6162
* Detect statement type and returning clause.
@@ -140,27 +141,28 @@ public LocalStatementType getStatementType() {
140141
return statementType;
141142
}
142143

143-
Token getSchemaToken() {
144+
@Nullable Token getSchemaToken() {
144145
return schemaToken;
145146
}
146147

147-
Token getTableNameToken() {
148+
@Nullable Token getTableNameToken() {
148149
return tableNameToken;
149150
}
150151

151152
private void updateStatementType(LocalStatementType statementType) {
152153
this.statementType = statementType;
153154
if (statementType == LocalStatementType.OTHER) {
154-
// clear any previously set table name
155+
// clear any previously set schema and table name
156+
setSchemaToken(null);
155157
setTableNameToken(null);
156158
}
157159
}
158160

159-
private void setSchemaToken(Token schemaToken) {
161+
private void setSchemaToken(@Nullable Token schemaToken) {
160162
this.schemaToken = schemaToken;
161163
}
162164

163-
private void setTableNameToken(Token tableNameToken) {
165+
private void setTableNameToken(@Nullable Token tableNameToken) {
164166
this.tableNameToken = tableNameToken;
165167
}
166168

src/main/org/firebirdsql/jaybird/parser/StatementIdentification.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: Copyright 2021-2025 Mark Rotteveel
1+
// SPDX-FileCopyrightText: Copyright 2021-2026 Mark Rotteveel
22
// SPDX-License-Identifier: LGPL-2.1-or-later
33
package org.firebirdsql.jaybird.parser;
44

@@ -20,6 +20,9 @@
2020
@NullMarked
2121
public final class StatementIdentification {
2222

23+
private static final StatementIdentification UNKNOWN_INSTANCE =
24+
new StatementIdentification(LocalStatementType.UNKNOWN, null, null, false);
25+
2326
private final LocalStatementType statementType;
2427
private final @Nullable String schema;
2528
private final @Nullable String tableName;
@@ -33,6 +36,13 @@ public final class StatementIdentification {
3336
this.returningClauseDetected = returningClauseDetected;
3437
}
3538

39+
/**
40+
* @return returns an instance with {@link LocalStatementType#UNKNOWN}, no schema+table, no returning clause
41+
*/
42+
public static StatementIdentification unknown() {
43+
return UNKNOWN_INSTANCE;
44+
}
45+
3646
public LocalStatementType getStatementType() {
3747
return statementType;
3848
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
// SPDX-FileCopyrightText: Copyright 2021-2022 Mark Rotteveel
1+
// SPDX-FileCopyrightText: Copyright 2021-2026 Mark Rotteveel
22
// SPDX-License-Identifier: LGPL-2.1-or-later
33
/**
44
* Statement parser for generated keys support.
55
* <p>
6-
* <b>DO NOT USE!</b> This packages is for driver-internal purposes only.
6+
* <b>DO NOT USE!</b> This package is for driver-internal purposes only.
77
* </p>
88
* <p>
99
* The parser in this package is not a full implementation of the Firebird SQL dialect. It only serves to obtain the
1010
* statement information necessary for internal purposes of Jaybird (like generated keys support).
1111
* </p>
1212
*/
1313
@InternalApi
14+
@NullMarked
1415
package org.firebirdsql.jaybird.parser;
1516

16-
import org.firebirdsql.util.InternalApi;
17+
import org.firebirdsql.util.InternalApi;
18+
import org.jspecify.annotations.NullMarked;

src/test/org/firebirdsql/jaybird/parser/SqlParserTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
// SPDX-FileCopyrightText: Copyright 2021-2022 Mark Rotteveel
1+
// SPDX-FileCopyrightText: Copyright 2021-2026 Mark Rotteveel
22
// SPDX-License-Identifier: LGPL-2.1-or-later
33
package org.firebirdsql.jaybird.parser;
44

55
import org.assertj.core.api.Assertions;
6+
import org.jspecify.annotations.NonNull;
7+
import org.jspecify.annotations.NullMarked;
68
import org.junit.jupiter.api.Test;
79
import org.junit.jupiter.api.extension.ExtendWith;
810
import org.mockito.InOrder;
@@ -102,7 +104,7 @@ void isParsingTrueDuringParsing() {
102104
final AtomicBoolean parsingValueComplete = new AtomicBoolean();
103105
TokenVisitor visitor = new AbstractTokenVisitor() {
104106
@Override
105-
protected void visitToken(Token token) {
107+
protected void visitToken(@NonNull Token token) {
106108
parsingValueVisitToken.set(parser.isParsing());
107109
}
108110

@@ -187,7 +189,7 @@ class ResumingVisitor extends AbstractTokenVisitor {
187189
Throwable throwableOnResume;
188190

189191
@Override
190-
protected void visitToken(Token token) {
192+
protected void visitToken(@NonNull Token token) {
191193
try {
192194
parser.resumeParsing();
193195
} catch (Throwable t) {
@@ -205,7 +207,8 @@ protected void visitToken(Token token) {
205207
.hasMessageContaining("in progress");
206208
}
207209

208-
private static class HaltingVisitor implements TokenVisitor {
210+
@NullMarked
211+
private static final class HaltingVisitor implements TokenVisitor {
209212

210213
private final Token stopToken;
211214

0 commit comments

Comments
 (0)