    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     *****************************************************************************/
    009    package org.picocontainer.injectors;
    010
    011    import org.picocontainer.NameBinding;
    012
    013    import java.lang.reflect.AccessibleObject;
    014    import java.lang.reflect.Constructor;
    015    import java.lang.reflect.Method;
    016
    017    import com.thoughtworks.paranamer.CachingParanamer;
    018    import com.thoughtworks.paranamer.Paranamer;
    019
    020    public class ParameterNameBinding implements NameBinding {
    021        private final Class impl;
    022        private final AccessibleObject member;
    023        private final int index;
    024        private final CachingParanamer paranamer;
    025
    026        private String name;
    027
    028        public ParameterNameBinding(CachingParanamer paranamer, Class impl, AccessibleObject member, int index) {
    029            this.impl = impl;
    030            this.member = member;
    031            this.paranamer = paranamer;
    032            this.index = index;
    033        }
    034
    035        public String getName() {
    036            if (name != null) {
    037                return name;
    038            }
    039            String[] strings = null;
    040            if (paranamer.areParameterNamesAvailable(impl, "<init>") != Paranamer.PARAMETER_NAMES_FOUND) {
    041                paranamer.switchtoAsm();
    042            }
    043            if (member instanceof Constructor) {
    044                strings = paranamer.lookupParameterNames((Constructor)member);
    045            } else {
    046                strings = paranamer.lookupParameterNames((Method)member);
    047            }
    048            name = strings.length == 0 ? "" : strings[index];
    049            return name;
    050        }
    051    }
    052