This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jxpath.git
commit 6bd9410ade712f577d7cb03cdee8f45201100ab2 Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Sat Apr 12 10:55:35 2025 -0400 ClassLoaderUtil.getClass(*) now uses generics --- src/changes/changes.xml | 4 ++++ .../apache/commons/jxpath/JXPathContextFactory.java | 3 +-- .../org/apache/commons/jxpath/JXPathIntrospector.java | 3 +-- .../org/apache/commons/jxpath/PackageFunctions.java | 2 +- .../commons/jxpath/ri/JXPathContextReferenceImpl.java | 3 +-- .../apache/commons/jxpath/util/ClassLoaderUtil.java | 19 ++++++++++++------- .../apache/commons/jxpath/xml/DocumentContainer.java | 3 +-- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index bb6cac7..4b7d7f3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -139,6 +139,10 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.iteratePointers(String, Expression) now uses generics.</action> <action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.CompiledExpression.iteratePointers(JXPathContext) now uses generics.</action> <action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.JXPathCompiledExpression.iteratePointers(JXPathContext) now uses generics.</action> + <action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.util.ClassLoaderUtil.getClass(ClassLoader, String) now uses generics.</action> + <action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.util.ClassLoaderUtil.getClass(ClassLoader, String, boolean) now uses generics.</action> + <action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.util.ClassLoaderUtil.getClass(String) now uses generics.</action> + <action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.util.ClassLoaderUtil.getClass(String, boolean) now uses generics.</action> <!-- ADD --> <action issue="JXPATH-123" dev="mbenson" type="add"> XPath function "ends-with" is not implemented (although "starts-with" is). diff --git a/src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java b/src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java index 10b3d36..dd36b3b 100644 --- a/src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java +++ b/src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java @@ -156,8 +156,7 @@ public abstract class JXPathContextFactory { public static JXPathContextFactory newInstance() { JXPathContextFactory factoryImpl; try { - final Class clazz = ClassLoaderUtil.getClass(FACTORY_IMPL_NAME, true); - factoryImpl = (JXPathContextFactory) clazz.getConstructor().newInstance(); + factoryImpl = ClassLoaderUtil.<JXPathContextFactory>getClass(FACTORY_IMPL_NAME, true).getConstructor().newInstance(); } catch (final ReflectiveOperationException ie) { throw new JXPathContextFactoryConfigurationError(ie); } diff --git a/src/main/java/org/apache/commons/jxpath/JXPathIntrospector.java b/src/main/java/org/apache/commons/jxpath/JXPathIntrospector.java index ed13843..e5ada65 100644 --- a/src/main/java/org/apache/commons/jxpath/JXPathIntrospector.java +++ b/src/main/java/org/apache/commons/jxpath/JXPathIntrospector.java @@ -166,8 +166,7 @@ public class JXPathIntrospector { } } // Now try the ClassLoaderUtil. - final Class cls = ClassLoaderUtil.getClass(className); - return cls.newInstance(); + return ClassLoaderUtil.getClass(className).newInstance(); } /** diff --git a/src/main/java/org/apache/commons/jxpath/PackageFunctions.java b/src/main/java/org/apache/commons/jxpath/PackageFunctions.java index 0d8038f..ecadc3e 100644 --- a/src/main/java/org/apache/commons/jxpath/PackageFunctions.java +++ b/src/main/java/org/apache/commons/jxpath/PackageFunctions.java @@ -144,7 +144,7 @@ public class PackageFunctions implements Functions { } final String className = fullName.substring(0, inx); final String methodName = fullName.substring(inx + 1); - Class functionClass; + Class<?> functionClass; try { functionClass = ClassLoaderUtil.getClass(className, true); } catch (final ClassNotFoundException ex) { diff --git a/src/main/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java b/src/main/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java index 549ef2a..f3580a0 100644 --- a/src/main/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java +++ b/src/main/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java @@ -125,8 +125,7 @@ public class JXPathContextReferenceImpl extends JXPathContext { } catch (final ClassNotFoundException ex) { return null; } - final Class<NodePointerFactory> cls = ClassLoaderUtil.getClass(className, true); - return cls.getConstructor().newInstance(); + return ClassLoaderUtil.getClass(className, true).getConstructor().newInstance(); } catch (final Exception ex) { throw new JXPathException("Cannot allocate " + className, ex); } diff --git a/src/main/java/org/apache/commons/jxpath/util/ClassLoaderUtil.java b/src/main/java/org/apache/commons/jxpath/util/ClassLoaderUtil.java index 9e40bac..7311436 100644 --- a/src/main/java/org/apache/commons/jxpath/util/ClassLoaderUtil.java +++ b/src/main/java/org/apache/commons/jxpath/util/ClassLoaderUtil.java @@ -62,12 +62,13 @@ public class ClassLoaderUtil { * Returns the (initialized) class represented by {@code className} using the {@code classLoader}. This implementation supports names like * "{@code java.lang.String[]}" as well as "{@code [Ljava.lang.String;}". * + * @param <T> The expected class type. * @param classLoader the class loader to use to load the class * @param className the class name * @return the class represented by {@code className} using the {@code classLoader} * @throws ClassNotFoundException if the class is not found */ - public static Class getClass(final ClassLoader classLoader, final String className) throws ClassNotFoundException { + public static <T> Class<T> getClass(final ClassLoader classLoader, final String className) throws ClassNotFoundException { return getClass(classLoader, className, true); } @@ -76,19 +77,21 @@ public class ClassLoaderUtil { * Returns the class represented by {@code className} using the {@code classLoader}. This implementation supports names like "{@code java.lang.String[]}" as * well as "{@code [Ljava.lang.String;}". * + * @param <T> The expected class type. * @param classLoader the class loader to use to load the class * @param className the class name * @param initialize whether the class must be initialized * @return the class represented by {@code className} using the {@code classLoader} * @throws ClassNotFoundException if the class is not found */ - public static Class getClass(final ClassLoader classLoader, final String className, final boolean initialize) throws ClassNotFoundException { - Class clazz; + @SuppressWarnings("unchecked") // assume the call site knows what it's doing. + public static <T> Class<T> getClass(final ClassLoader classLoader, final String className, final boolean initialize) throws ClassNotFoundException { + Class<T> clazz; if (abbreviationMap.containsKey(className)) { final String clsName = "[" + abbreviationMap.get(className); - clazz = Class.forName(clsName, initialize, classLoader).getComponentType(); + clazz = (Class<T>) Class.forName(clsName, initialize, classLoader).getComponentType(); } else { - clazz = Class.forName(toCanonicalName(className), initialize, classLoader); + clazz = (Class<T>) Class.forName(toCanonicalName(className), initialize, classLoader); } return clazz; } @@ -97,11 +100,12 @@ public class ClassLoaderUtil { * Returns the (initialized) class represented by {@code className} using the current thread's context class loader. This implementation supports names like * "{@code java.lang.String[]}" as well as "{@code [Ljava.lang.String;}". * + * @param <T> The expected class type. * @param className the class name * @return the class represented by {@code className} using the current thread's context class loader * @throws ClassNotFoundException if the class is not found */ - public static Class getClass(final String className) throws ClassNotFoundException { + public static <T> Class<T> getClass(final String className) throws ClassNotFoundException { return getClass(className, true); } @@ -109,12 +113,13 @@ public class ClassLoaderUtil { * Returns the class represented by {@code className} using the current thread's context class loader. This implementation supports names like * "{@code java.lang.String[]}" as well as "{@code [Ljava.lang.String;}". * + * @param <T> The expected class type. * @param className the class name * @param initialize whether the class must be initialized * @return the class represented by {@code className} using the current thread's context class loader * @throws ClassNotFoundException if the class is not found */ - public static Class getClass(final String className, final boolean initialize) throws ClassNotFoundException { + public static <T> Class<T> getClass(final String className, final boolean initialize) throws ClassNotFoundException { final ClassLoader contextCL = Thread.currentThread().getContextClassLoader(); final ClassLoader currentCL = ClassLoaderUtil.class.getClassLoader(); if (contextCL != null) { diff --git a/src/main/java/org/apache/commons/jxpath/xml/DocumentContainer.java b/src/main/java/org/apache/commons/jxpath/xml/DocumentContainer.java index c4ad189..d8aad23 100644 --- a/src/main/java/org/apache/commons/jxpath/xml/DocumentContainer.java +++ b/src/main/java/org/apache/commons/jxpath/xml/DocumentContainer.java @@ -60,8 +60,7 @@ public class DocumentContainer extends XMLParser2 implements Container { throw new JXPathException("Unsupported XML model: " + model); } try { - final Class<XMLParser> clazz = ClassLoaderUtil.getClass(className, true); - return clazz.getConstructor().newInstance(); + return ClassLoaderUtil.<XMLParser>getClass(className, true).getConstructor().newInstance(); } catch (final Exception ex) { throw new JXPathException("Cannot allocate XMLParser: " + className, ex); }