Skip to content

Commit 6bd9410

Browse files
committed
ClassLoaderUtil.getClass(*) now uses generics
1 parent 56fa34a commit 6bd9410

File tree

7 files changed

+21
-16
lines changed

7 files changed

+21
-16
lines changed

src/changes/changes.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ The <action> type attribute can be add,update,fix,remove.
139139
<action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.iteratePointers(String, Expression) now uses generics.</action>
140140
<action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.CompiledExpression.iteratePointers(JXPathContext) now uses generics.</action>
141141
<action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.JXPathCompiledExpression.iteratePointers(JXPathContext) now uses generics.</action>
142+
<action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.util.ClassLoaderUtil.getClass(ClassLoader, String) now uses generics.</action>
143+
<action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.util.ClassLoaderUtil.getClass(ClassLoader, String, boolean) now uses generics.</action>
144+
<action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.util.ClassLoaderUtil.getClass(String) now uses generics.</action>
145+
<action dev="ggregory" type="fix" due-to="Gary Gregory">org.apache.commons.jxpath.util.ClassLoaderUtil.getClass(String, boolean) now uses generics.</action>
142146
<!-- ADD -->
143147
<action issue="JXPATH-123" dev="mbenson" type="add">
144148
XPath function "ends-with" is not implemented (although "starts-with" is).

src/main/java/org/apache/commons/jxpath/JXPathContextFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ private static String findFactory(final String property, final String defaultFac
156156
public static JXPathContextFactory newInstance() {
157157
JXPathContextFactory factoryImpl;
158158
try {
159-
final Class clazz = ClassLoaderUtil.getClass(FACTORY_IMPL_NAME, true);
160-
factoryImpl = (JXPathContextFactory) clazz.getConstructor().newInstance();
159+
factoryImpl = ClassLoaderUtil.<JXPathContextFactory>getClass(FACTORY_IMPL_NAME, true).getConstructor().newInstance();
161160
} catch (final ReflectiveOperationException ie) {
162161
throw new JXPathContextFactoryConfigurationError(ie);
163162
}

src/main/java/org/apache/commons/jxpath/JXPathIntrospector.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ private static Object instantiate(final Class sibling, final String className) t
166166
}
167167
}
168168
// Now try the ClassLoaderUtil.
169-
final Class cls = ClassLoaderUtil.getClass(className);
170-
return cls.newInstance();
169+
return ClassLoaderUtil.getClass(className).newInstance();
171170
}
172171

173172
/**

src/main/java/org/apache/commons/jxpath/PackageFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public Function getFunction(final String namespace, final String name, Object[]
144144
}
145145
final String className = fullName.substring(0, inx);
146146
final String methodName = fullName.substring(inx + 1);
147-
Class functionClass;
147+
Class<?> functionClass;
148148
try {
149149
functionClass = ClassLoaderUtil.getClass(className, true);
150150
} catch (final ClassNotFoundException ex) {

src/main/java/org/apache/commons/jxpath/ri/JXPathContextReferenceImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ public static Object allocateConditionally(final String className, final String
125125
} catch (final ClassNotFoundException ex) {
126126
return null;
127127
}
128-
final Class<NodePointerFactory> cls = ClassLoaderUtil.getClass(className, true);
129-
return cls.getConstructor().newInstance();
128+
return ClassLoaderUtil.getClass(className, true).getConstructor().newInstance();
130129
} catch (final Exception ex) {
131130
throw new JXPathException("Cannot allocate " + className, ex);
132131
}

src/main/java/org/apache/commons/jxpath/util/ClassLoaderUtil.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,13 @@ private static void addAbbreviation(final String primitive, final String abbrevi
6262
* Returns the (initialized) class represented by {@code className} using the {@code classLoader}. This implementation supports names like
6363
* "{@code java.lang.String[]}" as well as "{@code [Ljava.lang.String;}".
6464
*
65+
* @param <T> The expected class type.
6566
* @param classLoader the class loader to use to load the class
6667
* @param className the class name
6768
* @return the class represented by {@code className} using the {@code classLoader}
6869
* @throws ClassNotFoundException if the class is not found
6970
*/
70-
public static Class getClass(final ClassLoader classLoader, final String className) throws ClassNotFoundException {
71+
public static <T> Class<T> getClass(final ClassLoader classLoader, final String className) throws ClassNotFoundException {
7172
return getClass(classLoader, className, true);
7273
}
7374

@@ -76,19 +77,21 @@ public static Class getClass(final ClassLoader classLoader, final String classNa
7677
* Returns the class represented by {@code className} using the {@code classLoader}. This implementation supports names like "{@code java.lang.String[]}" as
7778
* well as "{@code [Ljava.lang.String;}".
7879
*
80+
* @param <T> The expected class type.
7981
* @param classLoader the class loader to use to load the class
8082
* @param className the class name
8183
* @param initialize whether the class must be initialized
8284
* @return the class represented by {@code className} using the {@code classLoader}
8385
* @throws ClassNotFoundException if the class is not found
8486
*/
85-
public static Class getClass(final ClassLoader classLoader, final String className, final boolean initialize) throws ClassNotFoundException {
86-
Class clazz;
87+
@SuppressWarnings("unchecked") // assume the call site knows what it's doing.
88+
public static <T> Class<T> getClass(final ClassLoader classLoader, final String className, final boolean initialize) throws ClassNotFoundException {
89+
Class<T> clazz;
8790
if (abbreviationMap.containsKey(className)) {
8891
final String clsName = "[" + abbreviationMap.get(className);
89-
clazz = Class.forName(clsName, initialize, classLoader).getComponentType();
92+
clazz = (Class<T>) Class.forName(clsName, initialize, classLoader).getComponentType();
9093
} else {
91-
clazz = Class.forName(toCanonicalName(className), initialize, classLoader);
94+
clazz = (Class<T>) Class.forName(toCanonicalName(className), initialize, classLoader);
9295
}
9396
return clazz;
9497
}
@@ -97,24 +100,26 @@ public static Class getClass(final ClassLoader classLoader, final String classNa
97100
* Returns the (initialized) class represented by {@code className} using the current thread's context class loader. This implementation supports names like
98101
* "{@code java.lang.String[]}" as well as "{@code [Ljava.lang.String;}".
99102
*
103+
* @param <T> The expected class type.
100104
* @param className the class name
101105
* @return the class represented by {@code className} using the current thread's context class loader
102106
* @throws ClassNotFoundException if the class is not found
103107
*/
104-
public static Class getClass(final String className) throws ClassNotFoundException {
108+
public static <T> Class<T> getClass(final String className) throws ClassNotFoundException {
105109
return getClass(className, true);
106110
}
107111

108112
/**
109113
* Returns the class represented by {@code className} using the current thread's context class loader. This implementation supports names like
110114
* "{@code java.lang.String[]}" as well as "{@code [Ljava.lang.String;}".
111115
*
116+
* @param <T> The expected class type.
112117
* @param className the class name
113118
* @param initialize whether the class must be initialized
114119
* @return the class represented by {@code className} using the current thread's context class loader
115120
* @throws ClassNotFoundException if the class is not found
116121
*/
117-
public static Class getClass(final String className, final boolean initialize) throws ClassNotFoundException {
122+
public static <T> Class<T> getClass(final String className, final boolean initialize) throws ClassNotFoundException {
118123
final ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
119124
final ClassLoader currentCL = ClassLoaderUtil.class.getClassLoader();
120125
if (contextCL != null) {

src/main/java/org/apache/commons/jxpath/xml/DocumentContainer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ private static XMLParser getParser(final String model) {
6060
throw new JXPathException("Unsupported XML model: " + model);
6161
}
6262
try {
63-
final Class<XMLParser> clazz = ClassLoaderUtil.getClass(className, true);
64-
return clazz.getConstructor().newInstance();
63+
return ClassLoaderUtil.<XMLParser>getClass(className, true).getConstructor().newInstance();
6564
} catch (final Exception ex) {
6665
throw new JXPathException("Cannot allocate XMLParser: " + className, ex);
6766
}

0 commit comments

Comments
 (0)