1 26 27 package org.objectweb.util.explorer.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.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 55 public class ClassContext 56 implements Context 57 { 58 59 65 66 protected MemberComparator comparator_; 67 68 74 public ClassContext(){ 75 comparator_ = new MemberComparator(); 76 } 77 78 84 protected void addFields(Field [] fields, List 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 name = new StringBuffer (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 [] methods, List 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 name = new StringBuffer (methods[i].getName() + "("); 108 if(displayParams){ 109 Class [] 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 [] classes, List 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 name = new StringBuffer (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 145 148 public Entry[] getEntries(Object object) { 149 Class theClass = (Class )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 entryList = new Vector (); 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 classesList = classesInheritance.getInheritClasses(); 164 List fields = new Vector (); 165 List methods = new Vector (); 166 List innerClasses = new Vector (); 167 Iterator it = classesList.iterator(); 168 while (it.hasNext()) { 169 Class element = (Class ) it.next(); 170 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 [])fields.toArray(new Field [0]), entryList, config); 178 addMethods((Method [])methods.toArray(new Method [0]), entryList, config); 179 addInnerClass((Class [])innerClasses.toArray(new Class [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 |