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.

SignatureAttribute.java 35KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import java.io.DataInputStream;
  18. import java.io.IOException;
  19. import java.util.Map;
  20. import java.util.ArrayList;
  21. import javassist.CtClass;
  22. /**
  23. * <code>Signature_attribute</code>.
  24. */
  25. public class SignatureAttribute extends AttributeInfo {
  26. /**
  27. * The name of this attribute <code>"Signature"</code>.
  28. */
  29. public static final String tag = "Signature";
  30. SignatureAttribute(ConstPool cp, int n, DataInputStream in)
  31. throws IOException
  32. {
  33. super(cp, n, in);
  34. }
  35. /**
  36. * Constructs a <code>Signature</code> attribute.
  37. *
  38. * @param cp a constant pool table.
  39. * @param signature the signature represented by this attribute.
  40. */
  41. public SignatureAttribute(ConstPool cp, String signature) {
  42. super(cp, tag);
  43. int index = cp.addUtf8Info(signature);
  44. byte[] bvalue = new byte[2];
  45. bvalue[0] = (byte)(index >>> 8);
  46. bvalue[1] = (byte)index;
  47. set(bvalue);
  48. }
  49. /**
  50. * Returns the generic signature indicated by <code>signature_index</code>.
  51. *
  52. * @see #toClassSignature(String)
  53. * @see #toMethodSignature(String)
  54. * @see #toFieldSignature(String)
  55. */
  56. public String getSignature() {
  57. return getConstPool().getUtf8Info(ByteArray.readU16bit(get(), 0));
  58. }
  59. /**
  60. * Sets <code>signature_index</code> to the index of the given generic signature,
  61. * which is added to a constant pool.
  62. *
  63. * @param sig new signature.
  64. * @since 3.11
  65. */
  66. public void setSignature(String sig) {
  67. int index = getConstPool().addUtf8Info(sig);
  68. ByteArray.write16bit(index, info, 0);
  69. }
  70. /**
  71. * Makes a copy. Class names are replaced according to the
  72. * given <code>Map</code> object.
  73. *
  74. * @param newCp the constant pool table used by the new copy.
  75. * @param classnames pairs of replaced and substituted
  76. * class names.
  77. */
  78. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  79. return new SignatureAttribute(newCp, getSignature());
  80. }
  81. void renameClass(String oldname, String newname) {
  82. String sig = renameClass(getSignature(), oldname, newname);
  83. setSignature(sig);
  84. }
  85. void renameClass(Map classnames) {
  86. String sig = renameClass(getSignature(), classnames);
  87. setSignature(sig);
  88. }
  89. static String renameClass(String desc, String oldname, String newname) {
  90. Map map = new java.util.HashMap();
  91. map.put(oldname, newname);
  92. return renameClass(desc, map);
  93. }
  94. static String renameClass(String desc, Map map) {
  95. if (map == null)
  96. return desc;
  97. StringBuilder newdesc = new StringBuilder();
  98. int head = 0;
  99. int i = 0;
  100. for (;;) {
  101. int j = desc.indexOf('L', i);
  102. if (j < 0)
  103. break;
  104. StringBuilder nameBuf = new StringBuilder();
  105. int k = j;
  106. char c;
  107. try {
  108. while ((c = desc.charAt(++k)) != ';') {
  109. nameBuf.append(c);
  110. if (c == '<') {
  111. while ((c = desc.charAt(++k)) != '>')
  112. nameBuf.append(c);
  113. nameBuf.append(c);
  114. }
  115. }
  116. }
  117. catch (IndexOutOfBoundsException e) { break; }
  118. i = k + 1;
  119. String name = nameBuf.toString();
  120. String name2 = (String)map.get(name);
  121. if (name2 != null) {
  122. newdesc.append(desc.substring(head, j));
  123. newdesc.append('L');
  124. newdesc.append(name2);
  125. newdesc.append(c);
  126. head = i;
  127. }
  128. }
  129. if (head == 0)
  130. return desc;
  131. else {
  132. int len = desc.length();
  133. if (head < len)
  134. newdesc.append(desc.substring(head, len));
  135. return newdesc.toString();
  136. }
  137. }
  138. private static boolean isNamePart(int c) {
  139. return c != ';' && c != '<';
  140. }
  141. static private class Cursor {
  142. int position = 0;
  143. int indexOf(String s, int ch) throws BadBytecode {
  144. int i = s.indexOf(ch, position);
  145. if (i < 0)
  146. throw error(s);
  147. else {
  148. position = i + 1;
  149. return i;
  150. }
  151. }
  152. }
  153. /**
  154. * Class signature.
  155. */
  156. public static class ClassSignature {
  157. TypeParameter[] params;
  158. ClassType superClass;
  159. ClassType[] interfaces;
  160. /**
  161. * Constructs a class signature.
  162. *
  163. * @param params type parameters.
  164. * @param superClass the super class.
  165. * @param interfaces the interface types.
  166. */
  167. public ClassSignature(TypeParameter[] params, ClassType superClass, ClassType[] interfaces) {
  168. this.params = params == null ? new TypeParameter[0] : params;
  169. this.superClass = superClass == null ? ClassType.OBJECT : superClass;
  170. this.interfaces = interfaces == null ? new ClassType[0] : interfaces;
  171. }
  172. /**
  173. * Constructs a class signature.
  174. *
  175. * @param p type parameters.
  176. */
  177. public ClassSignature(TypeParameter[] p) {
  178. this(p, null, null);
  179. }
  180. /**
  181. * Returns the type parameters.
  182. *
  183. * @return a zero-length array if the type parameters are not specified.
  184. */
  185. public TypeParameter[] getParameters() {
  186. return params;
  187. }
  188. /**
  189. * Returns the super class.
  190. */
  191. public ClassType getSuperClass() { return superClass; }
  192. /**
  193. * Returns the super interfaces.
  194. *
  195. * @return a zero-length array if the super interfaces are not specified.
  196. */
  197. public ClassType[] getInterfaces() { return interfaces; }
  198. /**
  199. * Returns the string representation.
  200. */
  201. public String toString() {
  202. StringBuffer sbuf = new StringBuffer();
  203. TypeParameter.toString(sbuf, params);
  204. sbuf.append(" extends ").append(superClass);
  205. if (interfaces.length > 0) {
  206. sbuf.append(" implements ");
  207. Type.toString(sbuf, interfaces);
  208. }
  209. return sbuf.toString();
  210. }
  211. /**
  212. * Returns the encoded string representing the method type signature.
  213. */
  214. public String encode() {
  215. StringBuffer sbuf = new StringBuffer();
  216. if (params.length > 0) {
  217. sbuf.append('<');
  218. for (int i = 0; i < params.length; i++)
  219. params[i].encode(sbuf);
  220. sbuf.append('>');
  221. }
  222. superClass.encode(sbuf);
  223. for (int i = 0; i < interfaces.length; i++)
  224. interfaces[i].encode(sbuf);
  225. return sbuf.toString();
  226. }
  227. }
  228. /**
  229. * Method type signature.
  230. */
  231. public static class MethodSignature {
  232. TypeParameter[] typeParams;
  233. Type[] params;
  234. Type retType;
  235. ObjectType[] exceptions;
  236. /**
  237. * Constructs a method type signature. Any parameter can be null
  238. * to represent <code>void</code> or nothing.
  239. *
  240. * @param tp type parameters.
  241. * @param params parameter types.
  242. * @param ret a return type, or null if the return type is <code>void</code>.
  243. * @param ex exception types.
  244. */
  245. public MethodSignature(TypeParameter[] tp, Type[] params, Type ret, ObjectType[] ex) {
  246. typeParams = tp == null ? new TypeParameter[0] : tp;
  247. this.params = params == null ? new Type[0] : params;
  248. retType = ret == null ? new BaseType("void") : ret;
  249. exceptions = ex == null ? new ObjectType[0] : ex;
  250. }
  251. /**
  252. * Returns the formal type parameters.
  253. *
  254. * @return a zero-length array if the type parameters are not specified.
  255. */
  256. public TypeParameter[] getTypeParameters() { return typeParams; }
  257. /**
  258. * Returns the types of the formal parameters.
  259. *
  260. * @return a zero-length array if no formal parameter is taken.
  261. */
  262. public Type[] getParameterTypes() { return params; }
  263. /**
  264. * Returns the type of the returned value.
  265. */
  266. public Type getReturnType() { return retType; }
  267. /**
  268. * Returns the types of the exceptions that may be thrown.
  269. *
  270. * @return a zero-length array if exceptions are never thrown or
  271. * the exception types are not parameterized types or type variables.
  272. */
  273. public ObjectType[] getExceptionTypes() { return exceptions; }
  274. /**
  275. * Returns the string representation.
  276. */
  277. public String toString() {
  278. StringBuffer sbuf = new StringBuffer();
  279. TypeParameter.toString(sbuf, typeParams);
  280. sbuf.append(" (");
  281. Type.toString(sbuf, params);
  282. sbuf.append(") ");
  283. sbuf.append(retType);
  284. if (exceptions.length > 0) {
  285. sbuf.append(" throws ");
  286. Type.toString(sbuf, exceptions);
  287. }
  288. return sbuf.toString();
  289. }
  290. /**
  291. * Returns the encoded string representing the method type signature.
  292. */
  293. public String encode() {
  294. StringBuffer sbuf = new StringBuffer();
  295. if (typeParams.length > 0) {
  296. sbuf.append('<');
  297. for (int i = 0; i < typeParams.length; i++)
  298. typeParams[i].encode(sbuf);
  299. sbuf.append('>');
  300. }
  301. sbuf.append('(');
  302. for (int i = 0; i < params.length; i++)
  303. params[i].encode(sbuf);
  304. sbuf.append(')');
  305. retType.encode(sbuf);
  306. if (exceptions.length > 0)
  307. for (int i = 0; i < exceptions.length; i++) {
  308. sbuf.append('^');
  309. exceptions[i].encode(sbuf);
  310. }
  311. return sbuf.toString();
  312. }
  313. }
  314. /**
  315. * Formal type parameters.
  316. *
  317. * @see TypeArgument
  318. */
  319. public static class TypeParameter {
  320. String name;
  321. ObjectType superClass;
  322. ObjectType[] superInterfaces;
  323. TypeParameter(String sig, int nb, int ne, ObjectType sc, ObjectType[] si) {
  324. name = sig.substring(nb, ne);
  325. superClass = sc;
  326. superInterfaces = si;
  327. }
  328. /**
  329. * Constructs a <code>TypeParameter</code> representing a type parametre
  330. * like <code>&lt;T extends ... &gt;</code>.
  331. *
  332. * @param name parameter name.
  333. * @param superClass an upper bound class-type (or null).
  334. * @param superInterfaces an upper bound interface-type (or null).
  335. */
  336. public TypeParameter(String name, ObjectType superClass, ObjectType[] superInterfaces) {
  337. this.name = name;
  338. this.superClass = superClass;
  339. if (superInterfaces == null)
  340. this.superInterfaces = new ObjectType[0];
  341. else
  342. this.superInterfaces = superInterfaces;
  343. }
  344. /**
  345. * Constructs a <code>TypeParameter</code> representing a type parameter
  346. * like <code>&lt;T&gt;</code>.
  347. *
  348. * @param name parameter name.
  349. */
  350. public TypeParameter(String name) {
  351. this(name, null, null);
  352. }
  353. /**
  354. * Returns the name of the type parameter.
  355. */
  356. public String getName() {
  357. return name;
  358. }
  359. /**
  360. * Returns the class bound of this parameter.
  361. */
  362. public ObjectType getClassBound() { return superClass; }
  363. /**
  364. * Returns the interface bound of this parameter.
  365. *
  366. * @return a zero-length array if the interface bound is not specified.
  367. */
  368. public ObjectType[] getInterfaceBound() { return superInterfaces; }
  369. /**
  370. * Returns the string representation.
  371. */
  372. public String toString() {
  373. StringBuffer sbuf = new StringBuffer(getName());
  374. if (superClass != null)
  375. sbuf.append(" extends ").append(superClass.toString());
  376. int len = superInterfaces.length;
  377. if (len > 0) {
  378. for (int i = 0; i < len; i++) {
  379. if (i > 0 || superClass != null)
  380. sbuf.append(" & ");
  381. else
  382. sbuf.append(" extends ");
  383. sbuf.append(superInterfaces[i].toString());
  384. }
  385. }
  386. return sbuf.toString();
  387. }
  388. static void toString(StringBuffer sbuf, TypeParameter[] tp) {
  389. sbuf.append('<');
  390. for (int i = 0; i < tp.length; i++) {
  391. if (i > 0)
  392. sbuf.append(", ");
  393. sbuf.append(tp[i]);
  394. }
  395. sbuf.append('>');
  396. }
  397. void encode(StringBuffer sb) {
  398. sb.append(name);
  399. if (superClass == null)
  400. sb.append(":Ljava/lang/Object;");
  401. else {
  402. sb.append(':');
  403. superClass.encode(sb);
  404. }
  405. for (int i = 0; i < superInterfaces.length; i++) {
  406. sb.append(':');
  407. superInterfaces[i].encode(sb);
  408. }
  409. }
  410. }
  411. /**
  412. * Type argument.
  413. *
  414. * @see TypeParameter
  415. */
  416. public static class TypeArgument {
  417. ObjectType arg;
  418. char wildcard;
  419. TypeArgument(ObjectType a, char w) {
  420. arg = a;
  421. wildcard = w;
  422. }
  423. /**
  424. * Constructs a <code>TypeArgument</code>.
  425. * A type argument is <code>&lt;String&gt;</code>, <code>&lt;int[]&gt;</code>,
  426. * or a type variable <code>&lt;T&gt;</code>, etc.
  427. *
  428. * @param t a class type, an array type, or a type variable.
  429. */
  430. public TypeArgument(ObjectType t) {
  431. this(t, ' ');
  432. }
  433. /**
  434. * Constructs a <code>TypeArgument</code> representing <code>&lt;?&gt;</code>.
  435. */
  436. public TypeArgument() {
  437. this(null, '*');
  438. }
  439. /**
  440. * A factory method constructing a <code>TypeArgument</code> with an upper bound.
  441. * It represents <code>&lt;? extends ... &gt;</code>
  442. *
  443. * @param t an upper bound type.
  444. */
  445. public static TypeArgument subclassOf(ObjectType t) {
  446. return new TypeArgument(t, '+');
  447. }
  448. /**
  449. * A factory method constructing a <code>TypeArgument</code> with an lower bound.
  450. * It represents <code>&lt;? super ... &gt;</code>
  451. *
  452. * @param t an lower bbound type.
  453. */
  454. public static TypeArgument superOf(ObjectType t) {
  455. return new TypeArgument(t, '-');
  456. }
  457. /**
  458. * Returns the kind of this type argument.
  459. *
  460. * @return <code>' '</code> (not-wildcard), <code>'*'</code> (wildcard), <code>'+'</code> (wildcard with
  461. * upper bound), or <code>'-'</code> (wildcard with lower bound).
  462. */
  463. public char getKind() { return wildcard; }
  464. /**
  465. * Returns true if this type argument is a wildcard type
  466. * such as <code>?</code>, <code>? extends String</code>, or <code>? super Integer</code>.
  467. */
  468. public boolean isWildcard() { return wildcard != ' '; }
  469. /**
  470. * Returns the type represented by this argument
  471. * if the argument is not a wildcard type. Otherwise, this method
  472. * returns the upper bound (if the kind is '+'),
  473. * the lower bound (if the kind is '-'), or null (if the upper or lower
  474. * bound is not specified).
  475. */
  476. public ObjectType getType() { return arg; }
  477. /**
  478. * Returns the string representation.
  479. */
  480. public String toString() {
  481. if (wildcard == '*')
  482. return "?";
  483. String type = arg.toString();
  484. if (wildcard == ' ')
  485. return type;
  486. else if (wildcard == '+')
  487. return "? extends " + type;
  488. else
  489. return "? super " + type;
  490. }
  491. static void encode(StringBuffer sb, TypeArgument[] args) {
  492. sb.append('<');
  493. for (int i = 0; i < args.length; i++) {
  494. TypeArgument ta = args[i];
  495. if (ta.isWildcard())
  496. sb.append(ta.wildcard);
  497. if (ta.getType() != null)
  498. ta.getType().encode(sb);
  499. }
  500. sb.append('>');
  501. }
  502. }
  503. /**
  504. * Primitive types and object types.
  505. */
  506. public static abstract class Type {
  507. abstract void encode(StringBuffer sb);
  508. static void toString(StringBuffer sbuf, Type[] ts) {
  509. for (int i = 0; i < ts.length; i++) {
  510. if (i > 0)
  511. sbuf.append(", ");
  512. sbuf.append(ts[i]);
  513. }
  514. }
  515. /**
  516. * Returns the type name in the JVM internal style.
  517. * For example, if the type is a nested class {@code foo.Bar.Baz},
  518. * then {@code foo.Bar$Baz} is returned.
  519. */
  520. public String jvmTypeName() { return toString(); }
  521. }
  522. /**
  523. * Primitive types.
  524. */
  525. public static class BaseType extends Type {
  526. char descriptor;
  527. BaseType(char c) { descriptor = c; }
  528. /**
  529. * Constructs a <code>BaseType</code>.
  530. *
  531. * @param typeName <code>void</code>, <code>int</code>, ...
  532. */
  533. public BaseType(String typeName) {
  534. this(Descriptor.of(typeName).charAt(0));
  535. }
  536. /**
  537. * Returns the descriptor representing this primitive type.
  538. *
  539. * @see javassist.bytecode.Descriptor
  540. */
  541. public char getDescriptor() { return descriptor; }
  542. /**
  543. * Returns the <code>CtClass</code> representing this
  544. * primitive type.
  545. */
  546. public CtClass getCtlass() {
  547. return Descriptor.toPrimitiveClass(descriptor);
  548. }
  549. /**
  550. * Returns the string representation.
  551. */
  552. public String toString() {
  553. return Descriptor.toClassName(Character.toString(descriptor));
  554. }
  555. void encode(StringBuffer sb) {
  556. sb.append(descriptor);
  557. }
  558. }
  559. /**
  560. * Class types, array types, and type variables.
  561. * This class is also used for representing a field type.
  562. */
  563. public static abstract class ObjectType extends Type {
  564. /**
  565. * Returns the encoded string representing the object type signature.
  566. */
  567. public String encode() {
  568. StringBuffer sb = new StringBuffer();
  569. encode(sb);
  570. return sb.toString();
  571. }
  572. }
  573. /**
  574. * Class types.
  575. */
  576. public static class ClassType extends ObjectType {
  577. String name;
  578. TypeArgument[] arguments;
  579. static ClassType make(String s, int b, int e,
  580. TypeArgument[] targs, ClassType parent) {
  581. if (parent == null)
  582. return new ClassType(s, b, e, targs);
  583. else
  584. return new NestedClassType(s, b, e, targs, parent);
  585. }
  586. ClassType(String signature, int begin, int end, TypeArgument[] targs) {
  587. name = signature.substring(begin, end).replace('/', '.');
  588. arguments = targs;
  589. }
  590. /**
  591. * A class type representing <code>java.lang.Object</code>.
  592. */
  593. public static ClassType OBJECT = new ClassType("java.lang.Object", null);
  594. /**
  595. * Constructs a <code>ClassType</code>. It represents
  596. * the name of a non-nested class.
  597. *
  598. * @param className a fully qualified class name.
  599. * @param args type arguments or null.
  600. */
  601. public ClassType(String className, TypeArgument[] args) {
  602. name = className;
  603. arguments = args;
  604. }
  605. /**
  606. * Constructs a <code>ClassType</code>. It represents
  607. * the name of a non-nested class.
  608. *
  609. * @param className a fully qualified class name.
  610. */
  611. public ClassType(String className) {
  612. this(className, null);
  613. }
  614. /**
  615. * Returns the class name.
  616. */
  617. public String getName() {
  618. return name;
  619. }
  620. /**
  621. * Returns the type arguments.
  622. *
  623. * @return null if no type arguments are given to this class.
  624. */
  625. public TypeArgument[] getTypeArguments() { return arguments; }
  626. /**
  627. * If this class is a member of another class, returns the
  628. * class in which this class is declared.
  629. *
  630. * @return null if this class is not a member of another class.
  631. */
  632. public ClassType getDeclaringClass() { return null; }
  633. /**
  634. * Returns the string representation.
  635. */
  636. public String toString() {
  637. StringBuffer sbuf = new StringBuffer();
  638. ClassType parent = getDeclaringClass();
  639. if (parent != null)
  640. sbuf.append(parent.toString()).append('.');
  641. return toString2(sbuf);
  642. }
  643. private String toString2(StringBuffer sbuf) {
  644. sbuf.append(name);
  645. if (arguments != null) {
  646. sbuf.append('<');
  647. int n = arguments.length;
  648. for (int i = 0; i < n; i++) {
  649. if (i > 0)
  650. sbuf.append(", ");
  651. sbuf.append(arguments[i].toString());
  652. }
  653. sbuf.append('>');
  654. }
  655. return sbuf.toString();
  656. }
  657. /**
  658. * Returns the type name in the JVM internal style.
  659. * For example, if the type is a nested class {@code foo.Bar.Baz},
  660. * then {@code foo.Bar$Baz} is returned.
  661. */
  662. public String jvmTypeName() {
  663. StringBuffer sbuf = new StringBuffer();
  664. ClassType parent = getDeclaringClass();
  665. if (parent != null)
  666. sbuf.append(parent.jvmTypeName()).append('$');
  667. return toString2(sbuf);
  668. }
  669. void encode(StringBuffer sb) {
  670. sb.append('L');
  671. encode2(sb);
  672. sb.append(';');
  673. }
  674. void encode2(StringBuffer sb) {
  675. ClassType parent = getDeclaringClass();
  676. if (parent != null) {
  677. parent.encode2(sb);
  678. sb.append('$');
  679. }
  680. sb.append(name.replace('.', '/'));
  681. if (arguments != null)
  682. TypeArgument.encode(sb, arguments);
  683. }
  684. }
  685. /**
  686. * Nested class types.
  687. */
  688. public static class NestedClassType extends ClassType {
  689. ClassType parent;
  690. NestedClassType(String s, int b, int e,
  691. TypeArgument[] targs, ClassType p) {
  692. super(s, b, e, targs);
  693. parent = p;
  694. }
  695. /**
  696. * Constructs a <code>NestedClassType</code>.
  697. *
  698. * @param parent the class surrounding this class type.
  699. * @param className a simple class name. It does not include
  700. * a package name or a parent's class name.
  701. * @param args type parameters or null.
  702. */
  703. public NestedClassType(ClassType parent, String className, TypeArgument[] args) {
  704. super(className, args);
  705. this.parent = parent;
  706. }
  707. /**
  708. * Returns the class that declares this nested class.
  709. * This nested class is a member of that declaring class.
  710. */
  711. public ClassType getDeclaringClass() { return parent; }
  712. }
  713. /**
  714. * Array types.
  715. */
  716. public static class ArrayType extends ObjectType {
  717. int dim;
  718. Type componentType;
  719. /**
  720. * Constructs an <code>ArrayType</code>.
  721. *
  722. * @param d dimension.
  723. * @param comp the component type.
  724. */
  725. public ArrayType(int d, Type comp) {
  726. dim = d;
  727. componentType = comp;
  728. }
  729. /**
  730. * Returns the dimension of the array.
  731. */
  732. public int getDimension() { return dim; }
  733. /**
  734. * Returns the component type.
  735. */
  736. public Type getComponentType() {
  737. return componentType;
  738. }
  739. /**
  740. * Returns the string representation.
  741. */
  742. public String toString() {
  743. StringBuffer sbuf = new StringBuffer(componentType.toString());
  744. for (int i = 0; i < dim; i++)
  745. sbuf.append("[]");
  746. return sbuf.toString();
  747. }
  748. void encode(StringBuffer sb) {
  749. for (int i = 0; i < dim; i++)
  750. sb.append('[');
  751. componentType.encode(sb);
  752. }
  753. }
  754. /**
  755. * Type variables.
  756. */
  757. public static class TypeVariable extends ObjectType {
  758. String name;
  759. TypeVariable(String sig, int begin, int end) {
  760. name = sig.substring(begin, end);
  761. }
  762. /**
  763. * Constructs a <code>TypeVariable</code>.
  764. *
  765. * @param name the name of a type variable.
  766. */
  767. public TypeVariable(String name) {
  768. this.name = name;
  769. }
  770. /**
  771. * Returns the variable name.
  772. */
  773. public String getName() {
  774. return name;
  775. }
  776. /**
  777. * Returns the string representation.
  778. */
  779. public String toString() {
  780. return name;
  781. }
  782. void encode(StringBuffer sb) {
  783. sb.append('T').append(name).append(';');
  784. }
  785. }
  786. /**
  787. * Parses the given signature string as a class signature.
  788. *
  789. * @param sig the signature obtained from the <code>SignatureAttribute</code>
  790. * of a <code>ClassFile</code>.
  791. * @return a tree-like data structure representing a class signature. It provides
  792. * convenient accessor methods.
  793. * @throws BadBytecode thrown when a syntactical error is found.
  794. * @see #getSignature()
  795. * @since 3.5
  796. */
  797. public static ClassSignature toClassSignature(String sig) throws BadBytecode {
  798. try {
  799. return parseSig(sig);
  800. }
  801. catch (IndexOutOfBoundsException e) {
  802. throw error(sig);
  803. }
  804. }
  805. /**
  806. * Parses the given signature string as a method type signature.
  807. *
  808. * @param sig the signature obtained from the <code>SignatureAttribute</code>
  809. * of a <code>MethodInfo</code>.
  810. * @return @return a tree-like data structure representing a method signature. It provides
  811. * convenient accessor methods.
  812. * @throws BadBytecode thrown when a syntactical error is found.
  813. * @see #getSignature()
  814. * @since 3.5
  815. */
  816. public static MethodSignature toMethodSignature(String sig) throws BadBytecode {
  817. try {
  818. return parseMethodSig(sig);
  819. }
  820. catch (IndexOutOfBoundsException e) {
  821. throw error(sig);
  822. }
  823. }
  824. /**
  825. * Parses the given signature string as a field type signature.
  826. *
  827. * @param sig the signature string obtained from the <code>SignatureAttribute</code>
  828. * of a <code>FieldInfo</code>.
  829. * @return the field type signature.
  830. * @throws BadBytecode thrown when a syntactical error is found.
  831. * @see #getSignature()
  832. * @since 3.5
  833. */
  834. public static ObjectType toFieldSignature(String sig) throws BadBytecode {
  835. try {
  836. return parseObjectType(sig, new Cursor(), false);
  837. }
  838. catch (IndexOutOfBoundsException e) {
  839. throw error(sig);
  840. }
  841. }
  842. /**
  843. * Parses the given signature string as a type signature.
  844. * The type signature is either the field type signature or a base type
  845. * descriptor including <code>void</code> type.
  846. *
  847. * @throws BadBytecode thrown when a syntactical error is found.
  848. * @since 3.18
  849. */
  850. public static Type toTypeSignature(String sig) throws BadBytecode {
  851. try {
  852. return parseType(sig, new Cursor());
  853. }
  854. catch (IndexOutOfBoundsException e) {
  855. throw error(sig);
  856. }
  857. }
  858. private static ClassSignature parseSig(String sig)
  859. throws BadBytecode, IndexOutOfBoundsException
  860. {
  861. Cursor cur = new Cursor();
  862. TypeParameter[] tp = parseTypeParams(sig, cur);
  863. ClassType superClass = parseClassType(sig, cur);
  864. int sigLen = sig.length();
  865. ArrayList ifArray = new ArrayList();
  866. while (cur.position < sigLen && sig.charAt(cur.position) == 'L')
  867. ifArray.add(parseClassType(sig, cur));
  868. ClassType[] ifs
  869. = (ClassType[])ifArray.toArray(new ClassType[ifArray.size()]);
  870. return new ClassSignature(tp, superClass, ifs);
  871. }
  872. private static MethodSignature parseMethodSig(String sig)
  873. throws BadBytecode
  874. {
  875. Cursor cur = new Cursor();
  876. TypeParameter[] tp = parseTypeParams(sig, cur);
  877. if (sig.charAt(cur.position++) != '(')
  878. throw error(sig);
  879. ArrayList params = new ArrayList();
  880. while (sig.charAt(cur.position) != ')') {
  881. Type t = parseType(sig, cur);
  882. params.add(t);
  883. }
  884. cur.position++;
  885. Type ret = parseType(sig, cur);
  886. int sigLen = sig.length();
  887. ArrayList exceptions = new ArrayList();
  888. while (cur.position < sigLen && sig.charAt(cur.position) == '^') {
  889. cur.position++;
  890. ObjectType t = parseObjectType(sig, cur, false);
  891. if (t instanceof ArrayType)
  892. throw error(sig);
  893. exceptions.add(t);
  894. }
  895. Type[] p = (Type[])params.toArray(new Type[params.size()]);
  896. ObjectType[] ex = (ObjectType[])exceptions.toArray(new ObjectType[exceptions.size()]);
  897. return new MethodSignature(tp, p, ret, ex);
  898. }
  899. private static TypeParameter[] parseTypeParams(String sig, Cursor cur)
  900. throws BadBytecode
  901. {
  902. ArrayList typeParam = new ArrayList();
  903. if (sig.charAt(cur.position) == '<') {
  904. cur.position++;
  905. while (sig.charAt(cur.position) != '>') {
  906. int nameBegin = cur.position;
  907. int nameEnd = cur.indexOf(sig, ':');
  908. ObjectType classBound = parseObjectType(sig, cur, true);
  909. ArrayList ifBound = new ArrayList();
  910. while (sig.charAt(cur.position) == ':') {
  911. cur.position++;
  912. ObjectType t = parseObjectType(sig, cur, false);
  913. ifBound.add(t);
  914. }
  915. TypeParameter p = new TypeParameter(sig, nameBegin, nameEnd,
  916. classBound, (ObjectType[])ifBound.toArray(new ObjectType[ifBound.size()]));
  917. typeParam.add(p);
  918. }
  919. cur.position++;
  920. }
  921. return (TypeParameter[])typeParam.toArray(new TypeParameter[typeParam.size()]);
  922. }
  923. private static ObjectType parseObjectType(String sig, Cursor c, boolean dontThrow)
  924. throws BadBytecode
  925. {
  926. int i;
  927. int begin = c.position;
  928. switch (sig.charAt(begin)) {
  929. case 'L' :
  930. return parseClassType2(sig, c, null);
  931. case 'T' :
  932. i = c.indexOf(sig, ';');
  933. return new TypeVariable(sig, begin + 1, i);
  934. case '[' :
  935. return parseArray(sig, c);
  936. default :
  937. if (dontThrow)
  938. return null;
  939. else
  940. throw error(sig);
  941. }
  942. }
  943. private static ClassType parseClassType(String sig, Cursor c)
  944. throws BadBytecode
  945. {
  946. if (sig.charAt(c.position) == 'L')
  947. return parseClassType2(sig, c, null);
  948. else
  949. throw error(sig);
  950. }
  951. private static ClassType parseClassType2(String sig, Cursor c, ClassType parent)
  952. throws BadBytecode
  953. {
  954. int start = ++c.position;
  955. char t;
  956. do {
  957. t = sig.charAt(c.position++);
  958. } while (t != '$' && t != '<' && t != ';');
  959. int end = c.position - 1;
  960. TypeArgument[] targs;
  961. if (t == '<') {
  962. targs = parseTypeArgs(sig, c);
  963. t = sig.charAt(c.position++);
  964. }
  965. else
  966. targs = null;
  967. ClassType thisClass = ClassType.make(sig, start, end, targs, parent);
  968. if (t == '$' || t == '.') {
  969. c.position--;
  970. return parseClassType2(sig, c, thisClass);
  971. }
  972. else
  973. return thisClass;
  974. }
  975. private static TypeArgument[] parseTypeArgs(String sig, Cursor c) throws BadBytecode {
  976. ArrayList args = new ArrayList();
  977. char t;
  978. while ((t = sig.charAt(c.position++)) != '>') {
  979. TypeArgument ta;
  980. if (t == '*' )
  981. ta = new TypeArgument(null, '*');
  982. else {
  983. if (t != '+' && t != '-') {
  984. t = ' ';
  985. c.position--;
  986. }
  987. ta = new TypeArgument(parseObjectType(sig, c, false), t);
  988. }
  989. args.add(ta);
  990. }
  991. return (TypeArgument[])args.toArray(new TypeArgument[args.size()]);
  992. }
  993. private static ObjectType parseArray(String sig, Cursor c) throws BadBytecode {
  994. int dim = 1;
  995. while (sig.charAt(++c.position) == '[')
  996. dim++;
  997. return new ArrayType(dim, parseType(sig, c));
  998. }
  999. private static Type parseType(String sig, Cursor c) throws BadBytecode {
  1000. Type t = parseObjectType(sig, c, true);
  1001. if (t == null)
  1002. t = new BaseType(sig.charAt(c.position++));
  1003. return t;
  1004. }
  1005. private static BadBytecode error(String sig) {
  1006. return new BadBytecode("bad signature: " + sig);
  1007. }
  1008. }