aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/javassist/bytecode/InsertGap0.java
blob: 31fec3aa866d8dbcb47764b7575c9c06a60d1574 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package javassist.bytecode;

import javassist.*;

final class Gap0Example {
    public static int counter = 1;

    public Gap0Example() {}

    public int run(int x) { return counter; }

    private static final int INTVALUE = 100000;
    private int i1, i2, i3, i4, i5, i6, i7, i8, i9, i10;
    private int i11, i12, i13, i14, i15, i16, i17, i18, i19, i20;
    private int i21, i22, i23, i24, i25, i26, i27, i28, i29, i30;
    private int i31, i32, i33, i34, i35, i36, i37, i38, i39, i40;

    public void doit() {

        i1 = INTVALUE;
        i2 = INTVALUE;
        i3 = INTVALUE;
        i4 = INTVALUE;
        i5 = INTVALUE;
        i6 = INTVALUE;
        i7 = INTVALUE;
        i8 = INTVALUE;
        i9 = INTVALUE;
        i10 = INTVALUE;
        i11 = INTVALUE;
        i12 = INTVALUE;
        i13 = INTVALUE;
        i14 = INTVALUE;
        i15 = INTVALUE;
        i16 = INTVALUE;
        i17 = INTVALUE;
        i18 = INTVALUE;
        i19 = INTVALUE;
        i20 = INTVALUE;
        i21 = INTVALUE;
        i22 = INTVALUE;
        i23 = INTVALUE;
        i24 = INTVALUE;
        i25 = INTVALUE;
        i26 = INTVALUE;
        i27 = INTVALUE;
        i28 = INTVALUE;
        i29 = INTVALUE;
        i20 = INTVALUE;
        i21 = INTVALUE;
        i22 = INTVALUE;
        i23 = INTVALUE;
        i24 = INTVALUE;
        i25 = INTVALUE;
        i26 = INTVALUE;
        i27 = INTVALUE;
        i28 = INTVALUE;
        i29 = INTVALUE;
        i30 = INTVALUE;
        i31 = INTVALUE;
        i32 = INTVALUE;
        i33 = INTVALUE;
        i34 = INTVALUE;
        i35 = INTVALUE;
        i36 = INTVALUE;
        i37 = INTVALUE;
        i38 = INTVALUE;
        i39 = INTVALUE;
        i40 = INTVALUE;
    }
}

final class Gap0Example2 {
    public static int counter = 1;

    public Gap0Example2() {}

    public int run(int x) { return counter; }

    private static final int INTVALUE = 100000;
    private int i1, i2, i3, i4, i5, i6, i7, i8, i9, i10;
    private int i11, i12, i13, i14, i15, i16, i17, i18, i19, i20;
    private int i21, i22, i23, i24, i25, i26, i27, i28, i29, i30;
    private int i31, i32, i33, i34, i35, i36, i37, i38, i39, i40;

    public int run2(int x) {
        switch (x) {
        case 0:
        i1 = INTVALUE;
        i2 = INTVALUE;
        i3 = INTVALUE;
        i4 = INTVALUE;
        i5 = INTVALUE;
        i6 = INTVALUE;
        i7 = INTVALUE;
        break;
        case 100:
        i8 = INTVALUE;
        i9 = INTVALUE;
        i10 = INTVALUE;
        i11 = INTVALUE;
        i12 = INTVALUE;
        i13 = INTVALUE;
        i14 = INTVALUE;
        break;
        default:
        i15 = INTVALUE;
        i16 = INTVALUE;
        i17 = INTVALUE;
        if (x > 0) {
        i18 = INTVALUE;
        i19 = INTVALUE;
        i20 = INTVALUE;
        i21 = INTVALUE;
        i22 = INTVALUE;
        i23 = INTVALUE;
        i24 = INTVALUE;
        }
        i25 = INTVALUE;
        i26 = INTVALUE;
        i27 = INTVALUE;
        i28 = INTVALUE;
        i29 = INTVALUE;
        i20 = INTVALUE;
        i21 = INTVALUE;
        i22 = INTVALUE;
        i23 = INTVALUE;
        i24 = INTVALUE;
        i25 = INTVALUE;
        i26 = INTVALUE;
        i27 = INTVALUE;
        i28 = INTVALUE;
        i29 = INTVALUE;
        i30 = INTVALUE;
        i31 = INTVALUE;
        i32 = INTVALUE;
        i33 = INTVALUE;
        i34 = INTVALUE;
        i35 = INTVALUE;
        i36 = INTVALUE;
        i37 = INTVALUE;
        i38 = INTVALUE;
        i39 = INTVALUE;
        i40 = INTVALUE;
        break;
        }
        switch (x) {
        case 0:
            break;
        default:
            return x + 1;
        }

        return x;
    }
}

public final class InsertGap0 extends JvstTestRoot {
    public InsertGap0(String name) {
        super(name);
    }

    public void testExample() throws Throwable {
        ClassPool pool = ClassPool.getDefault();
        CtClass cc = pool.get("javassist.bytecode.Gap0Example");
        CtMethod[] ms = cc.getDeclaredMethods();
        for (int i = 0; i < ms.length; i++) {
            addMethod(ms[i], cc);
        }

        cc.setModifiers(Modifier.PUBLIC | Modifier.FINAL);
        cc.addField(new CtField(CtClass.intType, "i", cc), "++counter");
        boolean p = cc.stopPruning(true);
        cc.writeFile();
        Class c = cc.toClass();
        cc.stopPruning(p);

        Object obj = c.newInstance();
        assertEquals(2, invoke(obj, "run", 0));
    }

    public void testExample2() throws Throwable {
        ClassPool pool = ClassPool.getDefault();
        CtClass cc = pool.get("javassist.bytecode.Gap0Example2");
        CtMethod[] ms = cc.getDeclaredMethods();
        for (int i = 0; i < ms.length; i++) {
            addMethod(ms[i], cc);
        }

        cc.setModifiers(Modifier.PUBLIC | Modifier.FINAL);
        cc.addField(new CtField(CtClass.intType, "i", cc), "++counter");
        boolean p = cc.stopPruning(true);
        cc.writeFile();
        Class c = cc.toClass();
        cc.stopPruning(p);

        Object obj = c.newInstance();
        assertEquals(0, invoke(obj, "run2", 0));
    }

    private void addMethod(CtMethod method, CtClass target)
        throws CannotCompileException, NotFoundException {

        CtClass[] ts = method.getParameterTypes();
        CtClass[] newts = new CtClass[ts.length + 1];
        for (int i = 0; i < ts.length; i++) {
            newts[i] = ts[i];
        }
        ClassPool p = method.getDeclaringClass().getClassPool();
        newts[ts.length] = target;

        CtMethod m =
            CtNewMethod.make(
                method.getModifiers(),
                method.getReturnType(),
                method.getName(),
                newts,
                method.getExceptionTypes(),
                null,
                method.getDeclaringClass());

        m.setBody(method, null);

        CodeAttribute ca = m.getMethodInfo().getCodeAttribute();
        ca.setMaxLocals(ca.getMaxLocals() + 1);
        target.addMethod(m);
    }
}