langtools/test/tools/javac/classfiles/attributes/lib/TestBase.java
author aeremeev
Mon, 28 Sep 2015 13:23:35 +0300
changeset 32908 bef9744cf56f
parent 31750 c65c37c0c691
child 36778 e04318f39f92
permissions -rw-r--r--
8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk Reviewed-by: ksrini
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
/*
31750
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
     2
 * Copyright (c) 2014, 2015, 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
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    39
/**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    40
 * Base class for class file attribute tests.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    41
 * Contains methods for compiling generated sources in memory,
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    42
 * 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
    43
 */
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    44
public class TestBase {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    45
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    46
    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
    47
    public static final boolean isDumpOfSourceEnabled = Boolean.getBoolean("dump.src");
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    48
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    49
    private <S> InMemoryFileManager compile(
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    50
            List<String> options,
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    51
            Function<S, ? extends JavaFileObject> src2JavaFileObject,
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    52
            List<S> sources)
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    53
            throws IOException, CompilationException {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    54
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    55
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    56
        List<? extends JavaFileObject> src = sources.stream()
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    57
                .map(src2JavaFileObject)
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    58
                .collect(Collectors.toList());
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    59
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    60
        DiagnosticCollector<? super JavaFileObject> dc = new DiagnosticCollector<>();
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    61
        try (InMemoryFileManager fileManager
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    62
                     = new InMemoryFileManager(compiler.getStandardFileManager(null, null, null))) {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    63
            JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, dc, options, null, src);
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    64
            boolean success = task.call();
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    65
            if (!success) {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    66
                String errorMessage = dc.getDiagnostics().stream()
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    67
                        .map(Object::toString)
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    68
                        .collect(Collectors.joining("\n"));
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    69
                throw new CompilationException("Compilation Error\n\n" + errorMessage);
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    70
            }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    71
            return fileManager;
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    72
        }
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
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    75
    /**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    76
     * Compiles sources in memory.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    77
     *
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
    78
     * @param sources to compile
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
    79
     * @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
    80
     */
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    81
    public InMemoryFileManager compile(String... sources)
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    82
            throws IOException, CompilationException {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    83
        return compile(Collections.emptyList(), sources);
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    84
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    85
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    86
    /**
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    87
     * Compiles sources in memory.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    88
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    89
     * @param options compiler options.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    90
     * @param sources sources to compile.
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
    91
     * @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
    92
     */
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    93
    public InMemoryFileManager compile(List<String> options, String... sources)
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    94
            throws IOException, CompilationException {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
    95
        return compile(options, ToolBox.JavaSource::new, Arrays.asList(sources));
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    96
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
    97
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    98
    /**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
    99
     * Compiles sources in memory.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   100
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   101
     * @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
   102
     * @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
   103
     */
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   104
    public InMemoryFileManager compile(String[]... sources) throws IOException,
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   105
            CompilationException {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   106
        return compile(Collections.emptyList(), sources);
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   107
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   108
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   109
    /**
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   110
     * Compiles sources in memory.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   111
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   112
     * @param options compiler options
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   113
     * @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
   114
     * @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
   115
     */
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   116
    public InMemoryFileManager compile(List<String> options, String[]... sources)
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   117
            throws IOException, CompilationException {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   118
        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
   119
    }
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   120
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   121
    /**
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   122
     * Returns class file that is read from {@code is}.
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
     * @param is an input stream
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   125
     * @return class file that is read from {@code is}
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   126
     * @throws IOException if I/O error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   127
     * @throws ConstantPoolException if constant pool error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   128
     */
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   129
    public ClassFile readClassFile(InputStream is) throws IOException, ConstantPoolException {
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   130
        return ClassFile.read(is);
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
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   133
    /**
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   134
     * Returns class file that is read from {@code fileObject}.
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
     * @param fileObject a file object
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   137
     * @return class file that is read from {@code fileObject}
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   138
     * @throws IOException if I/O error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   139
     * @throws ConstantPoolException if constant pool error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   140
     */
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   141
    public ClassFile readClassFile(JavaFileObject fileObject) throws IOException, ConstantPoolException {
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   142
        try (InputStream is = fileObject.openInputStream()) {
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   143
            return readClassFile(is);
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   144
        }
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   145
    }
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   146
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   147
    /**
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   148
     * Returns class file that corresponds to {@code clazz}.
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
     * @param clazz a class
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   151
     * @return class file that is read from {@code clazz}
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   152
     * @throws IOException if I/O error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   153
     * @throws ConstantPoolException if constant pool error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   154
     */
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   155
    public ClassFile readClassFile(Class<?> clazz) throws IOException, ConstantPoolException {
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   156
        return readClassFile(getClassFile(clazz));
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
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   159
    /**
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   160
     * Returns class file that corresponds to {@code className}.
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
     * @param className a class name
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   163
     * @return class file that is read from {@code className}
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   164
     * @throws IOException if I/O error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   165
     * @throws ConstantPoolException if constant pool error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   166
     */
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   167
    public ClassFile readClassFile(String className) throws IOException, ConstantPoolException {
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   168
        return readClassFile(getClassFile(className + ".class"));
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
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   171
    /**
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   172
     * Returns class file that is read from {@code file}.
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
     * @param file a file
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   175
     * @return class file that is read from {@code file}
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   176
     * @throws IOException if I/O error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   177
     * @throws ConstantPoolException if constant pool error occurs
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   178
     */
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   179
    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
   180
        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
   181
            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
   182
        }
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   183
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   184
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   185
    public void assertEquals(Object actual, Object expected, String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   186
        if (!Objects.equals(actual, expected))
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   187
            throw new AssertionFailedException(String.format("%s%nGot: %s, Expected: %s",
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   188
                    message, actual, expected));
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   189
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   190
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   191
    public void assertNull(Object actual, String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   192
        assertEquals(actual, null, message);
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
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   195
    public void assertNotNull(Object actual, String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   196
        if (Objects.isNull(actual)) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   197
            throw new AssertionFailedException(message + " : Expected not null value");
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   198
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   199
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   200
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   201
    public void assertTrue(boolean actual, String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   202
        assertEquals(actual, true, message);
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
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   205
    public void assertFalse(boolean actual, String message) {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   206
        assertEquals(actual, false, message);
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   207
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   208
27552
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   209
    public void assertContains(Set<?> found, Set<?> expected, String message) {
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   210
        Set<?> copy = new HashSet<>(expected);
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   211
        copy.removeAll(found);
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   212
        assertTrue(found.containsAll(expected), message + " : " + copy);
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   213
    }
8a4b2d3639c1 8042931: Implement classfile tests for EnclosingMethod attribute.
aeremeev
parents: 26101
diff changeset
   214
31750
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   215
    public void writeToFile(Path path, String source) throws IOException {
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   216
        try (BufferedWriter writer = Files.newBufferedWriter(path)) {
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   217
            writer.write(source);
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   218
        }
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   219
    }
c65c37c0c691 8044411: Implement classfile tests for RuntimeAnnotations and RuntimeParameterAnnotations attribute.
aeremeev
parents: 27552
diff changeset
   220
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
   221
    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
   222
        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
   223
            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
   224
        } 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
   225
            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
   226
        }
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
    }
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
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   229
    public File getSourceDir() {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   230
        return new File(System.getProperty("test.src", "."));
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   231
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   232
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   233
    public File getClassDir() {
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   234
        return new File(System.getProperty("test.classes", TestBase.class.getResource(".").getPath()));
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
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   237
    public File getSourceFile(String fileName) {
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   238
        return new File(getSourceDir(), fileName);
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   239
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   240
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   241
    public File getClassFile(String fileName) {
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   242
        return new File(getClassDir(), fileName);
24223
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
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   245
    public File getClassFile(Class clazz) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   246
        return getClassFile(clazz.getName().replace(".", "/") + ".class");
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
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   249
    /**
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   250
     * 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
   251
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   252
     * @param message string to print.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   253
     */
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   254
    public void echo(String message) {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   255
        printf(message + "\n");
25434
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   256
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   257
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   258
    /**
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   259
     * Substitutes args in template and prints result to standard error.
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   260
     * New lines are converted to system dependent NL.
25845
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   261
     *
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   262
     * @param template template in standard String.format(...) format.
14935053bb07 8050979: Provide javadoc for "framework" classes in langtools tests
anazarov
parents: 25434
diff changeset
   263
     * @param args arguments to substitute in template.
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
    public void printf(String template, Object... args) {
26101
d5dd2ecd2353 8042251: Implement classfile tests for InnerClasses attribute.
aeremeev
parents: 25845
diff changeset
   266
        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
   267
    }
823512a72660 8042261: Implement classfile tests for Deprecated attribute.
jjg
parents: 24223
diff changeset
   268
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   269
    public static class CompilationException extends Exception {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   270
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   271
        public CompilationException(String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   272
            super(message);
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
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   275
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   276
    public static class AssertionFailedException extends RuntimeException {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   277
        public AssertionFailedException(String message) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   278
            super(message);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   279
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   280
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   281
}