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

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