From 3ad757011966ac7c2138d9222aaf815b05fe2e92 Mon Sep 17 00:00:00 2001 From: Markus Spann Date: Sat, 22 Jun 2024 12:39:38 +0200 Subject: [PATCH] Updates for code style --- .../commands/CompositeFeedbackAction.java | 5 +---- .../net/ucanaccess/complex/ComplexBase.java | 6 ++---- .../ucanaccess/converters/SQLConverter.java | 21 ++++++++----------- .../net/ucanaccess/jdbc/AbstractExecute.java | 1 + .../triggers/TriggerAppendOnly.java | 4 ++-- .../triggers/TriggerAutoNumber.java | 4 ++-- .../net/ucanaccess/type/AccessVersion.java | 2 +- src/main/java/net/ucanaccess/util/Try.java | 8 +++---- .../ucanaccess/jdbc/ConcatNullsFalseTest.java | 1 + .../net/ucanaccess/jdbc/DataSourceTest.java | 1 + .../jdbc/ReloadPersistentMirrorTest.java | 1 + 11 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/ucanaccess/commands/CompositeFeedbackAction.java b/src/main/java/net/ucanaccess/commands/CompositeFeedbackAction.java index e54a3010..8ad86d4d 100644 --- a/src/main/java/net/ucanaccess/commands/CompositeFeedbackAction.java +++ b/src/main/java/net/ucanaccess/commands/CompositeFeedbackAction.java @@ -16,10 +16,7 @@ public void doAction(ICommand toChange) throws SQLException { } public boolean add(IFeedbackAction ifa) { - if (ifa == null) { - return false; - } - return actions.add(ifa); + return ifa != null && actions.add(ifa); } } diff --git a/src/main/java/net/ucanaccess/complex/ComplexBase.java b/src/main/java/net/ucanaccess/complex/ComplexBase.java index 78b9ee8c..2a7c2708 100644 --- a/src/main/java/net/ucanaccess/complex/ComplexBase.java +++ b/src/main/java/net/ucanaccess/complex/ComplexBase.java @@ -74,10 +74,8 @@ public boolean equals(Object obj) { } else if (!columnName.equals(other.columnName)) { return false; } - if (id != other.id) { - return false; - } - return Objects.equals(tableName, other.tableName); + return id == other.id + && Objects.equals(tableName, other.tableName); } public static Object[] convert(ComplexValueForeignKey fk) throws IOException, UcanaccessSQLException { diff --git a/src/main/java/net/ucanaccess/converters/SQLConverter.java b/src/main/java/net/ucanaccess/converters/SQLConverter.java index 213a94f8..9eeec3ce 100644 --- a/src/main/java/net/ucanaccess/converters/SQLConverter.java +++ b/src/main/java/net/ucanaccess/converters/SQLConverter.java @@ -13,7 +13,8 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; -@SuppressWarnings({"PMD.FieldDeclarationsShouldBeAtStartOfClass", "java:S1192", "java:S6353"}) +@SuppressWarnings({"PMD.FieldDeclarationsShouldBeAtStartOfClass", "PMD.UnnecessaryFullyQualifiedName", + "java:S1192", "java:S6353"}) public final class SQLConverter { @SuppressWarnings({"java:S5842", "java:S5852", "java:S5998"}) @@ -103,7 +104,7 @@ private SQLConverter() { } public static boolean hasIdentity(String sql) { - return sql.indexOf("@@") > 0 && sql.toUpperCase(java.util.Locale.US).indexOf("@@IDENTITY") > 0; + return sql.indexOf("@@") > 0 && sql.toUpperCase(Locale.US).indexOf("@@IDENTITY") > 0; } private static void aliases(String sql, NormalizedSQL nsql) { @@ -128,7 +129,7 @@ private static void aliases(String sql, NormalizedSQL nsql) { StringBuilder sb = new StringBuilder(); for (char c : sqlc) { if (c == ' ' || c == '\n' || c == '\r' || c == ',') { - String key = SQLConverter.preEscapingIdentifier(sb.toString()); + String key = preEscapingIdentifier(sb.toString()); nsql.put(key, sb.toString()); break; } else { @@ -343,8 +344,7 @@ private static String replaceBacktick(String sql) { private static String replaceAposNames(String sql) { for (String an : APOSTROPHISED_NAMES) { - sql = sql.replaceAll("(?i)" + Pattern.quote('[' + an + ']'), - '[' + SQLConverter.basicEscapingIdentifier(an) + ']'); + sql = sql.replaceAll("(?i)" + Pattern.quote('[' + an + ']'), '[' + basicEscapingIdentifier(an) + ']'); } return sql; } @@ -421,7 +421,7 @@ private static String convertQuotedAliases(String sql, NormalizedSQL nsql) { hs.add(g2); } String value = g2.substring(1, g2.length() - 1); - nsql.put(SQLConverter.preEscapingIdentifier(value), value); + nsql.put(preEscapingIdentifier(value), value); sqlN += sqle.substring(0, m.start()) + m.group(1) + g2.replaceAll("['\"]", "") + m.group(3); sqle = sqle.substring(m.end()); } @@ -813,10 +813,7 @@ public static String convertCreateTable(String sql) throws SQLException { } public static boolean checkDDL(String sql) { - if (sql == null) { - return false; - } - return Patterns.CHECK_DDL.matcher(sql.replaceAll("\\s+", " ")).matches(); + return sql != null && Patterns.CHECK_DDL.matcher(sql.replaceAll("\\s+", " ")).matches(); } private static String convertLike(String sql) { @@ -866,7 +863,7 @@ public static boolean isSupportsAccessLike() { } public static void setSupportsAccessLike(boolean _supportsAccessLike) { - SQLConverter.supportsAccessLike = _supportsAccessLike; + supportsAccessLike = _supportsAccessLike; } public static boolean isXescaped(String identifier) { @@ -1075,7 +1072,7 @@ static boolean isDualUsedAsTableName() { } static void setDualUsedAsTableName(boolean _dualUsedAsTableName) { - SQLConverter.dualUsedAsTableName = _dualUsedAsTableName; + dualUsedAsTableName = _dualUsedAsTableName; } public static String removeParameters(String qtxt) { diff --git a/src/main/java/net/ucanaccess/jdbc/AbstractExecute.java b/src/main/java/net/ucanaccess/jdbc/AbstractExecute.java index 4e5b806c..08584eaf 100644 --- a/src/main/java/net/ucanaccess/jdbc/AbstractExecute.java +++ b/src/main/java/net/ucanaccess/jdbc/AbstractExecute.java @@ -82,6 +82,7 @@ private int count(String tableName) throws SQLException { } } + @SuppressWarnings("PMD.UnusedLocalVariable") private SQLException checkDdlException() { UcanaccessConnection conn = statement.getConnection(); try (PreparedStatement ps = conn.getHSQLDBConnection().prepareStatement(SQLConverter.convertSQL(sql).getSql())) { diff --git a/src/main/java/net/ucanaccess/triggers/TriggerAppendOnly.java b/src/main/java/net/ucanaccess/triggers/TriggerAppendOnly.java index 65649029..cddbf4b0 100644 --- a/src/main/java/net/ucanaccess/triggers/TriggerAppendOnly.java +++ b/src/main/java/net/ucanaccess/triggers/TriggerAppendOnly.java @@ -30,9 +30,9 @@ public void fire(int type, String name, String tableName, Object[] oldR, Object[ ColumnImpl verCol = (ColumnImpl) col.getVersionHistoryColumn(); LocalDateTime upTime = LocalDateTime.now(); String val = newR[i] == null ? null : newR[i].toString(); - if (type == org.hsqldb.trigger.Trigger.INSERT_BEFORE_ROW) { + if (INSERT_BEFORE_ROW == type) { newR[verCol.getColumnNumber()] = new JavaObjectData(new Version[] {new Version(val, upTime)}); - } else if (type == org.hsqldb.trigger.Trigger.UPDATE_BEFORE_ROW && (oldR[i] != null || newR[i] != null)) { + } else if (UPDATE_BEFORE_ROW == type && (oldR[i] != null || newR[i] != null)) { if (oldR[i] == null && newR[i] != null || oldR[i] != null && newR[i] == null || !oldR[i].equals(newR[i])) { Version[] oldV = (Version[]) ((JavaObjectData) oldR[verCol.getColumnNumber()]).getObject(); diff --git a/src/main/java/net/ucanaccess/triggers/TriggerAutoNumber.java b/src/main/java/net/ucanaccess/triggers/TriggerAutoNumber.java index 59e1d8d2..3605b4ee 100644 --- a/src/main/java/net/ucanaccess/triggers/TriggerAutoNumber.java +++ b/src/main/java/net/ucanaccess/triggers/TriggerAutoNumber.java @@ -43,7 +43,7 @@ public void fire(int type, String name, String tableName, Object[] oldR, Object[ } else if (cl.isAutoNumber()) { - if (type == org.hsqldb.trigger.Trigger.INSERT_BEFORE_ROW) { + if (INSERT_BEFORE_ROW == type) { if (t.isAllowAutoNumberInsert()) { if (cl.getAutoNumberGenerator().getType().equals(DataType.LONG) && newR[i] != null) { @@ -62,7 +62,7 @@ public void fire(int type, String name, String tableName, Object[] oldR, Object[ conn.setGeneratedKey(newR[i]); } } - } else if (type == org.hsqldb.trigger.Trigger.UPDATE_BEFORE_ROW + } else if (UPDATE_BEFORE_ROW == type && cl.getAutoNumberGenerator().getType().equals(DataType.LONG)) { if (!oldR[i].equals(newR[i])) { throw new UcanaccessRuntimeException("Cannot update autoincrement column"); diff --git a/src/main/java/net/ucanaccess/type/AccessVersion.java b/src/main/java/net/ucanaccess/type/AccessVersion.java index 25fe2c88..cde3c78b 100644 --- a/src/main/java/net/ucanaccess/type/AccessVersion.java +++ b/src/main/java/net/ucanaccess/type/AccessVersion.java @@ -13,7 +13,7 @@ public enum AccessVersion { V2010(FileFormat.V2010), V2016(FileFormat.V2016); - private static final AccessVersion DEFAULT_ACCESS_VERSION = AccessVersion.V2003; + private static final AccessVersion DEFAULT_ACCESS_VERSION = V2003; private final FileFormat fileFormat; diff --git a/src/main/java/net/ucanaccess/util/Try.java b/src/main/java/net/ucanaccess/util/Try.java index e63a1d40..b2ba54b3 100644 --- a/src/main/java/net/ucanaccess/util/Try.java +++ b/src/main/java/net/ucanaccess/util/Try.java @@ -186,7 +186,7 @@ public Try map(Function((V1) null, e1); } - return Try.catching((IThrowingSupplier) () -> _mapper.apply(val)); + return catching((IThrowingSupplier) () -> _mapper.apply(val)); } /** @@ -235,7 +235,7 @@ public V orElse(V _other) { */ public void orElse(IThrowingConsumer _consumer) { if (hasThrown()) { - Try.catching(() -> _consumer.accept(t)).orThrow(); + catching(() -> _consumer.accept(t)).orThrow(); } } @@ -247,7 +247,7 @@ public void orElse(IThrowingConsumer _consumer) { */ public V orElseApply(IThrowingFunction _function) { if (hasThrown()) { - return Try.catching(() -> _function.apply(t)).orThrow(); + return catching(() -> _function.apply(t)).orThrow(); } return val; } @@ -259,7 +259,7 @@ public V orElseApply(IThrowingFunction _function) { */ public V orElseGet(IThrowingSupplier _supplier) { if (hasThrown()) { - return Try.catching(_supplier::get).orThrow(); + return catching(_supplier::get).orThrow(); } return val; } diff --git a/src/test/java/net/ucanaccess/jdbc/ConcatNullsFalseTest.java b/src/test/java/net/ucanaccess/jdbc/ConcatNullsFalseTest.java index d9e78419..5f4fd597 100644 --- a/src/test/java/net/ucanaccess/jdbc/ConcatNullsFalseTest.java +++ b/src/test/java/net/ucanaccess/jdbc/ConcatNullsFalseTest.java @@ -7,6 +7,7 @@ class ConcatNullsFalseTest extends UcanaccessBaseTest { + @Override protected UcanaccessConnectionBuilder buildConnection() { return super.buildConnection().withConcatNulls(false); } diff --git a/src/test/java/net/ucanaccess/jdbc/DataSourceTest.java b/src/test/java/net/ucanaccess/jdbc/DataSourceTest.java index 767b03c5..3d777fdc 100644 --- a/src/test/java/net/ucanaccess/jdbc/DataSourceTest.java +++ b/src/test/java/net/ucanaccess/jdbc/DataSourceTest.java @@ -50,6 +50,7 @@ void setLobScaleGood() { } @Test + @SuppressWarnings("PMD.UnusedLocalVariable") void createNewDatabase() throws SQLException { File fileMdb = createTempFileName("ucaDataSourceTest", ".mdb"); assertThat(fileMdb).doesNotExist(); diff --git a/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java b/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java index 51d76593..6b88e3fe 100644 --- a/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java +++ b/src/test/java/net/ucanaccess/jdbc/ReloadPersistentMirrorTest.java @@ -14,6 +14,7 @@ class ReloadPersistentMirrorTest extends UcanaccessBaseTest { @ParameterizedTest(name = "[{index}] {0}") @AccessVersionSource + @SuppressWarnings("PMD.UnusedLocalVariable") void testReloadMirror(AccessVersion _accessVersion) throws Exception { init(_accessVersion);