/* * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser General Public License * (LGPL) version 2.1 which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl.html * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * Contributors: * bstefanescu * * $Id$ */ package org.nuxeo.ecm.core.api.model.impl.osm.util; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> * */ public class ObjectAccessorHelper { // Utility class. private ObjectAccessorHelper() { } public static Method getMethod(Class<?> containerType, String name) throws NoSuchMethodException { try { return containerType.getDeclaredMethod(name); } catch (NoSuchMethodException e) { Class<?> superClass = containerType.getSuperclass(); if (superClass == null) { throw e; } return getMethod(superClass, name); } } public static Field getField(Class<?> containerType, String name) throws NoSuchFieldException { try { return containerType.getDeclaredField(name); } catch (NoSuchFieldException e) { Class<?> superClass = containerType.getSuperclass(); if (superClass == null) { throw e; } return getField(superClass, name); } } public static String getGetterName(String name) { if (name == null) { return "get"; } int len = name.length(); if (len == 0) { return "get"; } StringBuilder sb = new StringBuilder(len + 3); sb.append("get").append(name); char ch = name.charAt(0); ch = Character.toUpperCase(ch); sb.setCharAt(3, ch); return sb.toString(); } public static String getSetterName(String name) { if (name == null) { return "set"; } int len = name.length(); if (len == 0) { return "set"; } StringBuilder sb = new StringBuilder(len + 3); sb.append("set").append(name); char ch = name.charAt(0); ch = Character.toUpperCase(ch); sb.setCharAt(3, ch); return sb.toString(); } // FIXME: this seems buggy. public static String getPropertyName(String methodName) { if ((methodName.startsWith("get") || methodName.startsWith("get")) && methodName.length() > 3) { methodName = methodName.substring(3); StringBuilder sb = new StringBuilder(methodName); sb.setCharAt(0, Character.toLowerCase(sb.charAt(0))); } return methodName; } public static MemberAccessor getFieldAccessor(Class<?> containerType, String name) throws NoSuchFieldException { return new FieldAccessor(getField(containerType, name)); } public static MemberAccessor getFieldAccessor(Class<?> containerType, String name, boolean isReadOnly) throws NoSuchFieldException { return new FieldAccessor(getField(containerType, name), isReadOnly); } public static MemberAccessor getPropertyAccessor(Class<?> containerType, String name) throws NoSuchMethodException { return new MethodAccessor(getMethod(containerType, getGetterName(name)), getMethod(containerType, getSetterName(name))); } public static MemberAccessor getPropertyAccessor( Class<?> containerType, String name, boolean IsReadOnly) throws NoSuchMethodException { String getter = getGetterName(name); String setter = IsReadOnly ? null : getSetterName(name); return getMethodAccessor(containerType, getter, setter); } public static MemberAccessor getMethodAccessor(Class<?> containerType, String getterName, String setterName) throws NoSuchMethodException { return new MethodAccessor(getMethod(containerType, getterName), setterName != null ? getMethod(containerType, setterName) : null); } public static MemberAccessor getMemberAccessor(Class<?> containerType, String name) throws NoSuchMemberException { return getMemberAccessor(containerType, name, false); } public static MemberAccessor getMemberAccessor(Class<?> containerType, String name, boolean isReadOnly) throws NoSuchMemberException { try { return getFieldAccessor(containerType, name, isReadOnly); } catch (NoSuchFieldException e) { try { return getPropertyAccessor(containerType, name, isReadOnly); } catch (NoSuchMethodException ee) { throw new NoSuchMemberException("No such member: " + name); } } } }