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

garydgregory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/commons-xml.git

commit 8bea1be4dc73cb281ddb94cd14f30c306995e1b4
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Aug 28 15:16:10 2026 -0400

    Reduce AI copy-pasta.
---
 .../org/apache/commons/xml/MethodHandleFactory.java  | 20 +++++++++++++++++++-
 .../commons/xml/SecureDocumentBuilderFactory.java    | 11 +----------
 .../apache/commons/xml/SecureSAXParserFactory.java   | 11 +----------
 .../org/apache/commons/xml/SecureSchemaFactory.java  | 11 +----------
 .../apache/commons/xml/SecureTransformerFactory.java | 11 +----------
 .../apache/commons/xml/SecureXMLInputFactory.java    | 11 +----------
 .../org/apache/commons/xml/ShadingFootprintTest.java | 12 ++++++++----
 7 files changed, 32 insertions(+), 55 deletions(-)

diff --git a/src/main/java/org/apache/commons/xml/MethodHandleFactory.java 
b/src/main/java/org/apache/commons/xml/MethodHandleFactory.java
index 18d11bb..d55b222 100644
--- a/src/main/java/org/apache/commons/xml/MethodHandleFactory.java
+++ b/src/main/java/org/apache/commons/xml/MethodHandleFactory.java
@@ -26,6 +26,12 @@
  */
 class MethodHandleFactory {
 
+    @FunctionalInterface
+    interface ThrowableCallable<V> {
+
+        V call() throws Throwable;
+    }
+
     /**
      * Finds a static method handle for the given class, method name, and 
method type.
      *
@@ -33,7 +39,7 @@ class MethodHandleFactory {
      * @param name the name of the method.
      * @param type the method type.
      * @return the method handle, or {@code null} if not found.
-     * @throws SecurityException if a security manager is present and it <a 
href="MethodHandles.Lookup.html#secmgr">refuses access</a>.
+     * @throws SecurityException    if a security manager is present and it <a 
href="MethodHandles.Lookup.html#secmgr">refuses access</a>.
      * @throws NullPointerException if any argument is null.
      */
     static MethodHandle findStatic(final Class<?> refc, final String name, 
final MethodType type) {
@@ -43,4 +49,16 @@ static MethodHandle findStatic(final Class<?> refc, final 
String name, final Met
             return null;
         }
     }
+
+    static <T, E extends Error> T invokeExact(final ThrowableCallable<T> 
methodHandle, final Class<E> rethrow) throws E {
+        try {
+            return methodHandle.call();
+        } catch (final Throwable e) {
+            if (e.getClass().isInstance(rethrow)) {
+                throw rethrow.cast(e);
+            }
+            // Unreachable: the looked-up method declares no other exceptions.
+            throw new IllegalStateException(e);
+        }
+    }
 }
