File: T6889255.java

package info (click to toggle)
libnb-javaparser-java 7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 35,940 kB
  • ctags: 55,696
  • sloc: java: 264,137; xml: 2,781; sh: 1,135; makefile: 425
file content (485 lines) | stat: -rw-r--r-- 24,165 bytes parent folder | download
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * Copyright (c) 2009, 2013, 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.
 */

/*
 * @test
 * @bug 6889255
 * @summary ClassReader does not read parameter names correctly
 */

import java.io.*;
import java.util.*;
import javax.tools.StandardLocation;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.jvm.ClassReader;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Names;

public class T6889255 {
    boolean testInterfaces = true;
    boolean testSyntheticMethods = true;

    // The following enums control the generation of the test methods to be compiled.
    enum GenericKind {
        NOT_GENERIC,
        GENERIC
    };

    enum ClassKind {
        CLASS("Clss"),
        INTERFACE("Intf"),
        ENUM("Enum");
        final String base;
        ClassKind(String base) { this.base = base; }
    };

    enum NestedKind {
        /** Declare methods inside the outermost container. */
        NONE,
        /** Declare methods inside a container with a 'static' modifier. */
        NESTED,
        /** Declare methods inside a container without a 'static' modifier. */
        INNER,
        /** Declare methods inside a local class in an initializer. */
        INIT_LOCAL,
        /** Declare methods inside an anonymous class in an initializer. */
        INIT_ANON,
        /** Declare methods inside a local class in a method. */
        METHOD_LOCAL,
        /** Declare methods inside an anonymous class in a method. */
        METHOD_ANON
    };

    enum MethodKind {
        ABSTRACT,
        CONSTRUCTOR,
        METHOD,
        STATIC_METHOD,
        BRIDGE_METHOD
    };

    enum FinalKind {
        /** Method body does not reference external final variables. */
        NO_FINAL,
        /** Method body references external final variables. */
        USE_FINAL
    };

    public static void main(String... args) throws Exception {
        new T6889255().run();
    }

    void run() throws Exception {
        genTest();

        test("no-args", false);
        test("g",       true,  "-g");

        if (errors > 0)
            throw new Exception(errors + " errors found");
    }

