/* * 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;
static { if (IS_SECURITY_ENABLED) { // Defaults to using a privileged block // When running on Tomcat this will be set to false in // $CATALINA_BASE/conf/catalina.properties
String value = AccessController.doPrivileged((PrivilegedAction<String>) () -> System
.getProperty("org.apache.el.GET_CLASSLOADER_USE_PRIVILEGED", "true"));
GET_CLASSLOADER_USE_PRIVILEGED = Boolean.parseBoolean(value);
} else { // No security manager - no need to use a privileged block.
GET_CLASSLOADER_USE_PRIVILEGED = false;
}
}
/** * Checks whether the supplied Throwable is one that needs to be rethrown and swallows all others. * * @param t the Throwable to check
*/ staticvoid handleThrowable(Throwable t) { if (t instanceof ThreadDeath) { throw (ThreadDeath) t;
} if (t instanceof VirtualMachineError) { throw (VirtualMachineError) t;
} // All other instances of Throwable will be silently swallowed
}
privatestaticfinal CacheValue nullTcclFactory = new CacheValue(); privatestaticfinal Map<CacheKey,CacheValue> factoryCache = new ConcurrentHashMap<>();
/** * Provides a per class loader cache of ExpressionFactory instances without pinning any in memory as that could * trigger a memory leak.
*/ static ExpressionFactory getExpressionFactory() {
/** * Key used to cache default ExpressionFactory information per class loader. The class loader reference is never * {@code null}, because {@code null} tccl is handled separately.
*/ privatestaticclass CacheKey { privatefinalint hash; privatefinal WeakReference<ClassLoader> ref;
Wrapper<Method> result = findWrapper(context, clazz, wrappers, methodName, paramTypes, paramValues);
return getMethod(clazz, base, result.unWrap());
}
/* * This method duplicates code in org.apache.el.util.ReflectionUtil. When making changes keep the code in sync.
*/
@SuppressWarnings("null") privatestatic <T> Wrapper<T> findWrapper(ELContext context, Class<?> clazz, List<Wrapper<T>> wrappers, String name, Class<?>[] paramTypes, Object[] paramValues) {
Map<Wrapper<T>,MatchResult> candidates = new HashMap<>();
int paramCount = paramTypes.length;
for (Wrapper<T> w : wrappers) { Class<?>[] mParamTypes = w.getParameterTypes(); int mParamCount; if (mParamTypes == null) {
mParamCount = 0;
} else {
mParamCount = mParamTypes.length;
}
// Check the number of parameters // Multiple tests to improve readability if (!w.isVarArgs() && paramCount != mParamCount) { // Method has wrong number of parameters continue;
} if (w.isVarArgs() && paramCount < mParamCount - 1) { // Method has wrong number of parameters continue;
} if (w.isVarArgs() && paramCount == mParamCount && paramValues != null && paramValues.length > paramCount &&
!paramTypes[mParamCount - 1].isArray()) { // Method arguments don't match continue;
} if (w.isVarArgs() && paramCount > mParamCount && paramValues != null && paramValues.length != paramCount) { // Might match a different varargs method continue;
} if (!w.isVarArgs() && paramValues != null && paramCount != paramValues.length) { // Might match a different varargs method continue;
}
// Check the parameters match int exactMatch = 0; int assignableMatch = 0; int coercibleMatch = 0; int varArgsMatch = 0; boolean noMatch = false; for (int i = 0; i < mParamCount; i++) { // Can't be null if (w.isVarArgs() && i == (mParamCount - 1)) { if (i == paramCount || (paramValues != null && paramValues.length == i)) { // Var args defined but nothing is passed as varargs // Use MAX_VALUE so this matches only if nothing else does
varArgsMatch = Integer.MAX_VALUE; break;
} Class<?> varType = mParamTypes[i].getComponentType(); for (int j = i; j < paramCount; j++) { if (isAssignableFrom(paramTypes[j], varType)) {
assignableMatch++;
varArgsMatch++;
} else { if (paramValues == null) {
noMatch = true; break;
} else { if (isCoercibleFrom(context, paramValues[j], varType)) {
coercibleMatch++;
varArgsMatch++;
} else {
noMatch = true; break;
}
}
} // Don't treat a varArgs match as an exact match, it can // lead to a varArgs method matching when the result // should be ambiguous
}
} else { if (mParamTypes[i].equals(paramTypes[i])) {
exactMatch++;
} elseif (paramTypes[i] != null && isAssignableFrom(paramTypes[i], mParamTypes[i])) {
assignableMatch++;
} else { if (paramValues == null) {
noMatch = true; break;
} else { if (isCoercibleFrom(context, paramValues[i], mParamTypes[i])) {
coercibleMatch++;
} else {
noMatch = true; break;
}
}
}
}
} if (noMatch) { continue;
}
// If a method is found where every parameter matches exactly, // and no vars args are present, return it if (exactMatch == paramCount && varArgsMatch == 0) { return w;
}
candidates.put(w, new MatchResult(w.isVarArgs(), exactMatch, assignableMatch, coercibleMatch, varArgsMatch,
w.isBridge()));
}
// Look for the method that has the highest number of parameters where // the type matches exactly
MatchResult bestMatch = new MatchResult(true, 0, 0, 0, 0, true);
Wrapper<T> match = null; boolean multiple = false; for (Map.Entry<Wrapper<T>,MatchResult> entry : candidates.entrySet()) { int cmp = entry.getValue().compareTo(bestMatch); if (cmp > 0 || match == null) {
bestMatch = entry.getValue();
match = entry.getKey();
multiple = false;
} elseif (cmp == 0) {
multiple = true;
}
} if (multiple) { if (bestMatch.getExactCount() == paramCount - 1) { // Only one parameter is not an exact match - try using the // super class
match = resolveAmbiguousWrapper(candidates.keySet(), paramTypes);
} else {
match = null;
}
if (match == null) { // If multiple methods have the same matching number of parameters // the match is ambiguous so throw an exception thrownew MethodNotFoundException(
message(null, "util.method.ambiguous", clazz, name, paramString(paramTypes)));
}
}
// Handle case where no match at all was found if (match == null) { thrownew MethodNotFoundException(
message(null, "util.method.notfound", clazz, name, paramString(paramTypes)));
}
return match;
}
privatestatic String paramString(Class<?>[] types) { if (types != null) {
StringBuilder sb = new StringBuilder(); for (Class<?> type : types) { if (type == null) {
sb.append("null, ");
} else {
sb.append(type.getName()).append(", ");
}
} if (sb.length() > 2) {
sb.setLength(sb.length() - 2);
} return sb.toString();
} returnnull;
}
/* * This method duplicates code in org.apache.el.util.ReflectionUtil. When making changes keep the code in sync.
*/ privatestatic <T> Wrapper<T> resolveAmbiguousWrapper(Set<Wrapper<T>> candidates, Class<?>[] paramTypes) { // Identify which parameter isn't an exact match
Wrapper<T> w = candidates.iterator().next();
int nonMatchIndex = 0; Class<?> nonMatchClass = null;
for (int i = 0; i < paramTypes.length; i++) { if (w.getParameterTypes()[i] != paramTypes[i]) {
nonMatchIndex = i;
nonMatchClass = paramTypes[i]; break;
}
}
if (nonMatchClass == null) { // Null will always be ambiguous returnnull;
}
for (Wrapper<T> c : candidates) { if (c.getParameterTypes()[nonMatchIndex] == paramTypes[nonMatchIndex]) { // Methods have different non-matching parameters // Result is ambiguous returnnull;
}
}
// Can't be null Class<?> superClass = nonMatchClass.getSuperclass(); while (superClass != null) { for (Wrapper<T> c : candidates) { if (c.getParameterTypes()[nonMatchIndex].equals(superClass)) { // Found a match return c;
}
}
superClass = superClass.getSuperclass();
}
// Treat instances of Number as a special case
Wrapper<T> match = null; if (Number.class.isAssignableFrom(nonMatchClass)) { for (Wrapper<T> c : candidates) { Class<?> candidateType = c.getParameterTypes()[nonMatchIndex]; if (Number.class.isAssignableFrom(candidateType) || candidateType.isPrimitive()) { if (match == null) {
match = c;
} else { // Match still ambiguous
match = null; break;
}
}
}
}
return match;
}
/* * This method duplicates code in org.apache.el.util.ReflectionUtil. When making changes keep the code in sync.
*/ staticboolean isAssignableFrom(Class<?> src, Class<?> target) { // src will always be an object // Short-cut. null is always assignable to an object and in EL null // can always be coerced to a valid value for a primitive if (src == null) { returntrue;
}
/* * This method duplicates code in org.apache.el.util.ReflectionUtil. When making changes keep the code in sync.
*/ privatestaticboolean isCoercibleFrom(ELContext context, Object src, Class<?> target) { // TODO: This isn't pretty but it works. Significant refactoring would // be required to avoid the exception. try {
context.convertToType(src, target);
} catch (ELException e) { returnfalse;
} returntrue;
}
/* * This class duplicates code in org.apache.el.util.ReflectionUtil. When making changes keep the code in sync.
*/ privatestaticclass MatchResult implements Comparable<MatchResult> {
@Override publicint compareTo(MatchResult o) { // Non-varArgs always beats varArgs int cmp = Boolean.compare(o.isVarArgs(), this.isVarArgs()); if (cmp == 0) {
cmp = Integer.compare(this.getExactCount(), o.getExactCount()); if (cmp == 0) {
cmp = Integer.compare(this.getAssignableCount(), o.getAssignableCount()); if (cmp == 0) {
cmp = Integer.compare(this.getCoercibleCount(), o.getCoercibleCount()); if (cmp == 0) { // Fewer var args matches are better
cmp = Integer.compare(o.getVarArgsCount(), this.getVarArgsCount()); if (cmp == 0) { // The nature of bridge methods is such that it actually // doesn't matter which one we pick as long as we pick // one. That said, pick the 'right' one (the non-bridge // one) anyway.
cmp = Boolean.compare(o.isBridge(), this.isBridge());
}
}
}
}
} return cmp;
}
@Override publicint hashCode() { finalint prime = 31; int result = 1;
result = prime * result + assignableCount;
result = prime * result + (bridge ? 1231 : 1237);
result = prime * result + coercibleCount;
result = prime * result + exactCount;
result = prime * result + (varArgs ? 1231 : 1237);
result = prime * result + varArgsCount; return result;
}
}
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.