1 21 package proguard.classfile.util; 22 23 import proguard.classfile.*; 24 25 35 public class DescriptorClassEnumeration 36 { 37 private String descriptor; 38 private int index; 39 40 41 public DescriptorClassEnumeration(String descriptor) 42 { 43 setDescriptor(descriptor); 44 } 45 46 47 DescriptorClassEnumeration() 48 { 49 } 50 51 52 void setDescriptor(String descriptor) 53 { 54 this.descriptor = descriptor; 55 56 reset(); 57 } 58 59 60 public void reset() 61 { 62 index = 0; 63 } 64 65 66 70 public int classCount() 71 { 72 int count = 0; 73 74 while (nextClassNameStartIndex() >= 0) 75 { 76 count++; 77 78 nextClassNameEndIndex(); 79 } 80 81 index = 0; 82 83 return count; 84 } 85 86 87 public boolean hasMoreClassNames() 88 { 89 return index >= 0 && nextClassNameStartIndex() >= 0; 90 } 91 92 93 public String nextFluff() 94 { 95 int fluffStartIndex = index; 96 int fluffEndIndex = nextClassNameStartIndex() + 1; 97 98 if (fluffEndIndex == 0) 100 { 101 fluffEndIndex = descriptor.length(); 102 } 103 104 return descriptor.substring(fluffStartIndex, fluffEndIndex); 105 } 106 107 108 public String nextClassName() 109 { 110 int classNameStartIndex = nextClassNameStartIndex() + 1; 111 int classNameEndIndex = nextClassNameEndIndex(); 112 113 return descriptor.substring(classNameStartIndex, classNameEndIndex); 114 } 115 116 117 private int nextClassNameStartIndex() 118 { 119 index = descriptor.indexOf(ClassConstants.INTERNAL_TYPE_CLASS_START, index); 120 121 return index; 122 } 123 124 125 private int nextClassNameEndIndex() 126 { 127 while (++index < descriptor.length()) 128 { 129 char c = descriptor.charAt(index); 130 if (c == ClassConstants.INTERNAL_TYPE_CLASS_END || 131 c == ClassConstants.INTERNAL_TYPE_GENERIC_START) 132 { 133 return index; 134 } 135 } 136 137 throw new IllegalArgumentException ("Missing class name terminator in descriptor ["+descriptor+"]"); 138 } 139 140 141 142 143 146 public static void main(String [] args) 147 { 148 try 149 { 150 System.out.println("Descriptor ["+args[0]+"]"); 151 DescriptorClassEnumeration enumeration = new DescriptorClassEnumeration(args[0]); 152 System.out.println("Class count = "+enumeration.classCount()); 153 System.out.println(" Fluff: ["+enumeration.nextFluff()+"]"); 154 while (enumeration.hasMoreClassNames()) 155 { 156 System.out.println(" Name: ["+enumeration.nextClassName()+"]"); 157 System.out.println(" Fluff: ["+enumeration.nextFluff()+"]"); 158 } 159 } 160 catch (Exception ex) 161 { 162 ex.printStackTrace(); 163 } 164 } 165 } 166 | Popular Tags |