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 34KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. /**
  517. * Primitive types.
  518. */
  519. public static class BaseType extends Type {
  520. char descriptor;
  521. BaseType(char c) { descriptor = c; }
  522. /**
  523. * Constructs a <code>BaseType</code>.
  524. *
  525. * @param typeName <code>void</code>, <code>int</code>, ...
  526. */
  527. public BaseType(String typeName) {
  528. this(Descriptor.of(typeName).charAt(0));
  529. }
  530. /**
  531. * Returns the descriptor representing this primitive type.
  532. *
  533. * @see javassist.bytecode.Descriptor
  534. */
  535. public char getDescriptor() { return descriptor; }
  536. /**
  537. * Returns the <code>CtClass</code> representing this
  538. * primitive type.
  539. */
  540. public CtClass getCtlass() {
  541. return Descriptor.toPrimitiveClass(descriptor);
  542. }
  543. /**
  544. * Returns the string representation.
  545. */
  546. public String toString() {
  547. return Descriptor.toClassName(Character.toString(descriptor));
  548. }
  549. void encode(StringBuffer sb) {
  550. sb.append(descriptor);
  551. }
  552. }
  553. /**
  554. * Class types, array types, and type variables.
  555. * This class is also used for representing a field type.
  556. */
  557. public static abstract class ObjectType extends Type {
  558. /**
  559. * Returns the encoded string representing the object type signature.
  560. */
  561. public String encode() {
  562. StringBuffer sb = new StringBuffer();
  563. encode(sb);
  564. return sb.toString();
  565. }
  566. }
  567. /**
  568. * Class types.
  569. */
  570. public static class ClassType extends ObjectType {
  571. String name;
  572. TypeArgument[] arguments;
  573. static ClassType make(String s, int b, int e,
  574. TypeArgument[] targs, ClassType parent) {
  575. if (parent == null)
  576. return new ClassType(s, b, e, targs);
  577. else
  578. return new NestedClassType(s, b, e, targs, parent);
  579. }
  580. ClassType(String signature, int begin, int end, TypeArgument[] targs) {
  581. name = signature.substring(begin, end).replace('/', '.');
  582. arguments = targs;
  583. }
  584. /**
  585. * A class type representing <code>java.lang.Object</code>.
  586. */
  587. public static ClassType OBJECT = new ClassType("java.lang.Object", null);
  588. /**
  589. * Constructs a <code>ClassType</code>. It represents
  590. * the name of a non-nested class.
  591. *
  592. * @param className a fully qualified class name.
  593. * @param args type arguments or null.
  594. */
  595. public ClassType(String className, TypeArgument[] args) {
  596. name = className;
  597. arguments = args;
  598. }
  599. /**
  600. * Constructs a <code>ClassType</code>. It represents
  601. * the name of a non-nested class.
  602. *
  603. * @param className a fully qualified class name.
  604. */
  605. public ClassType(String className) {
  606. this(className, null);
  607. }
  608. /**
  609. * Returns the class name.
  610. */
  611. public String getName() {
  612. return name;
  613. }
  614. /**
  615. * Returns the type arguments.
  616. *
  617. * @return null if no type arguments are given to this class.
  618. */
  619. public TypeArgument[] getTypeArguments() { return arguments; }
  620. /**
  621. * If this class is a member of another class, returns the
  622. * class in which this class is declared.
  623. *
  624. * @return null if this class is not a member of another class.
  625. */
  626. public ClassType getDeclaringClass() { return null; }
  627. /**
  628. * Returns the string representation.
  629. */
  630. public String toString() {
  631. StringBuffer sbuf = new StringBuffer();
  632. ClassType parent = getDeclaringClass();
  633. if (parent != null)
  634. sbuf.append(parent.toString()).append('.');
  635. sbuf.append(name);
  636. if (arguments != null) {
  637. sbuf.append('<');
  638. int n = arguments.length;
  639. for (int i = 0; i < n; i++) {
  640. if (i > 0)
  641. sbuf.append(", ");
  642. sbuf.append(arguments[i].toString());
  643. }
  644. sbuf.append('>');
  645. }
  646. return sbuf.toString();
  647. }
  648. void encode(StringBuffer sb) {
  649. sb.append('L');
  650. encode2(sb);
  651. sb.append(';');
  652. }
  653. void encode2(StringBuffer sb) {
  654. ClassType parent = getDeclaringClass();
  655. if (parent != null) {
  656. parent.encode2(sb);
  657. sb.append('$');
  658. }
  659. sb.append(name.replace('.', '/'));
  660. if (arguments != null)
  661. TypeArgument.encode(sb, arguments);
  662. }
  663. }
  664. /**
  665. * Nested class types.
  666. */
  667. public static class NestedClassType extends ClassType {
  668. ClassType parent;
  669. NestedClassType(String s, int b, int e,
  670. TypeArgument[] targs, ClassType p) {
  671. super(s, b, e, targs);
  672. parent = p;
  673. }
  674. /**
  675. * Constructs a <code>NestedClassType</code>.
  676. *
  677. * @param parent the class surrounding this class type.
  678. * @param className a simple class name. It does not include
  679. * a package name or a parent's class name.
  680. * @param args type parameters or null.
  681. */
  682. public NestedClassType(ClassType parent, String className, TypeArgument[] args) {
  683. super(className, args);
  684. this.parent = parent;
  685. }
  686. /**
  687. * Returns the class that declares this nested class.
  688. * This nested class is a member of that declaring class.
  689. */
  690. public ClassType getDeclaringClass() { return parent; }
  691. }
  692. /**
  693. * Array types.
  694. */
  695. public static class ArrayType extends ObjectType {
  696. int dim;
  697. Type componentType;
  698. /**
  699. * Constructs an <code>ArrayType</code>.
  700. *
  701. * @param d dimension.
  702. * @param comp the component type.
  703. */
  704. public ArrayType(int d, Type comp) {
  705. dim = d;
  706. componentType = comp;
  707. }
  708. /**
  709. * Returns the dimension of the array.
  710. */
  711. public int getDimension() { return dim; }
  712. /**
  713. * Returns the component type.
  714. */
  715. public Type getComponentType() {
  716. return componentType;
  717. }
  718. /**
  719. * Returns the string representation.
  720. */
  721. public String toString() {
  722. StringBuffer sbuf = new StringBuffer(componentType.toString());
  723. for (int i = 0; i < dim; i++)
  724. sbuf.append("[]");
  725. return sbuf.toString();
  726. }
  727. void encode(StringBuffer sb) {
  728. for (int i = 0; i < dim; i++)
  729. sb.append('[');
  730. componentType.encode(sb);
  731. }
  732. }
  733. /**
  734. * Type variables.
  735. */
  736. public static class TypeVariable extends ObjectType {
  737. String name;
  738. TypeVariable(String sig, int begin, int end) {
  739. name = sig.substring(begin, end);
  740. }
  741. /**
  742. * Constructs a <code>TypeVariable</code>.
  743. *
  744. * @param name the name of a type variable.
  745. */
  746. public TypeVariable(String name) {
  747. this.name = name;
  748. }
  749. /**
  750. * Returns the variable name.
  751. */
  752. public String getName() {
  753. return name;
  754. }
  755. /**
  756. * Returns the string representation.
  757. */
  758. public String toString() {
  759. return name;
  760. }
  761. void encode(StringBuffer sb) {
  762. sb.append('T').append(name).append(';');
  763. }
  764. }
  765. /**
  766. * Parses the given signature string as a class signature.
  767. *
  768. * @param sig the signature obtained from the <code>SignatureAttribute</code>
  769. * of a <code>ClassFile</code>.
  770. * @return a tree-like data structure representing a class signature. It provides
  771. * convenient accessor methods.
  772. * @throws BadBytecode thrown when a syntactical error is found.
  773. * @see #getSignature()
  774. * @since 3.5
  775. */
  776. public static ClassSignature toClassSignature(String sig) throws BadBytecode {
  777. try {
  778. return parseSig(sig);
  779. }
  780. catch (IndexOutOfBoundsException e) {
  781. throw error(sig);
  782. }
  783. }
  784. /**
  785. * Parses the given signature string as a method type signature.
  786. *
  787. * @param sig the signature obtained from the <code>SignatureAttribute</code>
  788. * of a <code>MethodInfo</code>.
  789. * @return @return a tree-like data structure representing a method signature. It provides
  790. * convenient accessor methods.
  791. * @throws BadBytecode thrown when a syntactical error is found.
  792. * @see #getSignature()
  793. * @since 3.5
  794. */
  795. public static MethodSignature toMethodSignature(String sig) throws BadBytecode {
  796. try {
  797. return parseMethodSig(sig);
  798. }
  799. catch (IndexOutOfBoundsException e) {
  800. throw error(sig);
  801. }
  802. }
  803. /**
  804. * Parses the given signature string as a field type signature.
  805. *
  806. * @param sig the signature string obtained from the <code>SignatureAttribute</code>
  807. * of a <code>FieldInfo</code>.
  808. * @return the field type signature.
  809. * @throws BadBytecode thrown when a syntactical error is found.
  810. * @see #getSignature()
  811. * @since 3.5
  812. */
  813. public static ObjectType toFieldSignature(String sig) throws BadBytecode {
  814. try {
  815. return parseObjectType(sig, new Cursor(), false);
  816. }
  817. catch (IndexOutOfBoundsException e) {
  818. throw error(sig);
  819. }
  820. }
  821. /**
  822. * Parses the given signature string as a type signature.
  823. * The type signature is either the field type signature or a base type
  824. * descriptor including <code>void</code> type.
  825. *
  826. * @throws BadBytecode thrown when a syntactical error is found.
  827. * @since 3.18
  828. */
  829. public static Type toTypeSignature(String sig) throws BadBytecode {
  830. try {
  831. return parseType(sig, new Cursor());
  832. }
  833. catch (IndexOutOfBoundsException e) {
  834. throw error(sig);
  835. }
  836. }
  837. private static ClassSignature parseSig(String sig)
  838. throws BadBytecode, IndexOutOfBoundsException
  839. {
  840. Cursor cur = new Cursor();
  841. TypeParameter[] tp = parseTypeParams(sig, cur);
  842. ClassType superClass = parseClassType(sig, cur);
  843. int sigLen = sig.length();
  844. ArrayList ifArray = new ArrayList();
  845. while (cur.position < sigLen && sig.charAt(cur.position) == 'L')
  846. ifArray.add(parseClassType(sig, cur));
  847. ClassType[] ifs
  848. = (ClassType[])ifArray.toArray(new ClassType[ifArray.size()]);
  849. return new ClassSignature(tp, superClass, ifs);
  850. }
  851. private static MethodSignature parseMethodSig(String sig)
  852. throws BadBytecode
  853. {
  854. Cursor cur = new Cursor();
  855. TypeParameter[] tp = parseTypeParams(sig, cur);
  856. if (sig.charAt(cur.position++) != '(')
  857. throw error(sig);
  858. ArrayList params = new ArrayList();
  859. while (sig.charAt(cur.position) != ')') {
  860. Type t = parseType(sig, cur);
  861. params.add(t);
  862. }
  863. cur.position++;
  864. Type ret = parseType(sig, cur);
  865. int sigLen = sig.length();
  866. ArrayList exceptions = new ArrayList();
  867. while (cur.position < sigLen && sig.charAt(cur.position) == '^') {
  868. cur.position++;
  869. ObjectType t = parseObjectType(sig, cur, false);
  870. if (t instanceof ArrayType)
  871. throw error(sig);
  872. exceptions.add(t);
  873. }
  874. Type[] p = (Type[])params.toArray(new Type[params.size()]);
  875. ObjectType[] ex = (ObjectType[])exceptions.toArray(new ObjectType[exceptions.size()]);
  876. return new MethodSignature(tp, p, ret, ex);
  877. }
  878. private static TypeParameter[] parseTypeParams(String sig, Cursor cur)
  879. throws BadBytecode
  880. {
  881. ArrayList typeParam = new ArrayList();
  882. if (sig.charAt(cur.position) == '<') {
  883. cur.position++;
  884. while (sig.charAt(cur.position) != '>') {
  885. int nameBegin = cur.position;
  886. int nameEnd = cur.indexOf(sig, ':');
  887. ObjectType classBound = parseObjectType(sig, cur, true);
  888. ArrayList ifBound = new ArrayList();
  889. while (sig.charAt(cur.position) == ':') {
  890. cur.position++;
  891. ObjectType t = parseObjectType(sig, cur, false);
  892. ifBound.add(t);
  893. }
  894. TypeParameter p = new TypeParameter(sig, nameBegin, nameEnd,
  895. classBound, (ObjectType[])ifBound.toArray(new ObjectType[ifBound.size()]));
  896. typeParam.add(p);
  897. }
  898. cur.position++;
  899. }
  900. return (TypeParameter[])typeParam.toArray(new TypeParameter[typeParam.size()]);
  901. }
  902. private static ObjectType parseObjectType(String sig, Cursor c, boolean dontThrow)
  903. throws BadBytecode
  904. {
  905. int i;
  906. int begin = c.position;
  907. switch (sig.charAt(begin)) {
  908. case 'L' :
  909. return parseClassType2(sig, c, null);
  910. case 'T' :
  911. i = c.indexOf(sig, ';');
  912. return new TypeVariable(sig, begin + 1, i);
  913. case '[' :
  914. return parseArray(sig, c);
  915. default :
  916. if (dontThrow)
  917. return null;
  918. else
  919. throw error(sig);
  920. }
  921. }
  922. private static ClassType parseClassType(String sig, Cursor c)
  923. throws BadBytecode
  924. {
  925. if (sig.charAt(c.position) == 'L')
  926. return parseClassType2(sig, c, null);
  927. else
  928. throw error(sig);
  929. }
  930. private static ClassType parseClassType2(String sig, Cursor c, ClassType parent)
  931. throws BadBytecode
  932. {
  933. int start = ++c.position;
  934. char t;
  935. do {
  936. t = sig.charAt(c.position++);
  937. } while (t != '$' && t != '<' && t != ';');
  938. int end = c.position - 1;
  939. TypeArgument[] targs;
  940. if (t == '<') {
  941. targs = parseTypeArgs(sig, c);
  942. t = sig.charAt(c.position++);
  943. }
  944. else
  945. targs = null;
  946. ClassType thisClass = ClassType.make(sig, start, end, targs, parent);
  947. if (t == '$' || t == '.') {
  948. c.position--;
  949. return parseClassType2(sig, c, thisClass);
  950. }
  951. else
  952. return thisClass;
  953. }
  954. private static TypeArgument[] parseTypeArgs(String sig, Cursor c) throws BadBytecode {
  955. ArrayList args = new ArrayList();
  956. char t;
  957. while ((t = sig.charAt(c.position++)) != '>') {
  958. TypeArgument ta;
  959. if (t == '*' )
  960. ta = new TypeArgument(null, '*');
  961. else {
  962. if (t != '+' && t != '-') {
  963. t = ' ';
  964. c.position--;
  965. }
  966. ta = new TypeArgument(parseObjectType(sig, c, false), t);
  967. }
  968. args.add(ta);
  969. }
  970. return (TypeArgument[])args.toArray(new TypeArgument[args.size()]);
  971. }
  972. private static ObjectType parseArray(String sig, Cursor c) throws BadBytecode {
  973. int dim = 1;
  974. while (sig.charAt(++c.position) == '[')
  975. dim++;
  976. return new ArrayType(dim, parseType(sig, c));
  977. }
  978. private static Type parseType(String sig, Cursor c) throws BadBytecode {
  979. Type t = parseObjectType(sig, c, true);
  980. if (t == null)
  981. t = new BaseType(sig.charAt(c.position++));
  982. return t;
  983. }
  984. private static BadBytecode error(String sig) {
  985. return new BadBytecode("bad signature: " + sig);
  986. }
  987. }