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.geronimo.gbean;
018    
019    import java.beans.Introspector;
020    import java.lang.reflect.Constructor;
021    import java.lang.reflect.Method;
022    import java.util.Collection;
023    import java.util.HashMap;
024    import java.util.HashSet;
025    import java.util.Iterator;
026    import java.util.List;
027    import java.util.Map;
028    import java.util.Set;
029    import java.util.Arrays;
030    
031    import org.apache.geronimo.kernel.ClassLoading;
032    import org.apache.geronimo.kernel.Kernel;
033    
034    /**
035     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
036     */
037    public class GBeanInfoBuilder {
038        public static GBeanInfoBuilder createStatic(Class gbeanType) {
039            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
040            return createStatic(gbeanType, gbeanType.getName(), gbeanType, null, null);
041        }
042    
043        public static GBeanInfoBuilder createStatic(Class gbeanType, String j2eeType) {
044            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
045            return createStatic(gbeanType, gbeanType.getName(), gbeanType, null, j2eeType);
046        }
047    
048        public static GBeanInfoBuilder createStatic(String name, Class gbeanType) {
049            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
050            return createStatic(gbeanType, name, gbeanType, null, null);
051        }
052    
053        public static GBeanInfoBuilder createStatic(String name, Class gbeanType, String j2eeType) {
054            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
055            return createStatic(gbeanType, name, gbeanType, null, j2eeType);
056        }
057    
058        public static GBeanInfoBuilder createStatic(Class gbeanType, GBeanInfo source) {
059            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
060            return createStatic(gbeanType, gbeanType.getName(), gbeanType, source, null);
061        }
062    
063        public static GBeanInfoBuilder createStatic(Class gbeanType, GBeanInfo source, String j2eeType) {
064            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
065            return createStatic(gbeanType, gbeanType.getName(), gbeanType, source, j2eeType);
066        }
067    
068        public static GBeanInfoBuilder createStatic(String name, Class gbeanType, GBeanInfo source) {
069            if (name == null) throw new NullPointerException("name is null");
070            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
071            return createStatic(gbeanType, name, gbeanType, source, null);
072        }
073    
074        //
075        // These methods are used by classes that declare a GBeanInfo for another class
076        //
077        public static GBeanInfoBuilder createStatic(Class sourceClass, Class gbeanType) {
078            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
079            return createStatic(sourceClass, gbeanType.getName(), gbeanType, null, null);
080        }
081    
082        public static GBeanInfoBuilder createStatic(Class sourceClass, Class gbeanType, String j2eeType) {
083            if (sourceClass == null) throw new NullPointerException("sourceClass is null");
084            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
085            return createStatic(sourceClass, gbeanType.getName(), gbeanType, null, j2eeType);
086        }
087    
088        public static GBeanInfoBuilder createStatic(Class sourceClass, Class gbeanType, GBeanInfo source, String j2eeType) {
089            if (sourceClass == null) throw new NullPointerException("sourceClass is null");
090            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
091            return createStatic(sourceClass, gbeanType.getName(), gbeanType, source, j2eeType);
092        }
093    
094        public static GBeanInfoBuilder createStatic(Class sourceClass, String name, Class gbeanType, String j2eeType) {
095            if (sourceClass == null) throw new NullPointerException("sourceClass is null");
096            if (name == null) throw new NullPointerException("name is null");
097            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
098            return createStatic(sourceClass, name, gbeanType, null, j2eeType);
099        }
100    
101        public static GBeanInfoBuilder createStatic(Class sourceClass, String name, Class gbeanType, GBeanInfo source, String j2eeType) {
102            if (sourceClass == null) throw new NullPointerException("sourceClass is null");
103            if (name == null) throw new NullPointerException("name is null");
104            if (gbeanType == null) throw new NullPointerException("gbeanType is null");
105            return new GBeanInfoBuilder(sourceClass.getName(), name, gbeanType, source, j2eeType);
106        }
107    
108        public static final String DEFAULT_J2EE_TYPE = "GBean"; //NameFactory.GERONIMO_SERVICE
109    
110        private static final Class[] NO_ARGS = {};
111    
112        /**
113         * The class from which the info can be retrieved using GBeanInfo.getGBeanInfo(className, classLoader)
114         */
115        private final String sourceClass;
116    
117        private final String name;
118    
119        private final String j2eeType;
120    
121        private final Class gbeanType;
122    
123        private final Map attributes = new HashMap();
124    
125        private GConstructorInfo constructor = new GConstructorInfo();
126    
127        private final Map operations = new HashMap();
128    
129        private final Map references = new HashMap();
130    
131        private final Set interfaces = new HashSet();
132    
133        private int priority = GBeanInfo.PRIORITY_NORMAL;
134    
135        public GBeanInfoBuilder(Class gbeanType) {
136            this(checkNotNull(gbeanType).getName(), gbeanType, null, null);
137        }
138    
139        public GBeanInfoBuilder(Class gbeanType, String j2eeType) {
140            this(checkNotNull(gbeanType).getName(), gbeanType, null, j2eeType);
141        }
142    
143        public GBeanInfoBuilder(String name, Class gbeanType) {
144            this(name, checkNotNull(gbeanType), null, null);
145        }
146    
147        public GBeanInfoBuilder(String name, Class gbeanType, String j2eeType) {
148            this(name, checkNotNull(gbeanType), null, j2eeType);
149        }
150    
151        public GBeanInfoBuilder(Class gbeanType, GBeanInfo source) {
152            this(checkNotNull(gbeanType).getName(), gbeanType, source);
153        }
154    
155        public GBeanInfoBuilder(Class gbeanType, GBeanInfo source, String j2eeType) {
156            this(checkNotNull(gbeanType).getName(), gbeanType, source, j2eeType);
157        }
158    
159        //TODO remove this
160        /**
161         * @deprecated This will be removed in a future release
162         */
163        public GBeanInfoBuilder(String name, ClassLoader classLoader) {
164            this(checkNotNull(name), loadClass(classLoader, name), GBeanInfo.getGBeanInfo(name, classLoader));
165        }
166    
167        public GBeanInfoBuilder(String name, Class gbeanType, GBeanInfo source) {
168            this(name, gbeanType, source, null);
169        }
170    
171        public GBeanInfoBuilder(String name, Class gbeanType, GBeanInfo source, String j2eeType) {
172            this(null, name, gbeanType, source, j2eeType);
173        }
174    
175        private GBeanInfoBuilder(String sourceClass, String name, Class gbeanType, GBeanInfo source, String j2eeType) {
176            checkNotNull(name);
177            checkNotNull(gbeanType);
178            this.name = name;
179            this.gbeanType = gbeanType;
180            this.sourceClass = sourceClass;
181    
182            if (source != null) {
183                for (Iterator i = source.getAttributes().iterator(); i.hasNext();) {
184                    GAttributeInfo attributeInfo = (GAttributeInfo) i.next();
185                    attributes.put(attributeInfo.getName(), attributeInfo);
186                }
187    
188                for (Iterator i = source.getOperations().iterator(); i.hasNext();) {
189                    GOperationInfo operationInfo = (GOperationInfo) i.next();
190                    operations.put(new GOperationSignature(operationInfo.getName(), operationInfo.getParameterList()), operationInfo);
191                }
192    
193                for (Iterator iterator = source.getReferences().iterator(); iterator.hasNext();) {
194                    GReferenceInfo referenceInfo = (GReferenceInfo) iterator.next();
195                    references.put(referenceInfo.getName(), new RefInfo(referenceInfo.getReferenceType(), referenceInfo.getNameTypeName()));
196                }
197    
198                for (Iterator iterator = source.getInterfaces().iterator(); iterator.hasNext();) {
199                    String intf = (String) iterator.next();
200                    interfaces.add(intf);
201                }
202    
203                //in case subclass constructor has same parameters as superclass.
204                constructor = source.getConstructor();
205    
206                priority = source.getPriority();
207            }
208            if (j2eeType != null) {
209                this.j2eeType = j2eeType;
210            } else if (source != null) {
211                this.j2eeType = source.getJ2eeType();
212            } else {
213                this.j2eeType = DEFAULT_J2EE_TYPE; //NameFactory.GERONIMO_SERVICE
214            }
215    
216            // add all interfaces based on GBean type
217            if (gbeanType.isArray()) {
218                throw new IllegalArgumentException("GBean is an array type: gbeanType=" + gbeanType.getName());
219            }
220            Set allTypes = ClassLoading.getAllTypes(gbeanType);
221            for (Iterator iterator = allTypes.iterator(); iterator.hasNext();) {
222                Class type = (Class) iterator.next();
223                addInterface(type);
224            }
225        }
226    
227        public void setPersistentAttributes(String[] persistentAttributes) {
228            for (int i = 0; i < persistentAttributes.length; i++) {
229                String attributeName = persistentAttributes[i];
230                GAttributeInfo attribute = (GAttributeInfo) attributes.get(attributeName);
231                if (attribute != null && !references.containsKey(attributeName)) {
232                    if (isMagicAttribute(attribute)) {
233                        // magic attributes can't be persistent
234                        continue;
235                    }
236                    attributes.put(attributeName,
237                            new GAttributeInfo(attributeName,
238                                    attribute.getType(),
239                                    true,
240                                    attribute.isManageable(),
241                                    attribute.getGetterName(),
242                                    attribute.getSetterName()));
243                } else {
244                    if (attributeName.equals("kernel")) {
245                        addAttribute("kernel", Kernel.class, false);
246                    } else if (attributeName.equals("classLoader")) {
247                        addAttribute("classLoader", ClassLoader.class, false);
248                    } else if (attributeName.equals("abstractName")) {
249                        addAttribute("abstractName", AbstractName.class, false);
250                    } else if (attributeName.equals("objectName")) {
251                        addAttribute("obectName", String.class, false);
252                    }
253                }
254            }
255        }
256    
257        public void setManageableAttributes(String[] manageableAttributes) {
258            for (int i = 0; i < manageableAttributes.length; i++) {
259                String attributeName = manageableAttributes[i];
260                GAttributeInfo attribute = (GAttributeInfo) attributes.get(attributeName);
261                if (attribute != null) {
262                    attributes.put(attributeName,
263                            new GAttributeInfo(attributeName,
264                                    attribute.getType(),
265                                    attribute.isPersistent(),
266                                    true,
267                                    attribute.getGetterName(),
268                                    attribute.getSetterName()));
269                }
270            }
271        }
272    
273        private boolean isMagicAttribute(GAttributeInfo attributeInfo) {
274            String name = attributeInfo.getName();
275            String type = attributeInfo.getType();
276            return ("kernel".equals(name) && Kernel.class.getName().equals(type)) ||
277                    ("classLoader".equals(name) && ClassLoader.class.getName().equals(type)) ||
278                    ("abstractName".equals(name) && AbstractName.class.getName().equals(type)) ||
279                    ("objectName".equals(name) && String.class.getName().equals(type));
280        }
281    
282        public void addInterface(Class intf) {
283            addInterface(intf, new String[0]);
284        }
285    
286        //do not use beaninfo Introspector to list the properties.  This method is primarily for interfaces,
287        //and it does not process superinterfaces.  It seems to really only work well for classes.
288        public void addInterface(Class intf, String[] persistentAttributes) {
289            addInterface(intf, persistentAttributes, new String[0]);
290        }
291    
292        public void addInterface(Class intf, String[] persistentAttributes, String[] manageableAttributes) {
293            Set persistentNames = new HashSet(Arrays.asList(persistentAttributes));
294            Set manageableNames = new HashSet(Arrays.asList(manageableAttributes));
295            Method[] methods = intf.getMethods();
296            for (int i = 0; i < methods.length; i++) {
297                Method method = methods[i];
298                if ("java.lang.Object".equals(method.getDeclaringClass().getName())) continue;
299                if (isGetter(method)) {
300                    String attributeName = getAttributeName(method);
301                    GAttributeInfo attribute = (GAttributeInfo) attributes.get(attributeName);
302                    String attributeType = method.getReturnType().getName();
303                    if (attribute == null) {
304                        attributes.put(attributeName,
305                                new GAttributeInfo(attributeName,
306                                        attributeType,
307                                        persistentNames.contains(attributeName),
308                                        manageableNames.contains(attributeName),
309                                        method.getName(),
310                                        null));
311                    } else {
312                        if (!attributeType.equals(attribute.getType())) {
313                            throw new IllegalArgumentException("Getter and setter type do not match: " + attributeName + " for gbeanType: " + gbeanType.getName());
314                        }
315                        attributes.put(attributeName,
316                                new GAttributeInfo(attributeName,
317                                        attributeType,
318                                        attribute.isPersistent() || persistentNames.contains(attributeName),
319                                        attribute.isManageable() || manageableNames.contains(attributeName),
320                                        method.getName(),
321                                        attribute.getSetterName()));
322                    }
323                } else if (isSetter(method)) {
324                    String attributeName = getAttributeName(method);
325                    String attributeType = method.getParameterTypes()[0].getName();
326                    GAttributeInfo attribute = (GAttributeInfo) attributes.get(attributeName);
327                    if (attribute == null) {
328                        attributes.put(attributeName,
329                                new GAttributeInfo(attributeName,
330                                        attributeType,
331                                        persistentNames.contains(attributeName),
332                                        manageableNames.contains(attributeName),
333                                        null,
334                                        method.getName()));
335                    } else {
336                        if (!attributeType.equals(attribute.getType())) {
337                            throw new IllegalArgumentException("Getter and setter type do not match: " + attributeName + " for gbeanType: " + gbeanType.getName());
338                        }
339                        attributes.put(attributeName,
340                                new GAttributeInfo(attributeName,
341                                        attributeType,
342                                        attribute.isPersistent() || persistentNames.contains(attributeName),
343                                        attribute.isManageable() || manageableNames.contains(attributeName),
344                                        attribute.getGetterName(),
345                                        method.getName()));
346                    }
347                } else {
348                    addOperation(new GOperationInfo(method.getName(), method.getParameterTypes(), method.getReturnType().getName()));
349                }
350            }
351            addInterface(interfaces, intf);
352        }
353    
354        private static void addInterface(Set set, Class intf) {
355            String name = intf.getName();
356            if(set.contains(name)) {
357                return;
358            }
359            set.add(name);
360            Class cls[] = intf.getInterfaces();
361            for (int i = 0; i < cls.length; i++) {
362                addInterface(set, cls[i]);
363            }
364        }
365    
366        public void addAttribute(String name, Class type, boolean persistent) {
367            addAttribute(name, type.getName(), persistent, true);
368        }
369    
370        public void addAttribute(String name, String type, boolean persistent) {
371            addAttribute(name, type, persistent, true);
372        }
373    
374        public void addAttribute(String name, Class type, boolean persistent, boolean manageable) {
375            addAttribute(name, type.getName(), persistent, manageable);
376        }
377    
378        public void addAttribute(String name, String type, boolean persistent, boolean manageable) {
379            String getter = searchForGetter(name, type, gbeanType);
380            String setter = searchForSetter(name, type, gbeanType);
381            addAttribute(new GAttributeInfo(name, type, persistent, manageable, getter, setter));
382        }
383    
384        public void addAttribute(GAttributeInfo info) {
385            attributes.put(info.getName(), info);
386        }
387    
388        public void setConstructor(GConstructorInfo constructor) {
389            assert constructor != null;
390            this.constructor = constructor;
391            List names = constructor.getAttributeNames();
392            setPersistentAttributes((String[]) names.toArray(new String[names.size()]));
393        }
394    
395        public void setConstructor(String[] names) {
396            constructor = new GConstructorInfo(names);
397            setPersistentAttributes(names);
398        }
399    
400        public void addOperation(GOperationInfo operationInfo) {
401            operations.put(new GOperationSignature(operationInfo.getName(), operationInfo.getParameterList()), operationInfo);
402        }
403        
404        /**
405         * @deprecated
406         */
407        public void addOperation(String name) {
408            // FIXME : This is needed because the getters/setters are not being added as operation
409            // i.e. kerenl.invoke("getX") fails.
410            addOperation(new GOperationInfo(name, NO_ARGS, ""));
411        }
412    
413        /**
414         * @deprecated
415         */
416        public void addOperation(String name, Class[] paramTypes) {
417            //addOperation(new GOperationInfo(name, paramTypes, ""));
418        }
419       
420        public void addOperation(String name, String returnType) {
421            addOperation(new GOperationInfo(name, NO_ARGS, returnType));
422        }
423    
424        // This is redundant because these operations are added automatically; it can be made private
425        public void addOperation(String name, Class[] paramTypes, String returnType) {
426            addOperation(new GOperationInfo(name, paramTypes, returnType));
427        }
428    
429        public void addReference(GReferenceInfo info) {
430            references.put(info.getName(), new RefInfo(info.getReferenceType(), info.getNameTypeName()));
431        }
432    
433        /**
434         * Add a reference to another GBean or collection of GBeans
435         * @param name the name of the reference
436         * @param type The proxy type of the GBean or objects in a ReferenceCollection
437         * @param namingType the string expected as the type component of the name.  For jsr-77 names this is the j2eeType value
438         */
439        public void addReference(String name, Class type, String namingType) {
440            references.put(name, new RefInfo(type.getName(), namingType));
441        }
442    
443        public void addReference(String name, Class type) {
444            references.put(name, new RefInfo(type.getName(), null));
445        }
446    
447        public void setPriority(int priority) {
448            this.priority = priority;
449        }
450    
451        public GBeanInfo getBeanInfo() {
452            // get the types of the constructor args
453            // this also verifies that we have a valid constructor
454            Map constructorTypes = getConstructorTypes();
455    
456            // build the reference infos now that we know the constructor types
457            Set referenceInfos = new HashSet();
458            for (Iterator iterator = references.entrySet().iterator(); iterator.hasNext();) {
459                Map.Entry entry = (Map.Entry) iterator.next();
460                String referenceName = (String) entry.getKey();
461                RefInfo refInfo = (RefInfo) entry.getValue();
462                String referenceType = refInfo.getJavaType();
463                String namingType = refInfo.getNamingType();
464    
465                String proxyType = (String) constructorTypes.get(referenceName);
466                String setterName = null;
467                if (proxyType == null) {
468                    Method setter = searchForSetterMethod(referenceName, referenceType, gbeanType);
469                    if (setter == null) {
470                        setter = searchForSetterMethod(referenceName, Collection.class.getName(), gbeanType);
471                        if (setter == null) {
472                            throw new InvalidConfigurationException("Reference must be a constructor argument or have a setter: name=" + referenceName + " for gbeanType: " + gbeanType);
473                        }
474                    }
475                    proxyType = setter.getParameterTypes()[0].getName();
476    
477                    setterName = setter.getName();
478                }
479    
480                if (!proxyType.equals(Collection.class.getName()) && !proxyType.equals(referenceType)) {
481                    throw new InvalidConfigurationException("Reference proxy type must be Collection or " + referenceType + ": name=" + referenceName + " for gbeanType: " + gbeanType.getName());
482                }
483    
484                referenceInfos.add(new GReferenceInfo(referenceName, referenceType, proxyType, setterName, namingType));
485            }
486    
487            return new GBeanInfo(sourceClass, name, gbeanType.getName(), j2eeType, attributes.values(), constructor, operations.values(), referenceInfos, interfaces, priority);
488        }
489    
490        private Map getConstructorTypes() throws InvalidConfigurationException {
491            List arguments = constructor.getAttributeNames();
492            String[] argumentTypes = new String[arguments.size()];
493            boolean[] isReference = new boolean[arguments.size()];
494            for (int i = 0; i < argumentTypes.length; i++) {
495                String argumentName = (String) arguments.get(i);
496                if (references.containsKey(argumentName)) {
497                    argumentTypes[i] = ((RefInfo) references.get(argumentName)).getJavaType();
498                    isReference[i] = true;
499                } else if (attributes.containsKey(argumentName)) {
500                    GAttributeInfo attribute = (GAttributeInfo) attributes.get(argumentName);
501                    argumentTypes[i] = attribute.getType();
502                    isReference[i] = false;
503                }  
504            }
505    
506            Constructor[] constructors = gbeanType.getConstructors();
507            Set validConstructors = new HashSet();
508            for (int i = 0; i < constructors.length; i++) {
509                Constructor constructor = constructors[i];
510                if (isValidConstructor(constructor, argumentTypes, isReference)) {
511                    validConstructors.add(constructor);
512                }
513            }
514    
515            if (validConstructors.isEmpty()) {
516                throw new InvalidConfigurationException("Could not find a valid constructor for GBean: " + gbeanType.getName());
517            }
518            if (validConstructors.size() > 1) {
519                throw new InvalidConfigurationException("More then one valid constructors found for GBean: " + gbeanType.getName());
520            }
521    
522            Map constructorTypes = new HashMap();
523            Constructor constructor = (Constructor) validConstructors.iterator().next();
524            Class[] parameterTypes = constructor.getParameterTypes();
525            Iterator argumentIterator = arguments.iterator();
526            for (int i = 0; i < parameterTypes.length; i++) {
527                String parameterType = parameterTypes[i].getName();
528                String argumentName = (String) argumentIterator.next();
529                constructorTypes.put(argumentName, parameterType);
530            }
531            return constructorTypes;
532        }
533    
534        private static String searchForGetter(String name, String type, Class gbeanType) throws InvalidConfigurationException {
535            Method getterMethod = null;
536    
537            // no explicit name give so we must search for a name
538            String getterName = "get" + name;
539            String isName = "is" + name;
540            Method[] methods = gbeanType.getMethods();
541            for (int i = 0; i < methods.length; i++) {
542                if (methods[i].getParameterTypes().length == 0 && methods[i].getReturnType() != Void.TYPE
543                        && (getterName.equalsIgnoreCase(methods[i].getName()) || isName.equalsIgnoreCase(methods[i].getName()))) {
544    
545                    // found it
546                    getterMethod = methods[i];
547                    break;
548                }
549            }
550    
551            // if the return type of the getter doesn't match, throw an exception
552            if (getterMethod != null && !type.equals(getterMethod.getReturnType().getName())) {
553                throw new InvalidConfigurationException("Incorrect return type for getter method:" +
554                        " name=" + name +
555                        ", targetClass=" + gbeanType.getName() +
556                        ", getter type=" + getterMethod.getReturnType() +
557                        ", expected type=" + type);
558            }
559    
560            if (getterMethod == null) {
561                return null;
562            }
563            return getterMethod.getName();
564        }
565    
566        private static String searchForSetter(String name, String type, Class gbeanType) throws InvalidConfigurationException {
567            Method method = searchForSetterMethod(name, type, gbeanType);
568            if (method == null) {
569                return null;
570            }
571            return method.getName();
572        }
573    
574        private static Method searchForSetterMethod(String name, String type, Class gbeanType) throws InvalidConfigurationException {
575            // no explicit name give so we must search for a name
576            String setterName = "set" + name;
577            Method[] methods = gbeanType.getMethods();
578            for (int i = 0; i < methods.length; i++) {
579                Method method = methods[i];
580                if (method.getParameterTypes().length == 1 &&
581                        method.getParameterTypes()[0].getName().equals(type) &&
582                        method.getReturnType() == Void.TYPE &&
583                        setterName.equalsIgnoreCase(method.getName())) {
584    
585                    return method;
586                }
587            }
588    
589            // a setter is not necessary for this attribute
590            return null;
591        }
592    
593        private static boolean isValidConstructor(Constructor constructor, String[] argumentTypes, boolean[] isReference) {
594            Class[] parameterTypes = constructor.getParameterTypes();
595    
596            // same number of parameters?
597            if (parameterTypes.length != argumentTypes.length) {
598                return false;
599            }
600    
601            // is each parameter the correct type?
602            for (int i = 0; i < parameterTypes.length; i++) {
603                String parameterType = parameterTypes[i].getName();
604                if (isReference[i]) {
605                    // reference: does type match
606                    // OR is it a java.util.Collection
607                    // OR is it a java.util.Set?
608                    if (!parameterType.equals(argumentTypes[i]) &&
609                            !parameterType.equals(Collection.class.getName()) &&
610                            !parameterType.equals(Set.class.getName())) {
611                        return false;
612                    }
613                } else {
614                    // attribute: does type match?
615                    if (!parameterType.equals(argumentTypes[i])) {
616                        return false;
617                    }
618                }
619            }
620            return true;
621        }
622    
623        private String getAttributeName(Method method) {
624            String name = method.getName();
625            String attributeName = (name.startsWith("get") || name.startsWith("set")) ? name.substring(3) : name.substring(2);
626            attributeName = Introspector.decapitalize(attributeName);
627            return attributeName;
628        }
629    
630        private boolean isSetter(Method method) {
631            return method.getName().startsWith("set") && method.getParameterTypes().length == 1;
632        }
633    
634        private static boolean isGetter(Method method) {
635            String name = method.getName();
636            return (name.startsWith("get") || name.startsWith("is")) && method.getParameterTypes().length == 0;
637        }
638    
639        /**
640         * Checks whether or not the input argument is null; otherwise it throws
641         * {@link IllegalArgumentException}.
642         *
643         * @param clazz the input argument to validate
644         * @throws IllegalArgumentException if input is null
645         */
646        private static Class checkNotNull(final Class clazz) {
647            if (clazz == null) {
648                throw new IllegalArgumentException("null argument supplied");
649            }
650            return clazz;
651        }
652    
653        /**
654         * Checks whether or not the input argument is null; otherwise it throws
655         * {@link IllegalArgumentException}.
656         *
657         * @param string the input argument to validate
658         * @throws IllegalArgumentException if input is null
659         */
660        private static String checkNotNull(final String string) {
661            if (string == null) {
662                throw new IllegalArgumentException("null argument supplied");
663            }
664            return string;
665        }
666    
667        private static Class loadClass(ClassLoader classLoader, String name) {
668            try {
669                return classLoader.loadClass(name);
670            } catch (ClassNotFoundException e) {
671                throw new InvalidConfigurationException("Could not load class " + name, e);
672            }
673        }
674    
675        private static class RefInfo {
676            private final String javaType;
677            private final String namingType;
678    
679            public RefInfo(String javaType, String namingType) {
680                this.javaType = javaType;
681                this.namingType = namingType;
682            }
683    
684            public String getJavaType() {
685                return javaType;
686            }
687    
688            public String getNamingType() {
689                return namingType;
690            }
691        }
692    }