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.

Module.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2016-17 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution,
  20. * if any, must include the following acknowledgment:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgment may appear in the software itself,
  24. * if and wherever such third-party acknowledgments normally appear.
  25. *
  26. * 4. The names "Apache" and "Apache Software Foundation" and
  27. * "Apache BCEL" must not be used to endorse or promote products
  28. * derived from this software without prior written permission. For
  29. * written permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache",
  32. * "Apache BCEL", nor may "Apache" appear in their name, without
  33. * prior written permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.aspectj.apache.bcel.classfile;
  55. import java.io.DataInputStream;
  56. import java.io.DataOutputStream;
  57. import java.io.IOException;
  58. import org.aspectj.apache.bcel.Constants;
  59. /**
  60. * This class is derived from <em>Attribute</em> and represents the module
  61. * information captured in a class file.
  62. * http://cr.openjdk.java.net/~mr/jigsaw/spec/lang-vm.html
  63. * http://cr.openjdk.java.net/~mr/jigsaw/spec/java-se-9-jvms-diffs.pdf 4.7.25
  64. *
  65. * @author Andy Clement
  66. */
  67. public final class Module extends Attribute {
  68. private static final String[] NO_MODULE_NAMES = {};
  69. private int moduleNameIndex; // u2 module_name_index
  70. private int moduleFlags; // u2 module_flags
  71. private int moduleVersionIndex; // u2 module_version_index
  72. private Require[] requires;
  73. private Export[] exports;
  74. private Open[] opens;
  75. private Uses[] uses;
  76. private Provide[] provides;
  77. private byte[] moduleInfo;
  78. private int ptr;
  79. private boolean unpacked = false;
  80. public Module(Module module) {
  81. super(module.getTag(), module.getNameIndex(), module.getLength(), module.getConstantPool());
  82. moduleInfo = module.getBytes();
  83. }
  84. public Module(int nameIndex, int length, byte[] data, ConstantPool cp) {
  85. super(Constants.ATTR_MODULE, nameIndex, length, cp);
  86. }
  87. Module(int nameIndex, int length, DataInputStream stream, ConstantPool cp) throws IOException {
  88. this(nameIndex, length, (byte[])null, cp);
  89. moduleInfo = new byte[length];
  90. stream.read(moduleInfo);
  91. unpacked = false;
  92. }
  93. public class Require {
  94. private final int moduleIndex;
  95. private final int flags;
  96. private final int versionIndex;
  97. public Require(int moduleIndex, int flags, int versionIndex) {
  98. this.moduleIndex = moduleIndex;
  99. this.flags = flags;
  100. this.versionIndex = versionIndex;
  101. }
  102. public String getModuleName() {
  103. return cpool.getModuleName(moduleIndex);
  104. }
  105. public int getFlags() {
  106. return flags;
  107. }
  108. public int getVersionIndex() {
  109. return versionIndex;
  110. }
  111. public String getVersionString() {
  112. if (versionIndex == 0) {
  113. return null;
  114. } else {
  115. return cpool.getConstantUtf8(versionIndex).getValue();
  116. }
  117. }
  118. public String getFlagsAsString() {
  119. StringBuilder s = new StringBuilder();
  120. if ((flags & Constants.MODULE_ACC_TRANSITIVE)!=0) {
  121. s.append(" transitive");
  122. }
  123. if ((flags & Constants.MODULE_ACC_STATIC_PHASE)!=0) {
  124. s.append(" static");
  125. }
  126. if ((flags & Constants.MODULE_ACC_SYNTHETIC)!=0) {
  127. s.append(" synthetic");
  128. }
  129. if ((flags & Constants.MODULE_ACC_MANDATED)!=0) {
  130. s.append(" mandated");
  131. }
  132. return s.toString();
  133. }
  134. public String toString() {
  135. return "requires"+getFlagsAsString()+" "+getModuleName()+(versionIndex==0?"":" "+getVersionString());
  136. }
  137. }
  138. public class Export {
  139. private final int packageIndex;
  140. private final int flags;
  141. private final int[] toModuleIndices;
  142. public Export(int packageIndex, int flags, int[] toModuleIndices) {
  143. this.packageIndex = packageIndex;
  144. this.flags = flags;
  145. this.toModuleIndices = toModuleIndices;
  146. }
  147. public int getPackageIndex() {
  148. return packageIndex;
  149. }
  150. public int getFlags() {
  151. return flags;
  152. }
  153. public int[] getToModuleIndices() {
  154. return toModuleIndices;
  155. }
  156. public String getPackage() {
  157. return cpool.getPackageName(packageIndex);
  158. }
  159. public String getFlagsAsString() {
  160. StringBuilder s = new StringBuilder();
  161. if ((flags & Constants.MODULE_ACC_SYNTHETIC)!=0) {
  162. s.append(" synthetic");
  163. }
  164. if ((flags & Constants.MODULE_ACC_MANDATED)!=0) {
  165. s.append(" synthetic");
  166. }
  167. return s.toString();
  168. }
  169. public String[] getToModuleNames() {
  170. if (toModuleIndices==null) {
  171. return NO_MODULE_NAMES;
  172. }
  173. String[] toModuleNames = new String[toModuleIndices.length];
  174. for (int i=0;i<toModuleIndices.length;i++) {
  175. toModuleNames[i] = cpool.getModuleName(toModuleIndices[i]);
  176. }
  177. return toModuleNames;
  178. }
  179. public String toString() {
  180. StringBuilder s =new StringBuilder();
  181. s.append("exports").append(getFlagsAsString()).append(" ").append(getPackage().replace('/', '.'));
  182. String[] toModules = getToModuleNames();
  183. if (toModules.length!=0) {
  184. s.append(" to ");
  185. for (int i=0;i<toModules.length;i++) {
  186. if (i>0) {
  187. s.append(", ");
  188. }
  189. s.append(toModules[i]);
  190. }
  191. }
  192. return s.toString().trim();
  193. }
  194. }
  195. public class Open {
  196. private final int packageIndex;
  197. private final int flags;
  198. private final int[] toModuleIndices;
  199. public Open(int packageIndex, int flags, int[] toModuleIndices) {
  200. this.packageIndex = packageIndex;
  201. this.flags = flags;
  202. this.toModuleIndices = toModuleIndices;
  203. }
  204. public int getPackageIndex() {
  205. return packageIndex;
  206. }
  207. public int getFlags() {
  208. return flags;
  209. }
  210. public int[] getToModuleIndices() {
  211. return toModuleIndices;
  212. }
  213. public String getPackage() {
  214. return cpool.getPackageName(packageIndex);
  215. }
  216. public String getFlagsAsString() {
  217. StringBuilder s = new StringBuilder();
  218. if ((flags & Constants.MODULE_ACC_SYNTHETIC)!=0) {
  219. s.append(" synthetic");
  220. }
  221. if ((flags & Constants.MODULE_ACC_MANDATED)!=0) {
  222. s.append(" synthetic");
  223. }
  224. return s.toString();
  225. }
  226. public String[] getToModuleNames() {
  227. if (toModuleIndices==null) {
  228. return NO_MODULE_NAMES;
  229. }
  230. String[] toModuleNames = new String[toModuleIndices.length];
  231. for (int i=0;i<toModuleIndices.length;i++) {
  232. toModuleNames[i] = cpool.getModuleName(toModuleIndices[i]);
  233. }
  234. return toModuleNames;
  235. }
  236. public String toString() {
  237. StringBuilder s =new StringBuilder();
  238. s.append("opens").append(getFlagsAsString()).append(" ").append(getPackage().replace('/', '.'));
  239. String[] toModules = getToModuleNames();
  240. if (toModules.length!=0) {
  241. s.append(" to ");
  242. for (int i=0;i<toModules.length;i++) {
  243. if (i>0) {
  244. s.append(", ");
  245. }
  246. s.append(toModules[i]);
  247. }
  248. }
  249. return s.toString().trim();
  250. }
  251. }
  252. public class Provide {
  253. private final int providedTypeIndex;
  254. private final int[] withTypeIndices;
  255. public Provide(int providedTypeIndex, int[] withTypeIndices) {
  256. this.providedTypeIndex = providedTypeIndex;
  257. this.withTypeIndices = withTypeIndices;
  258. }
  259. public String getProvidedType() {
  260. return cpool.getConstantString_CONSTANTClass(providedTypeIndex);
  261. }
  262. public int getProvidedTypeIndex() {
  263. return providedTypeIndex;
  264. }
  265. public String[] getWithTypeStrings() {
  266. String[] result = new String[withTypeIndices.length];
  267. for (int i=0;i<withTypeIndices.length;i++) {
  268. result[i] = cpool.getConstantString_CONSTANTClass(withTypeIndices[i]);
  269. }
  270. return result;
  271. }
  272. public int[] getWithTypeIndices() {
  273. return withTypeIndices;
  274. }
  275. public String toString() {
  276. StringBuilder s =new StringBuilder();
  277. s.append("provides ").append(getProvidedType().replace('/', '.'));
  278. s.append(" with ");
  279. String[] withtypes = getWithTypeStrings();
  280. for (int i=0;i< withtypes.length;i++) {
  281. if (i>0) s.append(",");
  282. s.append(withtypes[i].replace('/','.'));
  283. }
  284. return s.toString();
  285. }
  286. }
  287. public class Uses {
  288. private final int typeNameIndex;
  289. public Uses(int typeNameIndex) {
  290. this.typeNameIndex = typeNameIndex;
  291. }
  292. public String getTypeName() {
  293. return cpool.getConstantString_CONSTANTClass(typeNameIndex);
  294. }
  295. public int getTypeNameIndex() {
  296. return typeNameIndex;
  297. }
  298. public String toString() {
  299. StringBuilder s =new StringBuilder();
  300. s.append("uses ").append(getTypeName().replace('/', '.'));
  301. return s.toString().trim();
  302. }
  303. }
  304. private final int readInt() {
  305. return ((moduleInfo[ptr++] & 0xFF) << 24) + ((moduleInfo[ptr++] & 0xFF) << 16)
  306. + ((moduleInfo[ptr++] & 0xFF) << 8) + (moduleInfo[ptr++] & 0xFF);
  307. }
  308. private final int readUnsignedShort() {
  309. return ((moduleInfo[ptr++] & 0xff) << 8) + (moduleInfo[ptr++] & 0xff);
  310. }
  311. private final int readUnsignedShort(int offset) {
  312. return ((moduleInfo[offset++] & 0xff) << 8) + (moduleInfo[offset] & 0xff);
  313. }
  314. // Format: http://cr.openjdk.java.net/~mr/jigsaw/spec/java-se-9-jvms-diffs.pdf 4.7.25
  315. private void ensureUnpacked() {
  316. if (!unpacked) {
  317. ptr = 0;
  318. moduleNameIndex = readUnsignedShort();
  319. moduleFlags = readUnsignedShort();
  320. moduleVersionIndex = readUnsignedShort();
  321. int count = readUnsignedShort();
  322. requires = new Require[count];
  323. for (int i = 0; i < count; i++) {
  324. requires[i] = new Require(readUnsignedShort(), readUnsignedShort(), readUnsignedShort());
  325. }
  326. count = readUnsignedShort();
  327. exports = new Export[count];
  328. for (int i = 0; i < count; i++) {
  329. int index = readUnsignedShort();
  330. int flags = readUnsignedShort();
  331. int toCount = readUnsignedShort();
  332. int[] to = new int[toCount];
  333. for (int j = 0; j < toCount; j++) {
  334. to[j] = readUnsignedShort();
  335. }
  336. exports[i] = new Export(index, flags, to);
  337. }
  338. count = readUnsignedShort();
  339. opens = new Open[count];
  340. for (int i = 0; i < count; i++) {
  341. int index = readUnsignedShort();
  342. int flags = readUnsignedShort();
  343. int toCount = readUnsignedShort();
  344. int[] to = new int[toCount];
  345. for (int j = 0; j < toCount; j++) {
  346. to[j] = readUnsignedShort();
  347. }
  348. opens[i] = new Open(index, flags, to);
  349. }
  350. count = readUnsignedShort();
  351. uses = new Uses[count];
  352. for (int i = 0; i < count; i++) {
  353. uses[i] = new Uses(readUnsignedShort());
  354. }
  355. count = readUnsignedShort();
  356. provides = new Provide[count];
  357. for (int i = 0; i < count; i++) {
  358. int index = readUnsignedShort();
  359. int toCount = readUnsignedShort();
  360. int[] to = new int[toCount];
  361. for (int j = 0; j < toCount; j++) {
  362. to[j] = readUnsignedShort();
  363. }
  364. provides[i] = new Provide(index, to);
  365. }
  366. unpacked = true;
  367. }
  368. }
  369. @Override
  370. public final void dump(DataOutputStream file) throws IOException {
  371. super.dump(file);
  372. if (!unpacked) {
  373. file.write(moduleInfo);
  374. } else {
  375. file.writeShort(moduleNameIndex);
  376. file.writeShort(moduleFlags);
  377. file.writeShort(moduleVersionIndex);
  378. file.writeShort(requires.length);
  379. for (Require require : requires) {
  380. file.writeShort(require.moduleIndex);
  381. file.writeShort(require.flags);
  382. file.writeShort(require.versionIndex);
  383. }
  384. file.writeShort(exports.length);
  385. for (Export export : exports) {
  386. file.writeShort(export.packageIndex);
  387. int[] toIndices = export.toModuleIndices;
  388. file.writeShort(toIndices.length);
  389. for (int index : toIndices) {
  390. file.writeShort(index);
  391. }
  392. }
  393. file.writeShort(opens.length);
  394. for (Open open : opens) {
  395. file.writeShort(open.packageIndex);
  396. int[] toIndices = open.toModuleIndices;
  397. file.writeShort(toIndices.length);
  398. for (int index : toIndices) {
  399. file.writeShort(index);
  400. }
  401. }
  402. file.writeShort(uses.length);
  403. for (Uses use : uses) {
  404. file.writeShort(use.getTypeNameIndex());
  405. }
  406. file.writeShort(provides.length);
  407. for (Provide provide : provides) {
  408. file.writeShort(provide.providedTypeIndex);
  409. int[] toIndices = provide.withTypeIndices;
  410. file.writeShort(toIndices.length);
  411. for (int index : toIndices) {
  412. file.writeShort(index);
  413. }
  414. }
  415. }
  416. }
  417. public String toStringRequires() {
  418. StringBuilder s = new StringBuilder();
  419. s.append('#').append(requires.length);
  420. if (requires.length > 0) {
  421. for (Require require : requires) {
  422. s.append(' ');
  423. s.append(require.moduleIndex).append(':').append(require.flags);
  424. }
  425. }
  426. return s.toString();
  427. }
  428. public String toStringExports() {
  429. StringBuilder s = new StringBuilder();
  430. s.append('#').append(exports.length);
  431. if (exports.length > 0) {
  432. for (Export export : exports) {
  433. s.append(' ');
  434. s.append(export.packageIndex).append(":[");
  435. int[] toIndices = export.toModuleIndices;
  436. for (int i = 0; i < toIndices.length; i++) {
  437. if (i > 0)
  438. s.append(',');
  439. s.append(toIndices[i]);
  440. }
  441. s.append("]");
  442. }
  443. }
  444. return s.toString();
  445. }
  446. public String toStringOpens() {
  447. StringBuilder s = new StringBuilder();
  448. s.append('#').append(opens.length);
  449. if (opens.length > 0) {
  450. for (Open open : opens) {
  451. s.append(' ');
  452. s.append(open.packageIndex).append(":[");
  453. int[] toIndices = open.toModuleIndices;
  454. for (int i = 0; i < toIndices.length; i++) {
  455. if (i > 0)
  456. s.append(',');
  457. s.append(toIndices[i]);
  458. }
  459. s.append("]");
  460. }
  461. }
  462. return s.toString();
  463. }
  464. public String toStringUses() {
  465. StringBuilder s = new StringBuilder();
  466. s.append('#').append(uses.length);
  467. if (uses.length > 0) {
  468. for (Uses use : uses) {
  469. s.append(' ');
  470. s.append(use.getTypeName());
  471. }
  472. }
  473. return s.toString();
  474. }
  475. public String toStringProvides() {
  476. StringBuilder s = new StringBuilder();
  477. s.append('#').append(provides.length);
  478. if (provides.length > 0) {
  479. for (Provide provide : provides) {
  480. s.append(' ');
  481. s.append(provide.providedTypeIndex).append(":[");
  482. int[] indices = provide.withTypeIndices;
  483. for (int i = 0; i < indices.length; i++) {
  484. if (i > 0)
  485. s.append(',');
  486. s.append(indices[i]);
  487. }
  488. s.append("]");
  489. }
  490. }
  491. return s.toString();
  492. }
  493. @Override
  494. public final String toString() {
  495. StringBuilder s = new StringBuilder();
  496. ensureUnpacked();
  497. s.append("Module(");
  498. if (requires.length != 0) {
  499. s.append("requires=");
  500. s.append(toStringRequires());
  501. s.append(" ");
  502. }
  503. if (exports.length != 0) {
  504. s.append("exports=");
  505. s.append(toStringExports());
  506. s.append(" ");
  507. }
  508. if (opens.length != 0) {
  509. s.append("opens=");
  510. s.append(toStringOpens());
  511. s.append(" ");
  512. }
  513. if (uses.length != 0) {
  514. s.append("uses=");
  515. s.append(toStringUses());
  516. s.append(" ");
  517. }
  518. if (provides.length != 0) {
  519. s.append("provides=");
  520. s.append(toStringProvides());
  521. s.append(" ");
  522. }
  523. return s.toString().trim()+")";
  524. }
  525. /**
  526. * @return deep copy of this attribute //
  527. */
  528. // @Override
  529. // public Attribute copy(ConstantPool constant_pool) {
  530. // return (Module) clone();
  531. // }
  532. @Override
  533. public void accept(ClassVisitor v) {
  534. v.visitModule(this);
  535. }
  536. public Require[] getRequires() {
  537. ensureUnpacked();
  538. return requires;
  539. }
  540. public String[] getRequiredModuleNames() {
  541. ensureUnpacked();
  542. String[] results = new String[requires.length];
  543. for (int i=0;i<requires.length;i++) {
  544. results[i] = cpool.getModuleName(requires[i].moduleIndex);
  545. }
  546. return results;
  547. }
  548. public byte[] getBytes() {
  549. return moduleInfo;
  550. }
  551. public Export[] getExports() {
  552. ensureUnpacked();
  553. return exports;
  554. }
  555. public Open[] getOpens() {
  556. ensureUnpacked();
  557. return opens;
  558. }
  559. public Uses[] getUses() {
  560. ensureUnpacked();
  561. return uses;
  562. }
  563. public Provide[] getProvides() {
  564. ensureUnpacked();
  565. return provides;
  566. }
  567. public String getModuleName() {
  568. return ((ConstantModule)cpool.getConstant(moduleNameIndex)).getModuleName(cpool);
  569. }
  570. public int getModuleFlags() {
  571. // 0x0020 (ACC_OPEN) - Indicates that this module is open.
  572. // 0x1000 (ACC_SYNTHETIC) - Indicates that this module was not explicitly or implicitly declared.
  573. // 0x8000 (ACC_MANDATED) - Indicates that this module was implicitly declared
  574. return moduleFlags;
  575. }
  576. /** @return the module version or null if no version information specified */
  577. public String getModuleVersion() {
  578. if (moduleVersionIndex == 0) {
  579. return null;
  580. } else {
  581. return cpool.getConstantUtf8(moduleVersionIndex).getValue();
  582. }
  583. }
  584. }