1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.lang.reflect.Modifier ; 22 import org.netbeans.jmi.javamodel.JavaClass; 23 import org.netbeans.jmi.javamodel.ClassDefinition; 24 import org.netbeans.jmi.javamodel.Resource; 25 import javax.jmi.reflect.TypeMismatchException; 26 import java.util.Collection ; 27 import java.util.AbstractCollection ; 28 import java.util.Iterator ; 29 import java.util.ArrayList ; 30 import org.netbeans.modules.javacore.internalapi.ProgressSupport; 31 import org.openide.util.Utilities; 32 33 37 class SubClassesCollection extends AbstractCollection implements TypeVerifier { 38 protected final JavaClass superClass; 39 protected boolean deterministicProgress; 40 41 42 protected SubClassesCollection(JavaClass superClass, boolean deterministicProgress) { 43 this.superClass = (JavaClass) ClassDefinitionImpl.getRealClassDefinition(superClass); 44 this.deterministicProgress = deterministicProgress; 45 } 46 47 private Resource[] getResources() { 48 int mods=superClass.getModifiers(); 50 51 if (Modifier.isFinal(mods)) 52 return new Resource[0]; 53 return ((FeatureImpl)superClass).findReferencedResources(true); 54 } 55 56 protected java.util.Iterator iterator(Resource[] resources) { 57 return new It(resources); 58 } 59 60 public Iterator iterator() { 61 return iterator(getResources()); 62 } 63 64 public int size() { 65 Resource[] resources = getResources(); 66 if (resources.length == 0) { 67 return 0; 68 } 69 int size = 0; 70 for (Iterator it = iterator(resources); it.hasNext(); size++, it.next()); 71 return size; 72 } 73 74 public boolean isEmpty() { 75 Resource[] resources = getResources(); 76 if (resources.length == 0) { 77 return true; 78 } else { 79 Iterator it = iterator(resources); 80 return !it.hasNext(); 81 } 82 } 83 84 public boolean add(Object obj) { 85 throw new UnsupportedOperationException (); 86 } 87 88 public void checkType(Object obj) throws TypeMismatchException { 89 throw new UnsupportedOperationException (); 90 } 91 92 protected boolean acceptClass(ClassDefinition cls) { 93 return Utilities.compareObjects(superClass.getName(), getName(ClassDefinitionImpl.getRealClassDefinition(cls.getSuperClass()))); 94 } 95 96 protected static String getName(ClassDefinition cd) { 97 return cd == null ? null : cd.getName(); 98 } 99 100 protected class It implements Iterator { 101 private Resource[] resources; 102 private int resourceIndex = 0; 103 private Iterator classes = null; 104 private ClassDefinition next = null; 105 private float step; 106 private int last; 107 private static final int MAX_COUNT=30; 108 private ProgressSupport progressSupport; 109 110 protected It(Resource[] resources) { 111 this.resources = resources; 112 113 step = 1; 114 if (resources.length > MAX_COUNT) { 115 step = (float) MAX_COUNT / resources.length; 116 } 117 last = 0; 118 119 progressSupport = org.netbeans.modules.javacore.internalapi.JavaMetamodel.getManager().getProgressSupport(); 120 121 } 122 123 protected void addInnerClasses(Collection col, JavaClass cls) { 124 Object [] features=cls.getContents().toArray(); 125 for (int i=0;i<features.length;i++) { 126 Object tmp = features[i]; 127 if (tmp instanceof JavaClass) { 128 col.add(tmp); 129 addInnerClasses(col, (JavaClass) tmp); 130 } 131 } 132 } 133 134 public boolean hasNext() { 135 while (next == null) { 136 while (classes == null || !classes.hasNext()) { 137 if (resourceIndex == 0) { 138 if (deterministicProgress) 139 progressSupport.fireProgressListenerStart(0, Math.min(resources.length,MAX_COUNT)); 140 } 141 if (resourceIndex < resources.length) { 142 ArrayList al = new ArrayList (); 143 for (Iterator it = resources[resourceIndex].getClassifiers().iterator(); it.hasNext();) { 144 Object tmp = it.next(); 145 if (tmp instanceof JavaClass) { 146 al.add(tmp); 147 addInnerClasses(al, (JavaClass) tmp); 148 } 149 } 150 classes = al.iterator(); 151 resourceIndex++; 152 153 if (resourceIndex*step >= last + 1 ) { 154 progressSupport.fireProgressListenerStep(); 155 last++; 156 } 157 158 } else { 159 if (deterministicProgress) 160 progressSupport.fireProgressListenerStop(); 161 return false; 162 } 163 } 164 while (classes.hasNext()) { 165 Object subClass = classes.next(); 166 if (subClass instanceof ClassDefinition) { 167 if (acceptClass((ClassDefinition) subClass)) { 168 next = (ClassDefinition) subClass; 169 break; 170 } 171 } 172 } 173 } 174 return true; 175 } 176 177 public Object next() { 178 if (!hasNext()) { 179 throw new java.util.NoSuchElementException (); 180 } 181 ClassDefinition last = next; 182 next = null; 183 return last; 184 } 185 186 public void remove() { 187 throw new UnsupportedOperationException (); 188 } 189 } 190 } 191 | Popular Tags |