001 package org.apache.commons.ognl;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.lang.reflect.Method;
023 import java.util.List;
024 import java.util.Map;
025
026 /**
027 * Implementation of PropertyAccessor that uses reflection on the target object's class to find a field or a pair of
028 * set/get methods with the given property name.
029 *
030 * @author Luke Blanshard (blanshlu@netscape.net)
031 * @author Drew Davidson (drew@ognl.org)
032 */
033 public class ObjectMethodAccessor
034 implements MethodAccessor
035 {
036
037 /**
038 * {@inheritDoc}
039 */
040 public Object callStaticMethod( Map<String, Object> context, Class<?> targetClass, String methodName,
041 Object[] args )
042 throws MethodFailedException
043 {
044 List<Method> methods = OgnlRuntime.getMethods( targetClass, methodName, true );
045
046 return OgnlRuntime.callAppropriateMethod( (OgnlContext) context, targetClass, null, methodName, null, methods,
047 args );
048 }
049
050 /**
051 * {@inheritDoc}
052 */
053 public Object callMethod( Map<String, Object> context, Object target, String methodName, Object[] args )
054 throws MethodFailedException
055 {
056 Class<?> targetClass = ( target == null ) ? null : target.getClass();
057 List<Method> methods = OgnlRuntime.getMethods( targetClass, methodName, false );
058
059 if ( ( methods == null ) || ( methods.size() == 0 ) )
060 {
061 methods = OgnlRuntime.getMethods( targetClass, methodName, true );
062
063 }
064
065 return OgnlRuntime.callAppropriateMethod( (OgnlContext) context, target, target, methodName, null, methods,
066 args );
067 }
068 }