KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > GBeanInfoBuilder


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.gbean;
18
19 import java.beans.Introspector JavaDoc;
20 import java.lang.reflect.Constructor JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Arrays JavaDoc;
30
31 import org.apache.geronimo.kernel.ClassLoading;
32 import org.apache.geronimo.kernel.Kernel;
33
34 /**
35  * @version $Rev: 485321 $ $Date: 2006-12-10 19:14:46 -0500 (Sun, 10 Dec 2006) $
36  */

37 public class GBeanInfoBuilder {
38     public static GBeanInfoBuilder createStatic(Class JavaDoc gbeanType) {
39         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
40         return createStatic(gbeanType, gbeanType.getName(), gbeanType, null, null);
41     }
42
43     public static GBeanInfoBuilder createStatic(Class JavaDoc gbeanType, String JavaDoc j2eeType) {
44         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
45         return createStatic(gbeanType, gbeanType.getName(), gbeanType, null, j2eeType);
46     }
47
48     public static GBeanInfoBuilder createStatic(String JavaDoc name, Class JavaDoc gbeanType) {
49         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
50         return createStatic(gbeanType, name, gbeanType, null, null);
51     }
52
53     public static GBeanInfoBuilder createStatic(String JavaDoc name, Class JavaDoc gbeanType, String JavaDoc j2eeType) {
54         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
55         return createStatic(gbeanType, name, gbeanType, null, j2eeType);
56     }
57
58     public static GBeanInfoBuilder createStatic(Class JavaDoc gbeanType, GBeanInfo source) {
59         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
60         return createStatic(gbeanType, gbeanType.getName(), gbeanType, source, null);
61     }
62
63     public static GBeanInfoBuilder createStatic(Class JavaDoc gbeanType, GBeanInfo source, String JavaDoc j2eeType) {
64         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
65         return createStatic(gbeanType, gbeanType.getName(), gbeanType, source, j2eeType);
66     }
67
68     public static GBeanInfoBuilder createStatic(String JavaDoc name, Class JavaDoc gbeanType, GBeanInfo source) {
69         if (name == null) throw new NullPointerException JavaDoc("name is null");
70         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
71         return createStatic(gbeanType, name, gbeanType, source, null);
72     }
73
74     //
75
// These methods are used by classes that declare a GBeanInfo for another class
76
//
77
public static GBeanInfoBuilder createStatic(Class JavaDoc sourceClass, Class JavaDoc gbeanType) {
78         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
79         return createStatic(sourceClass, gbeanType.getName(), gbeanType, null, null);
80     }
81
82     public static GBeanInfoBuilder createStatic(Class JavaDoc sourceClass, Class JavaDoc gbeanType, String JavaDoc j2eeType) {
83         if (sourceClass == null) throw new NullPointerException JavaDoc("sourceClass is null");
84         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
85         return createStatic(sourceClass, gbeanType.getName(), gbeanType, null, j2eeType);
86     }
87
88     public static GBeanInfoBuilder createStatic(Class JavaDoc sourceClass, Class JavaDoc gbeanType, GBeanInfo source, String JavaDoc j2eeType) {
89         if (sourceClass == null) throw new NullPointerException JavaDoc("sourceClass is null");
90         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
91         return createStatic(sourceClass, gbeanType.getName(), gbeanType, source, j2eeType);
92     }
93
94     public static GBeanInfoBuilder createStatic(Class JavaDoc sourceClass, String JavaDoc name, Class JavaDoc gbeanType, String JavaDoc j2eeType) {
95         if (sourceClass == null) throw new NullPointerException JavaDoc("sourceClass is null");
96         if (name == null) throw new NullPointerException JavaDoc("name is null");
97         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
98         return createStatic(sourceClass, name, gbeanType, null, j2eeType);
99     }
100
101     public static GBeanInfoBuilder createStatic(Class JavaDoc sourceClass, String JavaDoc name, Class JavaDoc gbeanType, GBeanInfo source, String JavaDoc j2eeType) {
102         if (sourceClass == null) throw new NullPointerException JavaDoc("sourceClass is null");
103         if (name == null) throw new NullPointerException JavaDoc("name is null");
104         if (gbeanType == null) throw new NullPointerException JavaDoc("gbeanType is null");
105         return new GBeanInfoBuilder(sourceClass.getName(), name, gbeanType, source, j2eeType);
106     }
107
108     public static final String JavaDoc DEFAULT_J2EE_TYPE = "GBean"; //NameFactory.GERONIMO_SERVICE
109

110     private static final Class JavaDoc[] NO_ARGS = {};
111
112     /**
113      * The class from which the info can be retrieved using GBeanInfo.getGBeanInfo(className, classLoader)
114      */

115     private final String JavaDoc sourceClass;
116
117     private final String JavaDoc name;
118
119     private final String JavaDoc j2eeType;
120
121     private final Class JavaDoc gbeanType;
122
123     private final Map JavaDoc attributes = new HashMap JavaDoc();
124
125     private GConstructorInfo constructor = new GConstructorInfo();
126
127     private final Map JavaDoc operations = new HashMap JavaDoc();
128
129     private final Map JavaDoc references = new HashMap JavaDoc();
130
131     private final Set JavaDoc interfaces = new HashSet JavaDoc();
132
133     private int priority = GBeanInfo.PRIORITY_NORMAL;
134
135     public GBeanInfoBuilder(Class JavaDoc gbeanType) {
136         this(checkNotNull(gbeanType).getName(), gbeanType, null, null);
137     }
138
139     public GBeanInfoBuilder(Class JavaDoc gbeanType, String JavaDoc j2eeType) {
140         this(checkNotNull(gbeanType).getName(), gbeanType, null, j2eeType);
141     }
142
143     public GBeanInfoBuilder(String JavaDoc name, Class JavaDoc gbeanType) {
144         this(name, checkNotNull(gbeanType), null, null);
145     }
146
147     public GBeanInfoBuilder(String JavaDoc name, Class JavaDoc gbeanType, String JavaDoc j2eeType) {
148         this(name, checkNotNull(gbeanType), null, j2eeType);
149     }
150
151     public GBeanInfoBuilder(Class JavaDoc gbeanType, GBeanInfo source) {
152         this(checkNotNull(gbeanType).getName(), gbeanType, source);
153     }
154
155     public GBeanInfoBuilder(Class JavaDoc gbeanType, GBeanInfo source, String JavaDoc 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 JavaDoc name, ClassLoader JavaDoc classLoader) {
164         this(checkNotNull(name), loadClass(classLoader, name), GBeanInfo.getGBeanInfo(name, classLoader));
165     }
166
167     public GBeanInfoBuilder(String JavaDoc name, Class JavaDoc gbeanType, GBeanInfo source) {
168         this(name, gbeanType, source, null);
169     }
170
171     public GBeanInfoBuilder(String JavaDoc name, Class JavaDoc gbeanType, GBeanInfo source, String JavaDoc j2eeType) {
172         this(null, name, gbeanType, source, j2eeType);
173     }
174
175     private GBeanInfoBuilder(String JavaDoc sourceClass, String JavaDoc name, Class JavaDoc gbeanType, GBeanInfo source, String JavaDoc 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 JavaDoc i = source.getAttributes().iterator(); i.hasNext();) {
184                 GAttributeInfo attributeInfo = (GAttributeInfo) i.next();
185                 attributes.put(attributeInfo.getName(), attributeInfo);
186             }
187
188             for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc iterator = source.getInterfaces().iterator(); iterator.hasNext();) {
199                 String JavaDoc intf = (String JavaDoc) 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 JavaDoc("GBean is an array type: gbeanType=" + gbeanType.getName());
219         }
220         Set JavaDoc allTypes = ClassLoading.getAllTypes(gbeanType);
221         for (Iterator JavaDoc iterator = allTypes.iterator(); iterator.hasNext();) {
222             Class JavaDoc type = (Class JavaDoc) iterator.next();
223             addInterface(type);
224         }
225     }
226
227     public void setPersistentAttributes(String JavaDoc[] persistentAttributes) {
228         for (int i = 0; i < persistentAttributes.length; i++) {
229             String JavaDoc 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 JavaDoc.class, false);
248                 } else if (attributeName.equals("abstractName")) {
249                     addAttribute("abstractName", AbstractName.class, false);
250                 } else if (attributeName.equals("objectName")) {
251                     addAttribute("obectName", String JavaDoc.class, false);
252                 }
253             }
254         }
255     }
256
257     public void setManageableAttributes(String JavaDoc[] manageableAttributes) {
258         for (int i = 0; i < manageableAttributes.length; i++) {
259             String JavaDoc 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 JavaDoc name = attributeInfo.getName();
275         String JavaDoc type = attributeInfo.getType();
276         return ("kernel".equals(name) && Kernel.class.getName().equals(type)) ||
277                 ("classLoader".equals(name) && ClassLoader JavaDoc.class.getName().equals(type)) ||
278                 ("abstractName".equals(name) && AbstractName.class.getName().equals(type)) ||
279                 ("objectName".equals(name) && String JavaDoc.class.getName().equals(type));
280     }
281
282     public void addInterface(Class JavaDoc intf) {
283         addInterface(intf, new String JavaDoc[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 JavaDoc intf, String JavaDoc[] persistentAttributes) {
289         addInterface(intf, persistentAttributes, new String JavaDoc[0]);
290     }
291
292     public void addInterface(Class JavaDoc intf, String JavaDoc[] persistentAttributes, String JavaDoc[] manageableAttributes) {
293         Set JavaDoc persistentNames = new HashSet JavaDoc(Arrays.asList(persistentAttributes));
294         Set JavaDoc manageableNames = new HashSet JavaDoc(Arrays.asList(manageableAttributes));
295         Method JavaDoc[] methods = intf.getMethods();
296         for (int i = 0; i < methods.length; i++) {
297             Method JavaDoc method = methods[i];
298             if ("java.lang.Object".equals(method.getDeclaringClass().getName())) continue;
299             if (isGetter(method)) {
300                 String JavaDoc attributeName = getAttributeName(method);
301                 GAttributeInfo attribute = (GAttributeInfo) attributes.get(attributeName);
302                 String JavaDoc 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 JavaDoc("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 JavaDoc attributeName = getAttributeName(method);
325                 String JavaDoc 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 JavaDoc("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 JavaDoc set, Class JavaDoc intf) {
355         String JavaDoc name = intf.getName();
356         if(set.contains(name)) {
357             return;
358         }
359         set.add(name);
360         Class JavaDoc 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 JavaDoc name, Class JavaDoc type, boolean persistent) {
367         addAttribute(name, type.getName(), persistent, true);
368     }
369
370     public void addAttribute(String JavaDoc name, String JavaDoc type, boolean persistent) {
371         addAttribute(name, type, persistent, true);
372     }
373
374     public void addAttribute(String JavaDoc name, Class JavaDoc type, boolean persistent, boolean manageable) {
375         addAttribute(name, type.getName(), persistent, manageable);
376     }
377
378     public void addAttribute(String JavaDoc name, String JavaDoc type, boolean persistent, boolean manageable) {
379         String JavaDoc getter = searchForGetter(name, type, gbeanType);
380         String JavaDoc 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 JavaDoc names = constructor.getAttributeNames();
392         setPersistentAttributes((String JavaDoc[]) names.toArray(new String JavaDoc[names.size()]));
393     }
394
395     public void setConstructor(String JavaDoc[] 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 JavaDoc name) {
408         addOperation(new GOperationInfo(name, NO_ARGS, ""));
409     }
410
411     /**
412      * @deprecated
413      */

414     public void addOperation(String JavaDoc name, Class JavaDoc[] paramTypes) {
415         addOperation(new GOperationInfo(name, paramTypes, ""));
416     }
417     
418     public void addOperation(String JavaDoc name, String JavaDoc returnType) {
419         addOperation(new GOperationInfo(name, NO_ARGS, returnType));
420     }
421
422     public void addOperation(String JavaDoc name, Class JavaDoc[] paramTypes, String JavaDoc returnType) {
423         addOperation(new GOperationInfo(name, paramTypes, returnType));
424     }
425
426     public void addReference(GReferenceInfo info) {
427         references.put(info.getName(), new RefInfo(info.getReferenceType(), info.getNameTypeName()));
428     }
429
430     /**
431      * Add a reference to another GBean or collection of GBeans
432      * @param name the name of the reference
433      * @param type The proxy type of the GBean or objects in a ReferenceCollection
434      * @param namingType the string expected as the type component of the name. For jsr-77 names this is the j2eeType value
435      */

436     public void addReference(String JavaDoc name, Class JavaDoc type, String JavaDoc namingType) {
437         references.put(name, new RefInfo(type.getName(), namingType));
438     }
439
440     public void addReference(String JavaDoc name, Class JavaDoc type) {
441         references.put(name, new RefInfo(type.getName(), null));
442     }
443
444     public void setPriority(int priority) {
445         this.priority = priority;
446     }
447
448     public GBeanInfo getBeanInfo() {
449         // get the types of the constructor args
450
// this also verifies that we have a valid constructor
451
Map JavaDoc constructorTypes = getConstructorTypes();
452
453         // build the reference infos now that we know the constructor types
454
Set JavaDoc referenceInfos = new HashSet JavaDoc();
455         for (Iterator JavaDoc iterator = references.entrySet().iterator(); iterator.hasNext();) {
456             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
457             String JavaDoc referenceName = (String JavaDoc) entry.getKey();
458             RefInfo refInfo = (RefInfo) entry.getValue();
459             String JavaDoc referenceType = refInfo.getJavaType();
460             String JavaDoc namingType = refInfo.getNamingType();
461
462             String JavaDoc proxyType = (String JavaDoc) constructorTypes.get(referenceName);
463             String JavaDoc setterName = null;
464             if (proxyType == null) {
465                 Method JavaDoc setter = searchForSetterMethod(referenceName, referenceType, gbeanType);
466                 if (setter == null) {
467                     setter = searchForSetterMethod(referenceName, Collection JavaDoc.class.getName(), gbeanType);
468                     if (setter == null) {
469                         throw new InvalidConfigurationException("Reference must be a constructor argument or have a setter: name=" + referenceName + " for gbeanType: " + gbeanType);
470                     }
471                 }
472                 proxyType = setter.getParameterTypes()[0].getName();
473
474                 setterName = setter.getName();
475             }
476
477             if (!proxyType.equals(Collection JavaDoc.class.getName()) && !proxyType.equals(referenceType)) {
478                 throw new InvalidConfigurationException("Reference proxy type must be Collection or " + referenceType + ": name=" + referenceName + " for gbeanType: " + gbeanType.getName());
479             }
480
481             referenceInfos.add(new GReferenceInfo(referenceName, referenceType, proxyType, setterName, namingType));
482         }
483
484         return new GBeanInfo(sourceClass, name, gbeanType.getName(), j2eeType, attributes.values(), constructor, operations.values(), referenceInfos, interfaces, priority);
485     }
486
487     private Map JavaDoc getConstructorTypes() throws InvalidConfigurationException {
488         List JavaDoc arguments = constructor.getAttributeNames();
489         String JavaDoc[] argumentTypes = new String JavaDoc[arguments.size()];
490         boolean[] isReference = new boolean[arguments.size()];
491         for (int i = 0; i < argumentTypes.length; i++) {
492             String JavaDoc argumentName = (String JavaDoc) arguments.get(i);
493             if (references.containsKey(argumentName)) {
494                 argumentTypes[i] = ((RefInfo) references.get(argumentName)).getJavaType();
495                 isReference[i] = true;
496             } else if (attributes.containsKey(argumentName)) {
497                 GAttributeInfo attribute = (GAttributeInfo) attributes.get(argumentName);
498                 argumentTypes[i] = attribute.getType();
499                 isReference[i] = false;
500             }
501         }
502
503         Constructor JavaDoc[] constructors = gbeanType.getConstructors();
504         Set JavaDoc validConstructors = new HashSet JavaDoc();
505         for (int i = 0; i < constructors.length; i++) {
506             Constructor JavaDoc constructor = constructors[i];
507             if (isValidConstructor(constructor, argumentTypes, isReference)) {
508                 validConstructors.add(constructor);
509             }
510         }
511
512         if (validConstructors.isEmpty()) {
513             throw new InvalidConfigurationException("Could not find a valid constructor for GBean: " + gbeanType.getName());
514         }
515         if (validConstructors.size() > 1) {
516             throw new InvalidConfigurationException("More then one valid constructors found for GBean: " + gbeanType.getName());
517         }
518
519         Map JavaDoc constructorTypes = new HashMap JavaDoc();
520         Constructor JavaDoc constructor = (Constructor JavaDoc) validConstructors.iterator().next();
521         Class JavaDoc[] parameterTypes = constructor.getParameterTypes();
522         Iterator JavaDoc argumentIterator = arguments.iterator();
523         for (int i = 0; i < parameterTypes.length; i++) {
524             String JavaDoc parameterType = parameterTypes[i].getName();
525             String JavaDoc argumentName = (String JavaDoc) argumentIterator.next();
526             constructorTypes.put(argumentName, parameterType);
527         }
528         return constructorTypes;
529     }
530
531     private static String JavaDoc searchForGetter(String JavaDoc name, String JavaDoc type, Class JavaDoc gbeanType) throws InvalidConfigurationException {
532         Method JavaDoc getterMethod = null;
533
534         // no explicit name give so we must search for a name
535
String JavaDoc getterName = "get" + name;
536         String JavaDoc isName = "is" + name;
537         Method JavaDoc[] methods = gbeanType.getMethods();
538         for (int i = 0; i < methods.length; i++) {
539             if (methods[i].getParameterTypes().length == 0 && methods[i].getReturnType() != Void.TYPE
540                     && (getterName.equalsIgnoreCase(methods[i].getName()) || isName.equalsIgnoreCase(methods[i].getName()))) {
541
542                 // found it
543
getterMethod = methods[i];
544                 break;
545             }
546         }
547
548         // if the return type of the getter doesn't match, throw an exception
549
if (getterMethod != null && !type.equals(getterMethod.getReturnType().getName())) {
550             throw new InvalidConfigurationException("Incorrect return type for getter method:" +
551                     " name=" + name +
552                     ", targetClass=" + gbeanType.getName() +
553                     ", getter type=" + getterMethod.getReturnType() +
554                     ", expected type=" + type);
555         }
556
557         if (getterMethod == null) {
558             return null;
559         }
560         return getterMethod.getName();
561     }
562
563     private static String JavaDoc searchForSetter(String JavaDoc name, String JavaDoc type, Class JavaDoc gbeanType) throws InvalidConfigurationException {
564         Method JavaDoc method = searchForSetterMethod(name, type, gbeanType);
565         if (method == null) {
566             return null;
567         }
568         return method.getName();
569     }
570
571     private static Method JavaDoc searchForSetterMethod(String JavaDoc name, String JavaDoc type, Class JavaDoc gbeanType) throws InvalidConfigurationException {
572         // no explicit name give so we must search for a name
573
String JavaDoc setterName = "set" + name;
574         Method JavaDoc[] methods = gbeanType.getMethods();
575         for (int i = 0; i < methods.length; i++) {
576             Method JavaDoc method = methods[i];
577             if (method.getParameterTypes().length == 1 &&
578                     method.getParameterTypes()[0].getName().equals(type) &&
579                     method.getReturnType() == Void.TYPE &&
580                     setterName.equalsIgnoreCase(method.getName())) {
581
582                 return method;
583             }
584         }
585
586         // a setter is not necessary for this attribute
587
return null;
588     }
589
590     private static boolean isValidConstructor(Constructor JavaDoc constructor, String JavaDoc[] argumentTypes, boolean[] isReference) {
591         Class JavaDoc[] parameterTypes = constructor.getParameterTypes();
592
593         // same number of parameters?
594
if (parameterTypes.length != argumentTypes.length) {
595             return false;
596         }
597
598         // is each parameter the correct type?
599
for (int i = 0; i < parameterTypes.length; i++) {
600             String JavaDoc parameterType = parameterTypes[i].getName();
601             if (isReference[i]) {
602                 // reference: does type match
603
// OR is it a java.util.Collection
604
// OR is it a java.util.Set?
605
if (!parameterType.equals(argumentTypes[i]) &&
606                         !parameterType.equals(Collection JavaDoc.class.getName()) &&
607                         !parameterType.equals(Set JavaDoc.class.getName())) {
608                     return false;
609                 }
610             } else {
611                 // attribute: does type match?
612
if (!parameterType.equals(argumentTypes[i])) {
613                     return false;
614                 }
615             }
616         }
617         return true;
618     }
619
620     private String JavaDoc getAttributeName(Method JavaDoc method) {
621         String JavaDoc name = method.getName();
622         String JavaDoc attributeName = (name.startsWith("get") || name.startsWith("set")) ? name.substring(3) : name.substring(2);
623         attributeName = Introspector.decapitalize(attributeName);
624         return attributeName;
625     }
626
627     private boolean isSetter(Method JavaDoc method) {
628         return method.getName().startsWith("set") && method.getParameterTypes().length == 1;
629     }
630
631     private static boolean isGetter(Method JavaDoc method) {
632         String JavaDoc name = method.getName();
633         return (name.startsWith("get") || name.startsWith("is")) && method.getParameterTypes().length == 0;
634     }
635
636     /**
637      * Checks whether or not the input argument is null; otherwise it throws
638      * {@link IllegalArgumentException}.
639      *
640      * @param clazz the input argument to validate
641      * @throws IllegalArgumentException if input is null
642      */

643     private static Class JavaDoc checkNotNull(final Class JavaDoc clazz) {
644         if (clazz == null) {
645             throw new IllegalArgumentException JavaDoc("null argument supplied");
646         }
647         return clazz;
648     }
649
650     /**
651      * Checks whether or not the input argument is null; otherwise it throws
652      * {@link IllegalArgumentException}.
653      *
654      * @param string the input argument to validate
655      * @throws IllegalArgumentException if input is null
656      */

657     private static String JavaDoc checkNotNull(final String JavaDoc string) {
658         if (string == null) {
659             throw new IllegalArgumentException JavaDoc("null argument supplied");
660         }
661         return string;
662     }
663
664     private static Class JavaDoc loadClass(ClassLoader JavaDoc classLoader, String JavaDoc name) {
665         try {
666             return classLoader.loadClass(name);
667         } catch (ClassNotFoundException JavaDoc e) {
668             throw new InvalidConfigurationException("Could not load class " + name, e);
669         }
670     }
671
672     private static class RefInfo {
673         private final String JavaDoc javaType;
674         private final String JavaDoc namingType;
675
676         public RefInfo(String JavaDoc javaType, String JavaDoc namingType) {
677             this.javaType = javaType;
678             this.namingType = namingType;
679         }
680
681         public String JavaDoc getJavaType() {
682             return javaType;
683         }
684
685         public String JavaDoc getNamingType() {
686             return namingType;
687         }
688     }
689 }
690
Popular Tags