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.

CompTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package javassist.compiler;
  2. import junit.framework.*;
  3. import javassist.*;
  4. import java.util.*;
  5. import javassist.compiler.ast.*;
  6. /*
  7. public class Test{
  8. public static void main(String[] args) throws Exception {
  9. ClassPool pool = ClassPool.getDefault();
  10. CtClass cc = pool.get("Print");
  11. CtMethod cm = cc.getDeclaredMethod("print");
  12. cm.insertBefore("{((Advice)(new
  13. HelloAspect().getAdvice().get(0))).print();}");
  14. //cm.insertBefore("{new Advice(\"advice\").print();}");
  15. pool.write(cc.getName());
  16. new Print().print();
  17. }
  18. }
  19. */
  20. public class CompTest extends TestCase {
  21. ClassPool sloader;
  22. public CompTest(String name) {
  23. super(name);
  24. }
  25. protected void print(String msg) {
  26. System.out.println(msg);
  27. }
  28. protected void setUp() throws Exception {
  29. sloader = ClassPool.getDefault();
  30. }
  31. public void testCast() throws Exception {
  32. Javac jc = new Javac(sloader.get("javassist.compiler.Print"));
  33. jc.compileStmnt(
  34. "{((javassist.compiler.Advice)"
  35. + " (new javassist.compiler.HelloAspect().getAdvice().get(0)))"
  36. + " .print();}");
  37. }
  38. public void testStaticMember() throws Exception {
  39. String src = "javassist.compiler.Print#k = 3;";
  40. Parser p = new Parser(new Lex(src));
  41. SymbolTable stb = new SymbolTable();
  42. Stmnt s = p.parseStatement(stb);
  43. Expr expr = (Expr)s.getLeft().getLeft();
  44. assertEquals('#', expr.getOperator());
  45. assertEquals("javassist.compiler.Print",
  46. ((Symbol)expr.oprand1()).get());
  47. }
  48. public void testStaticMember2() throws Exception {
  49. String src = "String#k = 3;";
  50. Parser p = new Parser(new Lex(src));
  51. SymbolTable stb = new SymbolTable();
  52. Stmnt s = p.parseStatement(stb);
  53. Expr expr = (Expr)s.getLeft().getLeft();
  54. assertEquals('#', expr.getOperator());
  55. assertEquals("String", ((Symbol)expr.oprand1()).get());
  56. }
  57. public void testDoubleConst() {
  58. Lex lex = new Lex("7d 0.3d 5e-2d .3d 3e2; .4D 2e-1D;");
  59. assertEquals(TokenId.DoubleConstant, lex.get());
  60. assertEquals(TokenId.DoubleConstant, lex.get());
  61. assertEquals(TokenId.DoubleConstant, lex.get());
  62. assertEquals(TokenId.DoubleConstant, lex.get());
  63. assertEquals(TokenId.DoubleConstant, lex.get());
  64. assertEquals(';', lex.get());
  65. assertEquals(TokenId.DoubleConstant, lex.get());
  66. assertEquals(TokenId.DoubleConstant, lex.get());
  67. assertEquals(';', lex.get());
  68. }
  69. public void testRecordLocalVar() throws Exception {
  70. Javac jv = new Javac(sloader.get("javassist.compiler.Print"));
  71. jv.gen.recordVariable("I", "i0", 0, jv.stable);
  72. isRightDecl((Declarator)jv.stable.get("i0"), TokenId.INT, 0, null);
  73. jv.gen.recordVariable("[I", "i1", 1, jv.stable);
  74. isRightDecl((Declarator)jv.stable.get("i1"), TokenId.INT, 1, null);
  75. jv.gen.recordVariable("[[D", "i2", 2, jv.stable);
  76. isRightDecl((Declarator)jv.stable.get("i2"), TokenId.DOUBLE, 2, null);
  77. jv.gen.recordVariable("Ljava/lang/String;", "i3", 4, jv.stable);
  78. isRightDecl((Declarator)jv.stable.get("i3"), TokenId.CLASS, 0,
  79. "java/lang/String");
  80. jv.gen.recordVariable("[LTest;", "i4", 5, jv.stable);
  81. isRightDecl((Declarator)jv.stable.get("i4"), TokenId.CLASS, 1,
  82. "Test");
  83. jv.gen.recordVariable("[[LTest;", "i5", 6, jv.stable);
  84. isRightDecl((Declarator)jv.stable.get("i5"), TokenId.CLASS, 2,
  85. "Test");
  86. }
  87. private void isRightDecl(Declarator d, int type, int dim, String cname) {
  88. assertEquals(type, d.getType());
  89. assertEquals(dim, d.getArrayDim());
  90. assertEquals(cname, d.getClassName());
  91. }
  92. public void testArgTypesToString() {
  93. String s;
  94. s = TypeChecker.argTypesToString(new int[0], new int[0], new String[0], 0);
  95. assertEquals("()", s);
  96. s = TypeChecker.argTypesToString(new int[] { TokenId.INT, TokenId.CHAR, TokenId.CLASS },
  97. new int[] { 0, 1, 0 },
  98. new String[] { null, null, "String" }, 0);
  99. assertEquals("(int,char[],String)", s);
  100. }
  101. public static Test suite() {
  102. TestSuite suite = new TestSuite("Compiler Tests");
  103. suite.addTestSuite(CompTest.class);
  104. return suite;
  105. }
  106. }
  107. class Print{
  108. public void print(){ System.out.println("@@@"); }
  109. public static int k;
  110. }
  111. @SuppressWarnings({"rawtypes","unchecked"})
  112. class HelloAspect{
  113. List list;
  114. HelloAspect() {
  115. list = new LinkedList();
  116. list.add(new Advice("advice"));
  117. }
  118. List getAdvice() {
  119. return list;
  120. }
  121. }
  122. class Advice{
  123. String str = "";
  124. Advice(String str) {
  125. this.str = str;
  126. }
  127. void print(){
  128. System.out.println(str);
  129. }
  130. }