/* * 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 jakarta.el;
/** * Map a method to a function name. * * @param prefix Function prefix * @param function Function name * @param method Method * * @throws NullPointerException If any of the arguments are null * @throws NoSuchMethodException If the method is not static
*/ publicvoid defineFunction(String prefix, String function, Method method) throws NoSuchMethodException {
if (prefix == null || function == null || method == null) { thrownew NullPointerException(Util.message(context, "elProcessor.defineFunctionNullParams"));
}
int modifiers = method.getModifiers();
// Check for static, public method and module access if (!Modifier.isStatic(modifiers) || !Util.canAccess(null, method)) { thrownew NoSuchMethodException(Util.message(context, "elProcessor.defineFunctionInvalidMethod",
method.getName(), method.getDeclaringClass().getName()));
}
if (paramIndex == -1) {
name = methodName.trim();
parameterTypeNames = null;
} else {
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) { thrownew 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(")")) { thrownew NoSuchMethodException(Util.message(context, "elProcessor.defineFunctionInvalidParameterList", paramString, methodName, className));
} // Trim '(' and ')'
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(); int dimension = 0; int bracketPos = parameterTypeName.indexOf('['); if (bracketPos > -1) {
String parameterTypeNameOnly = parameterTypeName.substring(0, bracketPos).trim(); while (bracketPos > -1) {
dimension++;
bracketPos = parameterTypeName.indexOf('[', bracketPos + 1);
}
parameterTypeName = parameterTypeNameOnly;
} boolean varArgs = false; if (parameterTypeName.endsWith("...")) {
varArgs = true;
dimension = 1;
parameterTypeName = parameterTypeName.substring(0, parameterTypeName.length() - 3).trim();
} boolean isPrimitive = PRIMITIVES.contains(parameterTypeName); if (isPrimitive && dimension > 0) { // When in an array, class name changes for primitive switch (parameterTypeName) { case"boolean":
parameterTypeName = "Z"; break; case"byte":
parameterTypeName = "B"; break; case"char":
parameterTypeName = "C"; break; case"double":
parameterTypeName = "D"; break; case"float":
parameterTypeName = "F"; break; case"int":
parameterTypeName = "I"; break; case"long":
parameterTypeName = "J"; break; case"short":
parameterTypeName = "S"; break; default: // Should never happen break;
}
} elseif (!isPrimitive && !parameterTypeName.contains(".")) { Class<?> clazz = importHandler.resolveClass(parameterTypeName); if (clazz == null) { thrownew NoSuchMethodException(
Util.message(context, "elProcessor.defineFunctionInvalidParameterTypeName",
parameterTypeNames[i], methodName, className));
}
parameterTypeName = clazz.getName();
} if (dimension > 0) { // Convert to array form of class name
StringBuilder sb = new StringBuilder(); for (int j = 0; j < dimension; j++) {
sb.append('[');
} if (!isPrimitive) {
sb.append('L');
}
sb.append(parameterTypeName); if (!isPrimitive) {
sb.append(';');
}
parameterTypeName = sb.toString();
} if (varArgs) {
parameterTypeName += "...";
}
parameterTypeNames[i] = parameterTypeName;
}
}
}
}
public String getName() { return name;
}
/** * @return <code>null</code> if just the method name was specified, an empty List if an empty parameter list was * specified - i.e. () - otherwise an ordered list of parameter type names
*/ public String[] getParamTypeNames() { return parameterTypeNames;
}
}
}
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.