From 0ae7fc7c2184b25e4a146ede0274a45a165b7618 Mon Sep 17 00:00:00 2001 From: jamesrdi Date: Tue, 19 Dec 2023 11:11:26 +0100 Subject: [PATCH] Closes #2459: Fix Code Smells --- .../sampledata/SampleDataGenerator.java | 5 +-- .../security/CurrentUserContextImpl.java | 3 +- .../common/test/security/JaasExtension.java | 32 +++++++++++-------- .../configuration/DbSchemaCreator.java | 6 ++-- .../configuration/parser/SimpleParser.java | 3 +- pom.xml | 6 ++-- 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/common/taskana-common-data/src/main/java/pro/taskana/sampledata/SampleDataGenerator.java b/common/taskana-common-data/src/main/java/pro/taskana/sampledata/SampleDataGenerator.java index 46edfaa4ac..cde3aabf44 100644 --- a/common/taskana-common-data/src/main/java/pro/taskana/sampledata/SampleDataGenerator.java +++ b/common/taskana-common-data/src/main/java/pro/taskana/sampledata/SampleDataGenerator.java @@ -13,7 +13,6 @@ import java.util.HashMap; import java.util.List; import java.util.function.Consumer; -import java.util.stream.Collectors; import java.util.stream.Stream; import javax.sql.DataSource; import org.apache.ibatis.jdbc.RuntimeSqlException; @@ -78,9 +77,7 @@ public void dropDb() { private List parseScripts(Stream scripts) { try (Connection connection = dataSource.getConnection()) { DB db = DB.getDB(connection); - return scripts - .map(script -> SqlReplacer.getScriptAsSql(db, now, script)) - .collect(Collectors.toList()); + return scripts.map(script -> SqlReplacer.getScriptAsSql(db, now, script)).toList(); } catch (SQLException e) { throw new RuntimeSqlException("Connection to database failed.", e); } diff --git a/common/taskana-common-security/src/main/java/pro/taskana/common/internal/security/CurrentUserContextImpl.java b/common/taskana-common-security/src/main/java/pro/taskana/common/internal/security/CurrentUserContextImpl.java index d6954f3d86..154e0249bb 100644 --- a/common/taskana-common-security/src/main/java/pro/taskana/common/internal/security/CurrentUserContextImpl.java +++ b/common/taskana-common-security/src/main/java/pro/taskana/common/internal/security/CurrentUserContextImpl.java @@ -10,7 +10,6 @@ import java.util.List; import java.util.Objects; import java.util.Set; -import java.util.stream.Collectors; import javax.security.auth.Subject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -61,7 +60,7 @@ public List getGroupIds() { .map(Principal::getName) .filter(Objects::nonNull) .map(this::convertAccessId) - .collect(Collectors.toList()); + .toList(); } LOGGER.trace("No groupIds found in subject!"); return Collections.emptyList(); diff --git a/common/taskana-common-test/src/main/java/pro/taskana/common/test/security/JaasExtension.java b/common/taskana-common-test/src/main/java/pro/taskana/common/test/security/JaasExtension.java index d5cb35f01d..0bd5ca6266 100644 --- a/common/taskana-common-test/src/main/java/pro/taskana/common/test/security/JaasExtension.java +++ b/common/taskana-common-test/src/main/java/pro/taskana/common/test/security/JaasExtension.java @@ -107,17 +107,17 @@ public T interceptTestFactoryMethod( Iterable newChildrenForDynamicContainer; // TestFactory must have one of the following return types. See link above for further details - if (factoryResult instanceof DynamicNode) { - newChildrenForDynamicContainer = Collections.singleton((DynamicNode) factoryResult); + if (factoryResult instanceof DynamicNode dynamicNode) { + newChildrenForDynamicContainer = Collections.singleton(dynamicNode); } else if (factoryResult instanceof Stream) { Stream nodes = (Stream) factoryResult; - newChildrenForDynamicContainer = nodes.collect(Collectors.toList()); + newChildrenForDynamicContainer = nodes.toList(); } else if (factoryResult instanceof Iterable) { newChildrenForDynamicContainer = (Iterable) factoryResult; } else if (factoryResult instanceof Iterator) { newChildrenForDynamicContainer = () -> (Iterator) factoryResult; - } else if (factoryResult instanceof DynamicNode[]) { - newChildrenForDynamicContainer = Arrays.asList((DynamicNode[]) factoryResult); + } else if (factoryResult instanceof DynamicNode[] dynamicNodes) { + newChildrenForDynamicContainer = Arrays.asList(dynamicNodes); } else { throw new SystemException( String.format( @@ -142,8 +142,11 @@ public T interceptTestFactoryMethod( Store store = getMethodLevelStore(extensionContext); return (T) Stream.of(annotation.value()) - .peek(a -> store.put(ACCESS_IDS_STORE_KEY, a)) - .map(wrapTestsInDynamicContainer); + .map( + a -> { + store.put(ACCESS_IDS_STORE_KEY, a); + return wrapTestsInDynamicContainer.apply(a); + }); } return extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable()); @@ -207,8 +210,11 @@ public Stream provideTestTemplateInvocationContex AnnotationSupport.findRepeatableAnnotations(context.getElement(), WithAccessId.class); Store store = getMethodLevelStore(context); return accessIds.stream() - .peek(a -> store.put(ACCESS_IDS_STORE_KEY, a)) - .map(JaasExtensionInvocationContext::new); + .map( + a -> { + store.put(ACCESS_IDS_STORE_KEY, a); + return new JaasExtensionInvocationContext(a); + }); } // endregion @@ -217,8 +223,7 @@ private static void persistDynamicContainerChildren( Iterable nodes, Map> childrenMap) { nodes.forEach( node -> { - if (node instanceof DynamicContainer) { - DynamicContainer container = (DynamicContainer) node; + if (node instanceof DynamicContainer container) { List children = container.getChildren().collect(Collectors.toList()); childrenMap.put(container.hashCode() + container.getDisplayName(), children); persistDynamicContainerChildren(children, childrenMap); @@ -228,8 +233,7 @@ private static void persistDynamicContainerChildren( private static DynamicNode duplicateDynamicNode( DynamicNode node, Map> lookupMap) { - if (node instanceof DynamicContainer) { - DynamicContainer container = (DynamicContainer) node; + if (node instanceof DynamicContainer container) { Stream children = lookupMap.get(node.hashCode() + node.getDisplayName()).stream() .map(x -> duplicateDynamicNode(x, lookupMap)); @@ -259,7 +263,7 @@ private static List getPrincipals(WithAccessId withAccessId) { return Stream.concat( Stream.of(withAccessId.user()).map(UserPrincipal::new), Arrays.stream(withAccessId.groups()).map(GroupPrincipal::new)) - .collect(Collectors.toList()); + .toList(); } return Collections.emptyList(); } diff --git a/common/taskana-common/src/main/java/pro/taskana/common/internal/configuration/DbSchemaCreator.java b/common/taskana-common/src/main/java/pro/taskana/common/internal/configuration/DbSchemaCreator.java index e79c231693..d7031432b2 100644 --- a/common/taskana-common/src/main/java/pro/taskana/common/internal/configuration/DbSchemaCreator.java +++ b/common/taskana-common/src/main/java/pro/taskana/common/internal/configuration/DbSchemaCreator.java @@ -64,9 +64,9 @@ public boolean run() throws SQLException { } if (LOGGER.isDebugEnabled()) { LOGGER.debug(outWriter.toString()); - } - if (!errorWriter.toString().trim().isEmpty()) { - LOGGER.error(errorWriter.toString()); + if (!errorWriter.toString().trim().isEmpty()) { + LOGGER.error(errorWriter.toString()); + } } return false; } diff --git a/common/taskana-common/src/main/java/pro/taskana/common/internal/configuration/parser/SimpleParser.java b/common/taskana-common/src/main/java/pro/taskana/common/internal/configuration/parser/SimpleParser.java index d24a98360f..dcdef8bc0d 100644 --- a/common/taskana-common/src/main/java/pro/taskana/common/internal/configuration/parser/SimpleParser.java +++ b/common/taskana-common/src/main/java/pro/taskana/common/internal/configuration/parser/SimpleParser.java @@ -8,7 +8,6 @@ import java.util.Optional; import java.util.function.Function; import java.util.regex.Pattern; -import java.util.stream.Collectors; import pro.taskana.common.api.exceptions.SystemException; import pro.taskana.common.internal.util.ReflectionUtil; @@ -43,6 +42,6 @@ protected static List splitStringAndTrimElements(String str, String sepa return Arrays.stream(str.split(Pattern.quote(separator))) .filter(not(String::isEmpty)) .map(String::trim) - .collect(Collectors.toList()); + .toList(); } } diff --git a/pom.xml b/pom.xml index 2c6bba3fec..4ec7134d6b 100644 --- a/pom.xml +++ b/pom.xml @@ -120,9 +120,7 @@ src/main/java src/test/java - org/camunda/bpm/dmn/**/*, - **/example/**/*, - **/*Example* + org/camunda/bpm/dmn/**/*, **/example/**/*, **/*Example* @@ -305,6 +303,8 @@ maven-compiler-plugin ${version.maven.compiler} + 17 + 17 true true true