ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/jakarta/el/BeanELResolver.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/jakarta/el/BeanELResolver.java
Status
scanned
Type
file
Name
BeanELResolver.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
10027 (9.8 KB)
MD5
e680686d57e70d47b65388ef2d56935b
SHA1
7489d706a514c0030afce5385671015e6bb54578
SHA256
2b2ccd7e6873fda278d366d458965a8ee80c4df57f51ceed17ea04fee289ff63
SHA512

      
    
SHA1_git
598e6a15912c3d0b2eda1bc9eaa71bd167beba1b
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
BeanELResolver.java | 9.8 KB |

/* * 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; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; /** * Standard ELResolver for working with JavaBeans. */ public class BeanELResolver extends ELResolver { private static final int CACHE_SIZE; private static final String CACHE_SIZE_PROP = "org.apache.el.BeanELResolver.CACHE_SIZE"; static { CACHE_SIZE = Integer.getInteger(CACHE_SIZE_PROP, 1000).intValue(); } private final boolean readOnly; private final ConcurrentCache<String,BeanProperties> cache = new ConcurrentCache<>(CACHE_SIZE); /** * Creates a writable instance of the standard JavaBean resolver. */ public BeanELResolver() { this.readOnly = false; } /** * Creates an instance of the standard JavaBean resolver. * * @param readOnly {@code true} if the created instance should be read-only otherwise false. */ public BeanELResolver(boolean readOnly) { this.readOnly = readOnly; } @Override public Class<?> getType(ELContext context, Object base, Object property) { Objects.requireNonNull(context); if (base == null || property == null) { return null; } context.setPropertyResolved(base, property); BeanProperty beanProperty = property(context, base, property); if (readOnly || beanProperty.isReadOnly(base)) { return null; } return beanProperty.getPropertyType(); } @Override public Object getValue(ELContext context, Object base, Object property) { Objects.requireNonNull(context); if (base == null || property == null) { return null; } context.setPropertyResolved(base, property); Method m = this.property(context, base, property).read(context, base); try { return m.invoke(base, (Object[]) null); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); Util.handleThrowable(cause); throw new ELException( Util.message(context, "propertyReadError", base.getClass().getName(), property.toString()), cause); } catch (Exception e) { throw new ELException(e); } } @Override public void setValue(ELContext context, Object base, Object property, Object value) { Objects.requireNonNull(context); if (base == null || property == null) { return; } context.setPropertyResolved(base, property); if (this.readOnly) { throw new PropertyNotWritableException( Util.message(context, "resolverNotWritable", base.getClass().getName())); } Method m = this.property(context, base, property).write(context, base); try { m.invoke(base, value); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); Util.handleThrowable(cause); throw new ELException( Util.message(context, "propertyWriteError", base.getClass().getName(), property.toString()), cause); } catch (Exception e) { throw new ELException(e); } } @Override public Object invoke(ELContext context, Object base, Object method, Class<?>[] paramTypes, Object[] params) { Objects.requireNonNull(context); if (base == null || method == null) { return null; } ExpressionFactory factory = ELManager.getExpressionFactory(); String methodName = factory.coerceToType(method, String.class); // Find the matching method Method matchingMethod = Util.findMethod(context, base.getClass(), base, methodName, paramTypes, params); Object[] parameters = Util.buildParameters(context, matchingMethod.getParameterTypes(), matchingMethod.isVarArgs(), params); Object result; try { result = matchingMethod.invoke(base, parameters); } catch (IllegalArgumentException | IllegalAccessException e) { throw new ELException(e); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); Util.handleThrowable(cause); throw new ELException(cause); } context.setPropertyResolved(base, method); return result; } @Override public boolean isReadOnly(ELContext context, Object base, Object property) { Objects.requireNonNull(context); if (base == null || property == null) { return false; } context.setPropertyResolved(base, property); return this.readOnly || this.property(context, base, property).isReadOnly(base); } @Override public Class<?> getCommonPropertyType(ELContext context, Object base) { if (base != null) { return Object.class; } return null; } abstract static class BeanProperties { protected final Map<String,BeanProperty> properties; protected final Class<?> type; BeanProperties(Class<?> type) throws ELException { this.type = type; this.properties = new HashMap<>(); } private BeanProperty get(ELContext ctx, String name) { BeanProperty property = this.properties.get(name); if (property == null) { throw new PropertyNotFoundException(Util.message(ctx, "propertyNotFound", type.getName(), name)); } return property; } private Class<?> getType() { return type; } } abstract static class BeanProperty { private final Class<?> type; private final Class<?> owner; private Method read; private Method write; BeanProperty(Class<?> owner, Class<?> type) { this.owner = owner; this.type = type; } public Class<?> getPropertyType() { return this.type; } public boolean isReadOnly(Object base) { return this.write == null && (null == (this.write = Util.getMethod(this.owner, base, getWriteMethod()))); } private Method write(ELContext ctx, Object base) { if (this.write == null) { this.write = Util.getMethod(this.owner, base, getWriteMethod()); if (this.write == null) { throw new PropertyNotWritableException( Util.message(ctx, "propertyNotWritable", owner.getName(), getName())); } } return this.write; } private Method read(ELContext ctx, Object base) { if (this.read == null) { this.read = Util.getMethod(this.owner, base, getReadMethod()); if (this.read == null) { throw new PropertyNotFoundException( Util.message(ctx, "propertyNotReadable", owner.getName(), getName())); } } return this.read; } abstract Method getWriteMethod(); abstract Method getReadMethod(); abstract String getName(); } private BeanProperty property(ELContext ctx, Object base, Object property) { Class<?> type = base.getClass(); String prop = property.toString(); BeanProperties props = this.cache.get(type.getName()); if (props == null || type != props.getType()) { props = BeanSupport.getInstance().getBeanProperties(type); this.cache.put(type.getName(), props); } return props.get(ctx, prop); } private static final class ConcurrentCache<K, V> { private final int size; private final Map<K,V> eden; private final Map<K,V> longterm; ConcurrentCache(int size) { this.size = size; this.eden = new ConcurrentHashMap<>(size); this.longterm = new WeakHashMap<>(size); } public V get(K key) { V value = this.eden.get(key); if (value == null) { synchronized (longterm) { value = this.longterm.get(key); } if (value != null) { this.eden.put(key, value); } } return value; } public void put(K key, V value) { if (this.eden.size() >= this.size) { synchronized (longterm) { this.longterm.putAll(this.eden); } this.eden.clear(); } this.eden.put(key, value); } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
13.11
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9