File: SelectionResolutionTestCase.java

package info (click to toggle)
openjdk-17 17.0.17%2B10-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 764,928 kB
  • sloc: java: 5,319,061; xml: 1,291,711; cpp: 1,202,358; ansic: 428,746; asm: 404,978; objc: 20,861; sh: 14,754; javascript: 10,743; python: 6,402; makefile: 2,404; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (452 lines) | stat: -rw-r--r-- 15,720 bytes parent folder | download | duplicates (20)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package selectionresolution;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;

/**
 * One individual test case.  This class also defines a builder, which
 * can be used to build up cases.
 */
public class SelectionResolutionTestCase {

    public enum InvokeInstruction {
        INVOKESTATIC,
        INVOKESPECIAL,
        INVOKEINTERFACE,
        INVOKEVIRTUAL;
    }

    /**
     * The class data (includes interface data).
     */
    public final HashMap<Integer, ClassData> classdata;
    /**
     * The hierarchy shape.
     */
    public final HierarchyShape hier;
    /**
     * The invoke instruction to use.
     */
    public final InvokeInstruction invoke;
    /**
     * Which class is the methodref (or interface methodref).
     */
    public final int methodref;
    /**
     * Which class is the objectref.
     */
    public final int objectref;
    /**
     * Which class is the callsite (this must be a class, not an interface.
     */
    public final int callsite;
    /**
     * The expected result.
     */
    public final Result result;

    private SelectionResolutionTestCase(final HashMap<Integer, ClassData> classdata,
                                        final HierarchyShape hier,
                                        final InvokeInstruction invoke,
                                        final int methodref,
                                        final int objectref,
                                        final int callsite,
                                        final int expected) {
        this.classdata = classdata;
        this.hier = hier;
        this.invoke = invoke;
        this.methodref = methodref;
        this.objectref = objectref;
        this.callsite = callsite;
        this.result = Result.is(expected);
    }

    private SelectionResolutionTestCase(final HashMap<Integer, ClassData> classdata,
                                        final HierarchyShape hier,
                                        final InvokeInstruction invoke,
                                        final int methodref,
                                        final int objectref,
                                        final int callsite,
                                        final Result result) {
        this.classdata = classdata;
        this.hier = hier;
        this.invoke = invoke;
        this.methodref = methodref;
        this.objectref = objectref;
        this.callsite = callsite;
        this.result = result;
    }

    private static int currError = 0;

    private String dumpClasses(final ClassConstruct[] classes)
        throws Exception {
        final String errorDirName = "error_" + currError++;
        final File errorDir = new File(errorDirName);
        errorDir.mkdirs();
        for (int i = 0; i < classes.length; i++) {
            classes[i].writeClass(errorDir);
        }
        try (final FileWriter fos =
             new FileWriter(new File(errorDir, "description.txt"))) {
            fos.write(this.toString());
        }
        return errorDirName;
    }

