Skip to content

Commit

Permalink
Various code cleanup/fixes as suggested by Sonarlint
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Jan 6, 2024
1 parent f5a063b commit eeee9f2
Show file tree
Hide file tree
Showing 121 changed files with 819 additions and 724 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.EnumMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.function.IntFunction;

import static java.lang.String.format;
import static org.firebirdsql.internal.tools.MessageConverter.toJaybirdMessageFormat;
Expand All @@ -36,13 +36,11 @@
*/
class PerFacilityStore extends MessageStore {

@SuppressWarnings("unused")
private static final int MAX_FACILITY = 25; // NOTE: This is Firebird max, Jaybird has facility 26
static final int FACILITY_SIZE = MAX_FACILITY + 1;

private final Map<Facility, Map<Integer, String>> messagesPerFacility =
new HashMap<>((int) (FACILITY_SIZE / 0.75f));
private final Map<Facility, Map<Integer, String>> sqlStatesPerFacility =
new HashMap<>((int) (FACILITY_SIZE / 0.75f));
private final Map<Facility, Map<Integer, String>> messagesPerFacility = new EnumMap<>(Facility.class);
private final Map<Facility, Map<Integer, String>> sqlStatesPerFacility = new EnumMap<>(Facility.class);
private final SymbolStore symbolStore = new SymbolStore();

@Override
Expand Down Expand Up @@ -79,8 +77,8 @@ public void save() throws IOException {
symbolStore.save();
}

private void save(Map<Facility, Map<Integer, String>> facilityMap,
Function<Integer, String> filenameGenerator) throws IOException {
private void save(Map<Facility, Map<Integer, String>> facilityMap, IntFunction<String> filenameGenerator)
throws IOException {
for (Map.Entry<Facility, Map<Integer, String>> facilityEntry : facilityMap.entrySet()) {
Map<Integer, String> facilityData = facilityEntry.getValue();
if (facilityData.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ private List<String> findLibrariesToTry() {
if (optionalFbEmbeddedInstance.isPresent()) {
FirebirdEmbeddedLibrary firebirdEmbeddedLibrary = optionalFbEmbeddedInstance.get();
log.log(INFO, "Found Firebird Embedded {0} on classpath", firebirdEmbeddedLibrary.getVersion());
if (firebirdEmbeddedLibrary instanceof DisposableFirebirdEmbeddedLibrary) {
NativeResourceTracker.strongRegisterNativeResource(new FirebirdEmbeddedLibraryNativeResource(
(DisposableFirebirdEmbeddedLibrary) firebirdEmbeddedLibrary));
if (firebirdEmbeddedLibrary instanceof DisposableFirebirdEmbeddedLibrary disposableLibrary) {
NativeResourceTracker.strongRegisterNativeResource(
new FirebirdEmbeddedLibraryNativeResource(disposableLibrary));
}

Path entryPointPath = firebirdEmbeddedLibrary.getEntryPointPath().toAbsolutePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
public abstract class JnaConnection<T extends IAttachProperties<T>, C extends JnaAttachment>
extends AbstractConnection<T, C> {

private static final boolean bigEndian = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
private static final boolean BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;

private final FbClientLibrary clientLibrary;
private final String attachUrl;
Expand Down Expand Up @@ -152,16 +152,16 @@ protected void processStatusVector(ISC_STATUS[] statusVector, WarningMessageCall

if (!builder.isEmpty()) {
SQLException exception = builder.toFlatSQLException();
if (exception instanceof SQLWarning) {
warningMessageCallback.processWarning((SQLWarning) exception);
if (exception instanceof SQLWarning warning) {
warningMessageCallback.processWarning(warning);
} else {
throw exception;
}
}
}

final DatatypeCoder createDatatypeCoder() {
if (bigEndian) {
if (BIG_ENDIAN) {
return BigEndianDatatypeCoder.forEncodingFactory(getEncodingFactory());
}
return LittleEndianDatatypeCoder.forEncodingFactory(getEncodingFactory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ public class JnaDatabase extends AbstractFbDatabase<JnaDatabaseConnection>
public JnaDatabase(JnaDatabaseConnection connection) {
super(connection, connection.createDatatypeCoder());
clientLibrary = connection.getClientLibrary();
if (clientLibrary instanceof FbClientFeatureAccess) {
clientFeatures = ((FbClientFeatureAccess) clientLibrary).getFeatures();
} else {
clientFeatures = emptySet();
}
clientFeatures = clientLibrary instanceof FbClientFeatureAccess featureAccess
? featureAccess.getFeatures()
: emptySet();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,9 @@ public static void shutdownNativeResources() {
}

static void disableShutdownHook() {
Object currentShutdownThread = shutdownThread.getAndSet(new Object());
if (currentShutdownThread instanceof Thread) {
if (shutdownThread.getAndSet(new Object()) instanceof Thread thread) {
try {
Runtime.getRuntime().removeShutdownHook((Thread) currentShutdownThread);
Runtime.getRuntime().removeShutdownHook(thread);
} catch (IllegalStateException e) {
// ignore
} catch (SecurityException e) {
Expand Down Expand Up @@ -182,7 +181,7 @@ public void run() {
/**
* A native resource that can be registered with {@link NativeResourceTracker}.
*/
static abstract class NativeResource {
abstract static class NativeResource {
/**
* Dispose method to clean up the native resource.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@
* @author Mark Rotteveel
* @since 5
*/
public class FirebirdEmbeddedLookup {
public final class FirebirdEmbeddedLookup {

private static final System.Logger log = System.getLogger(FirebirdEmbeddedLookup.class.getName());

private FirebirdEmbeddedLookup() {
// no instances
}

/**
* Tries to find a Firebird Embedded library service provider for the current platform and install it.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,14 @@ public void setUseCatalogAsPackage(boolean useCatalogAsPackage) {
}

@SuppressWarnings("deprecation")
@Deprecated
@Deprecated(since = "5")
@Override
public String getUserName() {
return FirebirdConnectionProperties.super.getUserName();
}

@SuppressWarnings("deprecation")
@Deprecated
@Deprecated(since = "5")
@Override
public void setUserName(String userName) {
FirebirdConnectionProperties.super.setUserName(userName);
Expand Down
14 changes: 7 additions & 7 deletions src/main/org/firebirdsql/ds/StatementHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ public boolean isClosed() {
}

// Statement methods
private final static Method STATEMENT_IS_CLOSED = findMethod(Statement.class, "isClosed", new Class[0]);
private final static Method FIREBIRD_STATEMENT_IS_CLOSED = findMethod(FirebirdStatement.class, "isClosed",
private static final Method STATEMENT_IS_CLOSED = findMethod(Statement.class, "isClosed", new Class[0]);
private static final Method FIREBIRD_STATEMENT_IS_CLOSED = findMethod(FirebirdStatement.class, "isClosed",
new Class[0]);
private final static Method STATEMENT_CLOSE = findMethod(Statement.class, "close", new Class[0]);
private final static Method GET_CONNECTION = findMethod(Statement.class, "getConnection", new Class[0]);
private static final Method STATEMENT_CLOSE = findMethod(Statement.class, "close", new Class[0]);
private static final Method GET_CONNECTION = findMethod(Statement.class, "getConnection", new Class[0]);

// Object Methods
private final static Method TO_STRING = findMethod(Object.class, "toString", new Class[0]);
private final static Method EQUALS = findMethod(Object.class, "equals", new Class[] { Object.class });
private final static Method HASH_CODE = findMethod(Object.class, "hashCode", new Class[0]);
private static final Method TO_STRING = findMethod(Object.class, "toString", new Class[0]);
private static final Method EQUALS = findMethod(Object.class, "equals", new Class[] { Object.class });
private static final Method HASH_CODE = findMethod(Object.class, "hashCode", new Class[0]);
}
3 changes: 2 additions & 1 deletion src/main/org/firebirdsql/ds/XAConnectionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ final class XAConnectionHandler extends PooledConnectionHandler {
super(connection, owner);
xaOwner = owner;
}


@Override
boolean isRollbackAllowed() throws SQLException {
return !(xaOwner.inDistributedTransaction() || connection.getAutoCommit());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/org/firebirdsql/encodings/EncodingFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static class DefaultEncodingFactory {
}
}

// Linked HashMap because we need to preserve insertion order for the lazy initialization;
// Linked HashMap because we need to preserve insertion order for the lazy initialization.
// Initial capacity of 128 should prevent resizing during loading (if only DefaultEncodingSet is present)
private final Map<String, EncodingDefinition> firebirdEncodingToDefinition = new LinkedHashMap<>(128);
private final EncodingDefinition[] firebirdCharacterSetIdToDefinition =
Expand Down
1 change: 1 addition & 0 deletions src/main/org/firebirdsql/event/FBEventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ private void terminateDispatcher() throws SQLException {
try {
dispatchThread.join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new SQLException(ex);
} finally {
this.eventDispatcher = null;
Expand Down
2 changes: 0 additions & 2 deletions src/main/org/firebirdsql/gds/impl/GDSFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
import org.firebirdsql.jaybird.util.PluginLoader;
import org.firebirdsql.util.InternalApi;

import java.io.Serial;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.*;
import java.util.Map.Entry;
Expand Down
4 changes: 2 additions & 2 deletions src/main/org/firebirdsql/gds/impl/GDSServerVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public String getFullVersion() {
* @return Protocol version of the connection, or {@code -1} if this information is not available.
*/
public int getProtocolVersion() {
// We assume the protocol information is in the second version string;
// We assume the protocol information is in the second version string,
// this assumption may be wrong for multi-hop connections
if (rawVersions.length == 1 || rawVersions[1] == null) return -1;
Matcher connectionMetadataMatcher = CONNECTION_METADATA_PATTERN.matcher(rawVersions[1]);
Expand All @@ -181,7 +181,7 @@ public boolean isWireCompressionUsed() {
}

private String getConnectionOptions() {
// We assume the protocol information is in the second version string;
// We assume the protocol information is in the second version string,
// this assumption may be wrong for multi-hop connections
if (rawVersions.length == 1 || rawVersions[1] == null) return "";
Matcher connectionMetadataMatcher = CONNECTION_METADATA_PATTERN.matcher(rawVersions[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public int read() throws IOException {
} while (i == 0);
if (i == -1) return -1;
}
return (int) outBuf[outPos++] & 0xFF;
return outBuf[outPos++] & 0xFF;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/org/firebirdsql/gds/impl/wire/XdrInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ public void setCipher(Cipher cipher) throws IOException {
throw new IOException("Input stream already encrypted");
}
InputStream currentStream = in;
if (currentStream instanceof EncryptedStreamSupport) {
((EncryptedStreamSupport) currentStream).setCipher(cipher);
if (currentStream instanceof EncryptedStreamSupport encryptedStreamSupport) {
encryptedStreamSupport.setCipher(cipher);
} else {
in = new FbCipherInputStream(currentStream, cipher);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/org/firebirdsql/gds/impl/wire/XdrOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ public void setCipher(Cipher cipher) throws IOException {
}
flush();
OutputStream currentStream = out;
if (currentStream instanceof EncryptedStreamSupport) {
((EncryptedStreamSupport) currentStream).setCipher(cipher);
if (currentStream instanceof EncryptedStreamSupport encryptedStreamSupport) {
encryptedStreamSupport.setCipher(cipher);
} else {
out = new CipherOutputStream(currentStream, cipher);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/org/firebirdsql/gds/ng/AbstractFbDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public final void transactionStateChanged(FbTransaction transaction, Transaction
This is acceptable as commit/rollback failure should be limited to situations were the database
is either inaccessible and the transaction is likely already rolled back or pending rollback by
the server, or the transaction was already committed or rolled back.
*/
*/
// TODO "register" transaction as pendingEnd for debugging?
case PREPARED:
try (LockCloseable ignored = withLock()) {
Expand Down
9 changes: 2 additions & 7 deletions src/main/org/firebirdsql/gds/ng/AbstractFbStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.sql.SQLWarning;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
Expand Down Expand Up @@ -56,13 +55,9 @@ public abstract class AbstractFbStatement implements FbStatement {
private static final int IN_CURSOR = 0;
private static final int AFTER_LAST = 1;

private final WarningMessageCallback warningCallback = new WarningMessageCallback() {
@Override
public void processWarning(SQLWarning warning) {
statementListenerDispatcher.warningReceived(AbstractFbStatement.this, warning);
}
};
protected final StatementListenerDispatcher statementListenerDispatcher = new StatementListenerDispatcher();
private final WarningMessageCallback warningCallback = warning ->
statementListenerDispatcher.warningReceived(AbstractFbStatement.this, warning);
protected final ExceptionListenerDispatcher exceptionListenerDispatcher = new ExceptionListenerDispatcher(this);
private volatile int cursorPosition = BEFORE_FIRST;
// Indicates whether at least one fetch was done for the current cursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected void populateDefaultProperties(final S connection, final ServiceParame
if (props.getRoleName() != null) {
spb.addArgument(isc_spb_sql_role_name, props.getRoleName());
}
if (props.getConnectTimeout() != IConnectionProperties.DEFAULT_CONNECT_TIMEOUT) {
if (props.getConnectTimeout() != IAttachProperties.DEFAULT_CONNECT_TIMEOUT) {
spb.addArgument(isc_spb_connect_timeout, props.getConnectTimeout());
}
}
Expand Down
1 change: 0 additions & 1 deletion src/main/org/firebirdsql/gds/ng/DatatypeCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Calendar;

/**
* Interface defining the encoding and decoding for Firebird (numerical) data types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof EncodingSpecificDatatypeCoder) {
EncodingSpecificDatatypeCoder other = (EncodingSpecificDatatypeCoder) o;
if (o instanceof EncodingSpecificDatatypeCoder other) {
return encodingDefinition.equals(other.encodingDefinition)
&& parentCoder.getClass() == other.parentCoder.getClass();
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/org/firebirdsql/gds/ng/FbBatchConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ default void populateBatchParameterBuffer(BatchParameterBuffer batchPb) {
if (batchBufferSize() >= 0) {
batchPb.addArgument(TAG_BUFFER_BYTES_SIZE, batchBufferSize());
}
// Doesn't seem to make a difference in current implementation;
// in practice we register existing blobs under their own id (see registerBlobs in V16Statement)
// Doesn't seem to make a difference in current implementation, in practice we register existing blobs under
// their own id (see registerBlobs in V16Statement)
// TODO Might need to change when implementing batching of blobs
batchPb.addArgument(TAG_BLOB_POLICY, BLOB_ID_USER);
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/org/firebirdsql/gds/ng/ServerVersionInformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public static ServerVersionInformation getForVersion(GDSServerVersion serverVers
return getForVersion(serverVersion.getMajorVersion(), serverVersion.getMinorVersion());
}

private static class Constants {
public static final byte[] V1_0_STATEMENT_INFO = new byte[] {
private static final class Constants {
static final byte[] V1_0_STATEMENT_INFO = new byte[] {
isc_info_sql_stmt_type,
isc_info_sql_select,
isc_info_sql_describe_vars,
Expand All @@ -165,7 +165,7 @@ private static class Constants {
//isc_info_sql_owner,
isc_info_sql_describe_end
};
public static final byte[] V_1_0_PARAMETER_INFO = new byte[] {
static final byte[] V_1_0_PARAMETER_INFO = new byte[] {
isc_info_sql_describe_vars,
isc_info_sql_sqlda_seq,
isc_info_sql_type, isc_info_sql_sub_type,
Expand All @@ -176,7 +176,7 @@ private static class Constants {
isc_info_sql_owner,
isc_info_sql_describe_end
};
public static final byte[] V_2_0_STATEMENT_INFO = new byte[] {
static final byte[] V_2_0_STATEMENT_INFO = new byte[] {
isc_info_sql_stmt_type,
isc_info_sql_select,
isc_info_sql_describe_vars,
Expand All @@ -203,7 +203,7 @@ private static class Constants {
//isc_info_sql_owner,
isc_info_sql_describe_end
};
public static final byte[] V_2_0_PARAMETER_INFO = new byte[] {
static final byte[] V_2_0_PARAMETER_INFO = new byte[] {
isc_info_sql_describe_vars,
isc_info_sql_sqlda_seq,
isc_info_sql_type, isc_info_sql_sub_type,
Expand All @@ -215,5 +215,9 @@ private static class Constants {
isc_info_sql_owner,
isc_info_sql_describe_end
};

private Constants() {
// no instances
}
}
}
4 changes: 2 additions & 2 deletions src/main/org/firebirdsql/gds/ng/tz/TimeZoneDatatypeCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private OffsetTime decodeTimeTzToOffsetTime(byte[] timeTzBytes) {
.withOffsetSameInstant((ZoneOffset) zoneId);
}

// We need to base on a date to determine value, we use the 2020-01-01 date;
// We need to base on a date to determine value, we use the 2020-01-01 date,
// this aligns closest with Firebird behaviour

return ZonedDateTime.of(TIME_TZ_BASE_DATE, utcTime, ZoneOffset.UTC)
Expand Down Expand Up @@ -225,7 +225,7 @@ private ZonedDateTime decodeTimeTzToZonedDateTime(byte[] timeTzBytes) {
}

private ZonedDateTime decodeTimeTzToZonedDateTime(LocalTime utcTime, ZoneId zoneId) {
// We need to base on a date to determine value, we use the 2020-01-01 date;
// We need to base on a date to determine value, we use the 2020-01-01 date,
// this aligns closest with Firebird behaviour

ZonedDateTime timeAtBaseDate = ZonedDateTime.of(TIME_TZ_BASE_DATE, utcTime, ZoneOffset.UTC)
Expand Down
Loading

0 comments on commit eeee9f2

Please sign in to comment.