KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > model > RuntimeModel


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * RuntimeModel.java
26  *
27  * Created on March 10, 2000, 11:05 AM
28  */

29
30 package com.sun.jdo.api.persistence.model;
31
32 import java.io.*;
33 import java.net.URL JavaDoc;
34 import java.util.*;
35 import java.lang.reflect.*;
36 import java.security.AccessController JavaDoc;
37 import java.security.PrivilegedAction JavaDoc;
38
39 import org.netbeans.modules.dbschema.SchemaElement;
40 import com.sun.jdo.api.persistence.model.mapping.MappingClassElement;
41 import com.sun.jdo.spi.persistence.utility.*;
42
43 /**
44  *
45  * @author raccah
46  * @version %I%
47  */

48 public class RuntimeModel extends Model
49 {
50     /** Extension of the class file, used to figure out the path for handling
51      * the mapping file.
52      */

53     private static final String JavaDoc CLASS_EXTENSION = "class"; // NOI18N
54

55     /** Constant which represents the prefix of the java package.
56      */

57     private static final String JavaDoc JAVA_PACKAGE = "java."; // NOI18N
58

59     /** Constant which represents the serializable interface.
60      */

61     private static final String JavaDoc SERIALIZABLE = "java.io.Serializable"; //NOI18N
62

63     /** Map of class loader used to find classes and mapping information. Keys
64      * are fully qualified class names.
65      */

66     private HashMap classLoaders = new HashMap();
67     
68     /** Creates a new RuntimeModel. This constructor should not be called
69      * directly; instead, the static instance accesible from the Model class
70      * should be used.
71      * @see Model#RUNTIME
72      */

73     protected RuntimeModel ()
74     {
75         super();
76     }
77
78     /** Determines if the specified className represents an interface type.
79      * @param className the fully qualified name of the class to be checked
80      * @return <code>true</code> if this class name represents an interface;
81      * <code>false</code> otherwise.
82      */

83     public boolean isInterface (String JavaDoc className)
84     {
85         Class JavaDoc classElement = (Class JavaDoc)getClass(className);
86     
87         return ((classElement != null) ? classElement.isInterface() : false);
88     }
89
90     /** Returns the input stream with the supplied resource name found with
91      * the supplied class name.
92      * NOTE, this implementation assumes the specified class loader is not null
93      * and needs not to be validated. Any validation is done by getMappingClass
94      * which is the only caller of this method.
95      * @param className the fully qualified name of the class which will
96      * be used as a base to find the resource
97      * @param classLoader the class loader used to find mapping information
98      * @param resourceName the name of the resource to be found
99      * @return the input stream for the specified resource, <code>null</code>
100      * if an error occurs or none exists
101      */

102     protected BufferedInputStream getInputStreamForResource (String JavaDoc className,
103         ClassLoader JavaDoc classLoader, String JavaDoc resourceName)
104     {
105         InputStream is = ((className != null) ?
106                           classLoader.getResourceAsStream(resourceName) : null);
107
108         BufferedInputStream rc = null;
109         if (is != null && !(is instanceof BufferedInputStream)) {
110             rc = new BufferedInputStream(is);
111         } else {
112             rc = (BufferedInputStream)is;
113         }
114         return rc;
115     }
116
117     /** Computes the class name (without package) for the supplied
118      * class name.
119      * @param className the fully qualified name of the class
120      * @return the class name (without package) for the supplied
121      * class name
122      */

123     private String JavaDoc getShortClassName (String JavaDoc className)
124     {
125          return JavaTypeHelper.getShortClassName(className);
126     }
127
128     /** Returns the name of the second to top (top excluding java.lang.Object)
129      * superclass for the given class name.
130      * @param className the fully qualified name of the class to be checked
131      * @return the top non-Object superclass for className,
132      * <code>className</code> if an error occurs or none exists
133      */

134     protected String JavaDoc findPenultimateSuperclass (String JavaDoc className)
135     {
136         Class JavaDoc classElement = (Class JavaDoc)getClass(className);
137         Class JavaDoc objectClass = java.lang.Object JavaDoc.class;
138         Class JavaDoc testClass = null;
139
140         if (classElement == null)
141             return className;
142
143         while ((testClass = classElement.getSuperclass()) != null)
144         {
145             if (testClass.equals(objectClass))
146                 break;
147
148             classElement = testClass;
149         }
150
151         return classElement.getName();
152     }
153
154     /** Returns the name of the superclass for the given class name.
155      * @param className the fully qualified name of the class to be checked
156      * @return the superclass for className, <code>null</code> if an error
157      * occurs or none exists
158      */

159     protected String JavaDoc getSuperclass (String JavaDoc className)
160     {
161         Class JavaDoc classElement = (Class JavaDoc)getClass(className);
162
163         if (classElement != null)
164             classElement = classElement.getSuperclass();
165
166         return ((classElement != null) ? classElement.getName() : null);
167     }
168
169     /** Returns the MappingClassElement created for the specified class name.
170      * This method looks up the class in the internal cache. If not present
171      * it loads the corresponding xml file containing the mapping information.
172      * @param className the fully qualified name of the mapping class
173      * @param classLoader the class loader used to find mapping information
174      * @return the MappingClassElement for className,
175      * <code>null</code> if an error occurs or none exists
176      * @see MappingClassElementImpl#forName
177      */

178     public MappingClassElement getMappingClass (String JavaDoc className,
179         ClassLoader JavaDoc classLoader)
180     {
181         MappingClassElement mappingClass = null;
182
183         // First check class loader. This has to be done before the super call!
184
// Method Model.getMappingClass will check the MappingClassElement cache
185
// and will find an entry in the case of a multiple class loader for the
186
// same class name. So we have to check the multiple class loader first.
187
classLoader = findClassLoader(className, classLoader);
188         mappingClass = super.getMappingClass(className, classLoader);
189         if ((mappingClass != null) && (classLoader != null))
190         {
191             // Lookup the SchemElement connected to mappingClass. This reads
192
// the .dbschema file using the specified classLoader and stores the
193
// SchemaElement in the SchemaElement cache. Any subsequent
194
// SchemaElement.forName or TableElement.forName lookups will use
195
// the cached version.
196
String JavaDoc databaseRoot = mappingClass.getDatabaseRoot();
197
198             // An unmapped mapping class is allowed in which case the
199
// databaseRoot will be null (never mapped) or empty
200
// (mapped once, unmapped now), but if the databaseRoot is
201
// not null or empty and we can't find the schema, throw a
202
// RuntimeException to notify the user that something is wrong.
203
if (!StringHelper.isEmpty(databaseRoot) &&
204                 (SchemaElement.forName(databaseRoot, classLoader) == null))
205             {
206                 throw new RuntimeException JavaDoc(I18NHelper.getMessage(
207                     getMessages(), "dbschema.not_found", // NOI18N
208
databaseRoot, className));
209             }
210         }
211     
212         return mappingClass;
213     }
214
215     /** Returns an unmodifiable copy of the ClassLoader cache.
216      * @return unmodifiable ClassLoader cache
217      */

218     public Map getClassLoaderCache ()
219     {
220         return Collections.unmodifiableMap(classLoaders);
221     }
222
223     /** Removes the classes cached with the specified class loader from all
224      * caches. The method iterates the ClassLoader cache to find classes
225      * cached with the specified class loader. These classes are removed
226      * from the ClassLoader cache, the cache of MappingClassElements and
227      * the set of classes known to be non PC. The associated SchemaElements
228      * are removed from the SchemaElement cache.
229      * @param classLoader used to determine the classes to be removed
230      */

231     public void removeResourcesFromCaches (ClassLoader JavaDoc classLoader)
232     {
233         Collection classNames = new HashSet();
234
235         synchronized(classLoaders)
236         {
237             for (Iterator i = classLoaders.entrySet().iterator(); i.hasNext();)
238             {
239                 Map.Entry next = (Map.Entry)i.next();
240
241                 // check the cached class loader
242
if (next.getValue() == classLoader)
243                 {
244                     // add className to the collection of classNames to be
245
// removed
246
classNames.add(next.getKey());
247                     // remove this entry from the classLoaders cache
248
i.remove();
249                 }
250             }
251         }
252
253         removeResourcesFromCaches(classNames);
254     }
255     
256     /** Creates a file with the given base file name and extension
257      * parallel to the supplied class (if it does not yet exist).
258      * @param className the fully qualified name of the class
259      * @param baseFileName the name of the base file
260      * @param extension the file extension
261      * @return the output stream for the specified resource, <code>null</code>
262      * if an error occurs or none exists
263      * @exception IOException if there is some error creating the file
264      */

265     protected BufferedOutputStream createFile (String JavaDoc className, String JavaDoc baseFileName,
266         String JavaDoc extension) throws IOException
267     {
268         char extensionCharacter = '.';
269         File file = getFile(className,
270             baseFileName + extensionCharacter + extension);
271
272         if (file == null)
273         {
274             Class JavaDoc classElement = (Class JavaDoc)getClass(className);
275
276             if (classElement != null)
277             {
278                 // need to find the path before the package name
279
String JavaDoc path = classElement.getResource(
280                     getShortClassName(className) + extensionCharacter +
281                     CLASS_EXTENSION).getFile();
282                 int index = path.lastIndexOf(extensionCharacter) + 1;
283                 
284                 file = new File(path.substring(0, index) + extension);
285                 file.createNewFile();
286             }
287         }
288         return ((file != null)
289                 ? (new BufferedOutputStream(new FileOutputStream(file)))
290                 : null);
291     }
292
293     /** Deletes the file with the given file name which is parallel
294      * to the supplied class.
295      * @param className the fully qualified name of the class
296      * @param fileName the name of the file
297      * @exception IOException if there is some error deleting the file
298      */

299     protected void deleteFile (String JavaDoc className, String JavaDoc fileName)
300         throws IOException
301     {
302         File file = getFile(className, fileName);
303
304         if ((file != null) && file.exists())
305             file.delete();
306     }
307
308     /** Returns a file with the given file name which is parallel to the
309      * supplied class.
310      * @param className the fully qualified name of the class
311      * @param fileName the name of the file
312      * @return the file object for the specified resource, <code>null</code>
313      * if an error occurs
314      * @exception IOException if there is some error getting the file
315      */

316     protected File getFile (String JavaDoc className, String JavaDoc fileName)
317         throws IOException
318     {
319         Class JavaDoc classElement = (Class JavaDoc)getClass(className);
320
321         if (classElement != null)
322         {
323             // need to find the path before the package name
324
URL JavaDoc path = classElement.getResource(fileName.substring(
325                 fileName.lastIndexOf(getShortClassName(className))));
326
327             return ((path != null) ? (new File(path.getFile())) : null);
328         }
329
330         return null;
331     }
332
333     /** Returns the class element with the specified className. The class is
334      * found using <code>Class.forName</code>.
335      * @param className the fully qualified name of the class to be checked
336      * @param classLoader the class loader used to find mapping information
337      * @return the class element for the specified className, <code>null</code>
338      * if an error occurs or none exists
339      */

340     public Object JavaDoc getClass (String JavaDoc className, ClassLoader JavaDoc classLoader)
341     {
342         if (className == null)
343             return null;
344         
345         try
346         {
347             classLoader = findClassLoader(className, classLoader);
348             return Class.forName(className, true, classLoader);
349         }
350         catch (ClassNotFoundException JavaDoc e)
351         {
352             return null;
353         }
354     }
355         
356     /**
357      * This method returns the class loader used to find mapping information
358      * for the specified className. If the classLoader argument is not null,
359      * the method updates the classLoaders cache and returns the specified
360      * classLoader. Otherwise it checks the cache for the specified className
361      * and returns this class loader. If there is no cached class loader it
362      * returns the current class loader.
363      * @param className the fully qualified name of the class to be checked
364      * @param classLoader the class loader used to find mapping information
365      * @return the class loader used to find mapping information for the
366      * specified className
367      * @exception IllegalArgumentException if there is class loader problem
368      */

369     protected ClassLoader JavaDoc findClassLoader (String JavaDoc className,
370         ClassLoader JavaDoc classLoader) throws IllegalArgumentException JavaDoc
371     {
372         ClassLoader JavaDoc cached = null;
373
374         if (className == null)
375             return null;
376         else if (className.startsWith(JAVA_PACKAGE) || isPrimitive(className))
377             // Use current class loader for java packages or primitive types
378
// - these classes cannot have the multiple class loader conflict
379
// - these classes should not show up in the classLoaders map
380
return getClass().getClassLoader();
381
382         synchronized (classLoaders)
383         {
384             cached = (ClassLoader JavaDoc)classLoaders.get(className);
385
386             if (classLoader == null)
387             {
388                 // Case 1: specified class loader is null =>
389
// return cached class loader if available, if not
390
// take current class loader
391
classLoader =
392                     (cached != null) ? cached : getClass().getClassLoader();
393             }
394             else if (cached == null)
395             {
396                 // Case 2: specified class loader is NOT null AND
397
// no class loader cached for the class name =>
398
// put specified class loader in cache
399
classLoaders.put(className, classLoader);
400             }
401             else if (classLoader != cached)
402             {
403                 // Case 3: specified class loader is NOT null AND
404
// cache contains class loader for this class name AND
405
// both class loaders are not identical =>
406
// pontential conflict
407
Class JavaDoc clazz = null;
408                 Class JavaDoc cachedClazz = null;
409
410                 try
411                 {
412                     String JavaDoc prop = ClassLoaderStrategy.getStrategy();
413
414                     // Load the class using specified and cached class loader.
415
// NOTE, do not change the order of the next two lines, the
416
// catch block relies on it!
417
clazz = Class.forName(className, true, classLoader);
418                     cachedClazz = Class.forName(className, true, cached);
419
420                     if (clazz.getClassLoader() == cachedClazz.getClassLoader())
421                     {
422                         // Case 3a: both class loaders are the same =>
423
// return it
424
return cached;
425                     }
426                     else if (ClassLoaderStrategy.MULTIPLE_CLASS_LOADERS_IGNORE.equals(prop))
427                     {
428                         // Case 3b: both class loaders are different and
429
// the system property is defined as ignore =>
430
// ignore the specified class loader and return
431
// the cached class loader
432
return cached;
433                     }
434                     else if (ClassLoaderStrategy.MULTIPLE_CLASS_LOADERS_RELOAD.equals(prop))
435                     {
436                         // Case 3c: both class loaders are different and
437
// the system property is defined as reload =>
438
// discard the cached class loader and replace it
439
// by the specified class loader
440
removeResourcesFromCaches(cachedClazz.getClassLoader());
441                         classLoaders.put(className, classLoader);
442                         return classLoader;
443                     }
444                     else
445                     {
446                         // Case 3d: both class loaders are different and
447
// the system property is defined as error or
448
// any other value =>
449
// throw exception
450
throw new IllegalArgumentException JavaDoc(I18NHelper.getMessage(
451                             getMessages(), "classloader.multiple", // NOI18N
452
className));
453                     }
454                 }
455                 catch (ClassNotFoundException JavaDoc ex)
456                 {
457                     // At least one of the class loader could not find the class.
458
// Update the classLoader map, if the specified class loader
459
// could find it, but the cached could not.
460
if ((clazz != null) && (cachedClazz == null))
461                         classLoaders.put(className, classLoader);
462                 }
463             }
464         }
465
466         return classLoader;
467     }
468
469     /** Determines if the specified class implements the specified interface.
470      * Note, class element is a model specific class representation as returned
471      * by a getClass call executed on the same model instance. This
472      * implementation expects the class element being a reflection instance.
473      * @param classElement the class element to be checked
474      * @param interfaceName the fully qualified name of the interface to
475      * be checked
476      * @return <code>true</code> if the class implements the interface;
477      * <code>false</code> otherwise.
478      * @see #getClass
479      */

480     public boolean implementsInterface (Object JavaDoc classElement,
481         String JavaDoc interfaceName)
482     {
483         Class JavaDoc interfaceClass = (Class JavaDoc)getClass(interfaceName);
484
485         if ((classElement == null) || !(classElement instanceof Class JavaDoc) ||
486             (interfaceClass == null))
487             return false;
488         
489         return interfaceClass.isAssignableFrom((Class JavaDoc)classElement);
490     }
491
492     /** Determines if the class with the specified name declares a constructor.
493      * @param className the name of the class to be checked
494      * @return <code>true</code> if the class declares a constructor;
495      * <code>false</code> otherwise.
496      * @see #getClass
497      */

498     public boolean hasConstructor (final String JavaDoc className)
499     {
500         final Class JavaDoc classElement = (Class JavaDoc)getClass(className);
501
502         if (classElement != null)
503         {
504             Boolean JavaDoc b = (Boolean JavaDoc)AccessController.doPrivileged(
505                 new PrivilegedAction JavaDoc()
506             {
507                 public Object JavaDoc run ()
508                 {
509                     return JavaTypeHelper.valueOf(((Class JavaDoc)classElement).
510                         getDeclaredConstructors().length != 0);
511                 }
512             });
513
514             return b.booleanValue();
515         }
516
517         return false;
518     }
519
520     /** Returns the constructor element for the specified argument types
521      * in the class with the specified name. Types are specified as type
522      * names for primitive type such as int, float or as fully qualified
523      * class names.
524      * @param className the name of the class which contains the constructor
525      * to be checked
526      * @param argTypeNames the fully qualified names of the argument types
527      * @return the constructor element
528      * @see #getClass
529      */

530     public Object JavaDoc getConstructor (final String JavaDoc className, String JavaDoc[] argTypeNames)
531     {
532         final Class JavaDoc classElement = (Class JavaDoc)getClass(className);
533
534         if (classElement != null)
535         {
536             final Class JavaDoc[] argTypes = getTypesForNames(argTypeNames);
537
538             return AccessController.doPrivileged(new PrivilegedAction JavaDoc()
539             {
540                 public Object JavaDoc run ()
541                 {
542                     try
543                     {
544                         return ((Class JavaDoc)classElement).getDeclaredConstructor(
545                             argTypes);
546                     }
547                     catch (NoSuchMethodException JavaDoc ex)
548                     {
549                         // constructor not found => return null
550
return null;
551                     }
552                 }
553             });
554         }
555
556         return null;
557     }
558
559     /** Returns the method element for the specified method name and argument
560      * types in the class with the specified name. Types are specified as
561      * type names for primitive type such as int, float or as fully qualified
562      * class names. Note, the method does not return inherited methods.
563      * @param className the name of the class which contains the method
564      * to be checked
565      * @param methodName the name of the method to be checked
566      * @param argTypeNames the fully qualified names of the argument types
567      * @return the method element
568      * @see #getClass
569      */

570     public Object JavaDoc getMethod (final String JavaDoc className, final String JavaDoc methodName,
571         String JavaDoc[] argTypeNames)
572     {
573         final Class JavaDoc classElement = (Class JavaDoc)getClass(className);
574
575         if (classElement != null)
576         {
577             final Class JavaDoc[] argTypes = getTypesForNames(argTypeNames);
578
579             return AccessController.doPrivileged(new PrivilegedAction JavaDoc()
580             {
581                 public Object JavaDoc run ()
582                 {
583                     try
584                     {
585                         return classElement.getDeclaredMethod(
586                             methodName, argTypes);
587                     }
588                     catch (NoSuchMethodException JavaDoc ex)
589                     {
590                         // method not found => return null
591
return null;
592                     }
593                 }
594             });
595         }
596
597         return null;
598     }
599
600     /** Returns the string representation of type of the specified element.
601      * If element denotes a field, it returns the type of the field.
602      * If element denotes a method, it returns the return type of the method.
603      * Note, element is either a field element as returned by getField, or a
604      * method element as returned by getMethod executed on the same model
605      * instance. This implementation expects the element being a reflection
606      * instance.
607      * @param element the element to be checked
608      * @return the string representation of the type of the element
609      * @see #getField
610      * @see #getMethod
611      */

612     public String JavaDoc getType (Object JavaDoc element)
613     {
614         return getNameForType(getTypeObject(element));
615     }
616
617     /** Returns a list of names of all the declared field elements in the
618      * class with the specified name.
619      * @param className the fully qualified name of the class to be checked
620      * @return the names of the field elements for the specified class
621      */

622     public List getFields (String JavaDoc className)
623     {
624         List returnList = new ArrayList();
625         final Class JavaDoc classElement = (Class JavaDoc)getClass(className);
626         
627         if (classElement != null)
628         {
629             Field[] fields = (Field[]) AccessController.doPrivileged(
630                 new PrivilegedAction JavaDoc()
631             {
632                 public Object JavaDoc run ()
633                 {
634                     return classElement.getDeclaredFields();
635                 }
636             });
637             int i, count = fields.length;
638
639             for (i = 0; i < count; i++)
640                 returnList.add(fields[i].getName());
641         }
642
643         return returnList;
644     }
645
646     /** Returns the field element for the specified fieldName in the class
647      * with the specified className.
648      * @param className the fully qualified name of the class which contains
649      * the field to be checked
650      * @param fieldName the name of the field to be checked
651      * @return the field element for the specified fieldName
652      */

653     public Object JavaDoc getField (String JavaDoc className, final String JavaDoc fieldName)
654     {
655         final Class JavaDoc classElement = (Class JavaDoc)getClass(className);
656
657         if (classElement != null)
658         {
659             return AccessController.doPrivileged(new PrivilegedAction JavaDoc()
660             {
661                 public Object JavaDoc run ()
662                 {
663                     try
664                     {
665                         return classElement.getDeclaredField(fieldName);
666                     }
667                     catch (NoSuchFieldException JavaDoc e)
668                     {
669                         // field not found => return null;
670
return null;
671                     }
672                 }
673             });
674         }
675
676         return null;
677     }
678
679     /** Determines if the specified field element has a serializable type.
680      * A type is serializable if it is a primitive type, a class that implements
681      * java.io.Serializable or an interface that inherits from
682      * java.io.Serializable.
683      * Note, the field element is a model specific field representation as
684      * returned by a getField call executed on the same model instance. This
685      * implementation expects the field element being a reflection instance.
686      * @param fieldElement the field element to be checked
687      * @return <code>true</code> if the field element has a serializable type;
688      * <code>false</code> otherwise.
689      * @see #getField
690      */

691     public boolean isSerializable (Object JavaDoc fieldElement)
692     {
693         Class JavaDoc type = getTypeObject(fieldElement);
694
695         // check if the topmost element type is serializable
696
while ((type != null) && type.isArray())
697             type = type.getComponentType();
698
699         return ((type != null) ?
700             (type.isPrimitive() || implementsInterface(type, SERIALIZABLE)) :
701             false);
702     }
703
704     /** Determines if a field with the specified fieldName in the class
705      * with the specified className is an array.
706      * @param className the fully qualified name of the class which contains
707      * the field to be checked
708      * @param fieldName the name of the field to be checked
709      * @return <code>true</code> if this field name represents a java array
710      * field; <code>false</code> otherwise.
711      * @see #getFieldType
712      */

713     public boolean isArray (String JavaDoc className, String JavaDoc fieldName)
714     {
715         Object JavaDoc fieldElement = getField(className, fieldName);
716
717         return ((fieldElement != null) ?
718             getTypeObject(fieldElement).isArray() : false);
719     }
720
721     /** Returns the string representation of declaring class of
722      * the specified member element. Note, the member element is
723      * either a class element as returned by getClass, a field element
724      * as returned by getField, a constructor element as returned by
725      * getConstructor, or a method element as returned by getMethod
726      * executed on the same model instance. This implementation
727      * expects the member element to be a reflection instance.
728      * @param memberElement the member element to be checked
729      * @return the string representation of the declaring class of
730      * the specified memberElement
731      * @see #getClass
732      * @see #getField
733      * @see #getConstructor
734      * @see #getMethod
735      */

736     public String JavaDoc getDeclaringClass (Object JavaDoc memberElement)
737     {
738         Class JavaDoc classElement = null;
739
740         if ((memberElement != null) && (memberElement instanceof Member))
741             classElement = ((Member)memberElement).getDeclaringClass();
742
743         return ((classElement != null) ? classElement.getName() : null);
744     }
745
746     /** Returns the modifier mask for the specified member element.
747      * Note, the member element is either a class element as returned by
748      * getClass, a field element as returned by getField, a constructor element
749      * as returned by getConstructor, or a method element as returned by
750      * getMethod executed on the same model instance. This implementation
751      * expects the member element to be a reflection instance.
752      * @param memberElement the member element to be checked
753      * @return the modifier mask for the specified memberElement
754      * @see java.lang.reflect.Modifier
755      * @see #getClass
756      * @see #getField
757      * @see #getConstructor
758      * @see #getMethod
759      */

760     public int getModifiers (Object JavaDoc memberElement)
761     {
762         int modifiers = 0;
763         
764         if (memberElement != null)
765         {
766             if (memberElement instanceof Class JavaDoc)
767             {
768                 modifiers = ((Class JavaDoc)memberElement).getModifiers();
769             }
770             else if (memberElement instanceof Member)
771             {
772                 modifiers = ((Member)memberElement).getModifiers();
773             }
774         }
775
776         return modifiers;
777     }
778
779     /** Returns the Class type of the specified element.
780      * If element denotes a field, it returns the type of the field.
781      * If element denotes a method, it returns the return type of the method.
782      * Note, element is either a field element as returned by getField, or a
783      * method element as returned by getMethod executed on the same model
784      * instance.
785      * @param element the element to be checked
786      * @return the Class type of the element
787      * @see #getField
788      * @see #getMethod
789      */

790     protected Class JavaDoc getTypeObject (Object JavaDoc element)
791     {
792         Class JavaDoc type = null;
793
794         if (element != null)
795         {
796             if (element instanceof Field)
797                 type = ((Field)element).getType();
798             else if (element instanceof Method)
799                 type = ((Method)element).getReturnType();
800         }
801
802         return type;
803     }
804
805     private String JavaDoc getNameForType (Class JavaDoc type)
806     {
807         String JavaDoc typeName = null;
808
809         if (type != null)
810         {
811             if (type.isArray())
812             {
813                 typeName = getNameForType(
814                     type.getComponentType()) + "[]"; // NOI18N
815
}
816             else
817                 typeName = type.getName();
818         }
819
820         return typeName;
821     }
822
823     /** Converts the array of type names into an array of Class objects.
824      */

825     private Class JavaDoc[] getTypesForNames (String JavaDoc[] typeNames)
826     {
827         Class JavaDoc[] classes = new Class JavaDoc[typeNames.length];
828
829         for (int i = 0; i < classes.length; i++)
830             classes[i] = getTypeForName(typeNames[i]);
831
832         return classes;
833     }
834
835     /** Converts the specified type name into its corresponding java.lang.Class
836      * representation.
837      */

838     private Class JavaDoc getTypeForName (String JavaDoc typeName)
839     {
840         Class JavaDoc clazz = JavaTypeHelper.getPrimitiveClass(typeName);
841
842         if (clazz == null)
843             clazz = (Class JavaDoc)getClass(typeName);
844
845         return clazz;
846     }
847 }
848
Popular Tags