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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 sample.preproc;
  17. import java.io.IOException;
  18. import java.io.BufferedReader;
  19. import java.io.FileReader;
  20. import java.io.BufferedWriter;
  21. import java.io.FileWriter;
  22. import java.util.Vector;
  23. import javassist.CannotCompileException;
  24. import javassist.CtClass;
  25. import javassist.ClassPool;
  26. /**
  27. * This is a preprocessor for Java source programs using annotated
  28. * import declarations.
  29. *
  30. * <ul><pre>
  31. * import <i>class-name</i> by <i>assistant-name</i> [(<i>arg1, arg2, ...</i>)]
  32. * </pre></ul>
  33. *
  34. * <p>To process this annotation, run this class as follows:
  35. *
  36. * <ul><pre>
  37. * java sample.preproc.Compiler sample.j
  38. * </pre></ul>
  39. *
  40. * <p>This command produces <code>sample.java</code>, which only includes
  41. * regular import declarations. Also, the Javassist program
  42. * specified by <i>assistant-name</i> is executed so that it produces
  43. * class files under the <code>./tmpjvst</code> directory. The class
  44. * specified by <i>assistant-name</i> must implement
  45. * <code>sample.preproc.Assistant</code>.
  46. *
  47. * @see sample.preproc.Assistant
  48. */
  49. public class Compiler {
  50. protected BufferedReader input;
  51. protected BufferedWriter output;
  52. protected ClassPool classPool;
  53. /**
  54. * Constructs a <code>Compiler</code> with a source file.
  55. *
  56. * @param inputname the name of the source file.
  57. */
  58. public Compiler(String inputname) throws CannotCompileException {
  59. try {
  60. input = new BufferedReader(new FileReader(inputname));
  61. }
  62. catch (IOException e) {
  63. throw new CannotCompileException("cannot open: " + inputname);
  64. }
  65. String outputname = getOutputFilename(inputname);
  66. if (outputname.equals(inputname))
  67. throw new CannotCompileException("invalid source name: "
  68. + inputname);
  69. try {
  70. output = new BufferedWriter(new FileWriter(outputname));
  71. }
  72. catch (IOException e) {
  73. throw new CannotCompileException("cannot open: " + outputname);
  74. }
  75. classPool = ClassPool.getDefault();
  76. }
  77. /**
  78. * Starts preprocessing.
  79. */
  80. public void process() throws IOException, CannotCompileException {
  81. int c;
  82. CommentSkipper reader = new CommentSkipper(input, output);
  83. while ((c = reader.read()) != -1) {
  84. output.write(c);
  85. if (c == 'p') {
  86. if (skipPackage(reader))
  87. break;
  88. }
  89. else if (c == 'i')
  90. readImport(reader);
  91. else if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
  92. break;
  93. }
  94. while ((c = input.read()) != -1)
  95. output.write(c);
  96. input.close();
  97. output.close();
  98. }
  99. private boolean skipPackage(CommentSkipper reader) throws IOException {
  100. int c;
  101. c = reader.read();
  102. output.write(c);
  103. if (c != 'a')
  104. return true;
  105. while ((c = reader.read()) != -1) {
  106. output.write(c);
  107. if (c == ';')
  108. break;
  109. }
  110. return false;
  111. }
  112. private void readImport(CommentSkipper reader)
  113. throws IOException, CannotCompileException
  114. {
  115. int word[] = new int[5];
  116. int c;
  117. for (int i = 0; i < 5; ++i) {
  118. word[i] = reader.read();
  119. output.write(word[i]);
  120. }
  121. if (word[0] != 'm' || word[1] != 'p' || word[2] != 'o'
  122. || word[3] != 'r' || word[4] != 't')
  123. return; // syntax error?
  124. c = skipSpaces(reader, ' ');
  125. StringBuffer classbuf = new StringBuffer();
  126. while (c != ' ' && c != '\t' && c != '\n' && c != '\r'
  127. && c != ';' && c != -1) {
  128. classbuf.append((char)c);
  129. c = reader.read();
  130. }
  131. String importclass = classbuf.toString();
  132. c = skipSpaces(reader, c);
  133. if (c == ';') {
  134. output.write(importclass);
  135. output.write(';');
  136. return;
  137. }
  138. if (c != 'b')
  139. syntaxError(importclass);
  140. reader.read(); // skip 'y'
  141. StringBuffer assistant = new StringBuffer();
  142. Vector args = new Vector();
  143. c = readAssistant(reader, importclass, assistant, args);
  144. c = skipSpaces(reader, c);
  145. if (c != ';')
  146. syntaxError(importclass);
  147. runAssistant(importclass, assistant.toString(), args);
  148. }
  149. void syntaxError(String importclass) throws CannotCompileException {
  150. throw new CannotCompileException("Syntax error. Cannot import "
  151. + importclass);
  152. }
  153. int readAssistant(CommentSkipper reader, String importclass,
  154. StringBuffer assistant, Vector args)
  155. throws IOException, CannotCompileException
  156. {
  157. int c = readArgument(reader, assistant);
  158. c = skipSpaces(reader, c);
  159. if (c == '(') {
  160. do {
  161. StringBuffer arg = new StringBuffer();
  162. c = readArgument(reader, arg);
  163. args.addElement(arg.toString());
  164. c = skipSpaces(reader, c);
  165. } while (c == ',');
  166. if (c != ')')
  167. syntaxError(importclass);
  168. return reader.read();
  169. }
  170. return c;
  171. }
  172. int readArgument(CommentSkipper reader, StringBuffer buf)
  173. throws IOException
  174. {
  175. int c = skipSpaces(reader, ' ');
  176. while ('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
  177. || '0' <= c && c <= '9' || c == '.' || c == '_') {
  178. buf.append((char)c);
  179. c = reader.read();
  180. }
  181. return c;
  182. }
  183. int skipSpaces(CommentSkipper reader, int c) throws IOException {
  184. while (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
  185. if (c == '\n' || c == '\r')
  186. output.write(c);
  187. c = reader.read();
  188. }
  189. return c;
  190. }
  191. /**
  192. * Is invoked if this compiler encoutenrs:
  193. *
  194. * <ul><pre>
  195. * import <i>class name</i> by <i>assistant</i> (<i>args1</i>, <i>args2</i>, ...);
  196. * </pre></ul>
  197. *
  198. * @param classname class name
  199. * @param assistantname assistant
  200. * @param argv args1, args2, ...
  201. */
  202. private void runAssistant(String importname, String assistantname,
  203. Vector argv)
  204. throws IOException, CannotCompileException
  205. {
  206. Class assistant;
  207. Assistant a;
  208. int s = argv.size();
  209. String[] args = new String[s];
  210. for (int i = 0; i < s; ++i)
  211. args[i] = (String)argv.elementAt(i);
  212. try {
  213. assistant = Class.forName(assistantname);
  214. }
  215. catch (ClassNotFoundException e) {
  216. throw new CannotCompileException("Cannot find " + assistantname);
  217. }
  218. try {
  219. a = (Assistant)assistant.newInstance();
  220. }
  221. catch (Exception e) {
  222. throw new CannotCompileException(e);
  223. }
  224. CtClass[] imports = a.assist(classPool, importname, args);
  225. s = imports.length;
  226. if (s < 1)
  227. output.write(" java.lang.Object;");
  228. else {
  229. output.write(' ');
  230. output.write(imports[0].getName());
  231. output.write(';');
  232. for (int i = 1; i < s; ++i) {
  233. output.write(" import ");
  234. output.write(imports[1].getName());
  235. output.write(';');
  236. }
  237. }
  238. }
  239. private String getOutputFilename(String input) {
  240. int i = input.lastIndexOf('.');
  241. if (i < 0)
  242. i = input.length();
  243. return input.substring(0, i) + ".java";
  244. }
  245. public static void main(String[] args) {
  246. if (args.length > 0)
  247. try {
  248. Compiler c = new Compiler(args[0]);
  249. c.process();
  250. }
  251. catch (IOException e) {
  252. System.err.println(e);
  253. }
  254. catch (CannotCompileException e) {
  255. System.err.println(e);
  256. }
  257. else {
  258. System.err.println("Javassist version " + CtClass.version);
  259. System.err.println("No source file is specified.");
  260. }
  261. }
  262. }
  263. class CommentSkipper {
  264. private BufferedReader input;
  265. private BufferedWriter output;
  266. public CommentSkipper(BufferedReader reader, BufferedWriter writer) {
  267. input = reader;
  268. output = writer;
  269. }
  270. public int read() throws IOException {
  271. int c;
  272. while ((c = input.read()) != -1)
  273. if (c != '/')
  274. return c;
  275. else {
  276. c = input.read();
  277. if (c == '/')
  278. skipCxxComments();
  279. else if (c == '*')
  280. skipCComments();
  281. else
  282. output.write('/');
  283. }
  284. return c;
  285. }
  286. private void skipCxxComments() throws IOException {
  287. int c;
  288. output.write("//");
  289. while ((c = input.read()) != -1) {
  290. output.write(c);
  291. if (c == '\n' || c == '\r')
  292. break;
  293. }
  294. }
  295. private void skipCComments() throws IOException {
  296. int c;
  297. boolean star = false;
  298. output.write("/*");
  299. while ((c = input.read()) != -1) {
  300. output.write(c);
  301. if (c == '*')
  302. star = true;
  303. else if(star && c == '/')
  304. break;
  305. else
  306. star = false;
  307. }
  308. }
  309. }