Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Correct various issues in method signature matching

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1603402 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Jun 18, 2014
1 parent b1f751b commit 72b29cb
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 17 deletions.
79 changes: 62 additions & 17 deletions java/javax/el/ELProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,28 @@

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;

/**
* @since EL 3.0
*/
public class ELProcessor {

private static final Set<String> PRIMITIVES = new HashSet<>();
static {
PRIMITIVES.add("boolean");
PRIMITIVES.add("byte");
PRIMITIVES.add("char");
PRIMITIVES.add("double");
PRIMITIVES.add("float");
PRIMITIVES.add("int");
PRIMITIVES.add("long");
PRIMITIVES.add("short");
}

private static final String[] EMPTY_STRING_ARRAY = new String[0];

private final ELManager manager = new ELManager();
private final ELContext context = manager.getELContext();
private final ExpressionFactory factory = ELManager.getExpressionFactory();
Expand Down Expand Up @@ -97,6 +113,15 @@ public void defineFunction(String prefix, String function, String className,
}
if (method.getName().equals(sig.getName())) {
if (sig.getParamTypeNames() == null) {
// Only a name provided, no signature so map the first
// method declared
manager.mapFunction(prefix, function, method);
return;
}
if (sig.getParamTypeNames().length != method.getParameterTypes().length) {
continue;
}
if (sig.getParamTypeNames().length == 0) {
manager.mapFunction(prefix, function, method);
return;
} else {
Expand Down Expand Up @@ -174,7 +199,22 @@ public MethodSignature(ELContext context, String methodName,
name = methodName.trim();
parameterTypeNames = null;
} else {
name = methodName.substring(0, paramIndex -1).trim();
String returnTypeAndName = methodName.substring(0, paramIndex).trim();
// Assume that the return type and the name are separated by
// whitespace. Given the use of trim() above, there should only
// be one sequence of whitespace characters.
int wsPos = -1;
for (int i = 0; i < returnTypeAndName.length(); i++) {
if (Character.isWhitespace(returnTypeAndName.charAt(i))) {
wsPos = i;
break;
}
}
if (wsPos == -1) {
throw new NoSuchMethodException();
}
name = returnTypeAndName.substring(wsPos).trim();

String paramString = methodName.substring(paramIndex).trim();
// We know the params start with '(', check they end with ')'
if (!paramString.endsWith(")")) {
Expand All @@ -183,26 +223,31 @@ public MethodSignature(ELContext context, String methodName,
paramString, methodName, className));
}
// Trim '(' and ')'
paramString =
paramString.substring(1, paramString.length() - 1);
parameterTypeNames = paramString.split(",");
ImportHandler importHandler = context.getImportHandler();
for (int i = 0; i < parameterTypeNames.length; i++) {
parameterTypeNames[i] = parameterTypeNames[i].trim();
if (!parameterTypeNames[i].contains(".")) {
Class<?> clazz = importHandler.resolveClass(
parameterTypeNames[i]);
if (clazz == null) {
throw new NoSuchMethodException(Util.message(
context,
"elProcessor.defineFunctionInvalidParameterTypeName",
parameterTypeNames[i], methodName,
className));
paramString = paramString.substring(1, paramString.length() - 1).trim();
if (paramString.length() == 0) {
parameterTypeNames = EMPTY_STRING_ARRAY;
} else {
parameterTypeNames = paramString.split(",");
ImportHandler importHandler = context.getImportHandler();
for (int i = 0; i < parameterTypeNames.length; i++) {
String parameterTypeName = parameterTypeNames[i].trim();
if (!PRIMITIVES.contains(parameterTypeName) &&
!parameterTypeName.contains(".")) {
Class<?> clazz = importHandler.resolveClass(
parameterTypeName);
if (clazz == null) {
throw new NoSuchMethodException(Util.message(
context,
"elProcessor.defineFunctionInvalidParameterTypeName",
parameterTypeNames[i], methodName,
className));
}
parameterTypeNames[i] = clazz.getName();
}
parameterTypeNames[i] = clazz.getName();
}
}
}

}

public String getName() {
Expand Down
38 changes: 38 additions & 0 deletions test/javax/el/TestELProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,44 @@ public void testDefineFunctionMethod01() throws Exception {
}


@Test
public void testDefineFunctionMethod02() throws Exception {
ELProcessor elp = new ELProcessor();
elp.defineFunction("fn", "test", "java.lang.Integer", "Integer valueOf(int)");
Assert.assertEquals(Integer.valueOf(1), elp.eval("fn:test(1)"));
}


@Test
public void testDefineFunctionMethod03() throws Exception {
TesterFunctions.resetCallList();
ELProcessor elp = new ELProcessor();
elp.defineFunction("fn", "test", "javax.el.TesterFunctions", "void doIt()");
elp.eval("fn:test()");
Assert.assertEquals("A", TesterFunctions.getCallList());
}


@Test
public void testDefineFunctionMethod04() throws Exception {
TesterFunctions.resetCallList();
ELProcessor elp = new ELProcessor();
elp.defineFunction("fn", "test", "javax.el.TesterFunctions", "void doIt(int)");
elp.eval("fn:test(5)");
Assert.assertEquals("B", TesterFunctions.getCallList());
}


@Test
public void testDefineFunctionMethod05() throws Exception {
TesterFunctions.resetCallList();
ELProcessor elp = new ELProcessor();
elp.defineFunction("fn", "test", "javax.el.TesterFunctions", "void doIt(Integer)");
elp.eval("fn:test(null)");
Assert.assertEquals("C", TesterFunctions.getCallList());
}


@Test
public void testDefineFunctionName01() throws Exception {
ELProcessor elp = new ELProcessor();
Expand Down
42 changes: 42 additions & 0 deletions test/javax/el/TesterFunctions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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
*
* http://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 javax.el;

public class TesterFunctions {

private static StringBuilder calls = new StringBuilder();

public static String getCallList() {
return calls.toString();
}

public static void resetCallList() {
calls = new StringBuilder();
}

public static void doIt() {
calls.append('A');
}

public static void doIt(@SuppressWarnings("unused") int a) {
calls.append('B');
}

public static void doIt(@SuppressWarnings("unused") Integer a) {
calls.append('C');
}
}
5 changes: 5 additions & 0 deletions webapps/docs/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@
<code>Parser</code> to <code>JspReader</code> class for better
performance. (kkolinko)
</update>
<fix>
<bug>56636</bug>: Correctly identify the required method when specified
via <code>ELProcessor.defineFunction(String,String,String,String)</code>
when using Expression Langauge. (markt)
</fix>
</changelog>
</subsection>
<subsection name="WebSocket">
Expand Down

0 comments on commit 72b29cb

Please sign in to comment.