KickJava   Java API By Example, From Geeks To Geeks.

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


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.Modifier JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  *
35  *
36  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>,
37  * <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>
38  *
39  * @version 0.1
40  */

41 public class ClassVisibility {
42
43     //==================================================================
44
//
45
// No internal state.
46
//
47
//==================================================================
48

49     /** The configuration map */
50     public static Map JavaDoc config_ = initMap();
51     
52     /** The <code>String</code> representing the default configuration */
53     public static final String JavaDoc DEFAULT_CONFIG = "_default_config_";
54         
55     //==================================================================
56
//
57
// No constructor.
58
//
59
//==================================================================
60

61     //==================================================================
62
//
63
// No internal method.
64
//
65
//==================================================================
66

67     //==================================================================
68
//
69
// Public static methods.
70
//
71
//==================================================================
72

73     /**
74      * Indicates if an element (<code>Field</code>, <code>Method</code> or <code>Class</code>)
75      * must be visible depending on the user configuration.
76      */

77     public static boolean hasToBeAdded(int modifier, ClassVisibilityConfig config, String JavaDoc type){
78         boolean addElement = false;
79             
80         if (((Modifier.isPrivate(modifier) && config.get(type + "." + ClassVisibilityConfig.PRIVATE)) || (Modifier.isProtected(modifier) && config.get(type + "." + ClassVisibilityConfig.PROTECTED)) || (Modifier.isPublic(modifier) && config.get(type + "." + ClassVisibilityConfig.PUBLIC)))||(!Modifier.isPrivate(modifier) && !Modifier.isProtected(modifier) && !Modifier.isPublic(modifier) && config.get(type + "." + ClassVisibilityConfig.PACKAGE))) {
81             if (Modifier.isStatic(modifier) && Modifier.isFinal(modifier) && config.get(type + "." + ClassVisibilityConfig.STATIC) && config.get(type + "." + ClassVisibilityConfig.FINAL))
82                 addElement = true;
83             else if (Modifier.isStatic(modifier) && !Modifier.isFinal(modifier) && config.get(type + "." + ClassVisibilityConfig.STATIC))
84                 addElement = true;
85             else if (Modifier.isFinal(modifier) && !Modifier.isStatic(modifier) && config.get(type + "." + ClassVisibilityConfig.FINAL))
86                 addElement = true;
87             else if (Modifier.isAbstract(modifier) && config.get(type + "." + ClassVisibilityConfig.ABSTRACT))
88                 addElement = true;
89             else if (!Modifier.isStatic(modifier) && !Modifier.isFinal(modifier) && !Modifier.isAbstract(modifier))
90                 addElement = true;
91         }
92         
93         return addElement;
94     }
95
96     /**
97      * Provides a displayable name representing the name of a class
98      * Example:
99      * <ul>
100      * <li><code>java.lang.String<code> returns <code>String</code></li>
101      * <li><code>byte<code> returns <code>byte</code></li>
102      * <li><code>[Ljava.lang.Object;<code> returns <code>Object[]</code></li>
103      * <li><code>[[I<code> returns <code>int[][]</code></li>
104      * </ul>
105      * @param name The class
106      * @return The displayable name
107      * @see java.lang.Class#getName()
108      */

109     public static String JavaDoc getTypeName(Class JavaDoc theClass){
110         if(theClass!=null){
111             StringBuffer JavaDoc name = new StringBuffer JavaDoc();
112             int dimension = 0;
113             if(theClass.isArray()){
114                 while (theClass.isArray()) {
115                     dimension++;
116                     theClass = theClass.getComponentType();
117                 }
118             }
119             if(theClass.isPrimitive()) {
120                 name.append(theClass.getName());
121             } else {
122                 if (theClass.getName().lastIndexOf('.')!=-1)
123                     name.append(theClass.getName().substring(theClass.getName().lastIndexOf('.') + 1));
124                 else
125                     name.append(theClass.getName());
126             }
127             for(int i=0 ; i<dimension ; i++)
128                 name.append("[]");
129             return name.toString();
130         }
131         return "";
132     }
133
134     /**
135      * Provides a Map with one entry: the default configuration.
136      * @return A new <code>Map</code>.
137      */

138     public static Map JavaDoc initMap(){
139             config_ = new HashMap JavaDoc();
140             config_.put(DEFAULT_CONFIG, createNewDefaultConfig());
141             return config_;
142         }
143
144     /**
145      * Creates a new default configuration. It displays:
146      * <ul>
147      * <li>Public attributes</li>
148      * <li>Public methods</li>
149      * <li>Public inner classes</li>
150      * </ul>
151      * @return
152      */

153     public static ClassVisibilityConfig createNewDefaultConfig(){
154         ClassVisibilityConfig defaultConfig = new ClassVisibilityConfig();
155         defaultConfig.put(ClassVisibilityConfig.ATTRIBUTE + "." + ClassVisibilityConfig.PUBLIC, true);
156         defaultConfig.put(ClassVisibilityConfig.ATTRIBUTE + "." + ClassVisibilityConfig.STATIC, true);
157         defaultConfig.put(ClassVisibilityConfig.ATTRIBUTE + "." + ClassVisibilityConfig.FINAL, true);
158         defaultConfig.put(ClassVisibilityConfig.ATTRIBUTE + "." + ClassVisibilityConfig.TYPE, true);
159         defaultConfig.put(ClassVisibilityConfig.ATTRIBUTE + "." + ClassVisibilityConfig.DECLARING_CLASS, true);
160         defaultConfig.put(ClassVisibilityConfig.METHOD + "." + ClassVisibilityConfig.PUBLIC, true);
161         defaultConfig.put(ClassVisibilityConfig.METHOD + "." + ClassVisibilityConfig.STATIC, true);
162         defaultConfig.put(ClassVisibilityConfig.METHOD + "." + ClassVisibilityConfig.ABSTRACT, true);
163         defaultConfig.put(ClassVisibilityConfig.METHOD + "." + ClassVisibilityConfig.PARAMS, true);
164         defaultConfig.put(ClassVisibilityConfig.METHOD + "." + ClassVisibilityConfig.RETURN_TYPE, true);
165         defaultConfig.put(ClassVisibilityConfig.METHOD + "." + ClassVisibilityConfig.DECLARING_CLASS, true);
166         defaultConfig.put(ClassVisibilityConfig.INNER_CLASS + "." + ClassVisibilityConfig.PUBLIC, true);
167         defaultConfig.put(ClassVisibilityConfig.INNER_CLASS + "." + ClassVisibilityConfig.STATIC, true);
168         defaultConfig.put(ClassVisibilityConfig.INNER_CLASS + "." + ClassVisibilityConfig.FINAL, true);
169         defaultConfig.put(ClassVisibilityConfig.INNER_CLASS + "." + ClassVisibilityConfig.DECLARING_CLASS, true);
170         return defaultConfig;
171     }
172
173 }
Popular Tags