You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2026/08/28 11:16:18 UTC

(commons-xml) branch main updated: Refactor AI copy-pasta slop.

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


The following commit(s) were added to refs/heads/main by this push:
     new 0befc63  Refactor AI copy-pasta slop.
0befc63 is described below

commit 0befc637d16484961f5fa7357eda4c919537088c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Aug 28 07:16:11 2026 -0400

    Refactor AI copy-pasta slop.
    
    Refactor into MethodHandleFactory
---
 .../xml/HardeningDocumentBuilderFactory.java       | 13 +-----
 .../commons/xml/HardeningSAXParserFactory.java     | 13 +-----
 .../apache/commons/xml/HardeningSchemaFactory.java | 14 +------
 .../commons/xml/HardeningTransformerFactory.java   | 14 +------
 .../commons/xml/HardeningXMLInputFactory.java      | 14 +------
 .../apache/commons/xml/HardeningXPathFactory.java  | 14 +------
 .../apache/commons/xml/MethodHandleFactory.java    | 46 ++++++++++++++++++++++
 .../apache/commons/xml/ShadingFootprintTest.java   | 12 ++++--
 8 files changed, 66 insertions(+), 74 deletions(-)

diff --git a/src/main/java/org/apache/commons/xml/HardeningDocumentBuilderFactory.java b/src/main/java/org/apache/commons/xml/HardeningDocumentBuilderFactory.java
index 1c16de6..d9cc806 100644
--- a/src/main/java/org/apache/commons/xml/HardeningDocumentBuilderFactory.java
+++ b/src/main/java/org/apache/commons/xml/HardeningDocumentBuilderFactory.java
@@ -18,7 +18,6 @@
 package org.apache.commons.xml;
 
 import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 import java.util.Objects;
 