    /**
     * Create a file containing lots of method definitions to be tested.
     * There are 3 sets of nested loops that generate the methods.
     * 1. The outermost set declares [generic] (class | interface | enum)
     * 2. The middle set declares [(nested | inner | anon | local)] class
     * 3. The innermost set declares
     *      [generic] (constructor|method|static-method|bridge-method) [using final variables in outer scope]
     * Invalid combinations are filtered out.
     */
    void genTest() throws Exception {
        BufferedWriter out = new BufferedWriter(new FileWriter("Test.java"));

        // This interface is used to force bridge methods to be generated, by
        // implementing its methods with subtypes of Object
        out.write("interface Base {\n");
        out.write("    Object base_m1(int i1);\n");
        out.write("    Object base_m2(int i1);\n");
        out.write("}\n");

        int outerNum = 0;
        // Outermost set of loops, to generate a top level container
        for (GenericKind outerGenericKind: GenericKind.values()) {
            for (ClassKind outerClassKind: ClassKind.values()) {
                if (outerGenericKind == GenericKind.GENERIC && outerClassKind == ClassKind.ENUM)
                    continue;
                String outerClassName = outerClassKind.base + (outerNum++);
                String outerTypeArg = outerClassKind.toString().charAt(0) + "T";
                if (outerClassKind == ClassKind.CLASS)
                    out.write("abstract ");
                out.write(outerClassKind.toString().toLowerCase() + " " + outerClassName);
                if (outerGenericKind == GenericKind.GENERIC)
                    out.write("<" + outerTypeArg + ">");
                if (outerClassKind == ClassKind.INTERFACE)
                    out.write(" extends Base");
                else
                    out.write(" implements Base");
                out.write(" {\n");
                if (outerClassKind == ClassKind.ENUM) {
                    out.write("    E1(0,0,0), E2(0,0,0), E3(0,0,0);\n");
                    out.write("    " + outerClassName + "(int i1, int i2, int i3) { }\n");
                }
                // Middle set of loops, to generate an optional nested container
                int nestedNum = 0;
                int methodNum = 0;
                for (GenericKind nestedGenericKind: GenericKind.values()) {
                    nextNestedKind:
                    for (NestedKind nestedKind: NestedKind.values()) {
                        // if the nested kind is none, there is no point iterating over all
                        // nested generic kinds, so arbitarily limit it to just one kind
                        if (nestedKind == NestedKind.NONE && nestedGenericKind != GenericKind.NOT_GENERIC)
                            continue;
                        if ((nestedKind == NestedKind.METHOD_ANON || nestedKind == NestedKind.INIT_ANON)
                                && nestedGenericKind == GenericKind.GENERIC)
                            continue;
                        String indent = "    ";
                        boolean haveFinal = false;
                        switch (nestedKind) {
                            case METHOD_ANON: case METHOD_LOCAL:
                                if (outerClassKind == ClassKind.INTERFACE)
                                    continue nextNestedKind;
                                out.write(indent + "void m" +  + (nestedNum++) + "() {\n");
                                indent += "    ";
                                out.write(indent + "final int fi1 = 0;\n");
                                haveFinal = true;
                                break;
                            case INIT_ANON: case INIT_LOCAL:
                                if (outerClassKind == ClassKind.INTERFACE)
                                    continue nextNestedKind;
                                out.write(indent + "{\n");
                                indent += "    ";
                                break;
                        }
                        for (ClassKind nestedClassKind: ClassKind.values()) {
                            if ((nestedGenericKind == GenericKind.GENERIC)
                                    && (nestedClassKind == ClassKind.ENUM))
                                continue;
                            if ((nestedKind == NestedKind.METHOD_ANON || nestedKind == NestedKind.METHOD_LOCAL
                                    || nestedKind == NestedKind.INIT_ANON || nestedKind == NestedKind.INIT_LOCAL)
                                    && nestedClassKind != ClassKind.CLASS)
                                continue;
                            // if the nested kind is none, there is no point iterating over all
                            // nested class kinds, so arbitarily limit it to just one kind
                            if (nestedKind == NestedKind.NONE && nestedClassKind != ClassKind.CLASS)
                                continue;

                            ClassKind methodClassKind;
                            String methodClassName;
                            boolean allowAbstractMethods;
                            boolean allowStaticMethods;
                            switch (nestedKind) {
                                case NONE:
                                    methodClassKind = outerClassKind;
                                    methodClassName = outerClassName;
                                    allowAbstractMethods = (outerClassKind == ClassKind.CLASS);
                                    allowStaticMethods = (outerClassKind != ClassKind.INTERFACE);
                                    break;
                                case METHOD_ANON:
                                case INIT_ANON:
                                    out.write(indent + "new Base() {\n");
                                    indent += "    ";
                                    methodClassKind = ClassKind.CLASS;
                                    methodClassName = null;
                                    allowAbstractMethods = false;
                                    allowStaticMethods = false;
                                    break;
                                default: { // INNER, NESTED, LOCAL
                                    String nestedClassName = "N" + nestedClassKind.base + (nestedNum++);
                                    String nestedTypeArg = nestedClassKind.toString().charAt(0) + "T";
                                    out.write(indent);
                                    if (nestedKind == NestedKind.NESTED)
                                        out.write("static ");
                                    if (nestedClassKind == ClassKind.CLASS)
                                        out.write("abstract ");
                                    out.write(nestedClassKind.toString().toLowerCase() + " " + nestedClassName);
                                    if (nestedGenericKind == GenericKind.GENERIC)
                                        out.write("<" + nestedTypeArg + ">");
                                    if (nestedClassKind == ClassKind.INTERFACE)
                                        out.write(" extends Base ");
                                    else
                                        out.write(" implements Base ");
                                    out.write(" {\n");
                                    indent += "    ";
                                    if (nestedClassKind == ClassKind.ENUM) {
                                        out.write(indent + "E1(0,0,0), E2(0,0,0), E3(0,0,0);\n");
                                        out.write(indent + nestedClassName + "(int i1, int i2, int i3) { }\n");
                                    }
                                    methodClassKind = nestedClassKind;
                                    methodClassName = nestedClassName;
                                    allowAbstractMethods = (nestedClassKind == ClassKind.CLASS);
                                    allowStaticMethods = (nestedKind == NestedKind.NESTED && nestedClassKind != ClassKind.INTERFACE);
                                    break;
                                }
                            }

                            // Innermost loops, to generate methods
                            for (GenericKind methodGenericKind: GenericKind.values()) {
                                for (FinalKind finalKind: FinalKind.values()) {
                                    for (MethodKind methodKind: MethodKind.values()) {
//                                        out.write("// " + outerGenericKind
//                                                + " " + outerClassKind
//                                                + " " + nestedKind
//                                                + " " + nestedGenericKind
//                                                + " " + nestedClassKind
//                                                + " " + methodGenericKind
//                                                + " " + finalKind
//                                                + " " + methodKind
//                                                + "\n");
                                        switch (methodKind) {
                                            case CONSTRUCTOR:
                                                if (nestedKind == NestedKind.METHOD_ANON || nestedKind == NestedKind.INIT_ANON)
                                                    break;
                                                if (methodClassKind != ClassKind.CLASS)
                                                    break;
                                                if (finalKind == FinalKind.USE_FINAL && !haveFinal)
                                                    break;
                                                out.write(indent);
                                                if (methodGenericKind == GenericKind.GENERIC) {
                                                    out.write("<CT> " + methodClassName + "(CT c1, CT c2");
                                                } else {
                                                    out.write(methodClassName + "(boolean b1, char c2");
                                                }
                                                if (finalKind == FinalKind.USE_FINAL) {
                                                    // add a dummy parameter to avoid duplicate declaration
                                                    out.write(", int i3) { int i = fi1; }\n");
                                                } else
                                                    out.write(") { }\n");
                                                break;
                                            case ABSTRACT:
                                                if (!allowAbstractMethods)
                                                    continue;
                                                // fallthrough
                                            case METHOD:
                                                if (finalKind == FinalKind.USE_FINAL && !haveFinal)
                                                    break;
                                                out.write(indent);
                                                if (methodKind == MethodKind.ABSTRACT)
                                                    out.write("abstract ");
                                                if (methodGenericKind == GenericKind.GENERIC)
                                                    out.write("<MT> ");
                                                out.write("void m" + (methodNum++) + "(int i1, long l2, float f3)");
                                                if (methodKind == MethodKind.ABSTRACT || methodClassKind == ClassKind.INTERFACE)
                                                    out.write(";\n");
                                                else {
                                                    out.write(" {");
                                                    if (finalKind == FinalKind.USE_FINAL)
                                                        out.write(" int i = fi1;");
                                                    out.write(" }\n");
                                                }
                                                break;
                                            case BRIDGE_METHOD:
                                                if (methodGenericKind == GenericKind.GENERIC)
                                                    break;
                                                out.write(indent);
                                                // methods Base.base_m1 and Base.base_m2 are declared for the
                                                // benefit of bridge methods. They need to be implemented
                                                // whether or not a final variable is used.
                                                String methodName = (finalKind == FinalKind.NO_FINAL ? "base_m1" : "base_m2");
                                                out.write("public String " + methodName + "(int i1)");
                                                if (methodClassKind == ClassKind.INTERFACE)
                                                    out.write(";\n");
                                                else {
                                                    out.write(" {");
                                                    if (finalKind == FinalKind.USE_FINAL && haveFinal)
                                                        out.write(" int i = fi1;");
                                                    out.write(" return null; }\n");
                                                }
                                                break;
                                            case STATIC_METHOD:
                                                if (!allowStaticMethods)
                                                    break;
                                                if (finalKind == FinalKind.USE_FINAL && !haveFinal)
                                                    break;
                                                out.write(indent + "static ");
                                                if (methodGenericKind == GenericKind.GENERIC)
                                                    out.write("<MT> ");
                                                out.write("void m" + (methodNum++) + "(int i1, long l2, float f3) {");
                                                if (finalKind == FinalKind.USE_FINAL)
                                                    out.write(" int i = fi1;");
                                                out.write(" }\n");
                                                break;
                                        }

                                    }
                                }
                            }
                            if (nestedKind != NestedKind.NONE) {
                                indent = indent.substring(0, indent.length() - 4);
                                out.write(indent + "};\n");
                            }
                        }
                        switch (nestedKind) {
                            case METHOD_ANON: case METHOD_LOCAL:
                            case INIT_ANON: case INIT_LOCAL:
                                indent = indent.substring(0, indent.length() - 4);
                                out.write(indent + "}\n\n");
                        }
                    }
                }
                out.write("}\n\n");
            }
        }
        out.close();
    }


