1 29 30 package com.caucho.bytecode; 31 32 import java.util.ArrayList ; 33 34 37 public class TypeSignatureParser { 38 private final JClassLoader _loader; 39 private final String _sig; 40 private final int _length; 41 private int _index; 42 43 public TypeSignatureParser(JClassLoader loader, String sig) 44 { 45 this(loader, sig, 0); 46 } 47 48 public TypeSignatureParser(JClassLoader loader, String sig, int index) 49 { 50 _loader = loader; 51 _sig = sig; 52 _length = sig.length(); 53 _index = index; 54 } 55 56 JType nextType() 57 { 58 int ch = read(); 59 60 switch (ch) { 61 case -1: 62 return null; 63 64 case '>': 65 case ')': 66 _index--; 67 return null; 68 69 case 'V': 70 return JClass.VOID; 71 case 'Z': 72 return JClass.BOOLEAN; 73 case 'B': 74 return JClass.BYTE; 75 case 'S': 76 return JClass.SHORT; 77 case 'I': 78 return JClass.INT; 79 case 'J': 80 return JClass.LONG; 81 case 'F': 82 return JClass.FLOAT; 83 case 'D': 84 return JClass.DOUBLE; 85 case 'C': 86 return JClass.CHAR; 87 88 case 'L': 89 return parseClass(); 90 91 default: 92 throw new IllegalStateException ("Can't parse: " + _sig); 93 } 94 } 95 96 private JType parseClass() 97 { 98 int begin = _index; 99 int end = begin; 100 101 int ch; 102 103 for (ch = read(); 104 ch >= 0 && 105 ch != ';' && ch != ')' && 106 ch != ',' && ch != '<' && ch != '>'; 107 ch = read()) { 108 end = _index; 109 } 110 111 String className = _sig.substring(begin, end).replace('/', '.'); 112 JClass rawClass = _loader.forName(className); 113 114 if (ch == '<') 115 return parseParameterizedType(rawClass); 116 else 117 return rawClass; 118 } 119 120 private JType parseParameterizedType(JClass rawClass) 121 { 122 ArrayList <JType> argList = new ArrayList <JType>(); 123 124 JType type; 125 while ((type = nextType()) != null) { 126 argList.add(type); 127 } 128 129 int ch = read(); 130 if (ch != '>') 131 throw new IllegalStateException ("expected '>' at " + (char) ch); 132 133 JType []args = new JType[argList.size()]; 134 argList.toArray(args); 135 136 return new JavaParameterizedType(_loader, rawClass, args); 137 } 138 139 142 private int read() 143 { 144 if (_index < _length) 145 return _sig.charAt(_index++); 146 else 147 return -1; 148 } 149 } 150 | Popular Tags |