test/langtools/tools/javac/lambda/deduplication/ClassFieldDeduplication.java
author jlahoda
Wed, 27 Nov 2019 09:00:01 +0100
changeset 59285 7799a51dbe30
parent 49887 39446351e625
permissions -rw-r--r--
8231826: Implement javac changes for pattern matching for instanceof Reviewed-by: mcimadamore Contributed-by: brian.goetz@oracle.com, gavin.bierman@oracle.com, maurizio.cimadamore@oracle.com, srikanth.adayapalam@oracle.com, vicente.romero@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49887
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
     1
/*
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
     4
 *
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
     7
 * published by the Free Software Foundation.
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
     8
 *
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    13
 * accompanied this code).
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    14
 *
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    18
 *
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    21
 * questions.
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    22
 */
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    23
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    24
/*
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    25
 * @test
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    26
 * @bug 8202141
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    27
 * @summary Verify that .class synthetic Symbols are not duplicated.
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    28
 * @library /tools/javac/lib
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.api
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.code
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.tree
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    32
 *          jdk.compiler/com.sun.tools.javac.util
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    33
 * @build combo.ComboTestHelper
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    34
 * @run main ClassFieldDeduplication
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    35
 */
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    36
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    37
import com.sun.source.util.TaskEvent;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    38
import com.sun.source.util.TaskListener;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    39
import com.sun.tools.javac.code.Symbol;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    40
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    41
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    42
import com.sun.tools.javac.tree.TreeScanner;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    43
import combo.ComboInstance;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    44
import combo.ComboParameter;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    45
import combo.ComboTestHelper;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    46
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    47
public class ClassFieldDeduplication extends ComboInstance<ClassFieldDeduplication> {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    48
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    49
    enum Type implements ComboParameter {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    50
        OBJECT("Object"),
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    51
        PRIMITIVE("int"),
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    52
        BOXED_PRIMITIVE("Integer"),
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    53
        VOID("void"),
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    54
        BOXED_VOID("Void"),
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    55
        OBJECT_ARRAY("Object[]"),
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    56
        PRIMITIVE_ARRAY("int[]"),
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    57
        BOXED_PRIMITIVE_ARRAY("Integer[]"),
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    58
        BOXED_VOID_ARRAY("Void[]");
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    59
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    60
        String type;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    61
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    62
        Type(String type) {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    63
            this.type = type;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    64
        }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    65
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    66
        @Override
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    67
        public String expand(String optParameter) {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    68
            return type;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    69
        }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    70
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    71
    }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    72
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    73
    public static void main(String... args) throws Exception {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    74
        new ComboTestHelper<ClassFieldDeduplication>()
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    75
                .withDimension("TYPE", Type.values())
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    76
                .run(ClassFieldDeduplication::new);
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    77
    }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    78
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    79
    private static final String TEMPLATE =
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    80
            "class Test { void t() { Object o1 = #{TYPE}.class; Object o2 = #{TYPE}.class; } }";
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    81
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    82
    @Override
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    83
    protected void doWork() throws Throwable {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    84
        newCompilationTask()
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    85
                .withSourceFromTemplate(TEMPLATE)
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    86
                .withListener(new TaskListener() {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    87
                    JCCompilationUnit cut;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    88
                        @Override
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    89
                        public void finished(TaskEvent e) {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    90
                            if (e.getKind() == TaskEvent.Kind.PARSE) {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    91
                                if (cut != null)
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    92
                                    throw new AssertionError();
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    93
                                cut = (JCCompilationUnit) e.getCompilationUnit();
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    94
                            }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    95
                            if (e.getKind() == TaskEvent.Kind.ANALYZE) {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    96
                                cut.accept(new TreeScanner() {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    97
                                    Symbol s;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    98
                                    @Override
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
    99
                                    public void visitSelect(JCFieldAccess tree) {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   100
                                        if (tree.name.contentEquals("class")) {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   101
                                            if (s == null) {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   102
                                                s = tree.sym;
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   103
                                            } else if (s != tree.sym) {
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   104
                                                throw new AssertionError("Duplicated field symbol.");
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   105
                                            }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   106
                                        }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   107
                                        super.visitSelect(tree);
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   108
                                    }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   109
                                });
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   110
                            }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   111
                        }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   112
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   113
                })
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   114
                .analyze(els -> {});
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   115
    }
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   116
39446351e625 8202141: Unique symbols for .class
bsrbnd
parents:
diff changeset
   117
}