You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenericSignatureParsingTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* *******************************************************************
  2. * Copyright (c) 2005 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement (IBM) initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.apache.bcel.classfile.tests;
  13. import java.util.ArrayList;
  14. import org.aspectj.apache.bcel.Constants;
  15. import org.aspectj.apache.bcel.classfile.Attribute;
  16. import org.aspectj.apache.bcel.classfile.ClassFormatException;
  17. import org.aspectj.apache.bcel.classfile.JavaClass;
  18. import org.aspectj.apache.bcel.classfile.Method;
  19. import org.aspectj.apache.bcel.classfile.Signature;
  20. import org.aspectj.apache.bcel.classfile.Utility;
  21. /**
  22. * Generics introduces more complex signature possibilities, they are no longer just
  23. * made up of primitives or big 'L' types. The addition of 'anglies' due to
  24. * parameterization and the ability to specify wildcards (possibly bounded)
  25. * when talking about parameterized types means we need to be much more sophisticated.
  26. *
  27. *
  28. * Notes:
  29. * Signatures are used to encode Java programming language type informaiton
  30. * that is not part of the JVM type system, such as generic type and method
  31. * declarations and parameterized types. This kind of information is
  32. * needed to support reflection and debugging, and by the Java compiler.
  33. *
  34. * =============================================
  35. *
  36. * ClassTypeSignature = LPackageSpecifier* SimpleClassTypeSignature ClassTypeSignatureSuffix*;
  37. *
  38. * PackageSpecifier = Identifier/PackageSpecifier*
  39. * SimpleClassTypeSignature= Identifier TypeArguments(opt)
  40. * ClassTypeSignatureSuffix= .SimpleClassTypeSignature
  41. * TypeVariableSignature = TIdentifier;
  42. * TypeArguments = <TypeArgument+>
  43. * TypeArgument = WildcardIndiciator(opt) FieldTypeSignature
  44. * *
  45. * WildcardIndicator = +
  46. * -
  47. * ArrayTypeSignature = [TypeSignature
  48. * TypeSignature = [FieldTypeSignature
  49. * [BaseType
  50. *
  51. * <not sure those [ should be prefixing fts and bt>
  52. * Examples:
  53. * Ljava/util/List; == java.util.List
  54. * Ljava/util/List<Ljava/lang/String;>; == java.util.List<java.lang.String>
  55. * Ljava/util/List<Ljava/lang/Double;>; == java.util.List<java.lang.Double>
  56. * Ljava/util/List<+Ljava/lang/Number;>; == java.util.List<? extends java.lang.Number>
  57. * Ljava/util/List<-Ljava/lang/Number;>; == java.util.List<? super java.lang.Number>
  58. * Ljava/util/List<*>; == java.util.List<?>
  59. * Ljava/util/Map<*-Ljava/lang/Number;>; == java.util.Map<?,? super java.lang.Number>
  60. *
  61. * =============================================
  62. *
  63. * ClassSignature = FormalTypeParameters(opt) SuperclassSignature SuperinterfaceSignatures*
  64. *
  65. * optional formal type parameters then a superclass signature then a superinterface signature
  66. *
  67. * FormalTypeParameters = <FormalTypeParameter+>
  68. * FormalTypeParameter = Identifier ClassBound InterfaceBound*
  69. * ClassBound = :FieldTypeSignature(opt)
  70. * InterfaceBound = :FieldTypeSignature
  71. *
  72. * If it exists, a set of formal type parameters are contained in anglies and consist of an identifier a classbound (assumed to be
  73. * object if not specified) and then an optional list of InterfaceBounds
  74. *
  75. * SuperclassSignature = ClassTypeSignature
  76. * SuperinterfaceSignature = ClassTypeSignature
  77. * FieldTypeSignature = ClassTypeSignature
  78. * ArrayTypeSignature
  79. * TypeVariableSignature
  80. *
  81. *
  82. * MethodTypeSignature = FormalTypeParameters(opt) ( TypeSignature* ) ReturnType ThrowsSignature*
  83. * ReturnType = TypeSignature
  84. * VoidDescriptor
  85. * ThrowsSignature = ^ClassTypeSignature
  86. * ^TypeVariableSignature
  87. *
  88. * Examples:
  89. *
  90. * <T::Ljava/lang/Comparable<-Ljava/lang/Number;>;>
  91. *
  92. * ClassBound not supplied, Object assumed. Interface bound is Comparable<? super Number>
  93. *
  94. * "T:Ljava/lang/Object;:Ljava/lang/Comparable<-TT;>;","T extends java.lang.Object & java.lang.Comparable<? super T>"
  95. *
  96. */
  97. public class GenericSignatureParsingTest extends BcelTestCase {
  98. /**
  99. * Throw some generic format signatures at the BCEL signature
  100. * parsing code and see what it does.
  101. */
  102. public void testParsingGenericSignatures_ClassTypeSignature() {
  103. // trivial
  104. checkClassTypeSignature("Ljava/util/List;","java.util.List");
  105. // basics
  106. checkClassTypeSignature("Ljava/util/List<Ljava/lang/String;>;","java.util.List<java.lang.String>");
  107. checkClassTypeSignature("Ljava/util/List<Ljava/lang/Double;>;","java.util.List<java.lang.Double>");
  108. // madness
  109. checkClassTypeSignature("Ljava/util/List<+Ljava/lang/Number;>;","java.util.List<? extends java.lang.Number>");
  110. checkClassTypeSignature("Ljava/util/List<-Ljava/lang/Number;>;","java.util.List<? super java.lang.Number>");
  111. checkClassTypeSignature("Ljava/util/List<*>;", "java.util.List<?>");
  112. checkClassTypeSignature("Ljava/util/Map<*-Ljava/lang/Number;>;","java.util.Map<?,? super java.lang.Number>");
  113. // with type params
  114. checkClassTypeSignature("Ljava/util/Collection<TT;>;","java.util.Collection<T>");
  115. // arrays
  116. checkClassTypeSignature("Ljava/util/List<[Ljava/lang/String;>;","java.util.List<java.lang.String[]>");
  117. checkClassTypeSignature("[Ljava/util/List<Ljava/lang/String;>;","java.util.List<java.lang.String>[]");
  118. }
  119. public void testMethodTypeToSignature() {
  120. checkMethodTypeToSignature("void",new String[]{"java.lang.String[]","boolean"},"([Ljava/lang/String;Z)V");
  121. checkMethodTypeToSignature("void",new String[]{"java.util.List<java/lang/String>"},"(Ljava/util/List<java/lang/String>;)V");
  122. }
  123. public void testMethodSignatureToArgumentTypes() {
  124. checkMethodSignatureArgumentTypes("([Ljava/lang/String;Z)V",new String[]{"java.lang.String[]","boolean"});
  125. // checkMethodSignatureArgumentTypes("(Ljava/util/List<java/lang/String>;)V",new String[]{"java.util.List<java/lang/String>"});
  126. }
  127. public void testMethodSignatureReturnType() {
  128. checkMethodSignatureReturnType("([Ljava/lang/String;)Z","boolean");
  129. }
  130. public void testLoadingGenerics() throws ClassNotFoundException {
  131. JavaClass clazz = getClassFromJar("PossibleGenericsSigs");
  132. // J5TODO asc fill this bit in...
  133. }
  134. // helper methods below
  135. // These routines call BCEL to determine if it can correctly translate from one form to the other.
  136. private void checkClassTypeSignature(String sig, String expected) {
  137. StringBuffer result = new StringBuffer();
  138. int p = GenericSignatureParsingTest.readClassTypeSignatureFrom(sig,0,result,false);
  139. assertTrue("Only swallowed "+p+" chars of this sig "+sig+" (len="+sig.length()+")",p==sig.length());
  140. assertTrue("Expected '"+expected+"' but got '"+result.toString()+"'",result.toString().equals(expected));
  141. }
  142. private void checkMethodTypeToSignature(String ret,String[] args,String expected) {
  143. String res = GenericSignatureParsingTest.methodTypeToSignature(ret,args);
  144. if (!res.equals(expected)) {
  145. fail("Should match. Got: "+res+" Expected:"+expected);
  146. }
  147. }
  148. private void checkMethodSignatureReturnType(String sig,String expected) {
  149. String result = GenericSignatureParsingTest.methodSignatureReturnType(sig,false);
  150. if (!result.equals(expected)) {
  151. fail("Should match. Got: "+result+" Expected:"+expected);
  152. }
  153. }
  154. private void checkMethodSignatureArgumentTypes(String in,String[] expected) {
  155. String[] result = GenericSignatureParsingTest.methodSignatureArgumentTypes(in,false);
  156. if (result.length!=expected.length) {
  157. fail("Expected "+expected.length+" entries to be returned but only got "+result.length);
  158. }
  159. for (int i = 0; i < expected.length; i++) {
  160. String string = result[i];
  161. if (!string.equals(expected[i]))
  162. fail("Argument: "+i+" should have been "+expected[i]+" but was "+string);
  163. }
  164. }
  165. public Signature getSignatureAttribute(JavaClass clazz,String name) {
  166. Method m = getMethod(clazz,name);
  167. Attribute[] as = m.getAttributes();
  168. for (Attribute attribute : as) {
  169. if (attribute.getName().equals("Signature")) {
  170. return (Signature) attribute;
  171. }
  172. }
  173. return null;
  174. }
  175. /**
  176. * Takes a string and consumes a single complete signature from it, returning
  177. * how many chars it consumed. The chopit flag indicates whether to shorten
  178. * type references ( java/lang/String => String )
  179. *
  180. * FIXME asc this should also create some kind of object you can query for information about whether its parameterized, what the bounds are, etc...
  181. */
  182. public static final int readClassTypeSignatureFrom(String signature, int posn, StringBuffer result, boolean chopit) {
  183. int idx = posn;
  184. try {
  185. switch (signature.charAt(idx)) {
  186. case 'B' : result.append("byte"); return 1;
  187. case 'C' : result.append("char"); return 1;
  188. case 'D' : result.append("double"); return 1;
  189. case 'F' : result.append("float"); return 1;
  190. case 'I' : result.append("int"); return 1;
  191. case 'J' : result.append("long"); return 1;
  192. case 'S' : result.append("short"); return 1;
  193. case 'Z' : result.append("boolean");return 1;
  194. case 'V' : result.append("void"); return 1;
  195. //FIXME ASC Need a state machine to check we are parsing the right stuff here !
  196. case 'T' :
  197. idx++;
  198. int nextSemiIdx = signature.indexOf(';',idx);
  199. result.append(signature.substring(idx,nextSemiIdx));
  200. return nextSemiIdx+1-posn;
  201. case '+' :
  202. result.append("? extends ");
  203. return readClassTypeSignatureFrom(signature,idx+1,result,chopit)+1;
  204. case '-' :
  205. result.append("? super ");
  206. return readClassTypeSignatureFrom(signature,idx+1,result,chopit)+1;
  207. case '*' :
  208. result.append("?");
  209. return 1;
  210. case 'L' : // Full class name
  211. boolean parameterized = false;
  212. int idxSemicolon = signature.indexOf(';',idx); // Look for closing ';' or '<'
  213. int idxAngly = signature.indexOf('<',idx);
  214. int endOfSig = idxSemicolon;
  215. if ((idxAngly!=-1) && idxAngly<endOfSig) { endOfSig = idxAngly; parameterized = true; }
  216. String p = signature.substring(idx+1,endOfSig);
  217. String t = Utility.compactClassName(p,chopit);
  218. result.append(t);
  219. idx=endOfSig;
  220. // we might have finished now, depending on whether this is a parameterized type...
  221. if (parameterized) {
  222. idx++;
  223. result.append("<");
  224. while (signature.charAt(idx)!='>') {
  225. idx+=readClassTypeSignatureFrom(signature,idx,result,chopit);
  226. if (signature.charAt(idx)!='>') result.append(",");
  227. }
  228. result.append(">");idx++;
  229. }
  230. if (signature.charAt(idx)!=';') throw new RuntimeException("Did not find ';' at end of signature, found "+signature.charAt(idx));
  231. idx++;
  232. return idx-posn;
  233. case '[' : // Array declaration
  234. int dim = 0;
  235. while (signature.charAt(idx)=='[') {dim++;idx++;}
  236. idx+=readClassTypeSignatureFrom(signature,idx,result,chopit);
  237. while (dim>0) {result.append("[]");dim--;}
  238. return idx-posn;
  239. default : throw new ClassFormatException("Invalid signature: `" +
  240. signature + "'");
  241. }
  242. } catch(StringIndexOutOfBoundsException e) { // Should never occur
  243. throw new ClassFormatException("Invalid signature: " + e + ":" + signature);
  244. }
  245. }
  246. public static final String readClassTypeSignatureFrom(String signature) {
  247. StringBuffer sb = new StringBuffer();
  248. GenericSignatureParsingTest.readClassTypeSignatureFrom(signature,0,sb,false);
  249. return sb.toString();
  250. }
  251. public static int countBrackets(String brackets) {
  252. char[] chars = brackets.toCharArray();
  253. int count = 0;
  254. boolean open = false;
  255. for (char aChar : chars) {
  256. switch (aChar) {
  257. case '[':
  258. if (open) throw new RuntimeException("Illegally nested brackets:" + brackets);
  259. open = true;
  260. break;
  261. case ']':
  262. if (!open) throw new RuntimeException("Illegally nested brackets:" + brackets);
  263. open = false;
  264. count++;
  265. break;
  266. default:
  267. }
  268. }
  269. if (open) throw new RuntimeException("Illegally nested brackets:" + brackets);
  270. return count;
  271. }
  272. /**
  273. * Parse Java type such as "char", or "java.lang.String[]" and return the
  274. * signature in byte code format, e.g. "C" or "[Ljava/lang/String;" respectively.
  275. *
  276. * @param type Java type
  277. * @return byte code signature
  278. */
  279. public static String getSignature(String type) {
  280. StringBuffer buf = new StringBuffer();
  281. char[] chars = type.toCharArray();
  282. boolean char_found = false, delim = false;
  283. int index = -1;
  284. loop:
  285. for (int i=0; i < chars.length; i++) {
  286. switch (chars[i]) {
  287. case ' ': case '\t': case '\n': case '\r': case '\f':
  288. if (char_found) delim = true;
  289. break;
  290. case '[':
  291. if (!char_found) throw new RuntimeException("Illegal type: " + type);
  292. index = i;
  293. break loop;
  294. default:
  295. char_found = true;
  296. if (!delim) buf.append(chars[i]);
  297. }
  298. }
  299. int brackets = 0;
  300. if(index > 0) brackets = GenericSignatureParsingTest.countBrackets(type.substring(index));
  301. type = buf.toString();
  302. buf.setLength(0);
  303. for (int i=0; i < brackets; i++) buf.append('[');
  304. boolean found = false;
  305. for(int i=Constants.T_BOOLEAN; (i <= Constants.T_VOID) && !found; i++) {
  306. if (Constants.TYPE_NAMES[i].equals(type)) {
  307. found = true;
  308. buf.append(Constants.SHORT_TYPE_NAMES[i]);
  309. }
  310. }
  311. // Class name
  312. if (!found) buf.append('L' + type.replace('.', '/') + ';');
  313. return buf.toString();
  314. }
  315. /**
  316. * For some method signature (class file format) like '([Ljava/lang/String;)Z' this returns
  317. * the string representing the return type its 'normal' form, e.g. 'boolean'
  318. *
  319. * @param signature Method signature
  320. * @param chopit Shorten class names
  321. * @return return type of method
  322. */
  323. public static final String methodSignatureReturnType(String signature,boolean chopit) throws ClassFormatException {
  324. int index;
  325. String type;
  326. try {
  327. // Read return type after `)'
  328. index = signature.lastIndexOf(')') + 1;
  329. type = Utility.signatureToString(signature.substring(index), chopit);
  330. } catch (StringIndexOutOfBoundsException e) {
  331. throw new ClassFormatException("Invalid method signature: " + signature);
  332. }
  333. return type;
  334. }
  335. /**
  336. * For some method signature (class file format) like '([Ljava/lang/String;)Z' this returns
  337. * the string representing the return type its 'normal' form, e.g. 'boolean'
  338. *
  339. * @param signature Method signature
  340. * @return return type of method
  341. * @throws ClassFormatException
  342. */
  343. public static final String methodSignatureReturnType(String signature) throws ClassFormatException {
  344. return GenericSignatureParsingTest.methodSignatureReturnType(signature, true);
  345. }
  346. /**
  347. * For some method signature (class file format) like '([Ljava/lang/String;Z)V' this returns an array
  348. * of strings representing the arguments in their 'normal' form, e.g. '{java.lang.String[],boolean}'
  349. *
  350. * @param signature Method signature
  351. * @param chopit Shorten class names
  352. * @return Array of argument types
  353. */
  354. public static final String[] methodSignatureArgumentTypes(String signature,boolean chopit) throws ClassFormatException {
  355. ArrayList<String> vec = new ArrayList<String>();
  356. int index;
  357. String[] types;
  358. try { // Read all declarations between for `(' and `)'
  359. if (signature.charAt(0) != '(')
  360. throw new ClassFormatException("Invalid method signature: " + signature);
  361. index = 1; // current string position
  362. while(signature.charAt(index) != ')') {
  363. Utility.ResultHolder rh = Utility.signatureToStringInternal(signature.substring(index),chopit);
  364. vec.add(rh.getResult());
  365. index += rh.getConsumedChars();
  366. }
  367. } catch(StringIndexOutOfBoundsException e) {
  368. throw new ClassFormatException("Invalid method signature: " + signature);
  369. }
  370. types = new String[vec.size()];
  371. vec.toArray(types);
  372. return types;
  373. }
  374. /**
  375. * Converts string containing the method return and argument types
  376. * to a byte code method signature.
  377. *
  378. * @param returnType Return type of method (e.g. "char" or "java.lang.String[]")
  379. * @param methodArgs Types of method arguments
  380. * @return Byte code representation of method signature
  381. */
  382. public final static String methodTypeToSignature(String returnType, String[] methodArgs) throws ClassFormatException {
  383. StringBuffer buf = new StringBuffer("(");
  384. if (methodArgs != null) {
  385. for (String methodArg : methodArgs) {
  386. String str = GenericSignatureParsingTest.getSignature(methodArg);
  387. if (str.equals("V")) // void can't be a method argument
  388. throw new ClassFormatException("Invalid type: " + methodArg);
  389. buf.append(str);
  390. }
  391. }
  392. buf.append(")" + GenericSignatureParsingTest.getSignature(returnType));
  393. return buf.toString();
  394. }
  395. }