langtools/test/tools/javac/classfiles/attributes/Synthetic/SyntheticTestDriver.java
author aeremeev
Tue, 28 Apr 2015 11:08:25 +0300
changeset 30065 a3873788f1b4
child 30067 08fb37c9b670
permissions -rw-r--r--
8044537: Implement classfile tests for Synthetic attribute. Reviewed-by: jjg, shurailine, anazarov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30065
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
     1
/*
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
     4
 *
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
     8
 *
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    13
 * accompanied this code).
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    14
 *
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    18
 *
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    21
 * questions.
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    22
 */
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    23
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    24
import java.io.IOException;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    25
import java.nio.file.Files;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    26
import java.nio.file.Path;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    27
import java.util.*;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    28
import java.util.function.Function;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    29
import java.util.function.Supplier;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    30
import java.util.stream.Collectors;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    31
import java.util.stream.Stream;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    32
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    33
import com.sun.tools.classfile.*;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    34
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    35
/**
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    36
 * The tests work as follows. Firstly, it looks through the test cases
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    37
 * and extracts the appropriate compiled classes. Each test case contains
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    38
 * a set of expected classes, methods and fields. Those class members must not have
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    39
 * the Synthetic attribute, while other found classes, methods and fields must have
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    40
 * the Synthetic attribute if they are not in the set of expected class members.
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    41
 *
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    42
 * Each test executes SyntheticTestDriver specifying the name of test cases and
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    43
 * the number of expected synthetic classes. Each test class is annotated by
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    44
 * annotations which contains non-synthetic class members.
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    45
 *
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    46
 * See the appropriate class for more information about a test case.
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    47
 */
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    48
public class SyntheticTestDriver extends TestResult {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    49
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    50
    private static final String ACC_SYNTHETIC = "ACC_SYNTHETIC";
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    51
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    52
    private final String testCaseName;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    53
    private final Map<String, ClassFile> classes;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    54
    private final Map<String, ExpectedClass> expectedClasses;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    55
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    56
    public static void main(String[] args)
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    57
            throws TestFailedException, ConstantPoolException, IOException, ClassNotFoundException {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    58
        if (args.length != 1 && args.length != 2) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    59
            throw new IllegalArgumentException("Usage: SyntheticTestDriver <class-name> [<number-of-synthetic-classes>]");
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    60
        }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    61
        int numberOfSyntheticClasses = args.length == 1 ? 0 : Integer.parseInt(args[1]);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    62
        new SyntheticTestDriver(args[0]).test(numberOfSyntheticClasses);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    63
    }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    64
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    65
    public SyntheticTestDriver(String testCaseName) throws IOException, ConstantPoolException, ClassNotFoundException {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    66
        Class<?> clazz = Class.forName(testCaseName);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    67
        this.testCaseName = testCaseName;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    68
        this.expectedClasses = Stream.of(clazz.getAnnotationsByType(ExpectedClass.class))
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    69
                .collect(Collectors.toMap(ExpectedClass::className, Function.identity()));
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    70
        this.classes = new HashMap<>();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    71
        Path classDir = getClassDir().toPath();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    72
        String sourceFileName = testCaseName.replace('.', '/');
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    73
        List<Path> paths = Files.walk(classDir)
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    74
                .map(p -> classDir.relativize(p.toAbsolutePath()))
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    75
                .filter(p -> p.toString().matches(sourceFileName + ".*\\.class"))
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    76
                .collect(Collectors.toList());
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    77
        for (Path path : paths) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    78
            String className = path.toString().replace(".class", "").replace('/', '.');
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    79
            classes.put(className, readClassFile(classDir.resolve(path).toFile()));
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    80
        }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    81
        if (classes.isEmpty()) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    82
            throw new RuntimeException("Classes have not been found.");
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    83
        }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    84
        boolean success = classes.entrySet().stream()
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    85
                .allMatch(e -> e.getKey().startsWith(testCaseName));
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    86
        if (!success) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    87
            classes.forEach((className, $) -> printf("Found class: %s\n", className));
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    88
            throw new RuntimeException("Found classes are not from the test case : " + testCaseName);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    89
        }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    90
    }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    91
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    92
    private String getMethodName(ClassFile classFile, Method method)
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    93
            throws ConstantPoolException, Descriptor.InvalidDescriptor {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    94
        String methodName = method.getName(classFile.constant_pool);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    95
        String parameters = method.descriptor.getParameterTypes(classFile.constant_pool);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    96
        return methodName + parameters;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    97
    }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    98
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
    99
    public void test(int expectedNumberOfSyntheticClasses) throws TestFailedException {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   100
        try {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   101
            addTestCase(testCaseName);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   102
            Set<String> foundClasses = new HashSet<>();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   103
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   104
            int numberOfSyntheticClasses = 0;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   105
            for (Map.Entry<String, ClassFile> entry : classes.entrySet()) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   106
                String className = entry.getKey();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   107
                ClassFile classFile = entry.getValue();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   108
                foundClasses.add(className);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   109
                if (testAttribute(
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   110
                        classFile,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   111
                        () -> (Synthetic_attribute) classFile.getAttribute(Attribute.Synthetic),
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   112
                        classFile.access_flags::getClassFlags,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   113
                        expectedClasses.keySet(),
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   114
                        className,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   115
                        "Testing class " + className)) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   116
                    ++numberOfSyntheticClasses;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   117
                }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   118
                ExpectedClass expectedClass = expectedClasses.get(className);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   119
                Set<String> expectedMethods = expectedClass != null
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   120
                        ? toSet(expectedClass.expectedMethods())
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   121
                        : new HashSet<>();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   122
                int numberOfSyntheticMethods = 0;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   123
                Set<String> foundMethods = new HashSet<>();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   124
                for (Method method : classFile.methods) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   125
                    String methodName = getMethodName(classFile, method);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   126
                    foundMethods.add(methodName);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   127
                    if (testAttribute(
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   128
                            classFile,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   129
                            () -> (Synthetic_attribute) method.attributes.get(Attribute.Synthetic),
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   130
                            method.access_flags::getMethodFlags,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   131
                            expectedMethods,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   132
                            methodName,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   133
                            "Testing method " + methodName + " in class "
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   134
                                    + className)) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   135
                        ++numberOfSyntheticMethods;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   136
                    }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   137
                }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   138
                checkContains(foundMethods, expectedMethods,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   139
                        "Checking that all methods of class " + className
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   140
                                + " without Synthetic attribute have been found");
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   141
                checkEquals(numberOfSyntheticMethods,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   142
                        expectedClass == null ? 0 : expectedClass.expectedNumberOfSyntheticMethods(),
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   143
                        "Checking number of synthetic methods in class: " + className);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   144
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   145
                Set<String> expectedFields = expectedClass != null
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   146
                        ? toSet(expectedClass.expectedFields())
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   147
                        : new HashSet<>();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   148
                int numberOfSyntheticFields = 0;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   149
                Set<String> foundFields = new HashSet<>();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   150
                for (Field field : classFile.fields) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   151
                    String fieldName = field.getName(classFile.constant_pool);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   152
                    foundFields.add(fieldName);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   153
                    if (testAttribute(
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   154
                            classFile,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   155
                            () -> (Synthetic_attribute) field.attributes.get(Attribute.Synthetic),
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   156
                            field.access_flags::getFieldFlags,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   157
                            expectedFields,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   158
                            fieldName,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   159
                            "Testing field " + fieldName + " in class "
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   160
                                    + className)) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   161
                        ++numberOfSyntheticFields;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   162
                    }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   163
                }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   164
                checkContains(foundFields, expectedFields,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   165
                        "Checking that all fields of class " + className
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   166
                                + " without Synthetic attribute have been found");
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   167
                checkEquals(numberOfSyntheticFields,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   168
                        expectedClass == null ? 0 : expectedClass.expectedNumberOfSyntheticFields(),
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   169
                        "Checking number of synthetic fields in class: " + className);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   170
            }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   171
            checkContains(foundClasses, expectedClasses.keySet(),
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   172
                    "Checking that all classes have been found");
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   173
            checkEquals(numberOfSyntheticClasses, expectedNumberOfSyntheticClasses,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   174
                    "Checking number of synthetic classes");
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   175
        } catch (Exception e) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   176
            addFailure(e);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   177
        } finally {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   178
            checkStatus();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   179
        }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   180
    }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   181
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   182
    private boolean testAttribute(ClassFile classFile,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   183
                               Supplier<Synthetic_attribute> getSyntheticAttribute,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   184
                               Supplier<Set<String>> getAccessFlags,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   185
                               Set<String> expectedMembers, String memberName,
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   186
                               String info) throws ConstantPoolException {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   187
        echo(info);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   188
        String className = classFile.getName();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   189
        Synthetic_attribute attr = getSyntheticAttribute.get();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   190
        Set<String> flags = getAccessFlags.get();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   191
        if (expectedMembers.contains(memberName)) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   192
            checkNull(attr, "Member must not have synthetic attribute : "
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   193
                    + memberName);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   194
            checkFalse(flags.contains(ACC_SYNTHETIC),
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   195
                    "Member must not have synthetic flag : " + memberName
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   196
                            + " in class : " + className);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   197
            return false;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   198
        } else {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   199
            return checkNull(attr, "Synthetic attribute should not be generated")
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   200
                    && checkTrue(flags.contains(ACC_SYNTHETIC), "Member must have synthetic flag : "
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   201
                                + memberName + " in class : " + className);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   202
        }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   203
    }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   204
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   205
    private Set<String> toSet(String[] strings) {
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   206
        HashSet<String> set = new HashSet<>();
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   207
        Collections.addAll(set, strings);
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   208
        return set;
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   209
    }
a3873788f1b4 8044537: Implement classfile tests for Synthetic attribute.
aeremeev
parents:
diff changeset
   210
}