ppkarwasz commented on code in PR #799:
URL: https://github.com/apache/commons-io/pull/799#discussion_r2430000682


##########
src/main/java/org/apache/commons/io/channels/CloseShieldChannel.java:
##########
@@ -66,10 +69,32 @@ public static <T extends Channel> T wrap(final T channel) {
         // Collect only Channel sub-interfaces.
         final Set<Class<?>> set = collectChannelInterfaces(channel.getClass(), 
new LinkedHashSet<>());
         // fallback to root surface
-        return (T) Proxy.newProxyInstance(channel.getClass().getClassLoader(), 
// use delegate's loader
+        return (Channel) 
Proxy.newProxyInstance(channel.getClass().getClassLoader(), // use delegate's 
loader
                 set.isEmpty() ? new Class<?>[] { Channel.class } : 
set.toArray(EMPTY), new CloseShieldChannelHandler(channel));
     }
 
+    /**
+     * Wraps a channel to shield it from being closed.
+     *
+     * @param channel The underlying channel to shield, not {@code null} and 
must implement {@code type}.
+     * @param type    The interface the returned proxy must implement;
+     *                the proxy will also implement all other {@link Channel} 
sub-interfaces that the delegate implements.
+     * @param <T>     A type that extends {@link Channel}.
+     * @return A proxy that shields {@code close()} and enforces closed 
semantics on other calls.
+     * @throws IllegalArgumentException if {@code type} is not an interface or 
if {@code channel} does not implement {@code type}.
+     */
+    @SuppressWarnings({ "unchecked", "resource" }) // caller closes
+    public static <T extends Channel> T wrap(final T channel, Class<T> type) {
+        Objects.requireNonNull(type, "type");
+        if (!type.isInterface()) {
+            throw new IllegalArgumentException(type.getName() + " is not an 
interface");
+        }
+        if (!type.isInstance(channel)) {
+            throw new IllegalArgumentException("channel of type " + 
channel.getClass().getName() + " is not an instance of " + type.getName());

Review Comment:
   I must say this method leaves a bitter aftertaste: yes, you are right, 
`ClassCastException` would be more appropriate, but in that case we should just 
remove it and use explicit casts.
   
   I removed this in 
https://github.com/apache/commons-io/pull/799/commits/7a0608f5b755e989930add275e3376d27cdb098c



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to