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.

TypeData.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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.stackmap;
  17. import java.util.ArrayList;
  18. import java.util.HashSet;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Set;
  22. import javassist.ClassPool;
  23. import javassist.CtClass;
  24. import javassist.NotFoundException;
  25. import javassist.bytecode.BadBytecode;
  26. import javassist.bytecode.ConstPool;
  27. import javassist.bytecode.Descriptor;
  28. import javassist.bytecode.StackMapTable;
  29. public abstract class TypeData {
  30. /* Memo:
  31. * array type is a subtype of Cloneable and Serializable
  32. */
  33. public static TypeData[] make(int size) {
  34. TypeData[] array = new TypeData[size];
  35. for (int i = 0; i < size; i++)
  36. array[i] = TypeTag.TOP;
  37. return array;
  38. }
  39. protected TypeData() {}
  40. /**
  41. * Sets the type name of this object type. If the given type name is
  42. * a subclass of the current type name, then the given name becomes
  43. * the name of this object type.
  44. *
  45. * @param className dot-separated name unless the type is an array type.
  46. */
  47. @SuppressWarnings("unused")
  48. private static void setType(TypeData td, String className, ClassPool cp) throws BadBytecode {
  49. td.setType(className, cp);
  50. }
  51. public abstract int getTypeTag();
  52. public abstract int getTypeData(ConstPool cp);
  53. public TypeData join() { return new TypeVar(this); }
  54. /**
  55. * If the type is a basic type, this method normalizes the type
  56. * and returns a BasicType object. Otherwise, it returns null.
  57. */
  58. public abstract BasicType isBasicType();
  59. public abstract boolean is2WordType();
  60. /**
  61. * Returns false if getName() returns a valid type name.
  62. */
  63. public boolean isNullType() { return false; }
  64. public boolean isUninit() { return false; }
  65. public abstract boolean eq(TypeData d);
  66. public abstract String getName();
  67. public abstract void setType(String s, ClassPool cp) throws BadBytecode;
  68. /**
  69. * @param dim array dimension. It may be negative.
  70. */
  71. public abstract TypeData getArrayType(int dim) throws NotFoundException;
  72. /**
  73. * Depth-first search by Tarjan's algorithm
  74. *
  75. * @param order a node stack in the order in which nodes are visited.
  76. * @param index the index used by the algorithm.
  77. */
  78. public int dfs(List<TypeData> order, int index, ClassPool cp)
  79. throws NotFoundException
  80. {
  81. return index;
  82. }
  83. /**
  84. * Returns this if it is a TypeVar or a TypeVar that this
  85. * type depends on. Otherwise, this method returns null.
  86. * It is used by dfs().
  87. *
  88. * @param dim dimension
  89. */
  90. protected TypeVar toTypeVar(int dim) { return null; }
  91. // see UninitTypeVar and UninitData
  92. public void constructorCalled(int offset) {}
  93. @Override
  94. public String toString() {
  95. return super.toString() + "(" + toString2(new HashSet<TypeData>()) + ")";
  96. }
  97. abstract String toString2(Set<TypeData> set);
  98. /**
  99. * Primitive types.
  100. */
  101. protected static class BasicType extends TypeData {
  102. private String name;
  103. private int typeTag;
  104. private char decodedName;
  105. public BasicType(String type, int tag, char decoded) {
  106. name = type;
  107. typeTag = tag;
  108. decodedName = decoded;
  109. }
  110. @Override
  111. public int getTypeTag() { return typeTag; }
  112. @Override
  113. public int getTypeData(ConstPool cp) { return 0; }
  114. @Override
  115. public TypeData join() {
  116. if (this == TypeTag.TOP)
  117. return this;
  118. return super.join();
  119. }
  120. @Override
  121. public BasicType isBasicType() { return this; }
  122. @Override
  123. public boolean is2WordType() {
  124. return typeTag == StackMapTable.LONG
  125. || typeTag == StackMapTable.DOUBLE;
  126. }
  127. @Override
  128. public boolean eq(TypeData d) { return this == d; }
  129. @Override
  130. public String getName() {
  131. return name;
  132. }
  133. public char getDecodedName() { return decodedName; }
  134. @Override
  135. public void setType(String s, ClassPool cp) throws BadBytecode {
  136. throw new BadBytecode("conflict: " + name + " and " + s);
  137. }
  138. /**
  139. * @param dim array dimension. It may be negative.
  140. */
  141. @Override
  142. public TypeData getArrayType(int dim) throws NotFoundException {
  143. if (this == TypeTag.TOP)
  144. return this;
  145. else if (dim < 0)
  146. throw new NotFoundException("no element type: " + name);
  147. else if (dim == 0)
  148. return this;
  149. else {
  150. char[] name = new char[dim + 1];
  151. for (int i = 0; i < dim; i++)
  152. name[i] = '[';
  153. name[dim] = decodedName;
  154. return new ClassName(new String(name));
  155. }
  156. }
  157. @Override
  158. String toString2(Set<TypeData> set) { return name; }
  159. }
  160. // a type variable
  161. public static abstract class AbsTypeVar extends TypeData {
  162. public AbsTypeVar() {}
  163. public abstract void merge(TypeData t);
  164. @Override
  165. public int getTypeTag() { return StackMapTable.OBJECT; }
  166. @Override
  167. public int getTypeData(ConstPool cp) {
  168. return cp.addClassInfo(getName());
  169. }
  170. @Override
  171. public boolean eq(TypeData d) {
  172. if (d.isUninit())
  173. return d.eq(this);
  174. else
  175. return getName().equals(d.getName());
  176. }
  177. }
  178. /* a type variable representing a class type or a basic type.
  179. */
  180. public static class TypeVar extends AbsTypeVar {
  181. protected List<TypeData> lowers;// lower bounds of this type. ArrayList<TypeData>
  182. protected List<TypeData> usedBy;// reverse relations of lowers
  183. protected List<String> uppers; // upper bounds of this type.
  184. protected String fixedType;
  185. private boolean is2WordType; // cache
  186. public TypeVar(TypeData t) {
  187. uppers = null;
  188. lowers = new ArrayList<TypeData>(2);
  189. usedBy = new ArrayList<TypeData>(2);
  190. merge(t);
  191. fixedType = null;
  192. is2WordType = t.is2WordType();
  193. }
  194. @Override
  195. public String getName() {
  196. if (fixedType == null)
  197. return lowers.get(0).getName();
  198. return fixedType;
  199. }
  200. @Override
  201. public BasicType isBasicType() {
  202. if (fixedType == null)
  203. return lowers.get(0).isBasicType();
  204. return null;
  205. }
  206. @Override
  207. public boolean is2WordType() {
  208. if (fixedType == null) {
  209. return is2WordType;
  210. // return ((TypeData)lowers.get(0)).is2WordType();
  211. }
  212. return false;
  213. }
  214. @Override
  215. public boolean isNullType() {
  216. if (fixedType == null)
  217. return lowers.get(0).isNullType();
  218. return false;
  219. }
  220. @Override
  221. public boolean isUninit() {
  222. if (fixedType == null)
  223. return lowers.get(0).isUninit();
  224. return false;
  225. }
  226. @Override
  227. public void merge(TypeData t) {
  228. lowers.add(t);
  229. if (t instanceof TypeVar)
  230. ((TypeVar)t).usedBy.add(this);
  231. }
  232. @Override
  233. public int getTypeTag() {
  234. /* If fixedType is null after calling dfs(), then this
  235. type is NULL, Uninit, or a basic type. So call
  236. getTypeTag() on the first element of lowers. */
  237. if (fixedType == null)
  238. return lowers.get(0).getTypeTag();
  239. return super.getTypeTag();
  240. }
  241. @Override
  242. public int getTypeData(ConstPool cp) {
  243. if (fixedType == null)
  244. return lowers.get(0).getTypeData(cp);
  245. return super.getTypeData(cp);
  246. }
  247. @Override
  248. public void setType(String typeName, ClassPool cp) throws BadBytecode {
  249. if (uppers == null)
  250. uppers = new ArrayList<String>();
  251. uppers.add(typeName);
  252. }
  253. private int visited = 0;
  254. private int smallest = 0;
  255. private boolean inList = false;
  256. private int dimension = 0;
  257. @Override
  258. protected TypeVar toTypeVar(int dim) {
  259. dimension = dim;
  260. return this;
  261. }
  262. /* When fixTypes() is called, getName() will return the correct
  263. * (i.e. fixed) type name.
  264. */
  265. @Override
  266. public TypeData getArrayType(int dim) throws NotFoundException {
  267. if (dim == 0)
  268. return this;
  269. BasicType bt = isBasicType();
  270. if (bt == null)
  271. if (isNullType())
  272. return new NullType();
  273. else
  274. return new ClassName(getName()).getArrayType(dim);
  275. return bt.getArrayType(dim);
  276. }
  277. // depth-first serach
  278. @Override
  279. public int dfs(List<TypeData> preOrder, int index, ClassPool cp) throws NotFoundException {
  280. if (visited > 0)
  281. return index; // MapMaker.make() may call an already visited node.
  282. visited = smallest = ++index;
  283. preOrder.add(this);
  284. inList = true;
  285. int n = lowers.size();
  286. for (int i = 0; i < n; i++) {
  287. TypeVar child = lowers.get(i).toTypeVar(dimension);
  288. if (child != null)
  289. if (child.visited == 0) {
  290. index = child.dfs(preOrder, index, cp);
  291. if (child.smallest < smallest)
  292. smallest = child.smallest;
  293. }
  294. else if (child.inList)
  295. if (child.visited < smallest)
  296. smallest = child.visited;
  297. }
  298. if (visited == smallest) {
  299. List<TypeData> scc = new ArrayList<TypeData>(); // strongly connected component
  300. TypeVar cv;
  301. do {
  302. cv = (TypeVar)preOrder.remove(preOrder.size() - 1);
  303. cv.inList = false;
  304. scc.add(cv);
  305. } while (cv != this);
  306. fixTypes(scc, cp);
  307. }
  308. return index;
  309. }
  310. private void fixTypes(List<TypeData> scc, ClassPool cp) throws NotFoundException {
  311. Set<String> lowersSet = new HashSet<String>();
  312. boolean isBasicType = false;
  313. TypeData kind = null;
  314. int size = scc.size();
  315. for (int i = 0; i < size; i++) {
  316. TypeVar tvar = (TypeVar)scc.get(i);
  317. List<TypeData> tds = tvar.lowers;
  318. int size2 = tds.size();
  319. for (int j = 0; j < size2; j++) {
  320. TypeData td = tds.get(j);
  321. TypeData d = td.getArrayType(tvar.dimension);
  322. BasicType bt = d.isBasicType();
  323. if (kind == null) {
  324. if (bt == null) {
  325. isBasicType = false;
  326. kind = d;
  327. /* If scc has only an UninitData, fixedType is kept null.
  328. So lowerSet must be empty. If scc has not only an UninitData
  329. but also another TypeData, an error must be thrown but this
  330. error detection has not been implemented. */
  331. if (d.isUninit())
  332. break;
  333. }
  334. else {
  335. isBasicType = true;
  336. kind = bt;
  337. }
  338. }
  339. else {
  340. if ((bt == null && isBasicType) || (bt != null && kind != bt)) {
  341. isBasicType = true;
  342. kind = TypeTag.TOP;
  343. break;
  344. }
  345. }
  346. if (bt == null && !d.isNullType())
  347. lowersSet.add(d.getName());
  348. }
  349. }
  350. if (isBasicType) {
  351. is2WordType = kind.is2WordType(); // necessary?
  352. fixTypes1(scc, kind);
  353. }
  354. else {
  355. String typeName = fixTypes2(scc, lowersSet, cp);
  356. fixTypes1(scc, new ClassName(typeName));
  357. }
  358. }
  359. private void fixTypes1(List<TypeData> scc, TypeData kind) throws NotFoundException {
  360. int size = scc.size();
  361. for (int i = 0; i < size; i++) {
  362. TypeVar cv = (TypeVar)scc.get(i);
  363. TypeData kind2 = kind.getArrayType(-cv.dimension);
  364. if (kind2.isBasicType() == null)
  365. cv.fixedType = kind2.getName();
  366. else {
  367. cv.lowers.clear();
  368. cv.lowers.add(kind2);
  369. cv.is2WordType = kind2.is2WordType();
  370. }
  371. }
  372. }
  373. private String fixTypes2(List<TypeData> scc, Set<String> lowersSet, ClassPool cp) throws NotFoundException {
  374. Iterator<String> it = lowersSet.iterator();
  375. if (lowersSet.isEmpty())
  376. return null; // only NullType
  377. else if (lowersSet.size() == 1)
  378. return it.next();
  379. else {
  380. CtClass cc = cp.get(it.next());
  381. while (it.hasNext())
  382. cc = commonSuperClassEx(cc, cp.get(it.next()));
  383. if (cc.getSuperclass() == null || isObjectArray(cc))
  384. cc = fixByUppers(scc, cp, new HashSet<TypeData>(), cc);
  385. if (cc.isArray())
  386. return Descriptor.toJvmName(cc);
  387. return cc.getName();
  388. }
  389. }
  390. private static boolean isObjectArray(CtClass cc) throws NotFoundException {
  391. return cc.isArray() && cc.getComponentType().getSuperclass() == null;
  392. }
  393. private CtClass fixByUppers(List<TypeData> users, ClassPool cp, Set<TypeData> visited, CtClass type)
  394. throws NotFoundException
  395. {
  396. if (users == null)
  397. return type;
  398. int size = users.size();
  399. for (int i = 0; i < size; i++) {
  400. TypeVar t = (TypeVar)users.get(i);
  401. if (!visited.add(t))
  402. return type;
  403. if (t.uppers != null) {
  404. int s = t.uppers.size();
  405. for (int k = 0; k < s; k++) {
  406. CtClass cc = cp.get(t.uppers.get(k));
  407. if (cc.subtypeOf(type))
  408. type = cc;
  409. }
  410. }
  411. type = fixByUppers(t.usedBy, cp, visited, type);
  412. }
  413. return type;
  414. }
  415. @Override
  416. String toString2(Set<TypeData> hash) {
  417. hash.add(this);
  418. if (lowers.size() > 0) {
  419. TypeData e = lowers.get(0);
  420. if (e != null && !hash.contains(e))
  421. return e.toString2(hash);
  422. }
  423. return "?";
  424. }
  425. }
  426. /**
  427. * Finds the most specific common super class of the given classes
  428. * by considering array types.
  429. */
  430. public static CtClass commonSuperClassEx(CtClass one, CtClass two) throws NotFoundException {
  431. if (one == two)
  432. return one;
  433. else if (one.isArray() && two.isArray()) {
  434. CtClass ele1 = one.getComponentType();
  435. CtClass ele2 = two.getComponentType();
  436. CtClass element = commonSuperClassEx(ele1, ele2);
  437. if (element == ele1)
  438. return one;
  439. else if (element == ele2)
  440. return two;
  441. else
  442. return one.getClassPool().get(element == null ? "java.lang.Object"
  443. : element.getName() + "[]");
  444. }
  445. else if (one.isPrimitive() || two.isPrimitive())
  446. return null; // TOP
  447. else if (one.isArray() || two.isArray()) // but !(one.isArray() && two.isArray())
  448. return one.getClassPool().get("java.lang.Object");
  449. else
  450. return commonSuperClass(one, two);
  451. }
  452. /**
  453. * Finds the most specific common super class of the given classes.
  454. * This method is a copy from javassist.bytecode.analysis.Type.
  455. */
  456. public static CtClass commonSuperClass(CtClass one, CtClass two) throws NotFoundException {
  457. CtClass deep = one;
  458. CtClass shallow = two;
  459. CtClass backupShallow = shallow;
  460. CtClass backupDeep = deep;
  461. // Phase 1 - Find the deepest hierarchy, set deep and shallow correctly
  462. for (;;) {
  463. // In case we get lucky, and find a match early
  464. if (eq(deep, shallow) && deep.getSuperclass() != null)
  465. return deep;
  466. CtClass deepSuper = deep.getSuperclass();
  467. CtClass shallowSuper = shallow.getSuperclass();
  468. if (shallowSuper == null) {
  469. // right, now reset shallow
  470. shallow = backupShallow;
  471. break;
  472. }
  473. if (deepSuper == null) {
  474. // wrong, swap them, since deep is now useless, its our tmp before we swap it
  475. deep = backupDeep;
  476. backupDeep = backupShallow;
  477. backupShallow = deep;
  478. deep = shallow;
  479. shallow = backupShallow;
  480. break;
  481. }
  482. deep = deepSuper;
  483. shallow = shallowSuper;
  484. }
  485. // Phase 2 - Move deepBackup up by (deep end - deep)
  486. for (;;) {
  487. deep = deep.getSuperclass();
  488. if (deep == null)
  489. break;
  490. backupDeep = backupDeep.getSuperclass();
  491. }
  492. deep = backupDeep;
  493. // Phase 3 - The hierarchy positions are now aligned
  494. // The common super class is easy to find now
  495. while (!eq(deep, shallow)) {
  496. deep = deep.getSuperclass();
  497. shallow = shallow.getSuperclass();
  498. }
  499. return deep;
  500. }
  501. static boolean eq(CtClass one, CtClass two) {
  502. return one == two || (one != null && two != null && one.getName().equals(two.getName()));
  503. }
  504. public static void aastore(TypeData array, TypeData value, ClassPool cp) throws BadBytecode {
  505. if (array instanceof AbsTypeVar)
  506. if (!value.isNullType())
  507. ((AbsTypeVar)array).merge(ArrayType.make(value));
  508. if (value instanceof AbsTypeVar)
  509. if (array instanceof AbsTypeVar)
  510. ArrayElement.make(array); // should call value.setType() later.
  511. else if (array instanceof ClassName) {
  512. if (!array.isNullType()) {
  513. String type = ArrayElement.typeName(array.getName());
  514. value.setType(type, cp);
  515. }
  516. }
  517. else
  518. throw new BadBytecode("bad AASTORE: " + array);
  519. }
  520. /* A type variable representing an array type.
  521. * It is a decorator of another type variable.
  522. */
  523. public static class ArrayType extends AbsTypeVar {
  524. private AbsTypeVar element;
  525. private ArrayType(AbsTypeVar elementType) {
  526. element = elementType;
  527. }
  528. static TypeData make(TypeData element) throws BadBytecode {
  529. if (element instanceof ArrayElement)
  530. return ((ArrayElement)element).arrayType();
  531. else if (element instanceof AbsTypeVar)
  532. return new ArrayType((AbsTypeVar)element);
  533. else if (element instanceof ClassName)
  534. if (!element.isNullType())
  535. return new ClassName(typeName(element.getName()));
  536. throw new BadBytecode("bad AASTORE: " + element);
  537. }
  538. @Override
  539. public void merge(TypeData t) {
  540. try {
  541. if (!t.isNullType())
  542. element.merge(ArrayElement.make(t));
  543. }
  544. catch (BadBytecode e) {
  545. // never happens
  546. throw new RuntimeException("fatal: " + e);
  547. }
  548. }
  549. @Override
  550. public String getName() {
  551. return typeName(element.getName());
  552. }
  553. public AbsTypeVar elementType() { return element; }
  554. @Override
  555. public BasicType isBasicType() { return null; }
  556. @Override
  557. public boolean is2WordType() { return false; }
  558. /* elementType must be a class name. Basic type names
  559. * are not allowed.
  560. */
  561. public static String typeName(String elementType) {
  562. if (elementType.charAt(0) == '[')
  563. return "[" + elementType;
  564. return "[L" + elementType.replace('.', '/') + ";";
  565. }
  566. @Override
  567. public void setType(String s, ClassPool cp) throws BadBytecode {
  568. element.setType(ArrayElement.typeName(s), cp);
  569. }
  570. @Override
  571. protected TypeVar toTypeVar(int dim) { return element.toTypeVar(dim + 1); }
  572. @Override
  573. public TypeData getArrayType(int dim) throws NotFoundException {
  574. return element.getArrayType(dim + 1);
  575. }
  576. @Override
  577. public int dfs(List<TypeData> order, int index, ClassPool cp) throws NotFoundException {
  578. return element.dfs(order, index, cp);
  579. }
  580. @Override
  581. String toString2(Set<TypeData> set) {
  582. return "[" + element.toString2(set);
  583. }
  584. }
  585. /* A type variable representing an array-element type.
  586. * It is a decorator of another type variable.
  587. */
  588. public static class ArrayElement extends AbsTypeVar {
  589. private AbsTypeVar array;
  590. private ArrayElement(AbsTypeVar a) { // a is never null
  591. array = a;
  592. }
  593. public static TypeData make(TypeData array) throws BadBytecode {
  594. if (array instanceof ArrayType)
  595. return ((ArrayType)array).elementType();
  596. else if (array instanceof AbsTypeVar)
  597. return new ArrayElement((AbsTypeVar)array);
  598. else if (array instanceof ClassName)
  599. if (!array.isNullType())
  600. return new ClassName(typeName(array.getName()));
  601. throw new BadBytecode("bad AASTORE: " + array);
  602. }
  603. @Override
  604. public void merge(TypeData t) {
  605. try {
  606. if (!t.isNullType())
  607. array.merge(ArrayType.make(t));
  608. }
  609. catch (BadBytecode e) {
  610. // never happens
  611. throw new RuntimeException("fatal: " + e);
  612. }
  613. }
  614. @Override
  615. public String getName() {
  616. return typeName(array.getName());
  617. }
  618. public AbsTypeVar arrayType() { return array; }
  619. /* arrayType must be a class name. Basic type names are
  620. * not allowed.
  621. */
  622. @Override
  623. public BasicType isBasicType() { return null; }
  624. @Override
  625. public boolean is2WordType() { return false; }
  626. private static String typeName(String arrayType) {
  627. if (arrayType.length() > 1 && arrayType.charAt(0) == '[') {
  628. char c = arrayType.charAt(1);
  629. if (c == 'L')
  630. return arrayType.substring(2, arrayType.length() - 1).replace('/', '.');
  631. else if (c == '[')
  632. return arrayType.substring(1);
  633. }
  634. return "java.lang.Object"; // the array type may be NullType
  635. }
  636. @Override
  637. public void setType(String s, ClassPool cp) throws BadBytecode {
  638. array.setType(ArrayType.typeName(s), cp);
  639. }
  640. @Override
  641. protected TypeVar toTypeVar(int dim) { return array.toTypeVar(dim - 1); }
  642. @Override
  643. public TypeData getArrayType(int dim) throws NotFoundException {
  644. return array.getArrayType(dim - 1);
  645. }
  646. @Override
  647. public int dfs(List<TypeData> order, int index, ClassPool cp) throws NotFoundException {
  648. return array.dfs(order, index, cp);
  649. }
  650. @Override
  651. String toString2(Set<TypeData> set) {
  652. return "*" + array.toString2(set);
  653. }
  654. }
  655. public static class UninitTypeVar extends AbsTypeVar {
  656. protected TypeData type; // UninitData or TOP
  657. public UninitTypeVar(UninitData t) { type = t; }
  658. @Override
  659. public int getTypeTag() { return type.getTypeTag(); }
  660. @Override
  661. public int getTypeData(ConstPool cp) { return type.getTypeData(cp); }
  662. @Override
  663. public BasicType isBasicType() { return type.isBasicType(); }
  664. @Override
  665. public boolean is2WordType() { return type.is2WordType(); }
  666. @Override
  667. public boolean isUninit() { return type.isUninit(); }
  668. @Override
  669. public boolean eq(TypeData d) { return type.eq(d); }
  670. @Override
  671. public String getName() { return type.getName(); }
  672. @Override
  673. protected TypeVar toTypeVar(int dim) { return null; }
  674. @Override
  675. public TypeData join() { return type.join(); }
  676. @Override
  677. public void setType(String s, ClassPool cp) throws BadBytecode {
  678. type.setType(s, cp);
  679. }
  680. @Override
  681. public void merge(TypeData t) {
  682. if (!t.eq(type))
  683. type = TypeTag.TOP;
  684. }
  685. @Override
  686. public void constructorCalled(int offset) {
  687. type.constructorCalled(offset);
  688. }
  689. public int offset() {
  690. if (type instanceof UninitData)
  691. return ((UninitData)type).offset;
  692. throw new RuntimeException("not available");
  693. }
  694. @Override
  695. public TypeData getArrayType(int dim) throws NotFoundException {
  696. return type.getArrayType(dim);
  697. }
  698. @Override
  699. String toString2(Set<TypeData> set) { return ""; }
  700. }
  701. /**
  702. * Type data for OBJECT.
  703. */
  704. public static class ClassName extends TypeData {
  705. private String name; // dot separated.
  706. public ClassName(String n) {
  707. name = n;
  708. }
  709. @Override
  710. public String getName() {
  711. return name;
  712. }
  713. @Override
  714. public BasicType isBasicType() { return null; }
  715. @Override
  716. public boolean is2WordType() { return false; }
  717. @Override
  718. public int getTypeTag() { return StackMapTable.OBJECT; }
  719. @Override
  720. public int getTypeData(ConstPool cp) {
  721. return cp.addClassInfo(getName());
  722. }
  723. @Override
  724. public boolean eq(TypeData d) {
  725. if (d.isUninit())
  726. return d.eq(this);
  727. else
  728. return name.equals(d.getName());
  729. }
  730. @Override
  731. public void setType(String typeName, ClassPool cp) throws BadBytecode {}
  732. @Override
  733. public TypeData getArrayType(int dim) throws NotFoundException {
  734. if (dim == 0)
  735. return this;
  736. else if (dim > 0) {
  737. char[] dimType = new char[dim];
  738. for (int i = 0; i < dim; i++)
  739. dimType[i] = '[';
  740. String elementType = getName();
  741. if (elementType.charAt(0) != '[')
  742. elementType = "L" + elementType.replace('.', '/') + ";";
  743. return new ClassName(new String(dimType) + elementType);
  744. }
  745. else {
  746. for (int i = 0; i < -dim; i++)
  747. if (name.charAt(i) != '[')
  748. throw new NotFoundException("no " + dim + " dimensional array type: " + getName());
  749. char type = name.charAt(-dim);
  750. if (type == '[')
  751. return new ClassName(name.substring(-dim));
  752. else if (type == 'L')
  753. return new ClassName(name.substring(-dim + 1, name.length() - 1).replace('/', '.'));
  754. else if (type == TypeTag.DOUBLE.decodedName)
  755. return TypeTag.DOUBLE;
  756. else if (type == TypeTag.FLOAT.decodedName)
  757. return TypeTag.FLOAT;
  758. else if (type == TypeTag.LONG.decodedName)
  759. return TypeTag.LONG;
  760. else
  761. return TypeTag.INTEGER;
  762. }
  763. }
  764. @Override
  765. String toString2(Set<TypeData> set) {
  766. return name;
  767. }
  768. }
  769. /**
  770. * Type data for NULL or OBJECT.
  771. * The types represented by the instances of this class are
  772. * initially NULL but will be OBJECT.
  773. */
  774. public static class NullType extends ClassName {
  775. public NullType() {
  776. super("null-type"); // type name
  777. }
  778. @Override
  779. public int getTypeTag() {
  780. return StackMapTable.NULL;
  781. }
  782. @Override
  783. public boolean isNullType() { return true; }
  784. @Override
  785. public int getTypeData(ConstPool cp) { return 0; }
  786. @Override
  787. public TypeData getArrayType(int dim) { return this; }
  788. }
  789. /**
  790. * Type data for UNINIT.
  791. */
  792. public static class UninitData extends ClassName {
  793. int offset;
  794. boolean initialized;
  795. UninitData(int offset, String className) {
  796. super(className);
  797. this.offset = offset;
  798. this.initialized = false;
  799. }
  800. public UninitData copy() { return new UninitData(offset, getName()); }
  801. @Override
  802. public int getTypeTag() {
  803. return StackMapTable.UNINIT;
  804. }
  805. @Override
  806. public int getTypeData(ConstPool cp) {
  807. return offset;
  808. }
  809. @Override
  810. public TypeData join() {
  811. if (initialized)
  812. return new TypeVar(new ClassName(getName()));
  813. return new UninitTypeVar(copy());
  814. }
  815. @Override
  816. public boolean isUninit() { return true; }
  817. @Override
  818. public boolean eq(TypeData d) {
  819. if (d instanceof UninitData) {
  820. UninitData ud = (UninitData)d;
  821. return offset == ud.offset && getName().equals(ud.getName());
  822. }
  823. return false;
  824. }
  825. public int offset() { return offset; }
  826. @Override
  827. public void constructorCalled(int offset) {
  828. if (offset == this.offset)
  829. initialized = true;
  830. }
  831. @Override
  832. String toString2(Set<TypeData> set) { return getName() + "," + offset; }
  833. }
  834. public static class UninitThis extends UninitData {
  835. UninitThis(String className) {
  836. super(-1, className);
  837. }
  838. @Override
  839. public UninitData copy() { return new UninitThis(getName()); }
  840. @Override
  841. public int getTypeTag() {
  842. return StackMapTable.THIS;
  843. }
  844. @Override
  845. public int getTypeData(ConstPool cp) {
  846. return 0;
  847. }
  848. @Override
  849. String toString2(Set<TypeData> set) { return "uninit:this"; }
  850. }
  851. }