This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new a63713b869 fix: Made the Request objects more null-safe fixes: #2280
a63713b869 is described below
commit a63713b86927c01605d47f71970c2e199904a78d
Author: Christofer Dutz <[email protected]>
AuthorDate: Fri Sep 26 16:07:08 2025 +0200
fix: Made the Request objects more null-safe
fixes: #2280
chore: Migrated to a more Java 21 code style
chore: Removed the "Proprietary" request options.
---
.../java/spi/messages/DefaultPlcDiscoveryItem.java | 6 +--
.../java/spi/messages/DefaultPlcReadRequest.java | 4 +-
.../java/spi/messages/DefaultPlcReadResponse.java | 48 ++++++++--------------
.../spi/messages/DefaultPlcSubscriptionEvent.java | 1 -
.../messages/DefaultPlcSubscriptionRequest.java | 2 +-
.../messages/DefaultPlcUnsubscriptionRequest.java | 5 +--
.../java/spi/messages/DefaultPlcWriteRequest.java | 12 +++---
.../java/spi/messages/PlcProprietaryRequest.java | 25 -----------
.../java/spi/messages/PlcProprietaryResponse.java | 25 -----------
.../java/spi/messages/PlcProprietarySender.java | 25 -----------
10 files changed, 29 insertions(+), 124 deletions(-)
diff --git
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcDiscoveryItem.java
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcDiscoveryItem.java
index 0aecb61003..85780ae7f5 100644
---
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcDiscoveryItem.java
+++
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcDiscoveryItem.java
@@ -85,7 +85,7 @@ public class DefaultPlcDiscoveryItem implements
PlcDiscoveryItem, Serializable {
@Override
public String getConnectionUrl() {
StringBuilder sb = new StringBuilder(String.format("%s:%s://%s",
- protocolCode, transportCode, transportUrl.toString()));
+ protocolCode, transportCode, transportUrl));
if(options != null && !options.isEmpty()) {
boolean first = true;
for (Map.Entry<String, String> optionEntry : options.entrySet()) {
@@ -112,8 +112,8 @@ public class DefaultPlcDiscoveryItem implements
PlcDiscoveryItem, Serializable {
transportCode.getBytes(StandardCharsets.UTF_8).length * 8,
transportCode,
WithOption.WithEncoding(StandardCharsets.UTF_8.name()));
writeBuffer.writeString("transportUrl",
- transportUrl.toString().getBytes(StandardCharsets.UTF_8).length *
8,
- transportUrl.toString(),
WithOption.WithEncoding(StandardCharsets.UTF_8.name()));
+ transportUrl.getBytes(StandardCharsets.UTF_8).length * 8,
+ transportUrl,
WithOption.WithEncoding(StandardCharsets.UTF_8.name()));
if(options != null && !options.isEmpty()) {
writeBuffer.pushContext("options");
for (Map.Entry<String, String> optionEntry : options.entrySet()) {
diff --git
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadRequest.java
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadRequest.java
index ebc112b26a..b3e1787e15 100644
---
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadRequest.java
+++
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadRequest.java
@@ -78,12 +78,12 @@ public class DefaultPlcReadRequest implements
PlcReadRequest, PlcTagRequest, Ser
@Override
public PlcTag getTag(String tagName) {
- return tags.get(tagName).getTag();
+ return tags.get(tagName) != null ? tags.get(tagName).getTag() : null;
}
@Override
public PlcResponseCode getTagResponseCode(String tagName) {
- return tags.get(tagName).getResponseCode();
+ return tags.get(tagName) != null ? tags.get(tagName).getResponseCode()
: null;
}
@Override
diff --git
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadResponse.java
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadResponse.java
index f0ee410501..2c6745a9e9 100644
---
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadResponse.java
+++
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcReadResponse.java
@@ -96,8 +96,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public int getNumberOfValues(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
return plcList.getLength();
} else {
return 1;
@@ -146,8 +145,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<Object> getAllObjects(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<Object> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getObject());
@@ -182,8 +180,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<Boolean> getAllBooleans(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<Boolean> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getBoolean());
@@ -218,8 +215,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<Byte> getAllBytes(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<Byte> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getByte());
@@ -254,8 +250,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<Short> getAllShorts(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<Short> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getShort());
@@ -290,8 +285,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<Integer> getAllIntegers(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<Integer> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getInteger());
@@ -326,8 +320,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<BigInteger> getAllBigIntegers(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<BigInteger> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getBigInteger());
@@ -362,8 +355,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<Long> getAllLongs(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<Long> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getLong());
@@ -398,8 +390,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<Float> getAllFloats(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<Float> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getFloat());
@@ -434,8 +425,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<Double> getAllDoubles(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<Double> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getDouble());
@@ -470,8 +460,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<BigDecimal> getAllBigDecimals(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<BigDecimal> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getBigDecimal());
@@ -506,8 +495,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<String> getAllStrings(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<String> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getString());
@@ -542,8 +530,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<LocalTime> getAllTimes(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<LocalTime> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getTime());
@@ -578,8 +565,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<LocalDate> getAllDates(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<LocalDate> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getDate());
@@ -614,8 +600,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
@Override
public Collection<LocalDateTime> getAllDateTimes(String name) {
PlcValue tagInternal = getTagInternal(name);
- if(tagInternal instanceof PlcList) {
- PlcList plcList = (PlcList) tagInternal;
+ if(tagInternal instanceof PlcList plcList) {
List<LocalDateTime> items = new ArrayList<>(plcList.getLength());
for (PlcValue plcValue : plcList.getList()) {
items.add(plcValue.getDateTime());
@@ -649,8 +634,7 @@ public class DefaultPlcReadResponse implements
PlcReadResponse, Serializable {
protected PlcValue getTagIndexInternal(String name, int index) {
final PlcValue values = getTagInternal(name);
- if(values instanceof PlcList) {
- PlcList plcList = (PlcList) values;
+ if(values instanceof PlcList plcList) {
if(index > (plcList.getLength() - 1)) {
return null;
}
diff --git
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionEvent.java
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionEvent.java
index e4ef1835aa..03a04d0793 100644
---
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionEvent.java
+++
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionEvent.java
@@ -21,7 +21,6 @@ package org.apache.plc4x.java.spi.messages;
import java.util.Collections;
import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent;
import org.apache.plc4x.java.api.metadata.Metadata;
-import org.apache.plc4x.java.spi.metadata.DefaultMetadata;
import org.apache.plc4x.java.api.model.PlcTag;
import org.apache.plc4x.java.api.value.PlcValue;
import org.apache.plc4x.java.spi.messages.utils.PlcResponseItem;
diff --git
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionRequest.java
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionRequest.java
index 65e1a3a6cb..1e1e8a98aa 100644
---
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionRequest.java
+++
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcSubscriptionRequest.java
@@ -77,7 +77,7 @@ public class DefaultPlcSubscriptionRequest implements
PlcSubscriptionRequest, Se
@Override
public PlcSubscriptionTag getTag(String tagName) {
- return tags.get(tagName).getTag();
+ return tags.get(tagName) != null ? tags.get(tagName).getTag() : null;
}
@Override
diff --git
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcUnsubscriptionRequest.java
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcUnsubscriptionRequest.java
index 64570741f8..ba97959179 100644
---
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcUnsubscriptionRequest.java
+++
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcUnsubscriptionRequest.java
@@ -28,7 +28,6 @@ import org.apache.plc4x.java.spi.utils.Serializable;
import java.util.*;
import java.util.concurrent.CompletableFuture;
-import java.util.stream.Collectors;
public class DefaultPlcUnsubscriptionRequest implements
PlcUnsubscriptionRequest, PlcRequest, Serializable {
@@ -83,13 +82,13 @@ public class DefaultPlcUnsubscriptionRequest implements
PlcUnsubscriptionRequest
@Override
public PlcUnsubscriptionRequest.Builder
addHandles(PlcSubscriptionHandle plcSubscriptionHandle1,
PlcSubscriptionHandle... plcSubscriptionHandles) {
this.plcSubscriptionHandles.add(plcSubscriptionHandle1);
-
this.plcSubscriptionHandles.addAll(Arrays.stream(plcSubscriptionHandles).map(PlcSubscriptionHandle.class::cast).collect(Collectors.toList()));
+
this.plcSubscriptionHandles.addAll(Arrays.stream(plcSubscriptionHandles).map(PlcSubscriptionHandle.class::cast).toList());
return this;
}
@Override
public PlcUnsubscriptionRequest.Builder
addHandles(Collection<PlcSubscriptionHandle> plcSubscriptionHandles) {
-
this.plcSubscriptionHandles.addAll(plcSubscriptionHandles.stream().map(PlcSubscriptionHandle.class::cast).collect(Collectors.toList()));
+
this.plcSubscriptionHandles.addAll(plcSubscriptionHandles.stream().map(PlcSubscriptionHandle.class::cast).toList());
return this;
}
diff --git
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcWriteRequest.java
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcWriteRequest.java
index d8530196f2..f4b23d1051 100644
---
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcWriteRequest.java
+++
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/DefaultPlcWriteRequest.java
@@ -77,12 +77,12 @@ public class DefaultPlcWriteRequest implements
PlcWriteRequest, Serializable {
@Override
public PlcResponseCode getTagResponseCode(String tagName) {
- return tags.get(tagName).getResponseCode();
+ return tags.get(tagName) != null ? tags.get(tagName).getResponseCode()
: null;
}
@Override
- public PlcTag getTag(String name) {
- return tags.get(name).getTag();
+ public PlcTag getTag(String tagName) {
+ return tags.get(tagName) != null ? tags.get(tagName).getTag() : null;
}
@Override
@@ -105,8 +105,7 @@ public class DefaultPlcWriteRequest implements
PlcWriteRequest, Serializable {
@Override
public int getNumberOfValues(String name) {
final PlcValue value = tags.get(name).getValue();
- if (value instanceof PlcList) {
- PlcList list = (PlcList) value;
+ if (value instanceof PlcList list) {
return list.getLength();
}
return 1;
@@ -135,8 +134,7 @@ public class DefaultPlcWriteRequest implements
PlcWriteRequest, Serializable {
}
protected void serializePlcValue(PlcValue plcValue, WriteBuffer
writeBuffer) throws SerializationException {
- if (plcValue instanceof Serializable) {
- Serializable serializable = (Serializable) plcValue;
+ if (plcValue instanceof Serializable serializable) {
serializable.serialize(writeBuffer);
} else {
String plcValueString = plcValue.getString();
diff --git
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/PlcProprietaryRequest.java
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/PlcProprietaryRequest.java
deleted file mode 100644
index a93fb2d7e0..0000000000
---
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/PlcProprietaryRequest.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.plc4x.java.spi.messages;
-
-import org.apache.plc4x.java.api.messages.PlcRequest;
-
-public interface PlcProprietaryRequest<REQUEST> extends PlcRequest {
- REQUEST getProprietaryRequest();
-}
diff --git
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/PlcProprietaryResponse.java
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/PlcProprietaryResponse.java
deleted file mode 100644
index 667bd960d7..0000000000
---
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/PlcProprietaryResponse.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.plc4x.java.spi.messages;
-
-import org.apache.plc4x.java.api.messages.PlcResponse;
-
-public interface PlcProprietaryResponse<T> extends PlcResponse {
- T getResponse();
-}
diff --git
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/PlcProprietarySender.java
b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/PlcProprietarySender.java
deleted file mode 100644
index af1b7c2070..0000000000
---
a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/messages/PlcProprietarySender.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.plc4x.java.spi.messages;
-
-import java.util.concurrent.CompletableFuture;
-
-public interface PlcProprietarySender {
- <T> CompletableFuture<PlcProprietaryResponse<T>>
send(PlcProprietaryRequest proprietaryRequest);
-}