    void test(String testName, boolean expectNames, String... opts) throws Exception {
        System.err.println("Test " + testName
                + ": expectNames:" + expectNames
                + " javacOpts:" + Arrays.asList(opts));

        File outDir = new File(testName);
        outDir.mkdirs();
        compile(outDir, opts);

        Context ctx = new Context();
        JavacFileManager fm = new JavacFileManager(ctx, true, null);
        fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(outDir));
        ClassReader cr = ClassReader.instance(ctx);
        cr.saveParameterNames = true;
        Names names = Names.instance(ctx);

        Set<String> classes = getTopLevelClasses(outDir);
        Deque<String> work = new LinkedList<String>(classes);
        String classname;
        while ((classname = work.poll()) != null) {
            System.err.println("Checking class " + classname);
            ClassSymbol sym = cr.enterClass(names.table.fromString(classname));
            sym.complete();

            if ((sym.flags() & Flags.INTERFACE) != 0 && !testInterfaces)
                continue;

            for (Scope.Entry e = sym.members_field.elems; e != null; e = e.sibling) {
                System.err.println("Checking member " + e.sym);
                switch (e.sym.kind) {
                    case Kinds.TYP: {
                        String name = e.sym.flatName().toString();
                        if (!classes.contains(name)) {
                            classes.add(name);
                            work.add(name);
                        }
                        break;
                    }
                    case Kinds.MTH:
                        verify((MethodSymbol) e.sym, expectNames);
                        break;
                }

            }
        }
    }

    void verify(MethodSymbol m, boolean expectNames) {
        if ((m.flags() & Flags.SYNTHETIC) != 0 && !testSyntheticMethods)
            return;

        //System.err.println("verify: " + m.params());
        int i = 1;
        for (VarSymbol v: m.params()) {
            String expectName;
            if (expectNames)
                expectName = getExpectedName(v, i);
            else
                expectName = "arg" + (i - 1);
            checkEqual(expectName, v.name.toString());
            i++;
        }
    }

    String getExpectedName(VarSymbol v, int i) {
        // special cases:
        // synthetic method
        if (((v.owner.owner.flags() & Flags.ENUM) != 0)
                && v.owner.name.toString().equals("valueOf"))
            return "name";
        // interfaces don't have saved names
        // -- no Code attribute for the LocalVariableTable attribute
        if ((v.owner.owner.flags() & Flags.INTERFACE) != 0)
            return "arg" + (i - 1);
        // abstract methods don't have saved names
        // -- no Code attribute for the LocalVariableTable attribute
        if ((v.owner.flags() & Flags.ABSTRACT) != 0)
            return "arg" + (i - 1);
        // bridge methods use argN. No LVT for them anymore
        if ((v.owner.flags() & Flags.BRIDGE) != 0)
            return "arg" + (i - 1);

        // The rest of this method assumes the local conventions in the test program
        Type t = v.type;
        String s;
        if (t.hasTag(TypeTag.CLASS))
            s = ((ClassType) t).tsym.name.toString();
        else
            s = t.toString();
        return String.valueOf(Character.toLowerCase(s.charAt(0))) + i;
    }

    void compile(File outDir, String... opts) throws Exception {
        //File testSrc = new File(System.getProperty("test.src"), ".");
        List<String> args = new ArrayList<String>();
        args.add("-d");
        args.add(outDir.getPath());
        args.addAll(Arrays.asList(opts));
        //args.add(new File(testSrc, "Test.java").getPath());
        args.add("Test.java");
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
        pw.close();
        if (rc != 0) {
            System.err.println(sw.toString());
            throw new Exception("compilation failed unexpectedly");
        }
    }

    Set<String> getTopLevelClasses(File outDir) {
        Set<String> classes = new HashSet<String>();
        for (String f: outDir.list()) {
            if (f.endsWith(".class") && !f.contains("$"))
                classes.add(f.replace(".class", ""));
        }
        return classes;
    }

    void checkEqual(String expect, String found) {
        if (!expect.equals(found))
            error("mismatch: expected:" + expect + " found:" + found);
    }

    void error(String msg) {
        System.err.println(msg);
        errors++;
        throw new Error();
    }

    int errors;
}