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

cziegeler pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git


The following commit(s) were added to refs/heads/master by this push:
     new 334fa10  SLING-12742 : Do not wrap IOException
334fa10 is described below

commit 334fa10956b101c24f62e22a54f33afc374085da
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Thu May 22 21:01:11 2025 +0200

    SLING-12742 : Do not wrap IOException
---
 .vscode/settings.json                              |  3 ++
 .../resource/internal/JcrModifiableValueMap.java   |  3 +-
 .../internal/helper/JcrPropertyMapCacheEntry.java  | 59 +++++++++++-----------
 .../helper/JcrPropertyMapCacheEntryTest.java       | 59 +++++++++++-----------
 4 files changed, 65 insertions(+), 59 deletions(-)

diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..8f2b711
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+    "java.compile.nullAnalysis.mode": "disabled"
+}
\ No newline at end of file
diff --git 
a/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
 
b/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
index efb2d90..fb9f86b 100644
--- 
a/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
+++ 
b/src/main/java/org/apache/sling/jcr/resource/internal/JcrModifiableValueMap.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.jcr.resource.internal;
 
+import java.io.IOException;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -72,7 +73,7 @@ public class JcrModifiableValueMap extends JcrValueMap 
implements ModifiableValu
             } else {
                 node.setProperty(name, entry.convertToType(Value.class, node, 
this.helper.getDynamicClassLoader()));
             }
-        } catch (final RepositoryException re) {
+        } catch (final IOException | RepositoryException re) {
             throw new IllegalArgumentException("Value of class '" + 
value.getClass() + "' for property '" + key + "' can't be put into node '" + 
getPath() + "'.", re);
         }
         this.valueCache.put(key, value);
diff --git 
a/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntry.java
 
