You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2026/08/14 09:41:20 UTC

(plc4x) branch develop updated (dd9667b503 -> 05a9be3da9)

This is an automated email from the ASF dual-hosted git repository.

chrisdutz pushed a change to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


    from dd9667b503 fix(plc4j/eip): Fixed an issue that generally prevented the reading of arrays.
     new 8d341dafad fix(plc4j/opm): Optimized the PlcEntityManager to reuse the proxies it creates to avoid issues with people not using the API correctly.
     new 05a9be3da9 docs(plc4j/eip): Updated the EIP driver documentation

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/plc4x/java/eip/base/tag/EipTagTest.java |  24 ++++
 .../apache/plc4x/java/opm/PlcEntityManager.java    |  55 +++++++--
 .../opm/issues/i1935/ProxyClassCachingTest.java    | 137 +++++++++++++++++++++
 .../modules/users/pages/protocols/eip.adoc         |  37 ++++--
 4 files changed, 233 insertions(+), 20 deletions(-)
 create mode 100644 plc4j/tools/opm/src/test/java/org/apache/plc4x/java/opm/issues/i1935/ProxyClassCachingTest.java


(plc4x) 01/02: fix(plc4j/opm): Optimized the PlcEntityManager to reuse the proxies it creates to avoid issues with people not using the API correctly.

Posted by cd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chrisdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 8d341dafada8d0d113040d7b612e02141756f6b5
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Fri Aug 14 11:35:58 2026 +0200

    fix(plc4j/opm): Optimized the PlcEntityManager to reuse the proxies it creates to avoid issues with people not using the API correctly.
    
    fixes: #1935
---
 .../apache/plc4x/java/opm/PlcEntityManager.java    |  55 +++++++--
 .../opm/issues/i1935/ProxyClassCachingTest.java    | 137 +++++++++++++++++++++
 2 files changed, 179 insertions(+), 13 deletions(-)

diff --git a/plc4j/tools/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityManager.java b/plc4j/tools/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityManager.java
index e522e93d13..3f820c9dd2 100644
--- a/plc4j/tools/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityManager.java
+++ b/plc4j/tools/opm/src/main/java/org/apache/plc4x/java/opm/PlcEntityManager.java
@@ -19,6 +19,7 @@
 package org.apache.plc4x.java.opm;
 
 import net.bytebuddy.ByteBuddy;
+import net.bytebuddy.TypeCache;
 import net.bytebuddy.description.modifier.Visibility;
 import net.bytebuddy.implementation.MethodDelegation;
 import org.apache.commons.lang3.reflect.FieldUtils;