@@ -54,16 +53,8 @@ public final class HardeningDocumentBuilderFactory {
     /** Class name of the JDK's built-in default implementation, the Java 8 fallback for {@link #newDefaultInstance()}. */
     private static final String JDK_DOCUMENT_BUILDER_FACTORY = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
 
-    private static final MethodHandle NEW_DEFAULT_INSTANCE = findStatic("newDefaultInstance", MethodType.methodType(DocumentBuilderFactory.class));
-
-    private static MethodHandle findStatic(final String name, final MethodType type) {
-        try {
-            return MethodHandles.publicLookup().findStatic(DocumentBuilderFactory.class, name, type);
-        } catch (final ReflectiveOperationException e) {
-            // The method is absent: the running platform predates it.
-            return null;
-        }
-    }
+    private static final MethodHandle NEW_DEFAULT_INSTANCE = MethodHandleFactory.findStatic(DocumentBuilderFactory.class, "newDefaultInstance",
+            MethodType.methodType(DocumentBuilderFactory.class));
 
     /**
      * Capability-driven hardening for any {@link DocumentBuilderFactory} on the classpath.
diff --git a/src/main/java/org/apache/commons/xml/HardeningSAXParserFactory.java b/src/main/java/org/apache/commons/xml/HardeningSAXParserFactory.java
index 9101acd..5e6a84f 100644
--- a/src/main/java/org/apache/commons/xml/HardeningSAXParserFactory.java
+++ b/src/main/java/org/apache/commons/xml/HardeningSAXParserFactory.java
@@ -18,7 +18,6 @@
 package org.apache.commons.xml;
 
 import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 import java.util.Objects;
 
@@ -64,16 +63,8 @@ public final class HardeningSAXParserFactory {
     /** Class name of the JDK's built-in default implementation, the Java 8 fallback for {@link #newDefaultInstance()}. */
     private static final String JDK_SAX_PARSER_FACTORY = "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl";
 
-    private static final MethodHandle NEW_DEFAULT_INSTANCE = findStatic("newDefaultInstance", MethodType.methodType(SAXParserFactory.class));
-
-    private static MethodHandle findStatic(final String name, final MethodType type) {
-        try {
-            return MethodHandles.publicLookup().findStatic(SAXParserFactory.class, name, type);
-        } catch (final ReflectiveOperationException e) {
-            // The method is absent: the running platform predates it.
-            return null;
-        }
-    }
+    private static final MethodHandle NEW_DEFAULT_INSTANCE = MethodHandleFactory.findStatic(SAXParserFactory.class, "newDefaultInstance",
+            MethodType.methodType(SAXParserFactory.class));
 
     /**
      * Capability-driven hardening for any {@link SAXParserFactory} on the classpath.
diff --git a/src/main/java/org/apache/commons/xml/HardeningSchemaFactory.java b/src/main/java/org/apache/commons/xml/HardeningSchemaFactory.java
index 539b8cf..c97cc48 100644
--- a/src/main/java/org/apache/commons/xml/HardeningSchemaFactory.java
+++ b/src/main/java/org/apache/commons/xml/HardeningSchemaFactory.java
@@ -18,7 +18,6 @@
 package org.apache.commons.xml;
 
 import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 import java.util.Objects;
 
@@ -62,17 +61,8 @@ public final class HardeningSchemaFactory {
     /** Class name of the JDK's built-in default implementation, the Java 8 fallback for {@link #newDefaultInstance()}. */
     private static final String JDK_SCHEMA_FACTORY = "com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory";
 
-    private static final MethodHandle NEW_DEFAULT_INSTANCE = findNewDefaultInstance();
-
-    private static MethodHandle findNewDefaultInstance() {
-        try {
-            return MethodHandles.publicLookup().findStatic(SchemaFactory.class, "newDefaultInstance",
-                    MethodType.methodType(SchemaFactory.class));
-        } catch (final ReflectiveOperationException e) {
-            // The method is absent: the running platform predates it.
-            return null;
-        }
-    }
+    private static final MethodHandle NEW_DEFAULT_INSTANCE = MethodHandleFactory.findStatic(SchemaFactory.class, "newDefaultInstance",
+            MethodType.methodType(SchemaFactory.class));
 
     /**
      * Hardening for any {@link SchemaFactory} on the classpath.
diff --git a/src/main/java/org/apache/commons/xml/HardeningTransformerFactory.java b/src/main/java/org/apache/commons/xml/HardeningTransformerFactory.java
index 7f030c5..b73c4d8 100644
--- a/src/main/java/org/apache/commons/xml/HardeningTransformerFactory.java
+++ b/src/main/java/org/apache/commons/xml/HardeningTransformerFactory.java
@@ -19,7 +19,6 @@
 
 import java.io.IOException;
 import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 import java.util.Objects;
 import java.util.function.Supplier;
@@ -80,17 +79,8 @@ public final class HardeningTransformerFactory {
     /** Class name of the JDK's built-in default implementation, the Java 8 fallback for {@link #newDefaultInstance()}. */
     private static final String JDK_TRANSFORMER_FACTORY = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
 
-    private static final MethodHandle NEW_DEFAULT_INSTANCE = findNewDefaultInstance();
-
-    private static MethodHandle findNewDefaultInstance() {
-        try {
-            return MethodHandles.publicLookup().findStatic(TransformerFactory.class, "newDefaultInstance",
-                    MethodType.methodType(TransformerFactory.class));
-        } catch (final ReflectiveOperationException e) {
-            // The method is absent: the running platform predates it.
-            return null;
-        }
-    }
+    private static final MethodHandle NEW_DEFAULT_INSTANCE = MethodHandleFactory.findStatic(TransformerFactory.class, "newDefaultInstance",
+            MethodType.methodType(TransformerFactory.class));
 
     /**
      * Capability-driven hardening for any {@link TransformerFactory} on the classpath.
diff --git a/src/main/java/org/apache/commons/xml/HardeningXMLInputFactory.java b/src/main/java/org/apache/commons/xml/HardeningXMLInputFactory.java
index 6f15542..c438154 100644
--- a/src/main/java/org/apache/commons/xml/HardeningXMLInputFactory.java
+++ b/src/main/java/org/apache/commons/xml/HardeningXMLInputFactory.java
@@ -20,7 +20,6 @@
 import java.io.InputStream;
 import java.io.Reader;
 import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 import java.util.Objects;
 
@@ -59,17 +58,8 @@ public final class HardeningXMLInputFactory {
     /** Class name of the JDK's built-in default implementation, the Java 8 fallback for {@link #newDefaultFactory()}. */
     private static final String JDK_XML_INPUT_FACTORY = "com.sun.xml.internal.stream.XMLInputFactoryImpl";
 
-    private static final MethodHandle NEW_DEFAULT_FACTORY = findNewDefaultFactory();
-
-    private static MethodHandle findNewDefaultFactory() {
-        try {
-            return MethodHandles.publicLookup().findStatic(XMLInputFactory.class, "newDefaultFactory",
-                    MethodType.methodType(XMLInputFactory.class));
-        } catch (final ReflectiveOperationException e) {
-            // The method is absent: the running platform predates it.
-            return null;
-        }
-    }
+    private static final MethodHandle NEW_DEFAULT_FACTORY = MethodHandleFactory.findStatic(XMLInputFactory.class, "newDefaultFactory",
+            MethodType.methodType(XMLInputFactory.class));
 
     /**
      * Capability-driven hardening for any {@link XMLInputFactory} (StAX) on the classpath.
diff --git a/src/main/java/org/apache/commons/xml/HardeningXPathFactory.java b/src/main/java/org/apache/commons/xml/HardeningXPathFactory.java
index cde63ab..abfa65a 100644
--- a/src/main/java/org/apache/commons/xml/HardeningXPathFactory.java
+++ b/src/main/java/org/apache/commons/xml/HardeningXPathFactory.java
@@ -18,7 +18,6 @@
 package org.apache.commons.xml;
 
 import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
 import java.util.Objects;
 
@@ -51,17 +50,8 @@ public final class HardeningXPathFactory {
     /** Class name of the JDK's built-in default implementation, the Java 8 fallback for {@link #newDefaultInstance()}. */
     private static final String JDK_XPATH_FACTORY = "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl";
 
-    private static final MethodHandle NEW_DEFAULT_INSTANCE = findNewDefaultInstance();
-
-    private static MethodHandle findNewDefaultInstance() {
-        try {
-            return MethodHandles.publicLookup().findStatic(XPathFactory.class, "newDefaultInstance",
-                    MethodType.methodType(XPathFactory.class));
-        } catch (final ReflectiveOperationException e) {
-            // The method is absent: the running platform predates it.
-            return null;
-        }
-    }
+    private static final MethodHandle NEW_DEFAULT_INSTANCE = MethodHandleFactory.findStatic(XPathFactory.class, "newDefaultInstance",
+            MethodType.methodType(XPathFactory.class));
 
     /**
      * Capability-driven hardening for any {@link XPathFactory} on the classpath.
diff --git a/src/main/java/org/apache/commons/xml/MethodHandleFactory.java b/src/main/java/org/apache/commons/xml/MethodHandleFactory.java
new file mode 100644
index 0000000..18d11bb
--- /dev/null
+++ b/src/main/java/org/apache/commons/xml/MethodHandleFactory.java
@@ -0,0 +1,46 @@
+/*
+ * 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.commons.xml;
+
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+
+/**
+ * MethodHandle utility.
+ */
+class MethodHandleFactory {
+
+    /**
+     * Finds a static method handle for the given class, method name, and method type.
+     *
+     * @param refc the class to search for the method.
+     * @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 NullPointerException if any argument is null.
+     */
+    static MethodHandle findStatic(final Class<?> refc, final String name, final MethodType type) {
+        try {
+            return MethodHandles.publicLookup().findStatic(refc, name, type);
+        } catch (final ReflectiveOperationException e) {
+            return null;
+        }
+    }
+}
diff --git a/src/test/java/org/apache/commons/xml/ShadingFootprintTest.java b/src/test/java/org/apache/commons/xml/ShadingFootprintTest.java
index 57ea76f..cdd9c3d 100644
--- a/src/test/java/org/apache/commons/xml/ShadingFootprintTest.java
+++ b/src/test/java/org/apache/commons/xml/ShadingFootprintTest.java
@@ -62,7 +62,8 @@ class ShadingFootprintTest {
             "HardeningDocumentBuilderFactory",
             "HardeningDocumentBuilderFactory$1",
             "HardeningDocumentBuilderFactory$Wrapper",
-            "HardeningException");
+            "HardeningException",
+            "MethodHandleFactory");
     // @formatter:on
 
     // @formatter:off
