    001    /*****************************************************************************
    002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     * Original code by Paul Hammaant                                            *
    009     *****************************************************************************/
    010
    011    package org.picocontainer.monitors;
    012
    013    import java.text.MessageFormat;
    014    import java.lang.reflect.Constructor;
    015    import java.lang.reflect.Method;
    016    import java.lang.reflect.Field;
    017    import java.lang.reflect.Member;
    018
    019    import org.picocontainer.ComponentMonitor;
    020
    021    /**
    022     * An abstract {@link ComponentMonitor} which supports all the message formats.
    023     *
    024     * @author Mauro Talevi
    025     */
    026    public final class ComponentMonitorHelper  {
    027
    028        public final static String INSTANTIATING = "PicoContainer: instantiating {0}";
    029        public final static String INSTANTIATED = "PicoContainer: instantiated {0} [{1} ms], component {2}, injected [{3}]";
    030        public final static String INSTANTIATION_FAILED = "PicoContainer: instantiation failed: {0}, reason: {1}";
    031        public final static String INVOKING = "PicoContainer: invoking {0} on {1}";
    032        public final static String INVOKED = "PicoContainer: invoked {0} on {1} [{2} ms]";
    033        public final static String INVOCATION_FAILED = "PicoContainer: invocation failed: {0} on {1}, reason: {2}";
    034        public final static String LIFECYCLE_INVOCATION_FAILED = "PicoContainer: lifecycle invocation failed: {0} on {1}, reason: {2}";
    035        public final static String NO_COMPONENT = "PicoContainer: No component for key: {0}";
    036
    037        public static String format(String template, Object... arguments) {
    038            return MessageFormat.format(template, arguments);
    039        }
    040
    041        public static String parmsToString(Object[] injected) {
    042            StringBuffer sb = new StringBuffer();
    043            for (int i = 0; i < injected.length; i++) {
    044                String s = injected[i].getClass().getName();
    045                sb.append(s);
    046                if (i < injected.length-1) {
    047                    sb.append(", ");
    048                }
    049            }
    050            return sb.toString();
    051        }
    052
    053        public static String ctorToString(Constructor constructor) {
    054            Class[] params = constructor.getParameterTypes();
    055            StringBuffer sb = new StringBuffer(constructor.getName());
    056            sb.append("(");
    057            for (int i = 0; i < params.length; i++) {
    058                String s = params[i].getName();
    059                sb.append(s);
    060                if (i < params.length-1) {
    061                    sb.append(", ");
    062                }
    063            }
    064            sb.append(")");
    065            return sb.toString();
    066        }
    067
    068        public static String methodToString(Method method) {
    069            Class[] params = method.getParameterTypes();
    070            StringBuffer sb = new StringBuffer(method.getName());
    071            sb.append("(");
    072            for (int i = 0; i < params.length; i++) {
    073                String s = params[i].getName();
    074                sb.append(s);
    075                if (i < params.length-1) {
    076                    sb.append(", ");
    077                }
    078            }
    079            sb.append(")");
    080            return sb.toString();
    081        }
    082
    083        public static String memberToString(Member m) {
    084            if (m instanceof Field) {
    085                return toString((Field) m);
    086            } else {
    087                return methodToString((Method) m);
    088            }
    089        }
    090
    091        public static String toString(Field field) {
    092            StringBuffer sb = new StringBuffer(field.getName());
    093            sb.append("(").append(field.getName()).append(")");
    094            return sb.toString();
    095        }
    096
    097    }