aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/javassist/compiler/CompTest.java
blob: 4154935fcc4c6fe24d3ca3473072075c442a2f22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package javassist.compiler;

import junit.framework.*;
import javassist.*;
import java.util.*;
import javassist.compiler.ast.*;

/*
public class Test{

  public static void main(String[] args) throws Exception {
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.get("Print");
    CtMethod cm = cc.getDeclaredMethod("print");
    cm.insertBefore("{((Advice)(new
    HelloAspect().getAdvice().get(0))).print();}");
    //cm.insertBefore("{new Advice(\"advice\").print();}");
    pool.write(cc.getName());
    new Print().print();
  }
}
*/

public class CompTest extends TestCase {
    ClassPool sloader;

    public CompTest(String name) {
         super(name);
    }

    protected void print(String msg) {
        System.out.println(msg);
    }

    protected void setUp() throws Exception {
        sloader = ClassPool.getDefault();
    }

    public void testCast() throws Exception {
        Javac jc = new Javac(sloader.get("javassist.compiler.Print"));
        jc.compileStmnt(
              "{((javassist.compiler.Advice)"
            + "  (new javassist.compiler.HelloAspect().getAdvice().get(0)))"
            + "  .print();}");
    }

    public void testStaticMember() throws Exception {
        String src = "javassist.compiler.Print#k = 3;";
        Parser p = new Parser(new Lex(src));
        SymbolTable stb = new SymbolTable();
        Stmnt s = p.parseStatement(stb);
        Expr expr = (Expr)s.getLeft().getLeft();
        assertEquals('#', expr.getOperator());
        assertEquals("javassist.compiler.Print",
                     ((Symbol)expr.oprand1()).get());
    }

    public void testStaticMember2() throws Exception {
        String src = "String#k = 3;";
        Parser p = new Parser(new Lex(src));
        SymbolTable stb = new SymbolTable();
        Stmnt s = p.parseStatement(stb);
        Expr expr = (Expr)s.getLeft().getLeft();
        assertEquals('#', expr.getOperator());
        assertEquals("String", ((Symbol)expr.oprand1()).get());
    }

    public void testDoubleConst() {
        Lex lex = new Lex("7d 0.3d 5e-2d .3d 3e2; .4D 2e-1D;");
        assertEquals(TokenId.DoubleConstant, lex.get());
        assertEquals(TokenId.DoubleConstant, lex.get());
        assertEquals(TokenId.DoubleConstant, lex.get());
        assertEquals(TokenId.DoubleConstant, lex.get());
        assertEquals(TokenId.DoubleConstant, lex.get());
        assertEquals(';', lex.get());
        assertEquals(TokenId.DoubleConstant, lex.get());
        assertEquals(TokenId.DoubleConstant, lex.get());
        assertEquals(';', lex.get());
    }

    public void testRecordLocalVar() throws Exception {
        Javac jv = new Javac(sloader.get("javassist.compiler.Print"));
        jv.gen.recordVariable("I", "i0", 0, jv.stable);
        isRightDecl((Declarator)jv.stable.get("i0"), TokenId.INT, 0, null); 
        jv.gen.recordVariable("[I", "i1", 1, jv.stable);
        isRightDecl((Declarator)jv.stable.get("i1"), TokenId.INT, 1, null); 
        jv.gen.recordVariable("[[D", "i2", 2, jv.stable);
        isRightDecl((Declarator)jv.stable.get("i2"), TokenId.DOUBLE, 2, null); 
        jv.gen.recordVariable("Ljava/lang/String;", "i3", 4, jv.stable);
        isRightDecl((Declarator)jv.stable.get("i3"), TokenId.CLASS, 0,
                    "java/lang/String");
        jv.gen.recordVariable("[LTest;", "i4", 5, jv.stable);
        isRightDecl((Declarator)jv.stable.get("i4"), TokenId.CLASS, 1,
                    "Test");
        jv.gen.recordVariable("[[LTest;", "i5", 6, jv.stable);
        isRightDecl((Declarator)jv.stable.get("i5"), TokenId.CLASS, 2,
                    "Test");
    }

    private void isRightDecl(Declarator d, int type, int dim, String cname) {
        assertEquals(type, d.getType());
        assertEquals(dim, d.getArrayDim());
        assertEquals(cname, d.getClassName());
    }

    public void testArgTypesToString() {
        String s;
        s = TypeChecker.argTypesToString(new int[0], new int[0], new String[0]);
        assertEquals("()", s);
        s = TypeChecker.argTypesToString(new int[] { TokenId.INT, TokenId.CHAR, TokenId.CLASS },
                                         new int[] { 0, 1, 0 },
                                         new String[] { null, null, "String" });
        assertEquals("(int,char[],String)", s);
    }

    public static Test suite() {
        TestSuite suite = new TestSuite("Compiler Tests");
        suite.addTestSuite(CompTest.class);
        return suite;
    }
}

class Print{
    public void print(){ System.out.println("@@@"); }
    public static int k;
}

@SuppressWarnings({"rawtypes","unchecked"})
class HelloAspect{
  List list;
  
  HelloAspect() {
    list = new LinkedList(); 
    list.add(new Advice("advice"));
  }
  
  List getAdvice() {
    return list;
  }
}

class Advice{
  String str = "";
  Advice(String str) {
    this.str = str;
  }
  void print(){
      System.out.println(str);
  }
}