@@ -74,7 +75,8 @@ class ShadingFootprintTest {
             "HardeningSAXParserFactory$1",
             "HardeningSAXParserFactory$HardeningExpatXMLReader",
             "HardeningSAXParserFactory$Wrapper",
-            "HardeningXMLReader");
+            "HardeningXMLReader",
+            "MethodHandleFactory");
     // @formatter:on
 
     // @formatter:off
@@ -83,7 +85,8 @@ class ShadingFootprintTest {
             "HardeningException",
             "HardeningXMLInputFactory",
             "HardeningXMLInputFactory$1",
-            "HardeningXMLInputFactory$Wrapper");
+            "HardeningXMLInputFactory$Wrapper",
+            "MethodHandleFactory");
     // @formatter:on
 
     /**
@@ -128,6 +131,7 @@ class ShadingFootprintTest {
             "HardeningDocumentBuilderFactory",
             "HardeningDocumentBuilderFactory$1",
             "HardeningDocumentBuilderFactory$Wrapper",
+            "MethodHandleFactory",
             "HardeningException",
             "HardeningSAXParser",
             "HardeningSAXParserFactory",
@@ -168,7 +172,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 = 6;
+    private static final int LIBRARY_CLASS_COUNT = 7;
 
     /**
      * Entry points reported by the {@link #reportFootprint()} diagnostic, most-focused first, ending with the whole library.