b/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntry.java
index 21a2849..21ab333 100644
--- 
a/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntry.java
+++ 
b/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntry.java
@@ -83,7 +83,7 @@ public class JcrPropertyMapCacheEntry {
      * @param node the node
      * @throws RepositoryException if the provided value cannot be stored
      */
-    public JcrPropertyMapCacheEntry(final @NotNull Object value, final 
@NotNull Node node) throws RepositoryException {
+    public JcrPropertyMapCacheEntry(final @NotNull Object value, final 
@NotNull Node node) throws IOException, RepositoryException {
         this.property = null;
         this.propertyValue = value;
         this.isArray = value.getClass().isArray();
@@ -98,7 +98,7 @@ public class JcrPropertyMapCacheEntry {
         }
     }
 
-    private static void failIfCannotStore(final @NotNull Object value, final 
@NotNull Node node) throws RepositoryException {
+    private static void failIfCannotStore(final @NotNull Object value, final 
@NotNull Node node) throws IOException, RepositoryException {
         if (value instanceof InputStream) {
             // InputStream is storable and calling createValue for nothing
             // eats its contents
@@ -120,20 +120,16 @@ public class JcrPropertyMapCacheEntry {
      * @param  node the node
      * @return the converted value
      */
-    private static @Nullable Value createValue(final @NotNull Object obj, 
final @NotNull Node node) throws RepositoryException {
+    private static @Nullable Value createValue(final @NotNull Object obj, 
final @NotNull Node node) throws IOException, RepositoryException {
         final Session session = node.getSession();
         Value value = JcrResourceUtil.createValue(obj, session);
         if (value == null && obj instanceof Serializable) {
-            try {
-                final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                final ObjectOutputStream oos = new ObjectOutputStream(baos);
-                oos.writeObject(obj);
-                oos.close();
-                final ByteArrayInputStream bais = new 
ByteArrayInputStream(baos.toByteArray());
-                value = 
session.getValueFactory().createValue(session.getValueFactory().createBinary(bais));
-            } catch (IOException ioe) {
-                throw new RepositoryException("Cannot serialize object", ioe);
-            }
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final ObjectOutputStream oos = new ObjectOutputStream(baos);
+            oos.writeObject(obj);
+            oos.close();
+            final ByteArrayInputStream bais = new 
ByteArrayInputStream(baos.toByteArray());
+            value = 
session.getValueFactory().createValue(session.getValueFactory().createBinary(bais));
         }
         return value;
     }
@@ -207,7 +203,7 @@ public class JcrPropertyMapCacheEntry {
     @SuppressWarnings("unchecked")
     public @Nullable<T> T convertToType(final @NotNull Class<T> type,
                                         final @NotNull Node node,
-                                        final @Nullable ClassLoader 
dynamicClassLoader) {
+                                        final @Nullable ClassLoader 
dynamicClassLoader){
         T result = null;
 
         try {
@@ -234,7 +230,7 @@ public class JcrPropertyMapCacheEntry {
 
         } catch (final IllegalArgumentException | ValueFormatException vfe) {
             LOGGER.info("convertToType: Cannot convert value of {} to {}.", 
this.getPropertyValueOrNull(), type, vfe);
-        } catch (RepositoryException re) {
+        } catch (IOException | RepositoryException re) {
             LOGGER.info("convertToType: Cannot get value of {}", 
this.getPropertyValueOrNull(), re);
         }
 
@@ -245,7 +241,8 @@ public class JcrPropertyMapCacheEntry {
     private @NotNull<T> T[] convertToArray(final @NotNull Object source,
                                            final @NotNull Class<T> type,
                                            final @NotNull Node node,
-                                           final @Nullable ClassLoader 
dynamicClassLoader) throws RepositoryException {
+                                           final @Nullable ClassLoader 
dynamicClassLoader)
+    throws IOException, RepositoryException {
         List<T> values = new ArrayList<>();
         T value = convertToType(-1, source, type, node, dynamicClassLoader);
         if (value != null) {
@@ -256,11 +253,12 @@ public class JcrPropertyMapCacheEntry {
         T[] result = (T[]) Array.newInstance(type, values.size());
         return values.toArray(result);
     }
-    
+
     private @NotNull<T> T[] convertToArray(final @NotNull Object[] sourceArray,
                                            final @NotNull Class<T> type,
                                            final @NotNull Node node,
-                                           final @Nullable ClassLoader 
dynamicClassLoader) throws RepositoryException {
+                                           final @Nullable ClassLoader 
dynamicClassLoader)
+    throws IOException, RepositoryException {
         List<T> values = new ArrayList<>();
         for (int i = 0; i < sourceArray.length; i++) {
             T value = convertToType(i, sourceArray[i], type, node, 
dynamicClassLoader);
@@ -280,23 +278,25 @@ public class JcrPropertyMapCacheEntry {
                                          final @NotNull Object initialValue,
                                          final @NotNull Class<T> type,
                                          final @NotNull Node node,
-                                         final @Nullable ClassLoader 
dynamicClassLoader) throws RepositoryException {
+                                         final @Nullable ClassLoader 
dynamicClassLoader)
+    throws IOException, RepositoryException {
         if (type.isInstance(initialValue)) {
             return (T) initialValue;
         }
-        
+
         if (initialValue instanceof InputStream) {
             return convertInputStream(index, (InputStream) initialValue, type, 
node, dynamicClassLoader);
         } else {
             return convert(initialValue, type, node);
         }
     }
-    
+
     private @Nullable <T> T convertInputStream(int index,
                                                final @NotNull InputStream 
value,
                                                final @NotNull Class<T> type,
                                                final @NotNull Node node,
-                                               final @Nullable ClassLoader 
dynamicClassLoader) throws RepositoryException {
+                                               final @Nullable ClassLoader 
dynamicClassLoader)
+    throws IOException, RepositoryException {
         // object input stream
         if (ObjectInputStream.class.isAssignableFrom(type)) {
             try {
@@ -312,11 +312,11 @@ public class JcrPropertyMapCacheEntry {
                 return null;
             }
             return convert(propertyToLength(property, index), type, node);
-            
+
         // string: read binary
         } else if (String.class == type) {
             return (T) inputStreamToString(value);
-            
+
         // any serializable
         } else if (Serializable.class.isAssignableFrom(type)) {
             try (ObjectInputStream ois = new PropertyObjectInputStream(value, 
dynamicClassLoader)) {
@@ -329,12 +329,12 @@ public class JcrPropertyMapCacheEntry {
                 // ignore and use fallback
             }
             // ignore
-        } 
-        
+        }
+
         // fallback
         return convert(value, type, node);
     }
-    
+
     private static @NotNull Long propertyToLength(@NotNull Property property, 
int index) throws RepositoryException {
         if (index == -1) {
             return Long.valueOf(property.getLength());
@@ -342,7 +342,7 @@ public class JcrPropertyMapCacheEntry {
             return Long.valueOf(property.getLengths()[index]);
         }
     }
-    
+
     private static @NotNull String inputStreamToString(@NotNull InputStream 
value) {
         try (InputStream in = value) {
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -361,7 +361,8 @@ public class JcrPropertyMapCacheEntry {
 
     private @Nullable <T> T convert(final @NotNull Object value,
                                    final @NotNull Class<T> type,
-                                   final @NotNull Node node) throws 
RepositoryException {
+                                   final @NotNull Node node)
+    throws IOException, RepositoryException {
         if (String.class == type) {
             return (T) getConverter(value).toString();
 
diff --git 
a/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntryTest.java
 
b/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntryTest.java
index d9a9f60..3f301a1 100644
--- 
a/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntryTest.java
+++ 
b/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntryTest.java
@@ -32,6 +32,7 @@ import static org.mockito.Mockito.when;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
@@ -66,7 +67,7 @@ public class JcrPropertyMapCacheEntryTest {
     private final ValueFactory vf = ValueFactoryImpl.getInstance();
     private final Session session = mock(Session.class);
     private final Node node = mock(Node.class);
-    
+
     @Before
     public void before() throws Exception {
         when(session.getValueFactory()).thenReturn(vf);
@@ -128,7 +129,7 @@ public class JcrPropertyMapCacheEntryTest {
         new JcrPropertyMapCacheEntry(new char[0], node);
         verifyNoMoreInteractions(node);
     }
-    
+
     @Test(expected = IllegalArgumentException.class)
     public void testCannotStore() throws Exception {
         Object value = new TestClass();
@@ -140,13 +141,13 @@ public class JcrPropertyMapCacheEntryTest {
         Object value = new TestClass();
         new JcrPropertyMapCacheEntry(new Object[] {value}, node);
     }
-    
+
     @Test
     public void testGetPropertyValueOrNull() throws Exception {
         JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(true, 
node);
         assertEquals(Boolean.TRUE, entry.getPropertyValueOrNull());
     }
-    
+
     @Test
     public void testGetPropertyValueOrNullWithRepositoryException() throws 
Exception {
         Property prop = mock(Property.class);
@@ -155,12 +156,12 @@ public class JcrPropertyMapCacheEntryTest {
         JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(prop);
         assertNull(entry.getPropertyValueOrNull());
     }
-    
+
     @Test
     public void testInputStreamToString() throws Exception {
         InputStream in = new ByteArrayInputStream("test".getBytes());
         JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(in, 
node);
-        
+
         String result = entry.convertToType(String.class, node, null);
         assertEquals("test", result);
         verifyNoMoreInteractions(node);
@@ -186,7 +187,7 @@ public class JcrPropertyMapCacheEntryTest {
         assertEquals(0, result.length);
         verifyNoMoreInteractions(node);
     }
-    
+
     @Test
     public void testBinaryPropertyToInteger() throws Exception {
         Property prop = mock(Property.class);
@@ -194,11 +195,11 @@ public class JcrPropertyMapCacheEntryTest {
         when(prop.getStream()).thenReturn(new 
ByteArrayInputStream("10".getBytes()));
         when(prop.getValue()).thenReturn(vf.createValue(new 
ByteArrayInputStream("10".getBytes())));
         when(prop.getLength()).thenReturn(2L);
-        
+
         JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(prop);
         Integer result = entry.convertToType(Integer.class, node, null);
         assertEquals(Integer.valueOf(2), result);
-        
+
         verify(prop, times(2)).isMultiple();
         verify(prop).getValue();
         verify(prop).getType();
@@ -221,7 +222,7 @@ public class JcrPropertyMapCacheEntryTest {
         assertNotNull(result);
         assertEquals(1, result.length);
         assertEquals(Double.valueOf(4.0), result[0]);
-        
+
         verify(prop, times(2)).isMultiple();
         verify(prop).getValue();
         verify(prop).getType();
@@ -254,7 +255,7 @@ public class JcrPropertyMapCacheEntryTest {
         verifyNoMoreInteractions(prop);
         verifyNoMoreInteractions(node);
     }
-    
+
     @Test
     public void testInputStreamToObjectInputStream() throws Exception {
         InputStream in = new ByteArrayInputStream("value".getBytes());
@@ -271,7 +272,7 @@ public class JcrPropertyMapCacheEntryTest {
         try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
             oos.writeObject(Maps.newHashMap());
         }
-        
+
         JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(new 
ByteArrayInputStream(out.toByteArray()), node);
         // same type
         Map<?,?> result = entry.convertToType(HashMap.class, node, null);
@@ -293,7 +294,7 @@ public class JcrPropertyMapCacheEntryTest {
 
         verifyNoMoreInteractions(node);
     }
-    
+
     @Test
     public void testBinaryPropertyToObjectInputStream() throws Exception {
         Property prop = mock(Property.class);
@@ -311,7 +312,7 @@ public class JcrPropertyMapCacheEntryTest {
         verifyNoMoreInteractions(prop);
         verifyNoMoreInteractions(node);
     }
-    
+
     @Test
     public void testMvPropertyToBoolean() throws Exception {
         Property prop = mock(Property.class);
@@ -323,7 +324,7 @@ public class JcrPropertyMapCacheEntryTest {
 
         JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(prop);
         assertTrue(entry.isArray());
-        
+
         Boolean result = entry.convertToType(Boolean.class, node, null);
         assertNotNull(result);
         assertTrue(result);
@@ -346,7 +347,7 @@ public class JcrPropertyMapCacheEntryTest {
 
         JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(prop);
         assertTrue(entry.isArray());
-        
+
         String result = entry.convertToType(String.class, node, null);
         assertNull(result);
 
@@ -356,7 +357,7 @@ public class JcrPropertyMapCacheEntryTest {
         verifyNoMoreInteractions(prop);
         verifyNoMoreInteractions(node);
     }
-    
+
     @Test
     public void testConversionFails() throws RepositoryException {
         Property prop = mock(Property.class);
@@ -398,14 +399,14 @@ public class JcrPropertyMapCacheEntryTest {
         verify(node).getSession();
         verifyNoMoreInteractions(prop, node);
     }
-    
+
     @Test
     public void testConvertToSameType() throws Exception {
         Calendar cal = Calendar.getInstance();
-        
+
         JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(cal, 
node);
         Calendar result = entry.convertToType(Calendar.class, node, null);
-        
+
         assertSame(cal, result);
         verify(node).getSession();
         verifyNoMoreInteractions(node);
@@ -416,7 +417,7 @@ public class JcrPropertyMapCacheEntryTest {
         JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry("value", 
node);
         Property result = entry.convertToType(Property.class, node, null);
         assertNull(result); // TODO is this expected?
-        
+
         verify(node).getSession();
         verify(session).getValueFactory();
         verifyNoMoreInteractions(node, session);
@@ -427,11 +428,11 @@ public class JcrPropertyMapCacheEntryTest {
         Property prop = mock(Property.class);
         when(prop.getType()).thenReturn(PropertyType.BOOLEAN);
         when(prop.getValue()).thenReturn(BooleanValue.valueOf("true"));
-        
+
         JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(prop);
         Property result = entry.convertToType(Property.class, node, null);
         assertSame(prop, result);
-        
+
         verifyNoMoreInteractions(node);
         verify(prop).getType();
         verify(prop).getValue();
@@ -446,25 +447,25 @@ public class JcrPropertyMapCacheEntryTest {
         Object propValue = entry.getPropertyValue();
         assertTrue(propValue instanceof HashMap);
     }
-    
+
     @Test(expected = IllegalArgumentException.class)
     public void testCreateFromNonSerializableComplexValue() throws Exception {
         Object value = new TestClass();
         new JcrPropertyMapCacheEntry(value, node);
     }
-    
+
     private static final class TestClass {}
-    
-    @Test(expected = RepositoryException.class)
+
+    @Test(expected = IOException.class)
     public void testCreateFromSerializeComplexValueWithUnserializableField() 
throws Exception {
         // this class cannot be serialized and throws an exception at runtime 
(as Optional is not serializable)
         Object value = new TestClass2();
         new JcrPropertyMapCacheEntry(value, node);
     }
-    
+
     private static final class TestClass2 implements Serializable {
         Optional<String> value;
-        
+
         public TestClass2() {
             this.value = Optional.empty();
         }

Reply via email to