⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapelresolver.java

📁 java属性邦定的(JSR-295)的一个实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2007 Sun Microsystems, Inc. All rights reserved. Use is
 * subject to license terms.
 */

package org.jdesktop.el;

import java.beans.FeatureDescriptor;
import java.util.Map;
import java.util.Iterator;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

/**
 * Defines property resolution behavior on instances of {@link java.util.Map}.
 *
 * <p>This resolver handles base objects of type <code>java.util.Map</code>.
 * It accepts any object as a property and uses that object as a key in
 * the map. The resulting value is the value in the map that is associated with 
 * that key.</p>
 *
 * <p>This resolver can be constructed in read-only mode, which means that
 * {@link #isReadOnly} will always return <code>true</code> and 
 * {@link #setValue} will always throw
 * <code>PropertyNotWritableException</code>.</p>
 *
 * <p><code>ELResolver</code>s are combined together using 
 * {@link CompositeELResolver}s, to define rich semantics for evaluating 
 * an expression. See the javadocs for {@link ELResolver} for details.</p>
 *
 * @see CompositeELResolver
 * @see ELResolver
 * @see java.util.Map
 * @since JSP 2.1
 */
public class MapELResolver extends ELResolver {

    /**
     * Creates a new read/write <code>MapELResolver</code>.
     */
    public MapELResolver() {
        this.isReadOnly = false;
    }

    /**
     * Creates a new <code>MapELResolver</code> whose read-only status is
     * determined by the given parameter.
     *
     * @param isReadOnly <code>true</code> if this resolver cannot modify
     *     maps; <code>false</code> otherwise.
     */
    public MapELResolver(boolean isReadOnly) {
        this.isReadOnly = isReadOnly;
    }

    /**
     * If the base object is a map, returns the most general acceptable type 
     * for a value in this map.
     *
     * <p>If the base is a <code>Map</code>, the <code>propertyResolved</code>
     * property of the <code>ELContext</code> object must be set to
     * <code>true</code> by this resolver, before returning. If this property
     * is not <code>true</code> after this method is called, the caller 
     * should ignore the return value.</p>
     *
     * <p>Assuming the base is a <code>Map</code>, this method will always
     * return <code>Object.class</code>. This is because <code>Map</code>s
     * accept any object as the value for a given key.</p>
     *
     * @param context The context of this evaluation.
     * @param base The map to analyze. Only bases of type <code>Map</code>
     *     are handled by this resolver.
     * @param property The key to return the acceptable type for.
     *     Ignored by this resolver.
     * @return If the <code>propertyResolved</code> property of 
     *     <code>ELContext</code> was set to <code>true</code>, then
     *     the most general acceptable type; otherwise undefined.
     * @throws NullPointerException if context is <code>null</code>
     * @throws ELException if an exception was thrown while performing
     *     the property or variable resolution. The thrown exception
     *     must be included as the cause property of this exception, if
     *     available.
     */
    public Class<?> getType(ELContext context,
                         Object base,
                         Object property) {

        if (context == null) {
            throw new NullPointerException();
        }
      
        if (base != null && base instanceof Map) {
            context.setPropertyResolved(true);
            return Object.class;
        }
        return null;
    }

    /**
     * If the base object is a map, returns the value associated with the
     * given key, as specified by the <code>property</code> argument. If the
     * key was not found, <code>null</code> is returned.
     *
     * <p>If the base is a <code>Map</code>, the <code>propertyResolved</code>
     * property of the <code>ELContext</code> object must be set to
     * <code>true</code> by this resolver, before returning. If this property
     * is not <code>true</code> after this method is called, the caller 
     * should ignore the return value.</p>
     *
     * <p>Just as in {@link java.util.Map#get}, just because <code>null</code>
     * is returned doesn't mean there is no mapping for the key; it's also
     * possible that the <code>Map</code> explicitly maps the key to
     * <code>null</code>.</p>
     *
     * @param context The context of this evaluation.
     * @param base The map to be analyzed. Only bases of type <code>Map</code>
     *     are handled by this resolver.
     * @param property The key whose associated value is to be returned.
     * @return If the <code>propertyResolved</code> property of 
     *     <code>ELContext</code> was set to <code>true</code>, then
     *     the value associated with the given key or <code>null</code>
     *     if the key was not found. Otherwise, undefined.
     * @throws ClassCastException if the key is of an inappropriate type 
     *     for this map (optionally thrown by the underlying <code>Map</code>).
     * @throws NullPointerException if context is <code>null</code>, or if 
     *     the key is null and this map does not permit null keys (the
     *     latter is optionally thrown by the underlying <code>Map</code>).
     * @throws ELException if an exception was thrown while performing
     *     the property or variable resolution. The thrown exception
     *     must be included as the cause property of this exception, if
     *     available.
     */
    public Object getValue(ELContext context,
                           Object base,
                           Object property) {

        if (context == null) {
            throw new NullPointerException();
        }

        if (base != null && base instanceof Map) {
            context.setPropertyResolved(true);
            Map map = (Map) base;
            return map.get(property);
        }
        return null;
    }

    static private Class<?> theUnmodifiableMapClass =
        Collections.unmodifiableMap(new HashMap()).getClass();

    /**
     * If the base object is a map, attempts to set the value associated with
     * the given key, as specified by the <code>property</code> argument.
     *
     * <p>If the base is a <code>Map</code>, the <code>propertyResolved</code>
     * property of the <code>ELContext</code> object must be set to
     * <code>true</code> by this resolver, before returning. If this property
     * is not <code>true</code> after this method is called, the caller 
     * can safely assume no value was set.</p>
     *
     * <p>If this resolver was constructed in read-only mode, this method will
     * always throw <code>PropertyNotWritableException</code>.</p>
     *
     * <p>If a <code>Map</code> was created using 
     * {@link java.util.Collections#unmodifiableMap}, this method must
     * throw <code>PropertyNotWritableException</code>. Unfortunately, 
     * there is no Collections API method to detect this. However, an 
     * implementation can create a prototype unmodifiable <code>Map</code> 
     * and query its runtime type to see if it matches the runtime type of 
     * the base object as a workaround.</p>
     *
     * @param context The context of this evaluation.
     * @param base The map to be modified. Only bases of type <code>Map</code>
     *     are handled by this resolver.
     * @param property The key with which the specified value is to be
     *     associated.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -