KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 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): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.explorer.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.explorer.api.Context;
37 import org.objectweb.util.explorer.api.Entry;
38 import org.objectweb.util.explorer.core.common.lib.ClassesInheritance;
39 import org.objectweb.util.explorer.core.naming.lib.DefaultEntry;
40
41 /**
42  * This class allows to browse a <code>java.lang.Class</code> object.
43  * It displays<br>
44  * <ul>
45  * <li>its fields</li>
46  * <li>its methods</li>
47  * <li>Its inner classes</li>
48  * </ul>
49  *
50  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>,
51  * <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>
52  *
53  * @version 0.1
54  */

55 public class ClassContext
56     implements Context
57 {
58
59     //==================================================================
60
//
61
// Internal states.
62
//
63
//==================================================================
64

65     /** The comparator to use to sort the arrays */
66     protected MemberComparator comparator_;
67     
68     //==================================================================
69
//
70
// Constructors.
71
//
72
//==================================================================
73

74     public ClassContext(){
75         comparator_ = new MemberComparator();
76     }
77
78     //==================================================================
79
//
80
// Internal methods.
81
//
82
//==================================================================
83

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

145     /* (non-Javadoc)
146      * @see org.objectweb.util.explorer.api.Context#getEntries(java.lang.Object)
147      */

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