KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > Classes


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: Classes.java,v 1.6 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.apache.log4j.*;
26
27 /**
28  * Simple Class utilities
29  */

30 public class Classes {
31
32     protected static final Logger logger = Logger.getLogger(Classes.class.getName());
33
34     protected static Map clCache = null;
35     protected static final String JavaDoc NULL_STR = "~Null~";
36     //csc_060903_3 - added
37
/**
38      * This method provides a cached interface to get a class. If the class exists in the
39      * cache, it is simply returned; if it does not, it is created via Class.forName() and then
40      * returned (saving a copy in the cache of course). If there is a problem creating the class
41      * the exception will be logged and a null value will be returned (which means its up to you
42      * to handle it).
43      *
44      * @param clName the fully qualified class name
45      * @return a reference to the Class
46      */

47     public synchronized static Class JavaDoc getClass(String JavaDoc clName) {
48         if (clName==null) return null;
49         if (clCache==null) clCache = new HashMap();
50         Class JavaDoc cl = null;
51         try {
52             Object JavaDoc o = clCache.get(clName);
53             if (NULL_STR.equals(o)) {
54                 logger.error("Error getting instance of class name:"+clName);
55                 return null;
56             }
57             if (o!=null) {
58                 cl = (Class JavaDoc) o;
59                 if (logger.isInfoEnabled()) logger.info("got cl from cache - name:"+clName+" class:"+cl);
60             } else {
61                 cl = Class.forName(clName, true, Thread.currentThread().getContextClassLoader());
62                 clCache.put(clName, cl);
63                 if (logger.isInfoEnabled()) logger.info("got cl from Class.forName() - name:"+clName+" class:"+cl);
64             }
65         } catch (Exception JavaDoc e) {
66             logger.error("Error creating Class reference for class name:"+clName+", err:"+e, e);
67             clCache.put(clName, NULL_STR);
68             cl = null;
69         }
70         return cl;
71     }
72
73     /**
74      * Get a new instance of the class, without throwing an InstantiationException. If there is
75      * a problem creating the class the exception will be logged and a null value will be
76      * returned (which means its up to you to handle it). Note that this means the class must
77      * provide a no-args constructor
78      *
79      * @param cl the class we wish to obtain an instance from
80      * @return a new instance of the class
81      */

82     public static Object JavaDoc newInstance(Class JavaDoc cl) {
83         if (cl==null) return null;
84         try {
85             return cl.newInstance();
86         } catch (Exception JavaDoc e) {
87             logger.error("Error instantiating class:"+cl+", err:"+e, e);
88             return null;
89         }
90     }
91
92     /**
93      * Get a new instance of the class, without throwing an InstantiationException. This is
94      * a convenience method which takes a String version of the class name, looks up the
95      * class via getClass(), and then tries to get an instance of it via newInstance().
96      *
97      * @param clName the fully qualified class name
98      * @return a new instance of the class
99      */

100     public static Object JavaDoc newInstance(String JavaDoc clName) {
101         return newInstance(getClass(clName));
102     }
103
104
105     /**
106      * Get a List of all interfaces that are implemented
107      * by an object
108      */

109     public static List getAllInterfaces(Object JavaDoc obj) {
110         List list = new ArrayList();
111         return new Classes().getAllInterfaces(obj.getClass(), list);
112     }
113     
114     public static List getAllInterfaces(Class JavaDoc cl) {
115         List list = new ArrayList();
116         list.add(cl);
117         return new Classes().getAllInterfaces(cl, list);
118     }
119     
120     private List getAllInterfaces(Class JavaDoc cl, List list) {
121         Class JavaDoc[] classes = cl.getInterfaces();
122         for (int i=0; i<classes.length; i++) {
123             if (!list.contains(classes[i])) {
124                 list.add(classes[i]);
125                 getAllInterfaces(classes[i], list);
126             }
127         }
128         
129       //saw_082603_1 begin - we also need to search the interfaces of the superclasses
130
Class JavaDoc supercl = cl.getSuperclass();
131         if (supercl!=null) getAllInterfaces(supercl, list);
132       //saw_082603_1 end
133

134         return list;
135     }
136     
137     /**
138      * Get a short version of a class name
139      */

140     public static String JavaDoc getShortClassName(Class JavaDoc cl) {
141         String JavaDoc className = cl.getName();
142         int spos = className.lastIndexOf(".");
143         if (spos<0) return className;
144         else return className.substring(spos+1);
145     }
146 }
Popular Tags