langtools/test/tools/javac/classfiles/attributes/lib/TestBase.java
author jjg
Thu, 31 Mar 2016 15:20:50 -0700
changeset 36778 e04318f39f92
parent 32908 bef9744cf56f
permissions -rw-r--r--
8152897: refactor ToolBox to allow reduced documented dependencies Reviewed-by: vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     1
/*
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 32908
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     4
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     8
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    13
 * accompanied this code).
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    14
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    18
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    21
 * questions.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    22
 */
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    23
31750
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
    24
import java.io.*;
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
    25
import java.nio.file.Files;
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
    26
import java.nio.file.Path;
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
    27
import java.util.*;
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    28
import java.util.function.Function;
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    29
import java.util.stream.Collectors;
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    30
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    31
import javax.tools.DiagnosticCollector;
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    32
import javax.tools.JavaCompiler;
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    33
import javax.tools.JavaFileObject;
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    34
import javax.tools.ToolProvider;
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    35
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    36
import com.sun.tools.classfile.ClassFile;
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    37
import com.sun.tools.classfile.ConstantPoolException;
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    38
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 32908
diff changeset
    39
import toolbox.JavacTask;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 32908
diff changeset
    40
import toolbox.ToolBox;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 32908
diff changeset
    41
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    42
/**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    43
 * Base class for class file attribute tests.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    44
 * Contains methods for compiling generated sources in memory,
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    45
 * for reading files from disk and a lot of assert* methods.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    46
 */
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    47
public class TestBase {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    48
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    49
    public static final String LINE_SEPARATOR = System.lineSeparator();
32908
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
    50
    public static final boolean isDumpOfSourceEnabled = Boolean.getBoolean("dump.src");
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    51
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    52
    private <S> InMemoryFileManager compile(
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    53
            List<String> options,
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    54
            Function<S, ? extends JavaFileObject> src2JavaFileObject,
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    55
            List<S> sources)
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    56
            throws IOException, CompilationException {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    57
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    58
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    59
        List<? extends JavaFileObject> src = sources.stream()
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    60
                .map(src2JavaFileObject)
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    61
                .collect(Collectors.toList());
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    62
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    63
        DiagnosticCollector<? super JavaFileObject> dc = new DiagnosticCollector<>();
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    64
        try (InMemoryFileManager fileManager
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    65
                     = new InMemoryFileManager(compiler.getStandardFileManager(null, null, null))) {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    66
            JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, dc, options, null, src);
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    67
            boolean success = task.call();
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    68
            if (!success) {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    69
                String errorMessage = dc.getDiagnostics().stream()
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    70
                        .map(Object::toString)
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    71
                        .collect(Collectors.joining("\n"));
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    72
                throw new CompilationException("Compilation Error\n\n" + errorMessage);
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    73
            }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    74
            return fileManager;
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    75
        }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    76
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    77
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    78
    /**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    79
     * Compiles sources in memory.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    80
     *
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
    81
     * @param sources to compile
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
    82
     * @return in-memory file manager which contains class files and class loader
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    83
     */
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    84
    public InMemoryFileManager compile(String... sources)
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    85
            throws IOException, CompilationException {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    86
        return compile(Collections.emptyList(), sources);
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    87
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    88
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    89
    /**
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    90
     * Compiles sources in memory.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    91
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    92
     * @param options compiler options.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    93
     * @param sources sources to compile.
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
    94
     * @return in-memory file manager which contains class files and class loader.
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    95
     */
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    96
    public InMemoryFileManager compile(List<String> options, String... sources)
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    97
            throws IOException, CompilationException {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    98
        return compile(options, ToolBox.JavaSource::new, Arrays.asList(sources));
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    99
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   100
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   101
    /**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   102
     * Compiles sources in memory.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   103
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   104
     * @param sources sources[i][0] - name of file, sources[i][1] - sources.
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   105
     * @return in-memory file manager which contains class files and class loader.
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   106
     */
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   107
    public InMemoryFileManager compile(String[]... sources) throws IOException,
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   108
            CompilationException {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   109
        return compile(Collections.emptyList(), sources);
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   110
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   111
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   112
    /**
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   113
     * Compiles sources in memory.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   114
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   115
     * @param options compiler options
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   116
     * @param sources sources[i][0] - name of file, sources[i][1] - sources.
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   117
     * @return in-memory file manager which contains class files and class loader.
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   118
     */
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   119
    public InMemoryFileManager compile(List<String> options, String[]... sources)
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   120
            throws IOException, CompilationException {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   121
        return compile(options, src -> new ToolBox.JavaSource(src[0], src[1]), Arrays.asList(sources));
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   122
    }
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   123
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   124
    /**
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   125
     * Returns class file that is read from {@code is}.
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   126
     *
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   127
     * @param is an input stream
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   128
     * @return class file that is read from {@code is}
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   129
     * @throws IOException if I/O error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   130
     * @throws ConstantPoolException if constant pool error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   131
     */
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   132
    public ClassFile readClassFile(InputStream is) throws IOException, ConstantPoolException {
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   133
        return ClassFile.read(is);
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   134
    }
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   135
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   136
    /**
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   137
     * Returns class file that is read from {@code fileObject}.
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   138
     *
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   139
     * @param fileObject a file object
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   140
     * @return class file that is read from {@code fileObject}
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   141
     * @throws IOException if I/O error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   142
     * @throws ConstantPoolException if constant pool error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   143
     */
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   144
    public ClassFile readClassFile(JavaFileObject fileObject) throws IOException, ConstantPoolException {
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   145
        try (InputStream is = fileObject.openInputStream()) {
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   146
            return readClassFile(is);
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   147
        }
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   148
    }
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   149
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   150
    /**
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   151
     * Returns class file that corresponds to {@code clazz}.
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   152
     *
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   153
     * @param clazz a class
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   154
     * @return class file that is read from {@code clazz}
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   155
     * @throws IOException if I/O error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   156
     * @throws ConstantPoolException if constant pool error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   157
     */
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   158
    public ClassFile readClassFile(Class<?> clazz) throws IOException, ConstantPoolException {
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   159
        return readClassFile(getClassFile(clazz));
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   160
    }
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   161
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   162
    /**
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   163
     * Returns class file that corresponds to {@code className}.
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   164
     *
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   165
     * @param className a class name
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   166
     * @return class file that is read from {@code className}
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   167
     * @throws IOException if I/O error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   168
     * @throws ConstantPoolException if constant pool error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   169
     */
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   170
    public ClassFile readClassFile(String className) throws IOException, ConstantPoolException {
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   171
        return readClassFile(getClassFile(className + ".class"));
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   172
    }
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   173
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   174
    /**
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   175
     * Returns class file that is read from {@code file}.
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   176
     *
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   177
     * @param file a file
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   178
     * @return class file that is read from {@code file}
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   179
     * @throws IOException if I/O error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   180
     * @throws ConstantPoolException if constant pool error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   181
     */
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   182
    public ClassFile readClassFile(File file) throws IOException, ConstantPoolException {
32908
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   183
        try (InputStream is = new FileInputStream(file)) {
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   184
            return readClassFile(is);
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   185
        }
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   186
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   187
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   188
    public void assertEquals(Object actual, Object expected, String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   189
        if (!Objects.equals(actual, expected))
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   190
            throw new AssertionFailedException(String.format("%s%nGot: %s, Expected: %s",
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   191
                    message, actual, expected));
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   192
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   193
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   194
    public void assertNull(Object actual, String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   195
        assertEquals(actual, null, message);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   196
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   197
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   198
    public void assertNotNull(Object actual, String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   199
        if (Objects.isNull(actual)) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   200
            throw new AssertionFailedException(message + " : Expected not null value");
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   201
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   202
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   203
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   204
    public void assertTrue(boolean actual, String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   205
        assertEquals(actual, true, message);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   206
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   207
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   208
    public void assertFalse(boolean actual, String message) {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   209
        assertEquals(actual, false, message);
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   210
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   211
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   212
    public void assertContains(Set<?> found, Set<?> expected, String message) {
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   213
        Set<?> copy = new HashSet<>(expected);
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   214
        copy.removeAll(found);
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   215
        assertTrue(found.containsAll(expected), message + " : " + copy);
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   216
    }
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   217
31750
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   218
    public void writeToFile(Path path, String source) throws IOException {
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   219
        try (BufferedWriter writer = Files.newBufferedWriter(path)) {
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   220
            writer.write(source);
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   221
        }
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   222
    }
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   223
32908
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   224
    public void writeToFileIfEnabled(Path path, String source) throws IOException {
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   225
        if (isDumpOfSourceEnabled) {
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   226
            writeToFile(path, source);
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   227
        } else {
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   228
            System.err.println("Source dumping disabled. To enable, run the test with '-Ddump.src=true'");
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   229
        }
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   230
    }
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 31750
diff changeset
   231
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   232
    public File getSourceDir() {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   233
        return new File(System.getProperty("test.src", "."));
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   234
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   235
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   236
    public File getClassDir() {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   237
        return new File(System.getProperty("test.classes", TestBase.class.getResource(".").getPath()));
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   238
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   239
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   240
    public File getSourceFile(String fileName) {
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   241
        return new File(getSourceDir(), fileName);
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   242
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   243
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   244
    public File getClassFile(String fileName) {
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   245
        return new File(getClassDir(), fileName);
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   246
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   247
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   248
    public File getClassFile(Class clazz) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   249
        return getClassFile(clazz.getName().replace(".", "/") + ".class");
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   250
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   251
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   252
    /**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   253
     * Prints message to standard error. New lines are converted to system dependent NL.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   254
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   255
     * @param message string to print.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   256
     */
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   257
    public void echo(String message) {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   258
        printf(message + "\n");
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   259
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   260
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   261
    /**
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   262
     * Substitutes args in template and prints result to standard error.
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   263
     * New lines are converted to system dependent NL.
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   264
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   265
     * @param template template in standard String.format(...) format.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   266
     * @param args arguments to substitute in template.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   267
     */
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   268
    public void printf(String template, Object... args) {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   269
        System.err.printf(String.format(template, args).replace("\n", LINE_SEPARATOR));
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   270
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   271
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   272
    public static class CompilationException extends Exception {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   273
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   274
        public CompilationException(String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   275
            super(message);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   276
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   277
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   278
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   279
    public static class AssertionFailedException extends RuntimeException {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   280
        public AssertionFailedException(String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   281
            super(message);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   282
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   283
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   284
}