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                  throw new MgnlInstantiationException("Unable to resolve parameters for constructor " + selectedConstructor);
78              }
79              return newInstance(selectedConstructor, parameters);
80          }
81  
82          // Find greediest satisfiable constructor
83          int bestScore = -1;
84          Object[] bestParameters = null;
85          for (Constructor<?> constructor : constructors) {
86              if (!Modifier.isPublic(constructor.getModifiers())) {
87                  continue;
88              }
89              int score = constructor.getParameterTypes().length;
90              if (score < bestScore) {
91                  continue;
92              }
93              Object[] parameters = resolveParameters(constructor, parameterResolvers);
94              if (parameters == null) {
95                  continue;
96              }
97              selectedConstructor = constructor;
98              bestScore = score;
99              bestParameters = parameters;
100         }
101         if (selectedConstructor != null) {
102             return newInstance(selectedConstructor, bestParameters);
103         }
104         throw new MgnlInstantiationException("No suitable constructor found for class [" + clazz + "]");
105     }
106 
107     private Object newInstance(Constructor constructor, Object[] parameters) {
108         try {
109             return constructor.newInstance(parameters);
110         } catch (InstantiationException e) {
111             throw new MgnlInstantiationException(e);
112         } catch (IllegalAccessException e) {
113             throw new MgnlInstantiationException(e);
114         } catch (InvocationTargetException e) {
115             throw new MgnlInstantiationException(e);
116         }
117     }
118 
119     private Object[] resolveParameters(Constructor<?> constructor, ParameterResolver[] parameterResolvers) {
120         Object[] parameters = new Object[constructor.getParameterTypes().length];
121         for (int parameterIndex = 0; parameterIndex < parameters.length; parameterIndex++) {
122             ParameterInfo constructorParameter = new ParameterInfo(constructor, parameterIndex);
123             Object parameter = resolveParameter(constructorParameter, parameterResolvers);
124             if (parameter == ParameterResolver.UNRESOLVED) {
125                 return null;
126             }
127             parameters[parameterIndex] = parameter;
128         }
129         return parameters;
130     }
131 
132     private Object resolveParameter(ParameterInfo constructorParameter, ParameterResolver[] parameterResolvers) {
133         for (ParameterResolver parameterResolver : parameterResolvers) {
134             Object parameter = parameterResolver.resolveParameter(constructorParameter);
135             if (parameter != ParameterResolver.UNRESOLVED) {
136                 return parameter;
137             }
138         }
139         log.debug("Failed to resolve {}. parameter {} in constructor of {}. Will use other constructor (if available).", (constructorParameter.getParameterIndex() + 1), constructorParameter.getParameterType(), constructorParameter.getDeclaringClass());
140         return ParameterResolver.UNRESOLVED;
141     }
142 
143 }