@@ -86,6 +87,13 @@ public class PlcEntityManager {
     public static final String LAST_FETCHED = "_lastFetched";
     public static final String LAST_WRITTEN = "_lastWritten";
 
+    /**
+     * Generated proxy classes, keyed by class loader and entity class. Soft references so the
+     * cache can't keep class loaders alive on its own.
+     */
+    private static final TypeCache<Class<?>> PROXY_CACHE =
+        new TypeCache.WithInlineExpunction<>(TypeCache.Sort.SOFT);
+
     private final PlcConnectionManager connectionManager;
     private final SimpleAliasRegistry registry;
 
@@ -142,19 +150,7 @@ public class PlcEntityManager {
     private <T> T connect(Class<T> clazz, String address, T existingInstance) throws OPMException {
         OpmUtils.getPlcEntityAndCheckPreconditions(clazz);
         try {
-            // Use Byte Buddy to generate a subclassed proxy that delegates all PlcField Methods
-            // to the intercept method
-            T instance = new ByteBuddy()
-                .subclass(clazz)
-                .defineField(PLC_ADDRESS_FIELD_NAME, String.class, Visibility.PRIVATE)
-                .defineField(CONNECTION_MANAGER_FIELD_NAME, PlcConnectionManager.class, Visibility.PRIVATE)
-                .defineField(ALIAS_REGISTRY, AliasRegistry.class, Visibility.PRIVATE)
-                .defineField(LAST_FETCHED, Map.class, Visibility.PRIVATE)
-                .defineField(LAST_WRITTEN, Map.class, Visibility.PRIVATE)
-                .method(not(isDeclaredBy(Object.class))).intercept(MethodDelegation.to(PlcEntityInterceptor.class))
-                .make()
-                .load(Thread.currentThread().getContextClassLoader())
-                .getLoaded()
+            T instance = proxyClassFor(clazz)
                 .getConstructor()
                 .newInstance();
             // Set connection value into the private field
@@ -184,6 +180,39 @@ public class PlcEntityManager {
         }
     }
 
+    /**
+     * Returns the generated proxy class for the given entity class, generating it on first use.
+     * <p>
+     * The generated type depends only on the entity class, so it is cached and reused - see
+     * GH-1935. Generating and loading a fresh subclass on every {@link #connect(Class, String)}
+     * (and therefore on every {@link #read(Class, String)} / {@link #write(Class, String, Object)})
+     * leaks one class per call, which fills up metaspace in any polling application.
+     */
+    @SuppressWarnings("unchecked")
+    private <T> Class<? extends T> proxyClassFor(Class<T> clazz) throws OPMException {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        try {
+            // The cache is keyed by class loader and entity class, so the same entity loaded by
+            // two class loaders correctly gets a proxy per loader. The monitor makes concurrent
+            // callers wait for one generation instead of racing to generate the same class.
+            return (Class<? extends T>) PROXY_CACHE.findOrInsert(classLoader, clazz,
+                () -> new ByteBuddy()
+                    .subclass(clazz)
+                    .defineField(PLC_ADDRESS_FIELD_NAME, String.class, Visibility.PRIVATE)
+                    .defineField(CONNECTION_MANAGER_FIELD_NAME, PlcConnectionManager.class, Visibility.PRIVATE)
+                    .defineField(ALIAS_REGISTRY, AliasRegistry.class, Visibility.PRIVATE)
+                    .defineField(LAST_FETCHED, Map.class, Visibility.PRIVATE)
+                    .defineField(LAST_WRITTEN, Map.class, Visibility.PRIVATE)
+                    .method(not(isDeclaredBy(Object.class))).intercept(MethodDelegation.to(PlcEntityInterceptor.class))
+                    .make()
+                    .load(classLoader)
+                    .getLoaded(),
+                PROXY_CACHE);
+        } catch (RuntimeException e) {
+            throw new OPMException("Unable to generate Proxy for " + clazz.getName(), e);
+        }
+    }
+
     private Object getValueFromField(Field field, Object object) {
         try {
             return field.get(object);
diff --git a/plc4j/tools/opm/src/test/java/org/apache/plc4x/java/opm/issues/i1935/ProxyClassCachingTest.java b/plc4j/tools/opm/src/test/java/org/apache/plc4x/java/opm/issues/i1935/ProxyClassCachingTest.java
new file mode 100644
index 0000000000..cf606df7d4
--- /dev/null
+++ b/plc4j/tools/opm/src/test/java/org/apache/plc4x/java/opm/issues/i1935/ProxyClassCachingTest.java
@@ -0,0 +1,137 @@
+/*
+ * 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.opm.issues.i1935;
+
+import org.apache.plc4x.java.DefaultPlcDriverManager;
+import org.apache.plc4x.java.api.types.PlcResponseCode;
+import org.apache.plc4x.java.mock.connection.MockConnection;
+import org.apache.plc4x.java.mock.connection.MockDevice;
+import org.apache.plc4x.java.opm.OPMException;
+import org.apache.plc4x.java.opm.PlcEntity;
+import org.apache.plc4x.java.opm.PlcEntityManager;
+import org.apache.plc4x.java.opm.PlcTag;
+import org.apache.plc4x.java.spi.drivers.messages.items.DefaultPlcResponseItem;
+import org.apache.plc4x.java.spi.values.PlcSTRING;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+/**
+ * The generated proxy type depends only on the entity class, so it has to be generated once and
+ * reused - see GH-1935. Generating one per call leaks a class per read, which is what filled up
+ * the reporter's heap.
+ */
+@ExtendWith(MockitoExtension.class)
+public class ProxyClassCachingTest {
+
+    private PlcEntityManager entityManager;
+
+    @Mock
+    MockDevice mockDevice;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        DefaultPlcDriverManager driverManager = new DefaultPlcDriverManager();
+        MockConnection connection = (MockConnection) driverManager.getConnection("mock:test");
+        when(mockDevice.read(any()))
+            .thenReturn(new DefaultPlcResponseItem<>(PlcResponseCode.OK, new PlcSTRING("hallo")));
+        connection.setDevice(mockDevice);
+        entityManager = new PlcEntityManager(driverManager);
+    }
+
+    @Test
+    void repeatedReadsReuseTheSameProxyClass() throws OPMException {
+        Set<Class<?>> proxyClasses = new HashSet<>();
+        for (int i = 0; i < 25; i++) {
+            proxyClasses.add(entityManager.read(SimpleEntity.class, "mock:test").getClass());
+        }
+
+        assertEquals(1, proxyClasses.size(),
+            "every read generated a new proxy class - the type cache is not being used");
+    }
+
+    @Test
+    void connectAndReadShareTheSameProxyClass() throws OPMException {
+        Object connected = entityManager.connect(SimpleEntity.class, "mock:test");
+        Object read = entityManager.read(SimpleEntity.class, "mock:test");
+
+        assertSame(connected.getClass(), read.getClass());
+    }
+
+    @Test
+    void separateEntityManagersShareTheSameProxyClass() throws Exception {
+        DefaultPlcDriverManager otherDriverManager = new DefaultPlcDriverManager();
+        MockConnection otherConnection = (MockConnection) otherDriverManager.getConnection("mock:other");
+        otherConnection.setDevice(mockDevice);
+        PlcEntityManager otherEntityManager = new PlcEntityManager(otherDriverManager);
+
+        Object first = entityManager.read(SimpleEntity.class, "mock:test");
+        Object second = otherEntityManager.read(SimpleEntity.class, "mock:other");
+
+        assertSame(first.getClass(), second.getClass());
+    }
+
+    @Test
+    void differentEntitiesGetDifferentProxyClasses() throws OPMException {
+        Object simple = entityManager.read(SimpleEntity.class, "mock:test");
+        Object other = entityManager.read(OtherEntity.class, "mock:test");
+
+        assertEquals(2, new HashSet<>(Set.of(simple.getClass(), other.getClass())).size());
+    }
+
+    @PlcEntity
+    public static class SimpleEntity {
+
+        @PlcTag("address")
+        private String field;
+
+        public SimpleEntity() {
+            // For OPM
+        }
+
+        public String getField() {
+            return field;
+        }
+    }
+
+    @PlcEntity
+    public static class OtherEntity {
+
+        @PlcTag("address")
+        private String field;
+
+        public OtherEntity() {
+            // For OPM
+        }
+
+        public String getField() {
+            return field;
+        }
+    }
+}


(plc4x) 02/02: docs(plc4j/eip): Updated the EIP driver documentation

Posted by cd...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

chrisdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 05a9be3da996860aa960e1f144391c9ae49dee9f
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Fri Aug 14 11:41:12 2026 +0200

    docs(plc4j/eip): Updated the EIP driver documentation
    
    fixes: #1481
---
 .../apache/plc4x/java/eip/base/tag/EipTagTest.java | 24 ++++++++++++++
 .../modules/users/pages/protocols/eip.adoc         | 37 ++++++++++++++++++----
 2 files changed, 54 insertions(+), 7 deletions(-)

diff --git a/plc4j/drivers/eip/src/test/java/org/apache/plc4x/java/eip/base/tag/EipTagTest.java b/plc4j/drivers/eip/src/test/java/org/apache/plc4x/java/eip/base/tag/EipTagTest.java
index 9932de7db4..982d23de14 100644
--- a/plc4j/drivers/eip/src/test/java/org/apache/plc4x/java/eip/base/tag/EipTagTest.java
+++ b/plc4j/drivers/eip/src/test/java/org/apache/plc4x/java/eip/base/tag/EipTagTest.java
@@ -35,4 +35,28 @@ public class EipTagTest {
         Assertions.assertEquals(eipTag.getElementNb(), 2);
     }
 
+    /**
+     * Every address form documented in the EtherNet/IP page (website/asciidoc, "Address Format")
+     * has to parse the way it is documented - see GH-1481, where the docs had the data type and
+     * the element count the wrong way round. If a form changes here, update the page too.
+     */
+    @Test
+    public void testDocumentedAddressForms() {
+        assertTag("myTag", "myTag", CIPDataTypeCode.DINT, 1);
+        assertTag("myTag:REAL", "myTag", CIPDataTypeCode.REAL, 1);
+        assertTag("myTag:4", "myTag", CIPDataTypeCode.DINT, 4);
+        assertTag("myArray[3]:DINT", "myArray[3]", CIPDataTypeCode.DINT, 1);
+        assertTag("myArray[0]:DINT:4", "myArray[0]", CIPDataTypeCode.DINT, 4);
+        // The '%' prefix is optional.
+        assertTag("%myTag:REAL", "%myTag", CIPDataTypeCode.REAL, 1);
+    }
+
+    private void assertTag(String address, String tag, CIPDataTypeCode type, int elementNb) {
+        EipTag eipTag = EipTag.of(address);
+        Assertions.assertNotNull(eipTag, address);
+        Assertions.assertEquals(tag, eipTag.getTag(), address);
+        Assertions.assertEquals(type, eipTag.getType(), address);
+        Assertions.assertEquals(elementNb, eipTag.getElementNb(), address);
+    }
+
 }
diff --git a/website/asciidoc/modules/users/pages/protocols/eip.adoc b/website/asciidoc/modules/users/pages/protocols/eip.adoc
index b0a9eef4b6..1ad647a620 100644
--- a/website/asciidoc/modules/users/pages/protocols/eip.adoc
+++ b/website/asciidoc/modules/users/pages/protocols/eip.adoc
@@ -33,28 +33,51 @@ To read and write data to a PLC4X device, the EtherNet/IP driver uses symbolic s
 
 ----
 
-%{tagname}[{startIndex}]:{numberOfElements}
-%{tagname}[{startIndex}]:{numberOfElements}:{DataType}
+{tagname}
+{tagname}:{DataType}
+{tagname}:{numberOfElements}
+{tagname}[{startIndex}]:{DataType}:{numberOfElements}
 
 ----
 [cols="2" ,options="header"]
 |===
 |Name |Description
-|Tagname |symbolic name of the Data
-|Start Index (optional)|if the data is an array, we can specify a starting index from where we want to read
-|Number of elements (optional)|if the data is an array, we can specify the number of elements we want to read. Use this in combination with the starting index to get the exact scope you want.
-|DataType (writing) |Specify the Data-type of the value you want to write (mandatory)
+|Tagname |symbolic name of the data. May optionally be prefixed with `%`.
+|Start Index (optional)|if the data is an array, we can specify a starting index from where we want to read. It is appended to the tag name and has to be the last part of it.
+|DataType (optional)|the data type of the value. Defaults to `DINT` when omitted, so for anything that is not a 32-bit integer it should be given explicitly - also when reading.
+|Number of elements (optional)|if the data is an array, we can specify the number of elements we want to read. Defaults to `1`. Use this in combination with the starting index to get the exact scope you want.
+|===
+
+NOTE: The data type comes *before* the number of elements. Reading four DINTs starting at index 0
+ is `myArray[0]:DINT:4`.
+
+.Examples
+[cols="2" ,options="header"]
+|===
+|Address |Meaning
+|`myTag` |a single element of `myTag`, decoded as `DINT`
+|`myTag:REAL` |a single element of `myTag`, decoded as `REAL`
+|`myTag:4` |four elements of `myTag`, decoded as `DINT`
+|`myArray[3]:DINT` |element 3 of `myArray`
+|`myArray[0]:DINT:4` |elements 0 to 3 of `myArray`, returned as a list
 |===
 
 === Data Types
+
+These are the data types the driver can encode and decode:
+
 [cols="2" ,options="header"]
 |===
 |To store a|Use this data type
 |Bit|BOOL
-|Bit array|DWORD (32-bit boolean array)
 |8-bit integer|SINT
 |16-bit integer|INT
 |32-bit integer|DINT
 |64-bit integer|LINT
 |32-bit float|REAL
+|64-bit float|LREAL
+|Character string|STRING
 |===
+
+NOTE: Other CIP data types are accepted by the address parser but are not encoded or decoded by the
+ driver; reading such a tag results in a response code of `INTERNAL_ERROR` rather than a value.