001    /* JAPI - (Yet another (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.lang;
022    
023    import java.lang.reflect.InvocationTargetException;
024    import java.lang.reflect.Method;
025    
026    /** Class for working with Properties.
027     * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
028     */
029    @SuppressWarnings({"UtilityClass"})
030    public class Properties {
031    
032        /** Get a property of an object.
033         * @param object
034         * @param propertyName
035         * @see #setPropertyFromString(Object, String, String)
036         */
037        public static String getPropertyAsString(final Object object, final String propertyName) {
038            final String getterName = new StringBuilder().append("get").append(propertyName.substring(0, 1).toUpperCase()).append(propertyName.substring(1)).toString();
039            final String isserName = new StringBuilder().append("is").append(propertyName.substring(0, 1).toUpperCase()).append(propertyName.substring(1)).toString();
040            Method getter = null;
041            for (final Method method : object.getClass().getMethods()) {
042                final Class<?>[] parameterTypes = method.getParameterTypes();
043                final String methodName = method.getName();
044                if (parameterTypes.length == 0 && (methodName.equals(getterName) || methodName.equals(isserName))) {
045                    getter = method;
046                }
047            }
048            if (getter == null) {
049                throw new NoSuchMethodError(getterName + '|' + isserName);
050            }
051            try {
052                return getter.invoke(object).toString();
053            } catch (final IllegalAccessException e) {
054                throw new NoSuchMethodError(getterName + '|' + isserName + ": " + e);
055            } catch (final InvocationTargetException e) {
056                throw new NoSuchMethodError(getterName + '|' + isserName + ": " + e);
057            }
058        }
059    
060        /** Set a property of an object.
061         * @param object Object to invoke setter on
062         * @param propertyName Name of the property to get setter for
063         * @param value Value to set as String (conversions from {@link java.beans} are used)
064         * @see #getPropertyAsString(Object, String)
065         */
066        @SuppressWarnings({"IfStatementWithTooManyBranches", "ObjectEquality", "StringToUpperCaseOrToLowerCaseWithoutLocale", "OverlyComplexMethod"})
067        public static void setPropertyFromString(final Object object, final String propertyName, final String value) {
068            final String setterName = new StringBuilder().append("set").append(propertyName.substring(0, 1).toUpperCase()).append(propertyName.substring(1)).toString();
069            Method setter = null;
070            Object rawValue = null;
071            for (final Method method : object.getClass().getMethods()) {
072                final Class<?>[] parameterTypes = method.getParameterTypes();
073                if (parameterTypes.length == 1 && method.getName().equals(setterName)) {
074                    final Class<?> parameterType = parameterTypes[0];
075                    setter = method;
076                    if (parameterType == Boolean.TYPE) {
077                        rawValue = Boolean.valueOf(value);
078                    } else if (parameterType == Character.TYPE) {
079                        rawValue = value.charAt(0);
080                    } else if (parameterType == Byte.TYPE) {
081                        rawValue = Byte.valueOf(value);
082                    } else if (parameterType == Short.TYPE) {
083                        rawValue = Short.valueOf(value);
084                    } else if (parameterType == Integer.TYPE) {
085                        rawValue = Integer.valueOf(value);
086                    } else if (parameterType == Long.TYPE) {
087                        rawValue = Long.valueOf(value);
088                    } else if (parameterType == Float.TYPE) {
089                        rawValue = Float.valueOf(value);
090                    } else if (parameterType == Double.TYPE) {
091                        rawValue = Double.valueOf(value);
092                    } else {
093                        rawValue = value;
094                    }
095                }
096            }
097            if (setter == null) {
098                throw new NoSuchMethodError(setterName);
099            }
100            try {
101                setter.invoke(object, rawValue);
102            } catch (final IllegalAccessException e) {
103                throw new NoSuchMethodError(setterName + ": " + e);
104            } catch (final InvocationTargetException e) {
105                throw new NoSuchMethodError(setterName + ": " + e);
106            }
107        }
108    
109        /** Private constructor for Utility class. */
110        private Properties() {
111        }
112    
113    } // class Properties