diff --git 
a/src/main/java/org/apache/commons/xml/SecureDocumentBuilderFactory.java 
b/src/main/java/org/apache/commons/xml/SecureDocumentBuilderFactory.java
index 1e9ea67..c4719a8 100644
--- a/src/main/java/org/apache/commons/xml/SecureDocumentBuilderFactory.java
+++ b/src/main/java/org/apache/commons/xml/SecureDocumentBuilderFactory.java
@@ -213,16 +213,7 @@ private static DocumentBuilderFactory makeNSAware(final 
DocumentBuilderFactory f
      */
     public static DocumentBuilderFactory newDefaultInstance() {
         if (MH_newDefaultInstance != null) {
-            final DocumentBuilderFactory factory;
-            try {
-                factory = (DocumentBuilderFactory) 
MH_newDefaultInstance.invokeExact();
-            } catch (final FactoryConfigurationError e) {
-                throw e;
-            } catch (final Throwable e) {
-                // Unreachable: the looked-up method declares no other 
exceptions.
-                throw new IllegalStateException(e);
-            }
-            return secure(factory);
+            return secure(MethodHandleFactory.invokeExact(() -> 
(DocumentBuilderFactory) MH_newDefaultInstance.invokeExact(), 
FactoryConfigurationError.class));
         }
         // Java 8: the method does not exist; instantiate the JDK's built-in 
default by its class name instead. Where that class does not exist either (for
         // example Android), the lookup miss surfaces as the factory's own 
FactoryConfigurationError, like any newInstance miss.
diff --git a/src/main/java/org/apache/commons/xml/SecureSAXParserFactory.java 
b/src/main/java/org/apache/commons/xml/SecureSAXParserFactory.java
index e7c9650..52d8256 100644
--- a/src/main/java/org/apache/commons/xml/SecureSAXParserFactory.java
+++ b/src/main/java/org/apache/commons/xml/SecureSAXParserFactory.java
@@ -205,16 +205,7 @@ private static SAXParserFactory makeNSAware(final 
SAXParserFactory factory) {
      */
     public static SAXParserFactory newDefaultInstance() {
         if (MH_newDefaultInstance != null) {
-            final SAXParserFactory factory;
-            try {
-                factory = (SAXParserFactory) 
MH_newDefaultInstance.invokeExact();
-            } catch (final FactoryConfigurationError e) {
-                throw e;
-            } catch (final Throwable e) {
-                // Unreachable: the looked-up method declares no other 
exceptions.
-                throw new IllegalStateException(e);
-            }
-            return secure(factory);
+            return secure(MethodHandleFactory.invokeExact(() -> 
(SAXParserFactory) MH_newDefaultInstance.invokeExact(), 
FactoryConfigurationError.class));
         }
         // Java 8: the method does not exist; instantiate the JDK's built-in 
default by its class name instead. Where that class does not exist either (for
         // example Android), the lookup miss surfaces as the factory's own 
FactoryConfigurationError, like any newInstance miss.
diff --git a/src/main/java/org/apache/commons/xml/SecureSchemaFactory.java 
b/src/main/java/org/apache/commons/xml/SecureSchemaFactory.java
index bf25765..8a0d40b 100644
--- a/src/main/java/org/apache/commons/xml/SecureSchemaFactory.java
+++ b/src/main/java/org/apache/commons/xml/SecureSchemaFactory.java
@@ -225,16 +225,7 @@ public void setResourceResolver(final LSResourceResolver 
resourceResolver) {
      */
     public static SchemaFactory newDefaultInstance() {
         if (MH_newDefaultInstance != null) {
-            final SchemaFactory factory;
-            try {
-                factory = (SchemaFactory) MH_newDefaultInstance.invokeExact();
-            } catch (final SchemaFactoryConfigurationError e) {
-                throw e;
-            } catch (final Throwable e) {
-                // Unreachable: the looked-up method declares no other 
exceptions.
-                throw new IllegalStateException(e);
-            }
-            return secure(factory);
+            return secure(MethodHandleFactory.invokeExact(() -> 
(SchemaFactory) MH_newDefaultInstance.invokeExact(), 
SchemaFactoryConfigurationError.class));
         }
         // Java 8: the method does not exist; instantiate the JDK's built-in 
default by its class name instead. Where that class does not exist either (for
         // example Android), the lookup miss surfaces as 
IllegalArgumentException, the error SchemaFactory.newInstance(String, String, 
ClassLoader) defines.
diff --git a/src/main/java/org/apache/commons/xml/SecureTransformerFactory.java 
b/src/main/java/org/apache/commons/xml/SecureTransformerFactory.java
index 9dc15ac..9079f0b 100644
--- a/src/main/java/org/apache/commons/xml/SecureTransformerFactory.java
+++ b/src/main/java/org/apache/commons/xml/SecureTransformerFactory.java
@@ -377,16 +377,7 @@ public void setURIResolver(final URIResolver resolver) {
      */
     public static TransformerFactory newDefaultInstance() {
         if (MH_newDefaultInstance != null) {
-            final TransformerFactory factory;
-            try {
-                factory = (TransformerFactory) 
MH_newDefaultInstance.invokeExact();
-            } catch (final TransformerFactoryConfigurationError e) {
-                throw e;
-            } catch (final Throwable e) {
-                // Unreachable: the looked-up method declares no other 
exceptions.
-                throw new IllegalStateException(e);
-            }
-            return secure(factory);
+            return secure(MethodHandleFactory.invokeExact(() -> 
(TransformerFactory) MH_newDefaultInstance.invokeExact(), 
TransformerFactoryConfigurationError.class));
         }
         // Java 8: the method does not exist; instantiate the JDK's built-in 
default by its class name instead. Where that class does not exist either (for
         // example Android), the lookup miss surfaces as 
TransformerFactoryConfigurationError, like any newInstance miss.
diff --git a/src/main/java/org/apache/commons/xml/SecureXMLInputFactory.java 
b/src/main/java/org/apache/commons/xml/SecureXMLInputFactory.java
index 3ef9196..4ed2ab6 100644
--- a/src/main/java/org/apache/commons/xml/SecureXMLInputFactory.java
+++ b/src/main/java/org/apache/commons/xml/SecureXMLInputFactory.java
@@ -272,16 +272,7 @@ public void setXMLResolver(final XMLResolver resolver) {
      */
     public static XMLInputFactory newDefaultFactory() {
         if (MH_newDefaultInstance != null) {
-            final XMLInputFactory factory;
-            try {
-                factory = (XMLInputFactory) 
MH_newDefaultInstance.invokeExact();
-            } catch (final FactoryConfigurationError e) {
-                throw e;
-            } catch (final Throwable e) {
-                // Unreachable: the looked-up method declares no other 
exceptions.
-                throw new IllegalStateException(e);
-            }
-            return secure(factory);
+            return secure(MethodHandleFactory.invokeExact(() -> 
(XMLInputFactory) MH_newDefaultInstance.invokeExact(), 
FactoryConfigurationError.class));
         }
         try {
             // Java 8: the method does not exist, and XMLInputFactory has no 
class-name-taking lookup; instantiate the JDK's built-in default directly.
diff --git a/src/test/java/org/apache/commons/xml/ShadingFootprintTest.java 
b/src/test/java/org/apache/commons/xml/ShadingFootprintTest.java
index a010752..47f8934 100644
--- a/src/test/java/org/apache/commons/xml/ShadingFootprintTest.java
+++ b/src/test/java/org/apache/commons/xml/ShadingFootprintTest.java
@@ -63,7 +63,8 @@ class ShadingFootprintTest {
             "SecureDocumentBuilderFactory$1",
             "SecureDocumentBuilderFactory$Wrapper",
             "SecureException",
-            "MethodHandleFactory");
+            "MethodHandleFactory",
+            "MethodHandleFactory$ThrowableCallable");
     // @formatter:on
 
     // @formatter:off
@@ -76,7 +77,8 @@ class ShadingFootprintTest {
             "SecureSAXParserFactory$SecureExpatXMLReader",
             "SecureSAXParserFactory$Wrapper",
             "SecureXMLReader",
-            "MethodHandleFactory");
+            "MethodHandleFactory",
+            "MethodHandleFactory$ThrowableCallable");
     // @formatter:on
 
     // @formatter:off
@@ -86,7 +88,8 @@ class ShadingFootprintTest {
             "SecureXMLInputFactory",
             "SecureXMLInputFactory$1",
             "SecureXMLInputFactory$Wrapper",
-            "MethodHandleFactory");
+            "MethodHandleFactory",
+            "MethodHandleFactory$ThrowableCallable");
     // @formatter:on
 
     /**
@@ -132,6 +135,7 @@ class ShadingFootprintTest {
             "SecureDocumentBuilderFactory$1",
             "SecureDocumentBuilderFactory$Wrapper",
             "MethodHandleFactory",
+            "MethodHandleFactory$ThrowableCallable",
             "SecureException",
             "SecureSAXParser",
             "SecureSAXParserFactory",
@@ -172,7 +176,7 @@ class ShadingFootprintTest {
     /**
      * Class count of the {@link #rootClosure()} DOM entry point, the baseline 
the {@link #reportFootprint()} percentages are computed against.
      */
-    private static final int LIBRARY_CLASS_COUNT = 7;
+    private static final int LIBRARY_CLASS_COUNT = 8;
 
     /**
      * Entry points reported by the {@link #reportFootprint()} diagnostic, 
most-focused first, ending with the whole library.

Reply via email to