View Javadoc
1   /**
2    * This file Copyright (c) 2011-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.objectfactory;
35  
36  import java.lang.reflect.Constructor;
37  import java.lang.reflect.InvocationTargetException;
38  import java.lang.reflect.Modifier;
39  
40  import javax.inject.Inject;
41  
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  
46  /**
47   * Creates objects by dynamically resolving the parameters to use.
48   */
49  public class ObjectManufacturer {
50  
51      private static final Logger log = LoggerFactory.getLogger(ObjectManufacturer.class);
52  
53      /**
54       * Creates an object of the given type using parameters provided by a set of parameter resolvers. Will first look
55       * for a constructor annotated with @Inject, it can be public, private, protected or package private. It will
56       * then look at public constructors and use the greediest. The greediest constructor is the one that has the most
57       * number of arguments that the parameter resolvers can fulfill. If there is more than one constructor that
58       * qualifies as greediest it is unspecified which one will be used.
59       */
60      public Object newInstance(Class<?> clazz, ParameterResolver... parameterResolvers) {
61  
62          Constructor<?>[] constructors = clazz.getDeclaredConstructors();
63  
64          Constructor<?> selectedConstructor = null;
65          for (Constructor<?> constructor : constructors) {
66              if (constructor.isAnnotationPresent(Inject.class)) {
67                  if (selectedConstructor != null) {
68                      throw new MgnlInstantiationException("Only one constructor can use @Inject [" + clazz + "]");
69                  }
70                  selectedConstructor = constructor;
71              }
72          }
73          if (selectedConstructor != null) {
74              selectedConstructor.setAccessible(true);
75              Object[] parameters = resolveParameters(selectedConstructor, parameterResolvers);
76              if (parameters == null) {
77                  StringBuilder params = new StringBuilder();
78                  int index = 1;
79                  for (Object param : getUnresolvableParameters(selectedConstructor, parameterResolvers)) {
80                      if (param != null) {
81                          params.append(index).append(index == 1 ? "st" : index == 2 ? "nd" : index == 3 ? "rd" : "th").append(" parameter which is of type ").append(((ParameterInfo) param).getParameterType().getName()).append(", ");
82                      }
83                      index++;
84                  }
85  
86                  throw new MgnlInstantiationException("Unable to resolve parameters for constructor " + selectedConstructor + (params.length() > 2 ? (". Unresolved parameter(s) are: " + params.toString().substring(0, params.length() - 2)) : ""));
87              }
88              return newInstance(selectedConstructor, parameters);
89          }
90  
91          // Find greediest satisfiable constructor
92          int bestScore = -1;
93          Object[] bestParameters = null;
94          for (Constructor<?> constructor : constructors) {
95              if (!Modifier.isPublic(constructor.getModifiers())) {
96                  continue;
97              }
98              int score = constructor.getParameterTypes().length;
99              if (score < bestScore) {
100                 continue;
101             }
102             Object[] parameters = resolveParameters(constructor, parameterResolvers);
103             if (parameters == null) {
104                 continue;
105             }
106             selectedConstructor = constructor;
107             bestScore = score;
108             bestParameters = parameters;
109         }
110         if (selectedConstructor != null) {
111             return newInstance(selectedConstructor, bestParameters);
112         }
113         throw new MgnlInstantiationException("No suitable constructor found for class [" + clazz + "]");
114     }
115 
116     private Object newInstance(Constructor constructor, Object[] parameters) {
117         try {
118             return constructor.newInstance(parameters);
119         } catch (InstantiationException e) {
120             throw new MgnlInstantiationException(e);
121         } catch (IllegalAccessException e) {
122             throw new MgnlInstantiationException(e);
123         } catch (InvocationTargetException e) {
124             throw new MgnlInstantiationException(e);
125         }
126     }
127 
128     private Object[] resolveParameters(Constructor<?> constructor, ParameterResolver[] parameterResolvers) {
129         Object[] parameters = new Object[constructor.getParameterTypes().length];
130         for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
131             ParameterInfo constructorParameter = new ParameterInfo(constructor, parameterIndex);
132             Object parameter = resolveParameter(constructorParameter, parameterResolvers);
133             if (parameter == ParameterResolver.UNRESOLVED) {
134                 return null;
135             }
136             parameters[parameterIndex] = parameter;
137         }
138         return parameters;
139     }
140 
141     private Object[] getUnresolvableParameters(Constructor<?> constructor, ParameterResolver[] parameterResolvers) {
142         Object[] parameters = new Object[constructor.getParameterTypes().length];
143         for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
144             ParameterInfo constructorParameter = new ParameterInfo(constructor, parameterIndex);
145             Object parameter = resolveParameter(constructorParameter, parameterResolvers);
146             if (parameter == ParameterResolver.UNRESOLVED) {
147                 parameters[parameterIndex] = constructorParameter;
148             }
149         }
150         return parameters;
151     }
152 
153     private Object resolveParameter(ParameterInfo constructorParameter, ParameterResolver[] parameterResolvers) {
154         for (ParameterResolver parameterResolver : parameterResolvers) {
155             Object parameter = parameterResolver.resolveParameter(constructorParameter);
156             if (parameter != ParameterResolver.UNRESOLVED) {
157                 return parameter;
158             }
159         }
160         log.debug("Failed to resolve {}. parameter {} in constructor of {}. Will use other constructor (if available).", (constructorParameter.getParameterIndex() + 1), constructorParameter.getParameterType(), constructorParameter.getDeclaringClass());
161         return ParameterResolver.UNRESOLVED;
162     }
163 
164 }