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.

Compiler.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.tools.reflect;
  17. import javassist.CtClass;
  18. import javassist.ClassPool;
  19. import java.io.PrintStream;
  20. class CompiledClass {
  21. public String classname;
  22. public String metaobject;
  23. public String classobject;
  24. }
  25. /**
  26. * A bytecode translator for reflection.
  27. *
  28. * <p>This translator directly modifies class files on a local disk so that
  29. * the classes represented by those class files are reflective.
  30. * After the modification, the class files can be run with the standard JVM
  31. * without <code>javassist.tools.reflect.Loader</code>
  32. * or any other user-defined class loader.
  33. *
  34. * <p>The modified class files are given as the command-line parameters,
  35. * which are a sequence of fully-qualified class names followed by options:
  36. *
  37. * <p><code>-m <i>classname</i></code> : specifies the class of the
  38. * metaobjects associated with instances of the class followed by
  39. * this option. The default is <code>javassit.reflect.Metaobject</code>.
  40. *
  41. * <p><code>-c <i>classname</i></code> : specifies the class of the
  42. * class metaobjects associated with instances of the class followed by
  43. * this option. The default is <code>javassit.reflect.ClassMetaobject</code>.
  44. *
  45. * <p>If a class name is not followed by any options, the class indicated
  46. * by that class name is not reflective.
  47. *
  48. * <p>For example,
  49. * <pre>% java Compiler Dog -m MetaDog -c CMetaDog Cat -m MetaCat Cow
  50. * </pre>
  51. *
  52. * <p>modifies class files <code>Dog.class</code>, <code>Cat.class</code>,
  53. * and <code>Cow.class</code>.
  54. * The metaobject of a Dog object is a MetaDog object and the class
  55. * metaobject is a CMetaDog object.
  56. * The metaobject of a Cat object is a MetaCat object but
  57. * the class metaobject is a default one.
  58. * Cow objects are not reflective.
  59. *
  60. * <p>Note that if the super class is also made reflective, it must be done
  61. * before the sub class.
  62. *
  63. * @see javassist.tools.reflect.Metaobject
  64. * @see javassist.tools.reflect.ClassMetaobject
  65. * @see javassist.tools.reflect.Reflection
  66. */
  67. public class Compiler {
  68. public static void main(String[] args) throws Exception {
  69. if (args.length == 0) {
  70. help(System.err);
  71. return;
  72. }
  73. CompiledClass[] entries = new CompiledClass[args.length];
  74. int n = parse(args, entries);
  75. if (n < 1) {
  76. System.err.println("bad parameter.");
  77. return;
  78. }
  79. processClasses(entries, n);
  80. }
  81. private static void processClasses(CompiledClass[] entries, int n)
  82. throws Exception
  83. {
  84. Reflection implementor = new Reflection();
  85. ClassPool pool = ClassPool.getDefault();
  86. implementor.start(pool);
  87. for (int i = 0; i < n; ++i) {
  88. CtClass c = pool.get(entries[i].classname);
  89. if (entries[i].metaobject != null
  90. || entries[i].classobject != null) {
  91. String metaobj, classobj;
  92. if (entries[i].metaobject == null)
  93. metaobj = "javassist.tools.reflect.Metaobject";
  94. else
  95. metaobj = entries[i].metaobject;
  96. if (entries[i].classobject == null)
  97. classobj = "javassist.tools.reflect.ClassMetaobject";
  98. else
  99. classobj = entries[i].classobject;
  100. if (!implementor.makeReflective(c, pool.get(metaobj),
  101. pool.get(classobj)))
  102. System.err.println("Warning: " + c.getName()
  103. + " is reflective. It was not changed.");
  104. System.err.println(c.getName() + ": " + metaobj + ", "
  105. + classobj);
  106. }
  107. else
  108. System.err.println(c.getName() + ": not reflective");
  109. }
  110. for (int i = 0; i < n; ++i) {
  111. implementor.onLoad(pool, entries[i].classname);
  112. pool.get(entries[i].classname).writeFile();
  113. }
  114. }
  115. private static int parse(String[] args, CompiledClass[] result) {
  116. int n = -1;
  117. for (int i = 0; i < args.length; ++i) {
  118. String a = args[i];
  119. if (a.equals("-m"))
  120. if (n < 0 || i + 1 > args.length)
  121. return -1;
  122. else
  123. result[n].metaobject = args[++i];
  124. else if (a.equals("-c"))
  125. if (n < 0 || i + 1 > args.length)
  126. return -1;
  127. else
  128. result[n].classobject = args[++i];
  129. else if (a.charAt(0) == '-')
  130. return -1;
  131. else {
  132. CompiledClass cc = new CompiledClass();
  133. cc.classname = a;
  134. cc.metaobject = null;
  135. cc.classobject = null;
  136. result[++n] = cc;
  137. }
  138. }
  139. return n + 1;
  140. }
  141. private static void help(PrintStream out) {
  142. out.println("Usage: java javassist.tools.reflect.Compiler");
  143. out.println(" (<class> [-m <metaobject>] [-c <class metaobject>])+");
  144. }
  145. }