test/langtools/tools/javac/lib/combo/ComboInstance.java
author jlahoda
Mon, 21 Oct 2019 15:38:26 +0200
changeset 58713 ad69fd32778e
parent 47216 71c04702a3d5
permissions -rw-r--r--
8226585: Improve javac messages for using a preview API Summary: Avoiding deprecation for removal for APIs associated with preview features, the features are marked with an annotation, and errors/warnings are produced for them based on the annotation. Reviewed-by: erikj, mcimadamore, alanb Contributed-by: joe.darcy@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     1
/*
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     4
 *
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     8
 *
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    14
 *
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    18
 *
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    21
 * questions.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    22
 */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    23
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    24
package combo;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    25
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 47216
diff changeset
    26
import java.lang.reflect.Method;
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    27
import javax.tools.StandardJavaFileManager;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    28
import java.util.Optional;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    29
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    30
/**
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    31
 * This class is the common superclass of all combo test instances. It defines few helper methods
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    32
 * to build compilation tasks using the shared context object, as well as entry points for
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    33
 * signalling test failures.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    34
 */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    35
public abstract class ComboInstance<X extends ComboInstance<X>> {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    36
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    37
    /** The test instance result status. */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    38
    private ResultStatus resultStatus = ResultStatus.PASSED;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    39
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    40
    /** The test instance execution environment. */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    41
    private ComboTestHelper<X>.Env env;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    42
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    43
    /**
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    44
     * Entry point for executing a combo test instance; first, the test environment is saved
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    45
     * in the corresponding field, then the instance is run (see {@link ComboInstance#doWork()}.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    46
     * During execution, the result status will be updated to match the test outcome.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    47
     */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    48
    final void run(ComboTestHelper<X>.Env env) {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    49
        try {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    50
            this.env = env;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    51
            doWork();
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    52
            if (resultStatus.isSuccess()) {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    53
                env.info().passCount++;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    54
            }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    55
        } catch (Throwable ex) {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    56
            resultStatus = ResultStatus.ERROR;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    57
            env.info().errCount++;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    58
            env.info().lastError = Optional.of(ex);
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    59
        } finally {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    60
            this.env = null;
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 47216
diff changeset
    61
            try {
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 47216
diff changeset
    62
                Class<?> fmClass = env.fileManager().getClass();
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 47216
diff changeset
    63
                Method clear = fmClass.getMethod("clear");
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 47216
diff changeset
    64
                clear.setAccessible(true);
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 47216
diff changeset
    65
                clear.invoke(env.fileManager());
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 47216
diff changeset
    66
            } catch (Exception ex) {
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 47216
diff changeset
    67
                throw new IllegalStateException(ex);
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 47216
diff changeset
    68
            }
32454
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    69
        }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    70
    }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    71
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    72
    /**
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    73
     * Retrieve a unique ID associated with this test instance.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    74
     */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    75
    public int id() {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    76
        return env.info().comboCount;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    77
    }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    78
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    79
    /**
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    80
     * Retrieve shared file manager.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    81
     */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    82
    public StandardJavaFileManager fileManager() {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    83
        return env.fileManager();
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    84
    }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    85
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    86
    /**
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    87
     * Create a new compilation task using shared compilation context.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    88
     */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    89
    protected ComboTask newCompilationTask() {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    90
        return new ComboTask(env);
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    91
    }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    92
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    93
    /**
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    94
     * Main test execution entry point; subclasses must implement this method to define the test
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    95
     * logic.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    96
     */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    97
    protected abstract void doWork() throws Throwable;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    98
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
    99
    /**
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   100
     * Report a test failure.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   101
     */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   102
    protected void fail() {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   103
        //dump some default info (such as dimension bindings)
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   104
        fail("Combo instance failed; " + env.bindings);
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   105
    }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   106
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   107
    /**
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   108
     * Report a test failure with corresponding failure message.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   109
     */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   110
    protected void fail(String msg) {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   111
        resultStatus = ResultStatus.FAILED;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   112
        env.info().failCount++;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   113
        env.info().lastFailure = Optional.of(msg);
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   114
    }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   115
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   116
    /**
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   117
     * The status associated with this test instance execution.
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   118
     */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   119
    enum ResultStatus {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   120
        /** Test passed. */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   121
        PASSED(true),
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   122
        /** Test failed. */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   123
        FAILED(false),
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   124
        /** Test thrown unexpected error/exception. */
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   125
        ERROR(false);
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   126
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   127
        boolean success;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   128
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   129
        ResultStatus(boolean success) {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   130
            this.success = success;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   131
        }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   132
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   133
        boolean isSuccess() {
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   134
            return success;
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   135
        }
b0ac04e0fefe 8129962: Investigate performance improvements in langtools combo tests
mcimadamore
parents:
diff changeset
   136
    }
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 47216
diff changeset
   137
}