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

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

      
    
Rootfs path

      
    
Size
8879 (8.7 KB)
MD5
2e687e7aefafe6777d7472f1cc3ac10a
SHA1
51be1bfb5d1472f59fb7f8017a0a43979b23e7c9
SHA256
b25d78a3687fbb40f4147ec47d5a53744b63222eb2f28545215b25be0b051a44
SHA512

      
    
SHA1_git
d0394e27790779d7a57638b8919e2db5629c2580
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
OptionalELResolver.java | 8.7 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.util.Objects; import java.util.Optional; /** * Defines property resolution, method invocation and type conversion behaviour on {@link Optional}s. * <p> * This resolver handles base objects that are instances of {@link Optional}. * <p> * This resolver is always a read-only resolver since {@link Optional} instances are immutable. * * @since EL 6.0 */ public class OptionalELResolver extends ELResolver { /** * {@inheritDoc} * * @return If the base object is an {@link Optional} and {@link Optional#isEmpty()} returns {@code true} then the * resulting value is {@code null}. * <p> * If the base object is an {@link Optional}, {@link Optional#isPresent()} returns {@code true} and the * property is {@code null} then the resulting value is the result of calling {@link Optional#get()} on * the base object. * <p> * If the base object is an {@link Optional}, {@link Optional#isPresent()} returns {@code true} and the * property is not {@code null} then the resulting value is the result of calling * {@link ELResolver#getValue(ELContext, Object, Object)} using the {@link ELResolver} obtained from * {@link ELContext#getELResolver()} with the following parameters: * <ul> * <li>The {@link ELContext} is the current context</li> * <li>The base object is the result of calling {@link Optional#get()} on the current base object</li> * <li>The property object is the current property object</li> * </ul> * <p> * If the base object is not an {@link Optional} then the return value is undefined. */ @Override public Object getValue(ELContext context, Object base, Object property) { Objects.requireNonNull(context); if (base instanceof Optional) { context.setPropertyResolved(base, property); if (((Optional<?>) base).isPresent()) { if (property == null) { return ((Optional<?>) base).get(); } else { Object resolvedBase = ((Optional<?>) base).get(); return context.getELResolver().getValue(context, resolvedBase, property); } } } return null; } /** * {@inheritDoc} * * @return If the base object is an {@link Optional} this method always returns {@code null} since instances of this * resolver are always read-only. * <p> * If the base object is not an {@link Optional} then the return value is undefined. */ @Override public Class<?> getType(ELContext context, Object base, Object property) { Objects.requireNonNull(context); if (base instanceof Optional) { context.setPropertyResolved(base, property); } return null; } /** * {@inheritDoc} * <p> * If the base object is an {@link Optional} this method always throws a {@link PropertyNotWritableException} since * instances of this resolver are always read-only. */ @Override public void setValue(ELContext context, Object base, Object property, Object value) { Objects.requireNonNull(context); if (base instanceof Optional) { throw new PropertyNotWritableException( Util.message(context, "resolverNotWritable", base.getClass().getName())); } } /** * {@inheritDoc} * * @return If the base object is an {@link Optional} this method always returns {@code true} since instances of this * resolver are always read-only. * <p> * If the base object is not an {@link Optional} then the return value is undefined. */ @Override public boolean isReadOnly(ELContext context, Object base, Object property) { Objects.requireNonNull(context); if (base instanceof Optional) { context.setPropertyResolved(base, property); return true; } return false; } /** * {@inheritDoc} * * @return If the base object is an {@link Optional} this method always returns {@code Object.class}. * <p> * If the base object is not an {@link Optional} then the return value is undefined. */ @Override public Class<?> getCommonPropertyType(ELContext context, Object base) { if (base instanceof Optional) { return Object.class; } return null; } /** * {@inheritDoc} * * @return If the base object is an {@link Optional} and {@link Optional#isEmpty()} returns {@code true} then this * method returns the result of coercing {@code null} to the requested {@code type}. * <p> * If the base object is an {@link Optional} and {@link Optional#isPresent()} returns {@code true} then * this method returns the result of coercing {@code Optional#get()} to the requested {@code type}. * <p> * If the base object is not an {@link Optional} then the return value is undefined. */ @Override public <T> T convertToType(ELContext context, Object obj, Class<T> type) { Objects.requireNonNull(context); if (obj instanceof Optional) { Object value = null; if (((Optional<?>) obj).isPresent()) { value = ((Optional<?>) obj).get(); // If the value is assignable to the required type, do so. if (type.isAssignableFrom(value.getClass())) { context.setPropertyResolved(true); @SuppressWarnings("unchecked") T result = (T) value; return result; } } try { T convertedValue = context.convertToType(value, type); context.setPropertyResolved(true); return convertedValue; } catch (ELException e) { /* * TODO: This isn't pretty but it works. Significant refactoring would be required to avoid the * exception. See also Util.isCoercibleFrom(). */ } } return null; } /** * {@inheritDoc} * * @return If the base object is an {@link Optional} and {@link Optional#isEmpty()} returns {@code true} then this * method returns {@code null}. * <p> * If the base object is an {@link Optional} and {@link Optional#isPresent()} returns {@code true} then * this method returns the result of invoking the specified method on the object obtained by calling * {@link Optional#get()} with the specified parameters. * <p> * If the base object is not an {@link Optional} then the return value is undefined. */ @Override public Object invoke(ELContext context, Object base, Object method, Class<?>[] paramTypes, Object[] params) { Objects.requireNonNull(context); if (base instanceof Optional && method != null) { context.setPropertyResolved(base, method); if (((Optional<?>) base).isEmpty()) { return null; } else { Object resolvedBase = ((Optional<?>) base).get(); return context.getELResolver().invoke(context, resolvedBase, method, paramTypes, params); } } return null; } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
13.21
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