From ef16df8674a3afbde2bd0796dacc167c2bd098b1 Mon Sep 17 00:00:00 2001 From: Szymon Sasin Date: Wed, 2 Sep 2026 09:32:47 +0300 Subject: [PATCH] fix: upgrade PMD to 7.26.0 to and correct issues --- .github/dependabot.yml | 3 +- build.gradle.kts | 4 +- .../java/com/mbed/coap/cli/KeystoreUtils.java | 5 +- .../coap/cli/providers/MbedtlsProvider.java | 10 +--- .../coap/cli/providers/OpensslProvider.java | 3 +- .../mbed/coap/client/RegistrationManager.java | 7 +-- .../com/mbed/coap/linkformat/LinkFormat.java | 4 +- .../mbed/coap/packet/BasicHeaderOptions.java | 12 ++-- .../java/com/mbed/coap/packet/CoapPacket.java | 4 +- .../main/java/com/mbed/coap/packet/Code.java | 4 +- .../coap/packet/DataConvertingUtility.java | 2 +- .../com/mbed/coap/packet/HeaderOptions.java | 4 +- .../java/com/mbed/coap/packet/MediaTypes.java | 59 ++++++++++--------- .../java/com/mbed/coap/packet/Opaque.java | 9 +-- .../mbed/coap/server/CoapServerBuilder.java | 3 +- .../server/DefaultDuplicateDetectorCache.java | 5 +- .../server/DuplicatedCoapMessageCallback.java | 4 +- .../coap/server/ObserveRequestFilter.java | 4 +- .../com/mbed/coap/server/RouterService.java | 6 +- .../coap/server/block/BlockWiseCallback.java | 4 +- .../server/filter/EtagGeneratorFilter.java | 2 +- .../server/filter/RequestLoggerFilter.java | 4 +- .../coap/server/messaging/ExchangeFilter.java | 2 +- .../messaging/MessageIdSupplierImpl.java | 4 +- .../coap/server/observe/ObserversManager.java | 4 +- .../mbed/coap/transport/TransportContext.java | 4 +- .../main/java/com/mbed/coap/utils/Filter.java | 5 +- .../coap/client/RegistrationManagerTest.java | 18 +++++- .../mbed/coap/linkformat/LinkFormatTest.java | 7 ++- .../mbedtls/DtlsTransportContext.java | 4 +- .../mbed/coap/packet/SignalingOptions.java | 15 +++-- .../messaging/CapabilitiesStorageImpl.java | 5 +- lwm2m/build.gradle.kts | 14 +++++ pmd-rules.xml | 39 ++++++------ 34 files changed, 162 insertions(+), 121 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index c6102d59..07773f67 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -11,9 +11,10 @@ updates: gradle: patterns: - "*" - # spotbugs upgrades usually require fixing new findings, keep them in a separate PR + # spotbugs and pmd upgrades usually require fixing new findings, keep them in a separate PR exclude-patterns: - "com.github.spotbugs*" + - "net.sourceforge.pmd*" update-types: - "minor" - "patch" diff --git a/build.gradle.kts b/build.gradle.kts index ba344c1b..ea3d6874 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -106,7 +106,7 @@ subprojects { } pmd { - toolVersion = "6.55.0" + toolVersion = "7.27.0" isConsoleOutput = true ruleSets = emptyList() ruleSetFiles = files(rootProject.file("pmd-rules.xml")) @@ -123,6 +123,8 @@ subprojects { } dependencies { + pmd("net.sourceforge.pmd:pmd-ant:7.27.0") + pmd("net.sourceforge.pmd:pmd-java:7.27.0") spotbugsPlugins("com.h3xstream.findsecbugs:findsecbugs-plugin:1.14.0") } diff --git a/coap-cli/src/main/java/com/mbed/coap/cli/KeystoreUtils.java b/coap-cli/src/main/java/com/mbed/coap/cli/KeystoreUtils.java index 9187706c..6188b8c2 100644 --- a/coap-cli/src/main/java/com/mbed/coap/cli/KeystoreUtils.java +++ b/coap-cli/src/main/java/com/mbed/coap/cli/KeystoreUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,7 +20,6 @@ import java.security.KeyStore; import java.security.KeyStoreException; import java.security.cert.X509Certificate; -import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; import java.util.LinkedList; @@ -49,7 +48,7 @@ static KeyStore loadKeystore(String keystoreFile) { } public static String findKeyAlias(KeyStore ks) throws KeyStoreException { - ArrayList aliases = Collections.list(ks.aliases()); + List aliases = Collections.list(ks.aliases()); for (String alias : aliases) { if (ks.isKeyEntry(alias) && !"ca".equals(alias)) { diff --git a/coap-cli/src/main/java/com/mbed/coap/cli/providers/MbedtlsProvider.java b/coap-cli/src/main/java/com/mbed/coap/cli/providers/MbedtlsProvider.java index ab7c55e9..5f38310d 100644 --- a/coap-cli/src/main/java/com/mbed/coap/cli/providers/MbedtlsProvider.java +++ b/coap-cli/src/main/java/com/mbed/coap/cli/providers/MbedtlsProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,10 +20,10 @@ import com.mbed.coap.packet.Opaque; import com.mbed.coap.transport.CoapTransport; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.InetSocketAddress; +import java.nio.file.Files; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.util.Collections; @@ -88,10 +88,6 @@ private static byte[] readBytes(File fileSession) throws IOException { return new byte[0]; } - try (FileInputStream fileInputStream = new FileInputStream(fileSession)) { - byte[] sessionBytes = new byte[(int) fileSession.length()]; - fileInputStream.read(sessionBytes); - return sessionBytes; - } + return Files.readAllBytes(fileSession.toPath()); } } diff --git a/coap-cli/src/main/java/com/mbed/coap/cli/providers/OpensslProvider.java b/coap-cli/src/main/java/com/mbed/coap/cli/providers/OpensslProvider.java index b59cadd2..21059e0a 100644 --- a/coap-cli/src/main/java/com/mbed/coap/cli/providers/OpensslProvider.java +++ b/coap-cli/src/main/java/com/mbed/coap/cli/providers/OpensslProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -72,6 +72,7 @@ private CoapTcpTransport create(CoapSerializer coapSerializer, InetSocketAddress return new OpensslProcessTransport(process.start(), destAdr, coapSerializer); } + @SuppressWarnings("PMD.RelianceOnDefaultCharset") // in jdk 8 there is no support to specify charset private static File keyPairToTempFile(String alias, KeyStore ks) throws KeyStoreException, IOException, CertificateEncodingException, NoSuchAlgorithmException, UnrecoverableKeyException { File temp = File.createTempFile("client", ".pem"); try (FileWriter writer = new FileWriter(temp)) { diff --git a/coap-core/src/main/java/com/mbed/coap/client/RegistrationManager.java b/coap-core/src/main/java/com/mbed/coap/client/RegistrationManager.java index fcb621c1..8a931b22 100644 --- a/coap-core/src/main/java/com/mbed/coap/client/RegistrationManager.java +++ b/coap-core/src/main/java/com/mbed/coap/client/RegistrationManager.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -45,7 +45,6 @@ public final class RegistrationManager { private volatile Optional registrationLocation = Optional.empty(); private volatile Duration lastRetryDelay = Duration.ZERO; - @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") public RegistrationManager(CoapServer server, URI registrationUri, String registrationLinks, ScheduledExecutorService scheduledExecutor, Duration minRetryDelay, Duration maxRetryDelay) { @@ -80,7 +79,7 @@ public CompletableFuture register() { .payload(registrationLinks, MediaTypes.CT_APPLICATION_LINK__FORMAT) ) .thenAccept(resp -> { - if (resp.getCode().equals(Code.C201_CREATED)) { + if (resp.getCode() == Code.C201_CREATED) { registrationSuccess(resp.options().getLocationPath(), resp.options().getMaxAgeValue()); } else { registrationFailed(String.format("%s '%s'", resp.getCode().codeToString(), resp.getPayload().toUtf8String())); @@ -107,7 +106,7 @@ private void scheduleUpdate(long lifetime) { private void updateRegistration() { client.send(post(registrationLocation.get())) .thenAccept(resp -> { - if (resp.getCode().equals(Code.C201_CREATED) || resp.getCode().equals(Code.C204_CHANGED)) { + if (resp.getCode() == Code.C201_CREATED || resp.getCode() == Code.C204_CHANGED) { LOGGER.info("[EP:{}] Updated, lifetime: {}s", epName, resp.options().getMaxAgeValue()); scheduleUpdate(resp.options().getMaxAgeValue()); } else { diff --git a/coap-core/src/main/java/com/mbed/coap/linkformat/LinkFormat.java b/coap-core/src/main/java/com/mbed/coap/linkformat/LinkFormat.java index 70b15400..39adf656 100644 --- a/coap-core/src/main/java/com/mbed/coap/linkformat/LinkFormat.java +++ b/coap-core/src/main/java/com/mbed/coap/linkformat/LinkFormat.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -76,7 +76,7 @@ static void parseParam(LinkFormat lf, String paramName, String paramValue) throw parse(paramName, lf, val); } catch (ClassCastException ex) { throw new ParseException("Expected ptoken value (without quotes)", 0); - } catch (NullPointerException ex) { //NOPMD + } catch (NullPointerException ex) { throw new ParseException("Expected value for parameter", 0); } catch (Exception ex) { throw new ParseException(ex.getMessage(), 0); diff --git a/coap-core/src/main/java/com/mbed/coap/packet/BasicHeaderOptions.java b/coap-core/src/main/java/com/mbed/coap/packet/BasicHeaderOptions.java index 656dcf09..8d859ef1 100644 --- a/coap-core/src/main/java/com/mbed/coap/packet/BasicHeaderOptions.java +++ b/coap-core/src/main/java/com/mbed/coap/packet/BasicHeaderOptions.java @@ -37,7 +37,7 @@ /** * Implements CoAP basic header options. */ -@SuppressWarnings({"PMD.NPathComplexity", "PMD.CyclomaticComplexity"}) +@SuppressWarnings({"PMD.NPathComplexity"}) public class BasicHeaderOptions { public static final byte IF_MATCH = 1; //multiple @@ -207,10 +207,10 @@ public final boolean put(int optionNumber, Opaque data) { * @return sorted list */ List getRawOptions() { - LinkedList list = new LinkedList<>(); + List list = new LinkedList<>(); if (contentFormat != null) { - list.add(RawOption.fromUint(CONTENT_FORMAT, contentFormat.longValue())); + list.add(RawOption.fromUint(CONTENT_FORMAT, contentFormat)); } if (maxAge != null && maxAge != DEFAULT_MAX_AGE) { list.add(RawOption.fromUint(MAX_AGE, maxAge)); @@ -246,10 +246,10 @@ List getRawOptions() { list.add(RawOption.fromString(PROXY_SCHEME, proxyScheme)); } if (this.accept != null) { - list.add(RawOption.fromUint(ACCEPT, accept.longValue())); + list.add(RawOption.fromUint(ACCEPT, accept)); } if (this.uriPort != null) { - list.add(RawOption.fromUint(URI_PORT, uriPort.longValue())); + list.add(RawOption.fromUint(URI_PORT, uriPort)); } if (ifNonMatch != null && ifNonMatch) { list.add(RawOption.fromEmpty(IF_NON_MATCH)); @@ -258,7 +258,7 @@ List getRawOptions() { list.add(new RawOption(IF_MATCH, ifMatch)); } if (size1 != null) { - list.add(RawOption.fromUint(SIZE1, size1.longValue())); + list.add(RawOption.fromUint(SIZE1, size1)); } if (unrecognizedOptions != null) { diff --git a/coap-core/src/main/java/com/mbed/coap/packet/CoapPacket.java b/coap-core/src/main/java/com/mbed/coap/packet/CoapPacket.java index 85904ab0..c231b7c6 100644 --- a/coap-core/src/main/java/com/mbed/coap/packet/CoapPacket.java +++ b/coap-core/src/main/java/com/mbed/coap/packet/CoapPacket.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -355,7 +355,7 @@ public String toString(boolean printFullPayload, boolean printPayloadOnlyAsHex, } public String toString(boolean printFullPayload, boolean printPayloadOnlyAsHex, boolean printAddress, boolean doNotPrintPayload) { - StringBuilder sb = new StringBuilder(); + StringBuilder sb = new StringBuilder(20); if (printAddress && this.getRemoteAddrString() != null) { sb.append(this.getRemoteAddrString()).append(' '); diff --git a/coap-core/src/main/java/com/mbed/coap/packet/Code.java b/coap-core/src/main/java/com/mbed/coap/packet/Code.java index b68a7c5a..9377c0a2 100644 --- a/coap-core/src/main/java/com/mbed/coap/packet/Code.java +++ b/coap-core/src/main/java/com/mbed/coap/packet/Code.java @@ -81,7 +81,7 @@ public int getCoapCode() { } public static Code valueOf(int code) { - for (Code c : Code.values()) { + for (Code c : values()) { if (c.getCoapCode() == code) { return c; } @@ -106,7 +106,7 @@ public boolean isSuccess() { return coapCode >>> 5 == 2; } - @SuppressWarnings("PMD.NcssCount") + @SuppressWarnings({"PMD.NcssCount", "PMD.ImplicitSwitchFallThrough"}) public static Code fromHttp(int httpStatus, Method method) { switch (httpStatus) { diff --git a/coap-core/src/main/java/com/mbed/coap/packet/DataConvertingUtility.java b/coap-core/src/main/java/com/mbed/coap/packet/DataConvertingUtility.java index c23ef632..5d252c55 100644 --- a/coap-core/src/main/java/com/mbed/coap/packet/DataConvertingUtility.java +++ b/coap-core/src/main/java/com/mbed/coap/packet/DataConvertingUtility.java @@ -46,7 +46,7 @@ private DataConvertingUtility() { */ static String[] split(String val, char ch) { int offset = 0; - ArrayList list = new ArrayList<>(); + List list = new ArrayList<>(); int nextPos = val.indexOf(ch, offset); while (nextPos != -1) { diff --git a/coap-core/src/main/java/com/mbed/coap/packet/HeaderOptions.java b/coap-core/src/main/java/com/mbed/coap/packet/HeaderOptions.java index f7a38e72..c7401dd3 100644 --- a/coap-core/src/main/java/com/mbed/coap/packet/HeaderOptions.java +++ b/coap-core/src/main/java/com/mbed/coap/packet/HeaderOptions.java @@ -90,7 +90,7 @@ List getRawOptions() { if (observe == 0) { l.add(RawOption.fromEmpty(OBSERVE)); } else { - l.add(RawOption.fromUint(OBSERVE, observe.longValue())); + l.add(RawOption.fromUint(OBSERVE, observe)); } } if (block1Req != null) { @@ -100,7 +100,7 @@ List getRawOptions() { l.add(new RawOption(BLOCK_2_RES, new Opaque[]{getBlock2Res().toBytes()})); } if (size2Res != null) { - l.add(RawOption.fromUint(SIZE_2_RES, size2Res.longValue())); + l.add(RawOption.fromUint(SIZE_2_RES, size2Res)); } if (echo != null) { l.add(new RawOption(ECHO, echo)); diff --git a/coap-core/src/main/java/com/mbed/coap/packet/MediaTypes.java b/coap-core/src/main/java/com/mbed/coap/packet/MediaTypes.java index 2b133f04..f5f20dc4 100644 --- a/coap-core/src/main/java/com/mbed/coap/packet/MediaTypes.java +++ b/coap-core/src/main/java/com/mbed/coap/packet/MediaTypes.java @@ -1,4 +1,5 @@ -/** +/* + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2018 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,41 +24,41 @@ public class MediaTypes { //https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#content-formats //RFC 7252 - public final static short CT_TEXT_PLAIN = 0; - public final static short CT_APPLICATION_LINK__FORMAT = 40; //RFC 6690 - public final static short CT_APPLICATION_XML = 41; - public final static short CT_APPLICATION_OCTET__STREAM = 42; - public final static short CT_APPLICATION_EXI = 47; - public final static short CT_APPLICATION_JSON = 50; + public static final short CT_TEXT_PLAIN = 0; + public static final short CT_APPLICATION_LINK__FORMAT = 40; //RFC 6690 + public static final short CT_APPLICATION_XML = 41; + public static final short CT_APPLICATION_OCTET__STREAM = 42; + public static final short CT_APPLICATION_EXI = 47; + public static final short CT_APPLICATION_JSON = 50; //--- OMA LwM2M --- - public final static short CT_APPLICATION_LWM2M_TLV = 11542; - public final static short CT_APPLICATION_LWM2M_JSON = 11543; + public static final short CT_APPLICATION_LWM2M_TLV = 11542; + public static final short CT_APPLICATION_LWM2M_JSON = 11543; //RFC8152 - public final static short CT_APPLICATION_CODE_ENCRYPT0 = 16; - public final static short CT_APPLICATION_CODE_MAC0 = 17; - public final static short CT_APPLICATION_CODE_SIGN1 = 18; - public final static short CT_APPLICATION_CODE_ENCRYPT = 96; - public final static short CT_APPLICATION_CODE_MAC = 97; - public final static short CT_APPLICATION_CODE_SIGN = 98; - public final static short CT_APPLICATION_CODE_KEY = 101; - public final static short CT_APPLICATION_CODE_KEY_SET = 102; + public static final short CT_APPLICATION_CODE_ENCRYPT0 = 16; + public static final short CT_APPLICATION_CODE_MAC0 = 17; + public static final short CT_APPLICATION_CODE_SIGN1 = 18; + public static final short CT_APPLICATION_CODE_ENCRYPT = 96; + public static final short CT_APPLICATION_CODE_MAC = 97; + public static final short CT_APPLICATION_CODE_SIGN = 98; + public static final short CT_APPLICATION_CODE_KEY = 101; + public static final short CT_APPLICATION_CODE_KEY_SET = 102; //RFC6902 - public final static short CT_APPLICATION_JSON_PATCH_JSON = 51; + public static final short CT_APPLICATION_JSON_PATCH_JSON = 51; //RFC7396 - public final static short CT_APPLICATION_MERGE_PATCH_JSON = 52; + public static final short CT_APPLICATION_MERGE_PATCH_JSON = 52; //RFC7049 - public final static short CT_APPLICATION_CBOR = 60; + public static final short CT_APPLICATION_CBOR = 60; //RFC7390 - public final static short CT_APPLICATION_COAP_GROUP_JSON = 256; + public static final short CT_APPLICATION_COAP_GROUP_JSON = 256; //RFC8428 - public final static short CT_APPLICATION_SENML_JSON = 110; - public final static short CT_APPLICATION_SENSML_JSON = 111; - public final static short CT_APPLICATION_SENML_CBOR = 112; - public final static short CT_APPLICATION_SENSML_CBOR = 113; - public final static short CT_APPLICATION_SENML_EXI = 114; - public final static short CT_APPLICATION_SENSML_EXI = 115; - public final static short CT_APPLICATION_SENML_XML = 310; - public final static short CT_APPLICATION_SENSML_XML = 311; + public static final short CT_APPLICATION_SENML_JSON = 110; + public static final short CT_APPLICATION_SENSML_JSON = 111; + public static final short CT_APPLICATION_SENML_CBOR = 112; + public static final short CT_APPLICATION_SENSML_CBOR = 113; + public static final short CT_APPLICATION_SENML_EXI = 114; + public static final short CT_APPLICATION_SENSML_EXI = 115; + public static final short CT_APPLICATION_SENML_XML = 310; + public static final short CT_APPLICATION_SENSML_XML = 311; static final Map MEDIA_TYPE_MAP = new HashMap<>(); diff --git a/coap-core/src/main/java/com/mbed/coap/packet/Opaque.java b/coap-core/src/main/java/com/mbed/coap/packet/Opaque.java index 9d6a82da..c6f7898b 100644 --- a/coap-core/src/main/java/com/mbed/coap/packet/Opaque.java +++ b/coap-core/src/main/java/com/mbed/coap/packet/Opaque.java @@ -25,8 +25,8 @@ import java.util.Arrays; public final class Opaque { - private final static String HEX_DIGIT_STRING = "0123456789abcdef"; - private final static char[] HEX_DIGITS = HEX_DIGIT_STRING.toCharArray(); + private static final String HEX_DIGIT_STRING = "0123456789abcdef"; + private static final char[] HEX_DIGITS = HEX_DIGIT_STRING.toCharArray(); private final byte[] data; public static final Opaque EMPTY = new Opaque(new byte[0]); @@ -97,7 +97,7 @@ private static int hexIndex(char ch) { public static Opaque read(InputStream inputStream, int len) throws IOException { if (len == 0) { - return Opaque.EMPTY; + return EMPTY; } byte[] data = new byte[len]; int totalRead = 0; @@ -141,6 +141,7 @@ String toHexShort(final int maxLen) { } } + @SuppressWarnings({"PMD.AssignmentInOperand"}) private String encodeToHex(final int len) { final char[] retVal = new char[len * 2]; int k = 0; @@ -217,7 +218,7 @@ public Opaque slice(int indexFrom, int len) { indexTo = data.length; } if (indexFrom > indexTo) { - return Opaque.EMPTY; + return EMPTY; } if (indexFrom == 0 && indexTo == data.length) { return this; diff --git a/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java b/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java index 586f0357..fb14368a 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java +++ b/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2025 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -64,6 +64,7 @@ import java.util.function.Supplier; import java.util.stream.Stream; +@SuppressWarnings("PMD.CouplingBetweenObjects") // it's a nature for a builder class to have many dependencies public final class CoapServerBuilder { private static final long DELAYED_TRANSACTION_TIMEOUT_MS = 120000; //2 minutes diff --git a/coap-core/src/main/java/com/mbed/coap/server/DefaultDuplicateDetectorCache.java b/coap-core/src/main/java/com/mbed/coap/server/DefaultDuplicateDetectorCache.java index d62900be..cf7a0b28 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/DefaultDuplicateDetectorCache.java +++ b/coap-core/src/main/java/com/mbed/coap/server/DefaultDuplicateDetectorCache.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,6 +19,7 @@ import com.mbed.coap.packet.CoapPacket; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -34,7 +35,7 @@ public class DefaultDuplicateDetectorCache implements PutOnlyMap underlying; + private final ConcurrentMap underlying; private final long maxSize; private final long overSizeMargin; private final long warnIntervalMillis; diff --git a/coap-core/src/main/java/com/mbed/coap/server/DuplicatedCoapMessageCallback.java b/coap-core/src/main/java/com/mbed/coap/server/DuplicatedCoapMessageCallback.java index d11579d5..e891f4c5 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/DuplicatedCoapMessageCallback.java +++ b/coap-core/src/main/java/com/mbed/coap/server/DuplicatedCoapMessageCallback.java @@ -1,4 +1,5 @@ -/** +/* + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2018 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,6 +18,7 @@ import com.mbed.coap.packet.CoapPacket; +@FunctionalInterface public interface DuplicatedCoapMessageCallback { DuplicatedCoapMessageCallback NULL = request -> { //ignore diff --git a/coap-core/src/main/java/com/mbed/coap/server/ObserveRequestFilter.java b/coap-core/src/main/java/com/mbed/coap/server/ObserveRequestFilter.java index ed924cc3..a7193f26 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/ObserveRequestFilter.java +++ b/coap-core/src/main/java/com/mbed/coap/server/ObserveRequestFilter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,7 @@ class ObserveRequestFilter implements Filter.SimpleFilter { private final AtomicLong nextToken = new AtomicLong(0); private final Consumer registerRelation; - private final static Integer INIT_OBSERVE = 0; + private static final Integer INIT_OBSERVE = 0; ObserveRequestFilter(Consumer registerRelation) { this.registerRelation = registerRelation; diff --git a/coap-core/src/main/java/com/mbed/coap/server/RouterService.java b/coap-core/src/main/java/com/mbed/coap/server/RouterService.java index 1e3d6f97..a9b35f91 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/RouterService.java +++ b/coap-core/src/main/java/com/mbed/coap/server/RouterService.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,7 +36,7 @@ public class RouterService implements Service { private final List>> prefixedHandlers; public final Service defaultHandler; - public final static Service NOT_FOUND_SERVICE = request -> CoapResponse.notFound().toFuture(); + public static final Service NOT_FOUND_SERVICE = request -> CoapResponse.notFound().toFuture(); public static RouteBuilder builder() { return new RouteBuilder(); @@ -151,7 +151,7 @@ public Service build() { static final class RequestMatcher { final Method method; final String uriPath; - private transient final boolean isPrefixed; + private final transient boolean isPrefixed; RequestMatcher(Method method, String uriPath) { this.method = method; diff --git a/coap-core/src/main/java/com/mbed/coap/server/block/BlockWiseCallback.java b/coap-core/src/main/java/com/mbed/coap/server/block/BlockWiseCallback.java index cedcb418..a9675e97 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/block/BlockWiseCallback.java +++ b/coap-core/src/main/java/com/mbed/coap/server/block/BlockWiseCallback.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -121,7 +121,7 @@ private CompletableFuture handleIfBlock1(CoapResponse response) { // see: https://tools.ietf.org/html/rfc7959#section-2.5 responseBlock = new BlockOption(responseBlock.getNr() + 1, responseBlock.getBlockSize(), origReqBlock.hasMore()); } else { - responseBlock = BlockWiseCallback.nextBertBlock(responseBlock, requestPayload.size(), numberOfBertBlocks, maxBlockPayload); + responseBlock = nextBertBlock(responseBlock, requestPayload.size(), numberOfBertBlocks, maxBlockPayload); } request.options().setBlock1Req(responseBlock); diff --git a/coap-core/src/main/java/com/mbed/coap/server/filter/EtagGeneratorFilter.java b/coap-core/src/main/java/com/mbed/coap/server/filter/EtagGeneratorFilter.java index 0aaa97b4..8ce8ac3b 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/filter/EtagGeneratorFilter.java +++ b/coap-core/src/main/java/com/mbed/coap/server/filter/EtagGeneratorFilter.java @@ -30,7 +30,7 @@ public final class EtagGeneratorFilter implements Filter.SimpleFilter etagGenerator; - public final static EtagGeneratorFilter PAYLOAD_HASHING = new EtagGeneratorFilter(payload -> Opaque.variableUInt(Arrays.hashCode(payload.getBytes()))); + public static final EtagGeneratorFilter PAYLOAD_HASHING = new EtagGeneratorFilter(payload -> Opaque.variableUInt(Arrays.hashCode(payload.getBytes()))); public EtagGeneratorFilter(Function etagGenerator) { this.etagGenerator = Objects.requireNonNull(etagGenerator); diff --git a/coap-core/src/main/java/com/mbed/coap/server/filter/RequestLoggerFilter.java b/coap-core/src/main/java/com/mbed/coap/server/filter/RequestLoggerFilter.java index ee6f8071..28cdf74e 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/filter/RequestLoggerFilter.java +++ b/coap-core/src/main/java/com/mbed/coap/server/filter/RequestLoggerFilter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,7 +50,7 @@ public interface Formatter { } public static class Builder { - public final static Formatter DEFAULT_FORMATTER = (req, resp, duration) -> String.format("[%s] %s -> %s (%dms)", req.getPeerAddress(), req, resp, duration); + public static final Formatter DEFAULT_FORMATTER = (req, resp, duration) -> String.format("[%s] %s -> %s (%dms)", req.getPeerAddress(), req, resp, duration); private Logger logger = LoggerFactory.getLogger(RequestLoggerFilter.class); private Level logLevel = Level.INFO; diff --git a/coap-core/src/main/java/com/mbed/coap/server/messaging/ExchangeFilter.java b/coap-core/src/main/java/com/mbed/coap/server/messaging/ExchangeFilter.java index f13fa713..37cc80a4 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/messaging/ExchangeFilter.java +++ b/coap-core/src/main/java/com/mbed/coap/server/messaging/ExchangeFilter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/coap-core/src/main/java/com/mbed/coap/server/messaging/MessageIdSupplierImpl.java b/coap-core/src/main/java/com/mbed/coap/server/messaging/MessageIdSupplierImpl.java index 69f771be..37aa2daa 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/messaging/MessageIdSupplierImpl.java +++ b/coap-core/src/main/java/com/mbed/coap/server/messaging/MessageIdSupplierImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -33,6 +33,6 @@ public MessageIdSupplierImpl(int initMid) { @Override public int getNextMID() { - return 0xFFFF & (globalMid.incrementAndGet()); + return 0xFFFF & globalMid.incrementAndGet(); } } diff --git a/coap-core/src/main/java/com/mbed/coap/server/observe/ObserversManager.java b/coap-core/src/main/java/com/mbed/coap/server/observe/ObserversManager.java index 99cbacde..8bd9c8c3 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/observe/ObserversManager.java +++ b/coap-core/src/main/java/com/mbed/coap/server/observe/ObserversManager.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,7 +39,7 @@ import org.slf4j.LoggerFactory; public class ObserversManager implements Filter.SimpleFilter { - private final static Logger LOGGER = LoggerFactory.getLogger(ObserversManager.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ObserversManager.class); private volatile Service outboundObservation; // uri-path, address, subscribing request private final Map> obsRelations = new ConcurrentHashMap<>(); diff --git a/coap-core/src/main/java/com/mbed/coap/transport/TransportContext.java b/coap-core/src/main/java/com/mbed/coap/transport/TransportContext.java index f01e18b7..3fb3e8ad 100644 --- a/coap-core/src/main/java/com/mbed/coap/transport/TransportContext.java +++ b/coap-core/src/main/java/com/mbed/coap/transport/TransportContext.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,7 +62,7 @@ private T get0(Key key) { public TransportContext with(Key key, T value) { if (this.equals(EMPTY)) { - return TransportContext.of(key, value); + return of(key, value); } return new TransportContext(requireNonNull(key), requireNonNull(value), this); diff --git a/coap-core/src/main/java/com/mbed/coap/utils/Filter.java b/coap-core/src/main/java/com/mbed/coap/utils/Filter.java index f918fadc..af1b157f 100644 --- a/coap-core/src/main/java/com/mbed/coap/utils/Filter.java +++ b/coap-core/src/main/java/com/mbed/coap/utils/Filter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,14 +54,17 @@ default Service then(Service function) { } + @FunctionalInterface interface SimpleFilter extends Filter { } + @FunctionalInterface interface UnaryFilter extends Filter { } + @SuppressWarnings("PMD.UseDiamondOperator") // looks like PMD bug static SimpleFilter identity() { return new SimpleFilter() { @Override diff --git a/coap-core/src/test/java/com/mbed/coap/client/RegistrationManagerTest.java b/coap-core/src/test/java/com/mbed/coap/client/RegistrationManagerTest.java index 6209fff5..7cba4084 100644 --- a/coap-core/src/test/java/com/mbed/coap/client/RegistrationManagerTest.java +++ b/coap-core/src/test/java/com/mbed/coap/client/RegistrationManagerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -142,6 +142,22 @@ public void update() throws Exception { verifyScheduledInSec(71L); } + @Test + public void update_with201Created() throws Exception { + //given + RegistrationManager reg = registered(); + + trnsport.when(newCoapPacket(2).post().uriPath("/stub/0001")) + .then(newCoapPacket(2).ack(Code.C201_CREATED).maxAge(101)); + + //when + runScheduledTask(); + + //then + assertTrue(reg.isRegistered()); + verifyScheduledInSec(71L); + } + @Test public void update_failedFromServer_immediately_reRegister() throws Exception { //given diff --git a/coap-core/src/test/java/com/mbed/coap/linkformat/LinkFormatTest.java b/coap-core/src/test/java/com/mbed/coap/linkformat/LinkFormatTest.java index 03327858..80695cde 100644 --- a/coap-core/src/test/java/com/mbed/coap/linkformat/LinkFormatTest.java +++ b/coap-core/src/test/java/com/mbed/coap/linkformat/LinkFormatTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -256,6 +256,11 @@ public void testSuccesfullHReflang() throws ParseException { assertNull(LinkFormatBuilder.parse("").getHRefLang()); } + @Test + public void testFailParseMissingParamValue() { + assertThrows(ParseException.class, () -> LinkFormatBuilder.parse(";sz")); + } + @Test public void testFailHReflang1() { assertThrows(IllegalArgumentException.class, () -> diff --git a/coap-mbedtls/src/main/java/org/opencoap/transport/mbedtls/DtlsTransportContext.java b/coap-mbedtls/src/main/java/org/opencoap/transport/mbedtls/DtlsTransportContext.java index f12e28de..94bbde03 100644 --- a/coap-mbedtls/src/main/java/org/opencoap/transport/mbedtls/DtlsTransportContext.java +++ b/coap-mbedtls/src/main/java/org/opencoap/transport/mbedtls/DtlsTransportContext.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,7 +39,7 @@ public class DtlsTransportContext { public static final BiFunction DTLS_COAP_TO_DATAGRAM_CONVERTER = (coapPacket, ctx) -> { ByteBuf buf = ctx.alloc().buffer(coapPacket.getPayload().size() + 128); CoapSerializer.serialize(coapPacket, new ByteBufOutputStream(buf)); - return new DatagramPacketWithContext(buf, coapPacket.getRemoteAddress(), null, DtlsTransportContext.toDtlsSessionContext(coapPacket.getTransportContext())); + return new DatagramPacketWithContext(buf, coapPacket.getRemoteAddress(), null, toDtlsSessionContext(coapPacket.getTransportContext())); }; public static TransportContext toTransportContext(DtlsSessionContext dtlsSessionContext) { diff --git a/coap-tcp/src/main/java/com/mbed/coap/packet/SignalingOptions.java b/coap-tcp/src/main/java/com/mbed/coap/packet/SignalingOptions.java index 3b2d9ed0..13a3cc2c 100644 --- a/coap-tcp/src/main/java/com/mbed/coap/packet/SignalingOptions.java +++ b/coap-tcp/src/main/java/com/mbed/coap/packet/SignalingOptions.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,12 +16,11 @@ */ package com.mbed.coap.packet; -import static com.mbed.coap.utils.Validations.*; +import static com.mbed.coap.utils.Validations.assume; /** * Implements CoAP signaling options from draft-ietf-core-coap-tcp-tls-09. */ -@SuppressWarnings({"PMD.CyclomaticComplexity"}) public class SignalingOptions { private static final byte MAX_MESSAGE_SIZE = 2; //7.01 @@ -46,6 +45,7 @@ public static SignalingOptions capabilities(int maxMessageSize, boolean useBlock return signalingOptions; } + @SuppressWarnings({"PMD.NPathComplexity"}) SignalingOptions parse(int type, Opaque data, Code code) { if (code == Code.C701_CSM && type == MAX_MESSAGE_SIZE) { setMaxMessageSize(data.toLong()); @@ -78,7 +78,7 @@ Opaque serializeOption2() { return Opaque.of(alternativeAddress); } if (badCsmOption != null) { - return Opaque.variableUInt(badCsmOption.longValue()); + return Opaque.variableUInt(badCsmOption); } return null; } @@ -89,14 +89,14 @@ Opaque serializeOption4() { } if (holdOff != null) { - return Opaque.variableUInt(holdOff.longValue()); + return Opaque.variableUInt(holdOff); } return null; } @Override public String toString() { - StringBuilder sb = new StringBuilder(32); + StringBuilder sb = new StringBuilder(64); if (maxMessageSize != null) { sb.append(" MaxMsgSz:").append(maxMessageSize); } @@ -107,8 +107,7 @@ public String toString() { sb.append(" Custody"); } if (alternativeAddress != null) { - sb.append(" AltAdr:"); - sb.append(alternativeAddress); + sb.append(" AltAdr:").append(alternativeAddress); } if (holdOff != null) { sb.append(" Hold-Off:").append(holdOff); diff --git a/coap-tcp/src/main/java/com/mbed/coap/server/messaging/CapabilitiesStorageImpl.java b/coap-tcp/src/main/java/com/mbed/coap/server/messaging/CapabilitiesStorageImpl.java index b6573105..6787370a 100644 --- a/coap-tcp/src/main/java/com/mbed/coap/server/messaging/CapabilitiesStorageImpl.java +++ b/coap-tcp/src/main/java/com/mbed/coap/server/messaging/CapabilitiesStorageImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 java-coap contributors (https://github.com/open-coap/java-coap) + * Copyright (C) 2022-2026 java-coap contributors (https://github.com/open-coap/java-coap) * Copyright (C) 2011-2021 ARM Limited. All rights reserved. * SPDX-License-Identifier: Apache-2.0 * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,13 +18,14 @@ import java.net.InetSocketAddress; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; /** * Per-connection CSM storage */ public class CapabilitiesStorageImpl implements CapabilitiesStorage { //package local for tests - final ConcurrentHashMap capabilitiesMap = new ConcurrentHashMap<>(); + final ConcurrentMap capabilitiesMap = new ConcurrentHashMap<>(); private final Capabilities defaultCapability; public CapabilitiesStorageImpl(Capabilities defaultCapability) { diff --git a/lwm2m/build.gradle.kts b/lwm2m/build.gradle.kts index 1cb75cfa..7541f874 100644 --- a/lwm2m/build.gradle.kts +++ b/lwm2m/build.gradle.kts @@ -15,3 +15,17 @@ dependencies { testImplementation("org.hamcrest:hamcrest-all:1.3") testImplementation("nl.jqno.equalsverifier:equalsverifier:4.5.2") } + +tasks { + // Disable PMD for this deprecated module + named("pmdMain").configure { + enabled = false + } +} + +tasks { + // Disable PMD for this deprecated module + named("pmdMain").configure { + enabled = false + } +} diff --git a/pmd-rules.xml b/pmd-rules.xml index a63c307e..d428f51f 100644 --- a/pmd-rules.xml +++ b/pmd-rules.xml @@ -8,46 +8,41 @@ - + - + - + - + - - - - + + + + + + + - - - - - - + value="//MethodDeclaration[@Name='equals' or @Name='hashCode']"/> - @@ -55,9 +50,11 @@ + + @@ -73,14 +70,14 @@ + value="//MethodDeclaration[@Name='equals']"/> - + @@ -103,12 +100,14 @@ + + - +