001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.jexl2; 018 019 /** 020 * Wraps an Object as a Jexl context. 021 * @param <T> the wrapped object type to use 022 * @since 2.1 023 */ 024 public class ObjectContext<T> implements JexlContext { 025 /** The property solving jexl engine. */ 026 private final JexlEngine jexl; 027 /** The object serving as context provider. */ 028 private final T object; 029 030 /** 031 * Creates a new ObjectContext. 032 * @param engine the jexl engine to use to solve properties 033 * @param wrapped the object to wrap in this context 034 */ 035 public ObjectContext(JexlEngine engine, T wrapped) { 036 this.jexl = engine; 037 this.object = wrapped; 038 } 039 040 /** {@inheritDoc} */ 041 public Object get(String name) { 042 return jexl.getProperty(object, name); 043 } 044 045 /** {@inheritDoc} */ 046 public void set(String name, Object value) { 047 jexl.setProperty(object, name, value); 048 } 049 050 /** {@inheritDoc} */ 051 public boolean has(String name) { 052 return jexl.getUberspect().getPropertyGet(object, name, null) != null; 053 } 054 }