Skip to content

Commit 4ae80cb

Browse files
committed
Use JXPathFilter based on the jxpath.allow.class system property
This has the additional benefit of not breaking the binary compatibility of the Functions interface
1 parent 021b8ce commit 4ae80cb

11 files changed

Lines changed: 210 additions & 256 deletions

File tree

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,8 @@ public Function getFunction(
9292
final String namespace,
9393
final String name,
9494
Object[] parameters) {
95-
return getFunction(namespace, name, parameters, new JXPathFilter());
96-
}
97-
98-
public Function getFunction(
99-
String namespace,
100-
String name,
101-
Object[] parameters,
102-
JXPathFilter jxPathFilter) {
103-
104-
// give chance to ClassFilter to filter out, if present
105-
if (jxPathFilter != null && !jxPathFilter.exposeToXPath(functionClass.getName())) {
95+
JXPathFilter jxPathFilter = new JXPathFilter();
96+
if (!jxPathFilter.exposeToXPath(functionClass.getName())) {
10697
throw new JXPathException(
10798
"Extension function is not allowed: " + (namespace != null ? namespace + ":" + name : name)
10899
+ " (in " + functionClass.getName() + ")");

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
package org.apache.commons.jxpath;
1818

19-
import org.apache.commons.jxpath.ri.JXPathFilter;
20-
2119
import java.util.ArrayList;
2220
import java.util.HashMap;
2321
import java.util.Iterator;
@@ -78,30 +76,12 @@ public Set getUsedNamespaces() {
7876
@Override
7977
public Function getFunction(final String namespace, final String name,
8078
final Object[] parameters) {
81-
return getFunction(namespace, name, parameters, new JXPathFilter());
82-
}
83-
84-
/**
85-
* Returns a Function, if any, for the specified namespace,
86-
* name and parameter types.
87-
* @param namespace function namespace
88-
* @param name function name
89-
* @param parameters parameters
90-
* @param jxPathFilter the XPath filter
91-
* @return Function found
92-
*/
93-
public Function getFunction(
94-
final String namespace,
95-
final String name,
96-
final Object[] parameters,
97-
final JXPathFilter jxPathFilter) {
98-
Object candidates = functionCache().get(namespace);
79+
final Object candidates = functionCache().get(namespace);
9980
if (candidates instanceof Functions) {
10081
return ((Functions) candidates).getFunction(
10182
namespace,
10283
name,
103-
parameters,
104-
jxPathFilter);
84+
parameters);
10585
}
10686
if (candidates instanceof List) {
10787
final List list = (List) candidates;
@@ -111,8 +91,7 @@ public Function getFunction(
11191
((Functions) list.get(i)).getFunction(
11292
namespace,
11393
name,
114-
parameters,
115-
jxPathFilter);
94+
parameters);
11695
if (function != null) {
11796
return function;
11897
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,4 @@ public interface Functions {
4545
* @return Function
4646
*/
4747
Function getFunction(String namespace, String name, Object[] parameters);
48-
49-
/**
50-
* Returns a Function, if any, for the specified namespace,
51-
* name and parameter types.
52-
* @param namespace ns
53-
* @param name function name
54-
* @param parameters Object[]
55-
* @param jxPathFilter the XPath filter
56-
* @return Function
57-
*/
58-
Function getFunction(String namespace, String name, Object[] parameters, JXPathFilter jxPathFilter);
5948
}

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

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -117,36 +117,6 @@ public Function getFunction(
117117
final String namespace,
118118
final String name,
119119
Object[] parameters) {
120-
return getFunction(namespace, name, parameters, new JXPathFilter());
121-
}
122-
123-
/**
124-
* Returns a {@link Function}, if found, for the specified namespace,
125-
* name and parameter types.
126-
* <p>
127-
* @param namespace - if it is not the same as specified in the
128-
* construction, this method returns null
129-
* @param name - name of the method, which can one these forms:
130-
* <ul>
131-
* <li><b>methodname</b>, if invoking a method on an object passed as the
132-
* first parameter</li>
133-
* <li><b>Classname.new</b>, if looking for a constructor</li>
134-
* <li><b>subpackage.subpackage.Classname.new</b>, if looking for a
135-
* constructor in a subpackage</li>
136-
* <li><b>Classname.methodname</b>, if looking for a static method</li>
137-
* <li><b>subpackage.subpackage.Classname.methodname</b>, if looking for a
138-
* static method of a class in a subpackage</li>
139-
* </ul>
140-
* @param parameters Object[] of parameters
141-
* @param jxPathFilter the XPath filter
142-
* @return a MethodFunction, a ConstructorFunction or null if no function
143-
* is found
144-
*/
145-
public Function getFunction(
146-
final String namespace,
147-
final String name,
148-
Object[] parameters,
149-
final JXPathFilter jxPathFilter) {
150120
if (!Objects.equals(this.namespace, namespace)) {
151121
return null;
152122
}
@@ -216,7 +186,7 @@ public Function getFunction(
216186

217187
Class functionClass;
218188
try {
219-
functionClass = ClassLoaderUtil.getClass(className, true, jxPathFilter);
189+
functionClass = ClassLoaderUtil.getClass(className, true);
220190
}
221191
catch (final ClassNotFoundException ex) {
222192
throw new JXPathException(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ public Function getFunction(final QName functionName, final Object[] parameters)
765765
while (funcCtx != null) {
766766
funcs = funcCtx.getFunctions();
767767
if (funcs != null) {
768-
func = funcs.getFunction(namespace, name, parameters, new JXPathFilter());
768+
func = funcs.getFunction(namespace, name, parameters);
769769
if (func != null) {
770770
return func;
771771
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static Class getClass(final ClassLoader classLoader, final String classNa
7979
Class clazz;
8080

8181
// give chance to ClassFilter to filter out, if present
82-
if (jxPathFilter != null && !jxPathFilter.exposeToXPath(className)) {
82+
if (jxPathFilter == null || !jxPathFilter.exposeToXPath(className)) {
8383
throw new ClassNotFoundException(className);
8484
}
8585

@@ -106,7 +106,7 @@ public static Class getClass(final ClassLoader classLoader, final String classNa
106106
*/
107107
public static Class getClass(ClassLoader classLoader, String className, boolean initialize)
108108
throws ClassNotFoundException {
109-
return getClass(classLoader, className, initialize, null);
109+
return getClass(classLoader, className, initialize, new JXPathFilter());
110110
}
111111

112112
/**
@@ -181,7 +181,7 @@ public static Class getClass(String className, JXPathFilter jxPathFilter) throws
181181
* @throws ClassNotFoundException if the class is not found
182182
*/
183183
public static Class getClass(final String className, final boolean initialize) throws ClassNotFoundException {
184-
return getClass(className, initialize, null);
184+
return getClass(className, initialize, new JXPathFilter());
185185
}
186186

187187
/**

src/test/java/org/apache/commons/jxpath/BasicNodeSetTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@ public class BasicNodeSetTest extends JXPathTestCase {
3535
@Override
3636
protected void setUp() throws Exception {
3737
super.setUp();
38+
System.setProperty("jxpath.class.allow", "*");
3839
context = JXPathContext.newContext(new TestMixedModelBean());
3940
nodeSet = new BasicNodeSet();
4041
}
4142

43+
@Override
44+
public void tearDown() throws Exception {
45+
System.clearProperty("jxpath.class.allow");
46+
super.tearDown();
47+
}
48+
4249
/**
4350
* Add the pointers for the specified path to <code>nodeSet</code>.
4451
*

src/test/java/org/apache/commons/jxpath/issues/JXPath172DynamicTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ public static TestSuite suite()
3636
return new TestSuite(JXPath172DynamicTest.class);
3737
}
3838

39+
@Override
40+
public void setUp() throws Exception {
41+
super.setUp();
42+
System.setProperty("jxpath.class.allow", "*");
43+
}
44+
45+
@Override
46+
public void tearDown() throws Exception {
47+
System.clearProperty("jxpath.class.allow");
48+
super.tearDown();
49+
}
50+
3951
public void testIssue172_propertyExistAndIsNotNull()
4052
{
4153
final JXPathContext context = getContext("ciao", false);

0 commit comments

Comments
 (0)