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(Member member) {
069            StringBuffer sb = new StringBuffer(member.getName());
070            if (member instanceof Method) {
071                Class[] params = ((Method) member).getParameterTypes();
072                sb.append("(");
073                for (int i = 0; i < params.length; i++) {
074                    String s = params[i].getName();
075                    sb.append(s);
076                    if (i < params.length-1) {
077                        sb.append(", ");
078                    }
079                }
080                sb.append(")");            
081            }
082            return sb.toString();
083        }
084    
085        public static String memberToString(Member m) {
086            if (m instanceof Field) {
087                return toString((Field) m);
088            } else {
089                return methodToString((Method) m);
090            }
091        }
092    
093        public static String toString(Field field) {
094            StringBuffer sb = new StringBuffer(field.getName());
095            sb.append("(").append(field.getName()).append(")");
096            return sb.toString();
097        }
098    
099    }