1 26 27 package org.objectweb.util.browser.plugin.java.reflect; 28 29 import java.lang.reflect.Field ; 30 import java.lang.reflect.Method ; 31 import java.util.Arrays ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Vector ; 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 57 public class ClassContext 58 implements Wrapper, Context { 59 60 66 67 protected Class class_; 68 69 70 protected MemberComparator comparator_; 71 72 78 public ClassContext(){ 79 comparator_ = new MemberComparator(); 80 } 81 82 88 protected void addFields(Field [] fields, List 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 name = new StringBuffer (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 [] methods, List 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 name = new StringBuffer (methods[i].getName() + "("); 112 if(displayParams){ 113 Class [] 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 [] classes, List 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 name = new StringBuffer (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 149 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 entryList = new Vector (); 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 classesList = classesInheritance.getInheritClasses(); 169 List fields = new Vector (); 170 List methods = new Vector (); 171 List innerClasses = new Vector (); 172 Iterator it = classesList.iterator(); 173 while (it.hasNext()) { 174 Class element = (Class ) it.next(); 175 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 [])fields.toArray(new Field [0]), entryList, config); 183 addMethods((Method [])methods.toArray(new Method [0]), entryList, config); 184 addInnerClass((Class [])innerClasses.toArray(new Class [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 201 206 public void 207 setWrapped(Object object) { 208 class_ = (Class )object; 209 } 210 211 216 public Object 217 getWrapped() { 218 return class_; 219 } 220 221 } | Popular Tags |