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