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: 487175 $ $Date: 2006-12-14 03:10:31 -0800 (Thu, 14 Dec 2006) $
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(),
191                            operationInfo.getParameterList()), operationInfo);
192                }
193    
194                for (Iterator iterator = source.getReferences().iterator(); iterator.hasNext();) {
195                    GReferenceInfo referenceInfo = (GReferenceInfo) iterator.next();
196                    references.put(referenceInfo.getName(), new RefInfo(referenceInfo.getReferenceType(), referenceInfo.getNameTypeName()));
197                }
198    
199                for (Iterator iterator = source.getInterfaces().iterator(); iterator.hasNext();) {
200                    String intf = (String) iterator.next();
201                    interfaces.add(intf);
202                }
203    
204                //in case subclass constructor has same parameters as superclass.
205                constructor = source.getConstructor();
206    
207                priority = source.getPriority();
208            }
209            if (j2eeType != null) {
210                this.j2eeType = j2eeType;
211            } else if (source != null) {
212                this.j2eeType = source.getJ2eeType();
213            } else {
214                this.j2eeType = DEFAULT_J2EE_TYPE; //NameFactory.GERONIMO_SERVICE
215            }
216    
217            // add all interfaces based on GBean type
218            if (gbeanType.isArray()) {
219                throw new IllegalArgumentException("GBean is an array type: gbeanType=" + gbeanType.getName());
220            }
221            Set allTypes = ClassLoading.getAllTypes(gbeanType);
222            for (Iterator iterator = allTypes.iterator(); iterator.hasNext();) {
223                Class type = (Class) iterator.next();
224                addInterface(type);
225            }
226        }
227    
228        public void setPersistentAttributes(String[] persistentAttributes) {
229            for (int i = 0; i < persistentAttributes.length; i++) {
230                String attributeName = persistentAttributes[i];
231                GAttributeInfo attribute = (GAttributeInfo) attributes.get(attributeName);
232                if (attribute != null && !references.containsKey(attributeName)) {
233                    if (isMagicAttribute(attribute)) {
234                        // magic attributes can't be persistent
235                        continue;
236                    }
237                    attributes.put(attributeName,
238                            new GAttributeInfo(attributeName,
239                                    attribute.getType(),
240                                    true,
241                                    attribute.isManageable(),
242                                    attribute.getGetterName(),
243                                    attribute.getSetterName()));
244                } else {
245                    if (attributeName.equals("kernel")) {
246                        addAttribute("kernel", Kernel.class, false);
247                    } else if (attributeName.equals("classLoader")) {
248                        addAttribute("classLoader", ClassLoader.class, false);
249                    } else if (attributeName.equals("abstractName")) {
250                        addAttribute("abstractName", AbstractName.class, false);
251                    } else if (attributeName.equals("objectName")) {
252                        addAttribute("obectName", String.class, false);
253                    }
254                }
255            }
256        }
257    
258        public void setManageableAttributes(String[] manageableAttributes) {
259            for (int i = 0; i < manageableAttributes.length; i++) {
260                String attributeName = manageableAttributes[i];
261                GAttributeInfo attribute = (GAttributeInfo) attributes.get(attributeName);
262                if (attribute != null) {
263                    attributes.put(attributeName,
264                            new GAttributeInfo(attributeName,
265                                    attribute.getType(),
266                                    attribute.isPersistent(),
267                                    true,
268                                    attribute.getGetterName(),
269                                    attribute.getSetterName()));
270                }
271            }
272        }
273    
274        private boolean isMagicAttribute(GAttributeInfo attributeInfo) {
275            String name = attributeInfo.getName();
276            String type = attributeInfo.getType();
277            return ("kernel".equals(name) && Kernel.class.getName().equals(type)) ||
278                    ("classLoader".equals(name) && ClassLoader.class.getName().equals(type)) ||
279                    ("abstractName".equals(name) && AbstractName.class.getName().equals(type)) ||
280                    ("objectName".equals(name) && String.class.getName().equals(type));
281        }
282    
283        public void addInterface(Class intf) {
284            addInterface(intf, new String[0]);
285        }
286    
287        //do not use beaninfo Introspector to list the properties.  This method is primarily for interfaces,
288        //and it does not process superinterfaces.  It seems to really only work well for classes.
289        public void addInterface(Class intf, String[] persistentAttributes) {
290            addInterface(intf, persistentAttributes, new String[0]);
291        }
292    
293        public void addInterface(Class intf, String[] persistentAttributes, String[] manageableAttributes) {
294            Set persistentNames = new HashSet(Arrays.asList(persistentAttributes));
295            Set manageableNames = new HashSet(Arrays.asList(manageableAttributes));
296            Method[] methods = intf.getMethods();
297            for (int i = 0; i < methods.length; i++) {
298                Method method = methods[i];
299                if ("java.lang.Object".equals(method.getDeclaringClass().getName())) continue;
300                if (isGetter(method)) {
301                    String attributeName = getAttributeName(method);
302                    GAttributeInfo attribute = (GAttributeInfo) attributes.get(attributeName);
303                    String attributeType = method.getReturnType().getName();
304                    if (attribute == null) {
305                        attributes.put(attributeName,
306                                new GAttributeInfo(attributeName,
307                                        attributeType,
308                                        persistentNames.contains(attributeName),
309                                        manageableNames.contains(attributeName),
310                                        method.getName(),
311                                        null));
312                    } else {
313                        if (!attributeType.equals(attribute.getType())) {
314                            throw new IllegalArgumentException("Getter and setter type do not match: " + attributeName + " for gbeanType: " + gbeanType.getName());
315                        }
316                        attributes.put(attributeName,
317                                new GAttributeInfo(attributeName,
318                                        attributeType,
319                                        attribute.isPersistent() || persistentNames.contains(attributeName),
320                                        attribute.isManageable() || manageableNames.contains(attributeName),
321                                        method.getName(),
322                                        attribute.getSetterName()));
323                    }
324                } else if (isSetter(method)) {
325                    String attributeName = getAttributeName(method);
326                    String attributeType = method.getParameterTypes()[0].getName();
327                    GAttributeInfo attribute = (GAttributeInfo) attributes.get(attributeName);
328                    if (attribute == null) {
329                        attributes.put(attributeName,
330                                new GAttributeInfo(attributeName,
331                                        attributeType,
332                                        persistentNames.contains(attributeName),
333                                        manageableNames.contains(attributeName),
334                                        null,
335                                        method.getName()));
336                    } else {
337                        if (!attributeType.equals(attribute.getType())) {
338                            throw new IllegalArgumentException("Getter and setter type do not match: " + attributeName + " for gbeanType: " + gbeanType.getName());
339                        }
340                        attributes.put(attributeName,
341                                new GAttributeInfo(attributeName,
342                                        attributeType,
343                                        attribute.isPersistent() || persistentNames.contains(attributeName),
344                                        attribute.isManageable() || manageableNames.contains(attributeName),
345                                        attribute.getGetterName(),
346                                        method.getName()));
347                    }
348                } else {
349                    addOperation(new GOperationInfo(method.getName(), method.getParameterTypes()));
350                }
351            }
352            addInterface(interfaces, intf);
353        }
354    
355        private static void addInterface(Set set, Class intf) {
356            String name = intf.getName();
357            if(set.contains(name)) {
358                return;
359            }
360            set.add(name);
361            Class cls[] = intf.getInterfaces();
362            for (int i = 0; i < cls.length; i++) {
363                addInterface(set, cls[i]);
364            }
365        }
366    
367        public void addAttribute(String name, Class type, boolean persistent) {
368            addAttribute(name, type.getName(), persistent, true);
369        }
370    
371        public void addAttribute(String name, String type, boolean persistent) {
372            addAttribute(name, type, persistent, true);
373        }
374    
375        public void addAttribute(String name, Class type, boolean persistent, boolean manageable) {
376            addAttribute(name, type.getName(), persistent, manageable);
377        }
378    
379        public void addAttribute(String name, String type, boolean persistent, boolean manageable) {
380            String getter = searchForGetter(name, type, gbeanType);
381            String setter = searchForSetter(name, type, gbeanType);
382            addAttribute(new GAttributeInfo(name, type, persistent, manageable, getter, setter));
383        }
384    
385        public void addAttribute(GAttributeInfo info) {
386            attributes.put(info.getName(), info);
387        }
388    
389        public void setConstructor(GConstructorInfo constructor) {
390            assert constructor != null;
391            this.constructor = constructor;
392            List names = constructor.getAttributeNames();
393            setPersistentAttributes((String[]) names.toArray(new String[names.size()]));
394        }
395    
396        public void setConstructor(String[] names) {
397            constructor = new GConstructorInfo(names);
398            setPersistentAttributes(names);
399        }
400    
401        public void addOperation(GOperationInfo operationInfo) {
402            operations.put(new GOperationSignature(operationInfo.getName(), operationInfo.getParameterList()), operationInfo);
403        }
404    
405        public void addOperation(String name) {
406            addOperation(new GOperationInfo(name, NO_ARGS));
407        }
408    
409        public void addOperation(String name, Class[] paramTypes) {
410            addOperation(new GOperationInfo(name, paramTypes));
411        }
412    
413        public void addReference(GReferenceInfo info) {
414            references.put(info.getName(), new RefInfo(info.getReferenceType(), info.getNameTypeName()));
415        }
416    
417        /**
418         * Add a reference to another GBean or collection of GBeans
419         * @param name the name of the reference
420         * @param type The proxy type of the GBean or objects in a ReferenceCollection
421         * @param namingType the string expected as the type component of the name.  For jsr-77 names this is the j2eeType value
422         */
423        public void addReference(String name, Class type, String namingType) {
424            references.put(name, new RefInfo(type.getName(), namingType));
425        }
426    
427        public void addReference(String name, Class type) {
428            references.put(name, new RefInfo(type.getName(), null));
429        }
430    
431        public void setPriority(int priority) {
432            this.priority = priority;
433        }
434    
435        public GBeanInfo getBeanInfo() {
436            // get the types of the constructor args
437            // this also verifies that we have a valid constructor
438            Map constructorTypes = getConstructorTypes();
439    
440            // build the reference infos now that we know the constructor types
441            Set referenceInfos = new HashSet();
442            for (Iterator iterator = references.entrySet().iterator(); iterator.hasNext();) {
443                Map.Entry entry = (Map.Entry) iterator.next();
444                String referenceName = (String) entry.getKey();
445                RefInfo refInfo = (RefInfo) entry.getValue();
446                String referenceType = refInfo.getJavaType();
447                String namingType = refInfo.getNamingType();
448    
449                String proxyType = (String) constructorTypes.get(referenceName);
450                String setterName = null;
451                if (proxyType == null) {
452                    Method setter = searchForSetterMethod(referenceName, referenceType, gbeanType);
453                    if (setter == null) {
454                        setter = searchForSetterMethod(referenceName, Collection.class.getName(), gbeanType);
455                        if (setter == null) {
456                            throw new InvalidConfigurationException("Reference must be a constructor argument or have a setter: name=" + referenceName + " for gbeanType: " + gbeanType);
457                        }
458                    }
459                    proxyType = setter.getParameterTypes()[0].getName();
460    
461                    setterName = setter.getName();
462                }
463    
464                if (!proxyType.equals(Collection.class.getName()) && !proxyType.equals(referenceType)) {
465                    throw new InvalidConfigurationException("Reference proxy type must be Collection or " + referenceType + ": name=" + referenceName + " for gbeanType: " + gbeanType.getName());
466                }
467    
468                referenceInfos.add(new GReferenceInfo(referenceName, referenceType, proxyType, setterName, namingType));
469            }
470    
471            return new GBeanInfo(sourceClass, name, gbeanType.getName(), j2eeType, attributes.values(), constructor, operations.values(), referenceInfos, interfaces, priority);
472        }
473    
474        private Map getConstructorTypes() throws InvalidConfigurationException {
475            List arguments = constructor.getAttributeNames();
476            String[] argumentTypes = new String[arguments.size()];
477            boolean[] isReference = new boolean[arguments.size()];
478            for (int i = 0; i < argumentTypes.length; i++) {
479                String argumentName = (String) arguments.get(i);
480                if (references.containsKey(argumentName)) {
481                    argumentTypes[i] = ((RefInfo) references.get(argumentName)).getJavaType();
482                    isReference[i] = true;
483                } else if (attributes.containsKey(argumentName)) {
484                    GAttributeInfo attribute = (GAttributeInfo) attributes.get(argumentName);
485                    argumentTypes[i] = attribute.getType();
486                    isReference[i] = false;
487                }  
488            }
489    
490            Constructor[] constructors = gbeanType.getConstructors();
491            Set validConstructors = new HashSet();
492            for (int i = 0; i < constructors.length; i++) {
493                Constructor constructor = constructors[i];
494                if (isValidConstructor(constructor, argumentTypes, isReference)) {
495                    validConstructors.add(constructor);
496                }
497            }
498    
499            if (validConstructors.isEmpty()) {
500                throw new InvalidConfigurationException("Could not find a valid constructor for GBean: " + gbeanType.getName());
501            }
502            if (validConstructors.size() > 1) {
503                throw new InvalidConfigurationException("More then one valid constructors found for GBean: " + gbeanType.getName());
504            }
505    
506            Map constructorTypes = new HashMap();
507            Constructor constructor = (Constructor) validConstructors.iterator().next();
508            Class[] parameterTypes = constructor.getParameterTypes();
509            Iterator argumentIterator = arguments.iterator();
510            for (int i = 0; i < parameterTypes.length; i++) {
511                String parameterType = parameterTypes[i].getName();
512                String argumentName = (String) argumentIterator.next();
513                constructorTypes.put(argumentName, parameterType);
514            }
515            return constructorTypes;
516        }
517    
518        private static String searchForGetter(String name, String type, Class gbeanType) throws InvalidConfigurationException {
519            Method getterMethod = null;
520    
521            // no explicit name give so we must search for a name
522            String getterName = "get" + name;
523            String isName = "is" + name;
524            Method[] methods = gbeanType.getMethods();
525            for (int i = 0; i < methods.length; i++) {
526                if (methods[i].getParameterTypes().length == 0 && methods[i].getReturnType() != Void.TYPE
527                        && (getterName.equalsIgnoreCase(methods[i].getName()) || isName.equalsIgnoreCase(methods[i].getName()))) {
528    
529                    // found it
530                    getterMethod = methods[i];
531                    break;
532                }
533            }
534    
535            // if the return type of the getter doesn't match, throw an exception
536            if (getterMethod != null && !type.equals(getterMethod.getReturnType().getName())) {
537                throw new InvalidConfigurationException("Incorrect return type for getter method:" +
538                        " name=" + name +
539                        ", targetClass=" + gbeanType.getName() +
540                        ", getter type=" + getterMethod.getReturnType() +
541                        ", expected type=" + type);
542            }
543    
544            if (getterMethod == null) {
545                return null;
546            }
547            return getterMethod.getName();
548        }
549    
550        private static String searchForSetter(String name, String type, Class gbeanType) throws InvalidConfigurationException {
551            Method method = searchForSetterMethod(name, type, gbeanType);
552            if (method == null) {
553                return null;
554            }
555            return method.getName();
556        }
557    
558        private static Method searchForSetterMethod(String name, String type, Class gbeanType) throws InvalidConfigurationException {
559            // no explicit name give so we must search for a name
560            String setterName = "set" + name;
561            Method[] methods = gbeanType.getMethods();
562            for (int i = 0; i < methods.length; i++) {
563                Method method = methods[i];
564                if (method.getParameterTypes().length == 1 &&
565                        method.getParameterTypes()[0].getName().equals(type) &&
566                        method.getReturnType() == Void.TYPE &&
567                        setterName.equalsIgnoreCase(method.getName())) {
568    
569                    return method;
570                }
571            }
572    
573            // a setter is not necessary for this attribute
574            return null;
575        }
576    
577        private static boolean isValidConstructor(Constructor constructor, String[] argumentTypes, boolean[] isReference) {
578            Class[] parameterTypes = constructor.getParameterTypes();
579    
580            // same number of parameters?
581            if (parameterTypes.length != argumentTypes.length) {
582                return false;
583            }
584    
585            // is each parameter the correct type?
586            for (int i = 0; i < parameterTypes.length; i++) {
587                String parameterType = parameterTypes[i].getName();
588                if (isReference[i]) {
589                    // reference: does type match
590                    // OR is it a java.util.Collection
591                    // OR is it a java.util.Set?
592                    if (!parameterType.equals(argumentTypes[i]) &&
593                            !parameterType.equals(Collection.class.getName()) &&
594                            !parameterType.equals(Set.class.getName())) {
595                        return false;
596                    }
597                } else {
598                    // attribute: does type match?
599                    if (!parameterType.equals(argumentTypes[i])) {
600                        return false;
601                    }
602                }
603            }
604            return true;
605        }
606    
607        private String getAttributeName(Method method) {
608            String name = method.getName();
609            String attributeName = (name.startsWith("get") || name.startsWith("set")) ? name.substring(3) : name.substring(2);
610            attributeName = Introspector.decapitalize(attributeName);
611            return attributeName;
612        }
613    
614        private boolean isSetter(Method method) {
615            return method.getName().startsWith("set") && method.getParameterTypes().length == 1;
616        }
617    
618        private static boolean isGetter(Method method) {
619            String name = method.getName();
620            return (name.startsWith("get") || name.startsWith("is")) && method.getParameterTypes().length == 0;
621        }
622    
623        /**
624         * Checks whether or not the input argument is null; otherwise it throws
625         * {@link IllegalArgumentException}.
626         *
627         * @param clazz the input argument to validate
628         * @throws IllegalArgumentException if input is null
629         */
630        private static Class checkNotNull(final Class clazz) {
631            if (clazz == null) {
632                throw new IllegalArgumentException("null argument supplied");
633            }
634            return clazz;
635        }
636    
637        /**
638         * Checks whether or not the input argument is null; otherwise it throws
639         * {@link IllegalArgumentException}.
640         *
641         * @param string the input argument to validate
642         * @throws IllegalArgumentException if input is null
643         */
644        private static String checkNotNull(final String string) {
645            if (string == null) {
646                throw new IllegalArgumentException("null argument supplied");
647            }
648            return string;
649        }
650    
651        private static Class loadClass(ClassLoader classLoader, String name) {
652            try {
653                return classLoader.loadClass(name);
654            } catch (ClassNotFoundException e) {
655                throw new InvalidConfigurationException("Could not load class " + name, e);
656            }
657        }
658    
659        private static class RefInfo {
660            private final String javaType;
661            private final String namingType;
662    
663            public RefInfo(String javaType, String namingType) {
664                this.javaType = javaType;
665                this.namingType = namingType;
666            }
667    
668            public String getJavaType() {
669                return javaType;
670            }
671    
672            public String getNamingType() {
673                return namingType;
674            }
675        }
676    }