KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > plugin > java > reflect > ClassContext


1 /*====================================================================
2
3 Objectweb Browser Framework
4 Copyright (C) 2000-2003 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.browser.plugin.java.reflect;
28
29 import java.lang.reflect.Field JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.Arrays JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import org.objectweb.util.browser.api.Context;
37 import org.objectweb.util.browser.api.Entry;
38 import org.objectweb.util.browser.api.Wrapper;
39 import org.objectweb.util.browser.core.common.ClassesInheritance;
40 import org.objectweb.util.browser.core.naming.DefaultEntry;
41 import org.objectweb.util.browser.core.naming.DefaultName;
42
43 /**
44  * This class allows to browse a <code>java.lang.Class</code> object.
45  * It displays<br>
46  * <ul>
47  * <li>its fields</li>
48  * <li>its methods</li>
49  * <li>Its inner classes</li>
50  * </ul>
51  *
52  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>,
53  * <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>
54  *
55  * @version 0.1
56  */

57 public class ClassContext
58     implements Wrapper, Context {
59
60     //==================================================================
61
//
62
// Internal states.
63
//
64
//==================================================================
65

66     /** The class to browse */
67     protected Class JavaDoc class_;
68     
69     /** The comparator to use to sort the arrays */
70     protected MemberComparator comparator_;
71     
72     //==================================================================
73
//
74
// Constructors.
75
//
76
//==================================================================
77

78     public ClassContext(){
79         comparator_ = new MemberComparator();
80     }
81
82     //==================================================================
83
//
84
// Internal methods.
85
//
86
//==================================================================
87

88     protected void addFields(Field JavaDoc[] fields, List JavaDoc entryList, ClassVisibilityConfig config){
89         Arrays.sort(fields,comparator_);
90         boolean displayType = config.get(ClassVisibilityConfig.ATTRIBUTE + "." + ClassVisibilityConfig.TYPE);
91         boolean displayDeclaringClass = config.get(ClassVisibilityConfig.ATTRIBUTE + "." + ClassVisibilityConfig.DECLARING_CLASS) && config.get(ClassVisibilityConfig.FLAT_VIEW);
92         for (int i = 0; i < fields.length; i++) {
93             if(ClassVisibility.hasToBeAdded(fields[i].getModifiers(), config, ClassVisibilityConfig.ATTRIBUTE)){
94                 StringBuffer JavaDoc name = new StringBuffer JavaDoc(fields[i].getName());
95                 if(displayType)
96                     name.append(": " + ClassVisibility.getTypeName(fields[i].getType()));
97                 if(displayDeclaringClass)
98                     name.append(" (from " + ClassVisibility.getTypeName(fields[i].getDeclaringClass()) + ")");
99                 entryList.add(new DefaultEntry(fields[i],new DefaultName(name.toString()),this));
100             }
101         }
102     }
103
104     protected void addMethods(Method JavaDoc[] methods, List JavaDoc entryList, ClassVisibilityConfig config){
105         Arrays.sort(methods,comparator_);
106         boolean displayParams = config.get(ClassVisibilityConfig.METHOD + "." + ClassVisibilityConfig.PARAMS);
107         boolean displayReturnType = config.get(ClassVisibilityConfig.METHOD + "." + ClassVisibilityConfig.RETURN_TYPE);
108         boolean displayDeclaringClass = config.get(ClassVisibilityConfig.METHOD + "." + ClassVisibilityConfig.DECLARING_CLASS) && config.get(ClassVisibilityConfig.FLAT_VIEW);
109         for (int i = 0; i < methods.length; i++) {
110             if(ClassVisibility.hasToBeAdded(methods[i].getModifiers(), config, ClassVisibilityConfig.METHOD)){
111                 StringBuffer JavaDoc name = new StringBuffer JavaDoc(methods[i].getName() + "(");
112                 if(displayParams){
113                     Class JavaDoc[] params = methods[i].getParameterTypes();
114                     for(int j=0 ; j<params.length ; j++) {
115                         name.append(ClassVisibility.getTypeName(params[j]));
116                         if(j<params.length-1)
117                             name.append(", ");
118                     }
119                 }
120                 name.append(")");
121                 if(displayReturnType)
122                     name.append(": " + ClassVisibility.getTypeName(methods[i].getReturnType()));
123                 if(displayDeclaringClass)
124                     name.append(" (from " + ClassVisibility.getTypeName(methods[i].getDeclaringClass()) + ")");
125                 entryList.add(new DefaultEntry(methods[i],new DefaultName(name.toString()),this));
126             }
127         }
128     }
129
130     protected void addInnerClass(Class JavaDoc[] classes, List JavaDoc entryList, ClassVisibilityConfig config){
131         Arrays.sort(classes,comparator_);
132         boolean displayDeclaringClass = config.get(ClassVisibilityConfig.INNER_CLASS + "." + ClassVisibilityConfig.DECLARING_CLASS) && config.get(ClassVisibilityConfig.FLAT_VIEW);
133         for (int i = 0; i < classes.length; i++) {
134             if(ClassVisibility.hasToBeAdded(classes[i].getModifiers(), config, ClassVisibilityConfig.INNER_CLASS)){
135                 StringBuffer JavaDoc name = new StringBuffer JavaDoc(classes[i].getName().substring(classes[i].getName().lastIndexOf('$') + 1));
136                 if(displayDeclaringClass)
137                     name.append(" (from " + ClassVisibility.getTypeName(classes[i].getDeclaringClass()) + ")");
138                 entryList.add(new DefaultEntry(classes[i],new DefaultName(name.toString()),this));
139             }
140         }
141     }
142
143     //==================================================================
144
//
145
// Public methods for Context interface.
146
//
147
//==================================================================
148

149     /**
150      * Returns an array containing the entries contained by the context.
151      * @return The entries of the context.
152      */

153     public Entry[] getEntries() {
154         
155         ClassVisibilityConfig config = (ClassVisibilityConfig)ClassVisibility.config_.get(class_.getName());
156         if(config == null)
157             config = (ClassVisibilityConfig)ClassVisibility.config_.get(ClassVisibility.DEFAULT_CONFIG);
158             
159         List JavaDoc entryList = new Vector JavaDoc();
160         if (config.get(ClassVisibilityConfig.TREE_INHERITANCE)) {
161             if(!config.get(ClassVisibilityConfig.FLAT_VIEW)){
162                 entryList.add(new DefaultEntry(new TreeInheritance(class_),new DefaultName("Tree inheritance"),this));
163                 addFields(class_.getDeclaredFields(), entryList, config);
164                 addMethods(class_.getDeclaredMethods(), entryList, config);
165                 addInnerClass(class_.getDeclaredClasses(), entryList, config);
166             } else {
167                 ClassesInheritance classesInheritance = new ClassesInheritance(class_);
168                 List JavaDoc classesList = classesInheritance.getInheritClasses();
169                 List JavaDoc fields = new Vector JavaDoc();
170                 List JavaDoc methods = new Vector JavaDoc();
171                 List JavaDoc innerClasses = new Vector JavaDoc();
172                 Iterator JavaDoc it = classesList.iterator();
173                 while (it.hasNext()) {
174                     Class JavaDoc element = (Class JavaDoc) it.next();
175                     // It is not necessary displaying interfaces from interface with flat view
176
if(!element.isInterface()){
177                         fields.addAll(Arrays.asList(element.getDeclaredFields()));
178                         methods.addAll(Arrays.asList(element.getDeclaredMethods()));
179                         innerClasses.addAll(Arrays.asList(element.getDeclaredClasses()));
180                     }
181                 }
182                 addFields((Field JavaDoc[])fields.toArray(new Field JavaDoc[0]), entryList, config);
183                 addMethods((Method JavaDoc[])methods.toArray(new Method JavaDoc[0]), entryList, config);
184                 addInnerClass((Class JavaDoc[])innerClasses.toArray(new Class JavaDoc[0]), entryList, config);
185             }
186         } else {
187             addFields(class_.getDeclaredFields(), entryList, config);
188             addMethods(class_.getDeclaredMethods(), entryList, config);
189             addInnerClass(class_.getDeclaredClasses(), entryList, config);
190         }
191
192         return (Entry[])entryList.toArray(new Entry[0]);
193     }
194
195     //==================================================================
196
//
197
// Public methods for Wrapper interface.
198
//
199
//==================================================================
200

201     /**
202      * Sets the wrapped object.
203      *
204      * @param object The object to wrap.
205      */

206     public void
207     setWrapped(Object JavaDoc object) {
208         class_ = (Class JavaDoc)object;
209     }
210
211     /**
212      * Gets the wrapped object.
213      *
214      * @return The wrapped object.
215      */

216     public Object JavaDoc
217     getWrapped() {
218         return class_;
219     }
220
221 }
Popular Tags