    /**
     * Run this case, return an error message, or null.
     *
     * @return An error message, or null if the case succeeded.
     */
    public String run() {
        /* Uncomment this line to print EVERY case */
        //System.err.println("Running\n" + this);
        final ClassBuilder builder =
            new ClassBuilder(this, ClassBuilder.ExecutionMode.DIRECT);
        try {
            final ByteCodeClassLoader bcl = new ByteCodeClassLoader();
            final ClassConstruct[] classes = builder.build();

            try {
                bcl.addClasses(classes);
                bcl.loadAll();

                // Grab the callsite class.
                final Class testclass =
                    bcl.findClass(builder.getCallsiteClass().getDottedName());

                // Get the 'test' method out of it and call it.  The
                // return value tess which class that got selected.
                final java.lang.reflect.Method method =
                    testclass.getDeclaredMethod("test");
                final int actual = (Integer) method.invoke(null);
                // Check the result.
                if (!result.complyWith(actual)) {
                    final String dump = dumpClasses(classes);
                    return "Failed:\n" + this + "\nExpected " + result + " got " + actual + "\nClasses written to " + dump;
                }
            } catch (Throwable t) {
                // This catch block is handling exceptions that we
                // might expect to see.
                final Throwable actual = t.getCause();
                if (actual == null) {
                    final String dump = dumpClasses(classes);
                    System.err.println("Unexpected exception in test\n" + this + "\nClasses written to " + dump);
                    throw t;
                } else if (result == null) {
                    final String dump = dumpClasses(classes);
                    return "Failed:\n" + this + "\nUnexpected exception " + actual + "\nClasses written to " + dump;
                } else if (!result.complyWith(actual)) {
                    final String dump = dumpClasses(classes);
                    return "Failed:\n" + this + "\nExpected " + this.result + " got " + actual + "\nClasses written to " + dump;
                }
            }
        } catch(Throwable e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    private static void addPackage(final StringBuilder sb,
                                  final ClassData cd) {
        switch (cd.packageId) {
        case SAME: sb.append("Same."); break;
        case DIFFERENT: sb.append("Different."); break;
        case OTHER: sb.append("Other."); break;
        case PLACEHOLDER: sb.append("_."); break;
        default: throw new RuntimeException("Impossible case");
        }
    }

    public String toString() {
        final StringBuilder sb = new StringBuilder();
        //sb.append("hierarchy:\n" + hier + "\n");
        sb.append("invoke:    " + invoke + "\n");
        if (methodref != -1) {
            if (hier.isClass(methodref)) {
                sb.append("methodref: C" + methodref + "\n");
            } else {
                sb.append("methodref: I" + methodref + "\n");
            }
        }
        if (objectref != -1) {
            if (hier.isClass(objectref)) {
                sb.append("objectref: C" + objectref + "\n");
            } else {
                sb.append("objectref: I" + objectref + "\n");
            }
        }
        if (callsite != -1) {
            if (hier.isClass(callsite)) {
                sb.append("callsite: C" + callsite + "\n");
            } else {
                sb.append("callsite: I" + callsite + "\n");
            }
        }
        sb.append("result: " + result + "\n");
        sb.append("classes:\n\n");

        for(int i = 0; classdata.containsKey(i); i++) {
            final ClassData cd = classdata.get(i);

            if (hier.isClass(i)) {
                sb.append("class ");
                addPackage(sb, cd);
                sb.append("C" + i);
            } else {
                sb.append("interface ");
                addPackage(sb, cd);
                sb.append("I" + i);
            }

            boolean first = true;
            for(final int j : hier.classes()) {
                if (hier.inherits(i, j)) {
                    if (first) {
                        sb.append(" extends C" + j);
                    } else {
                        sb.append(", C" + j);
                    }
                }
            }

            first = true;
            for(final int j : hier.interfaces()) {
                if (hier.inherits(i, j)) {
                    if (first) {
                        sb.append(" implements I" + j);
                    } else {
                        sb.append(", I" + j);
                    }
                }
            }

            sb.append(cd);
        }

        return sb.toString();
    }

    /**
     * A builder, facilitating building up test cases.
     */
    public static class Builder {
        /**
         * A map from class (or interface) id's to ClassDatas
         */
        public final HashMap<Integer, ClassData> classdata;
        /**
         * The hierarchy shape.
         */
        public final HierarchyShape hier;
        /**
         * Which invoke instruction to use.
         */
        public InvokeInstruction invoke;
        /**
         * The id of the methodref (or interface methodref).
         */
        public int methodref = -1;
        /**
         * The id of the object ref.  Note that for the generator
         * framework to work, this must be set to something.  If an
         * objectref isn't used, just set it to the methodref.
         */
        public int objectref = -1;
        /**
         * The id of the callsite.
         */
        public int callsite = -1;
        /**
         * The id of the expected result.  This is used to store the
         * expected resolution result.
         */
        public int expected;
        /**
         * The expected result.  This needs to be set before the final
         * test case is built.
         */
        public Result result;

        /**
         * Create an empty Builder object.
         */
        public Builder() {
            classdata = new HashMap<>();
            hier = new HierarchyShape();
        }

        private Builder(final HashMap<Integer, ClassData> classdata,
                        final HierarchyShape hier,
                        final InvokeInstruction invoke,
                        final int methodref,
                        final int objectref,
                        final int callsite,
                        final int expected,
                        final Result result) {
            this.classdata = classdata;
            this.hier = hier;
            this.invoke = invoke;
            this.methodref = methodref;
            this.objectref = objectref;
            this.callsite = callsite;
            this.expected = expected;
            this.result = result;
        }

        private Builder(final Builder other) {
            this((HashMap<Integer, ClassData>) other.classdata.clone(),
                 other.hier.copy(), other.invoke, other.methodref, other.objectref,
                 other.callsite, other.expected, other.result);
        }

        public SelectionResolutionTestCase build() {
            if (result != null) {
                return new SelectionResolutionTestCase(classdata, hier, invoke,
                                                       methodref, objectref,
                                                       callsite, result);
            } else {
                return new SelectionResolutionTestCase(classdata, hier, invoke,
                                                       methodref, objectref,
                                                       callsite, expected);
            }
        }

        /**
         * Set the expected result.
         */
        public void setResult(final Result result) {
            this.result = result;
        }

        /**
         * Add a class, and return its id.
         *
         * @return The new class' id.
         */
        public int addClass(final ClassData data) {
            final int id = hier.addClass();
            classdata.put(id, data);
            return id;
        }

        /**
         * Add an interface, and return its id.
         *
         * @return The new class' id.
         */
        public int addInterface(final ClassData data) {
            final int id = hier.addInterface();
            classdata.put(id, data);
            return id;
        }

        /**
         * Make a copy of this builder.
         */
        public Builder copy() {
            return new Builder(this);
        }

        public String toString() {
            final StringBuilder sb = new StringBuilder();
            //sb.append("hierarchy:\n" + hier + "\n");
            sb.append("invoke:    " + invoke + "\n");
            if (methodref != -1) {
                if (hier.isClass(methodref)) {
                    sb.append("methodref: C" + methodref + "\n");
                } else {
                    sb.append("methodref: I" + methodref + "\n");
                }
            }
            if (objectref != -1) {
                if (hier.isClass(objectref)) {
                    sb.append("objectref: C" + objectref + "\n");
                } else {
                    sb.append("objectref: I" + objectref + "\n");
                }
            }
            if (callsite != -1) {
                if (hier.isClass(callsite)) {
                    sb.append("callsite: C" + callsite + "\n");
                } else {
                    sb.append("callsite: I" + callsite + "\n");
                }
            }
            if (expected != -1) {
                if (hier.isClass(expected)) {
                    sb.append("expected: C" + expected + "\n");
                } else {
                    sb.append("expected: I" + expected + "\n");
                }
            }
            sb.append("result: " + result + "\n");
            sb.append("classes:\n\n");

            for(int i = 0; classdata.containsKey(i); i++) {
                final ClassData cd = classdata.get(i);

                if (hier.isClass(i)) {
                    sb.append("class ");
                    addPackage(sb, cd);
                    sb.append("C" + i);
                } else {
                    sb.append("interface ");
                    addPackage(sb, cd);
                    sb.append("I" + i);
                }

                boolean first = true;
                for(final int j : hier.classes()) {
                    if (hier.inherits(i, j)) {
                        if (first) {
                            sb.append(" extends C" + j);
                        } else {
                            sb.append(", C" + j);
                        }
                    }
                }

                first = true;
                for(final int j : hier.interfaces()) {
                    if (hier.inherits(i, j)) {
                        if (first) {
                            sb.append(" implements I" + j);
                        } else {
                            sb.append(", I" + j);
                        }
                    }
                }

                sb.append(cd);
            }

            return sb.toString();
        }
    }
}