View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.river.example.browser;
19  
20  import java.lang.reflect.Field;
21  import java.lang.reflect.Array;
22  import java.lang.reflect.Modifier;
23  
24  /**
25   * Introspection related methods
26   *
27   * @author Sun Microsystems, Inc.
28   *
29   * @version 0.2 06/04/98
30   *
31   */
32  
33  class Introspector {
34  
35    public static boolean isHidden(Field f){
36      int m = f.getModifiers();
37  
38      if(Modifier.isPrivate(m) || Modifier.isStatic(m))
39        return true;
40  
41      return false;
42    }
43  
44    public static boolean isString(Class clazz){
45      if("java.lang.String".equals(clazz.getName()))
46        return true;
47      return false;
48    }
49  
50    public static boolean isWrapper(Class clazz){
51      String name = clazz.getName();
52  
53      if("java.lang.Integer".equals(name) ||
54         "java.lang.Boolean".equals(name) ||
55         "java.lang.Byte".equals(name) ||
56         "java.lang.Char".equals(name) ||
57         "java.lang.Double".equals(name) ||
58         "java.lang.Float".equals(name) ||
59         "java.lang.Long".equals(name)){
60        return true;
61      }
62      return false;
63    }
64  
65    /** Return the name of an interface or primitive type, handling arrays. */
66    public static String getTypename(Class t, boolean showPackage) {
67      String brackets = "";
68      while(t.isArray()) {
69        brackets += "[]";
70        t = t.getComponentType();
71      }
72  
73      if(showPackage)
74        return t.getName() + brackets;
75      else
76        return extractClassName(t.getName()) + brackets;
77    }
78  
79    /** Return a string version of modifiers, handling spaces nicely. */
80    public static String getModifierString(int m) {
81      if(m == 0)
82        return "";
83      else
84        return Modifier.toString(m) + " ";
85    }
86  
87    /** Print the modifiers, type, and name of a field. */
88    public static String getFieldString(Field f, boolean showModifier, boolean showPackage) {
89      String fstring = "";
90      
91      if(showModifier)
92        fstring += getModifierString(f.getModifiers());
93  
94      fstring += getTypename(f.getType(), showPackage);
95      fstring += " ";
96      fstring += f.getName();
97  
98      return fstring;
99    }
100 
101   public static String extractClassName(String fullName){
102     int index = fullName.lastIndexOf(".");
103     
104     return fullName.substring(index + 1);
105   }
106 }