001    /**
002     Licensed to the Apache Software Foundation (ASF) under one or more
003     contributor license agreements.  See the NOTICE file distributed with
004     this work for additional information regarding copyright ownership.
005     The ASF licenses this file to You under the Apache License, Version 2.0
006     (the "License"); you may not use this file except in compliance with
007     the License.  You may obtain a copy of the License at
008    
009          http://www.apache.org/licenses/LICENSE-2.0
010    
011     Unless required by applicable law or agreed to in writing, software
012     distributed under the License is distributed on an "AS IS" BASIS,
013     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     See the License for the specific language governing permissions and
015     limitations under the License.
016    */
017    package org.apache.commons.inject.impl;
018    
019    import java.lang.reflect.Constructor;
020    import java.lang.reflect.Method;
021    
022    import org.apache.commons.inject.api.IBinding;
023    import org.apache.commons.inject.api.IInjector;
024    import org.apache.commons.inject.api.IPoint;
025    import org.apache.commons.inject.util.Exceptions;
026    
027    public class FactoryMethodProvider<T> extends AbstractBaseProvider<T> implements IInjectorAware {
028            private final Constructor<T> constructor;
029            private final Method method;
030            private final IBinding<Object>[] parameterBindings;
031    
032            public FactoryMethodProvider(Constructor<T> pConstructor, IPoint<T> pPoint, IBinding<Object>[] pBindings) {
033                    super(pConstructor.getDeclaringClass(), pPoint);
034                    constructor = pConstructor;
035                    method = null;
036                    parameterBindings = pBindings;
037            }
038    
039            @SuppressWarnings("unchecked")
040            public FactoryMethodProvider(Method pMethod, IBinding<Object>[] pBindings, IPoint<T> pPoint) {
041                    super((Class<T>) pMethod.getReturnType(), pPoint);
042                    constructor = null;
043                    method = pMethod;
044                    parameterBindings = pBindings;
045                    
046            }
047    
048            @Override
049            public T get() {
050                    try {
051                            final Object[] parameters = new Object[parameterBindings.length];
052                            for (int i = 0;  i < parameters.length;  i++) {
053                                    parameters[i] = parameterBindings[i].getProvider().get();
054                            }
055                            if (constructor == null) {
056                                    @SuppressWarnings("unchecked")
057                                    final T instance = (T) method.invoke(null, parameters);
058                                    return instance;
059                            } else {
060                                    if (!constructor.isAccessible()) {
061                                            constructor.setAccessible(true);
062                                    }
063                                    return constructor.newInstance(parameters);
064                            }
065                    } catch (Throwable t) {
066                            throw Exceptions.show(t);
067                    }
068            }
069    
070            @Override
071            public void init(IInjector pInjector) {
072                    for (IBinding<Object> binding : parameterBindings) {
073                            if (binding instanceof IInjectorAware) {
074                                    ((IInjectorAware) binding).init(pInjector);
075                            }
076                    }
077            }
078    }