langtools/test/tools/javac/classfiles/attributes/Signature/Driver.java
author aeremeev
Wed, 25 Mar 2015 10:29:14 +0300
changeset 29637 c03745b71c70
permissions -rw-r--r--
8049238: Implement classfile tests for Signature attribute Reviewed-by: jjg, shurailine, anazarov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29637
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
     1
/*
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
     4
 *
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
     8
 *
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    13
 * accompanied this code).
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    14
 *
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    18
 *
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    21
 * questions.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    22
 */
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    23
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    24
import com.sun.tools.classfile.*;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    25
import com.sun.tools.classfile.Field;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    26
import com.sun.tools.classfile.Method;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    27
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    28
import java.io.File;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    29
import java.io.FilenameFilter;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    30
import java.lang.reflect.*;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    31
import java.util.*;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    32
import java.util.function.Function;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    33
import java.util.function.Predicate;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    34
import java.util.function.Supplier;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    35
import java.util.stream.Collectors;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    36
import java.util.stream.Stream;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    37
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    38
/**
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    39
 * The main class of Signature tests.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    40
 * Driver reads golden data of each class member that must have a Signature attribute,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    41
 * after that the class compares expected data with actual one.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    42
 *
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    43
 * Example of usage Driver:
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    44
 * java Driver Test
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    45
 *
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    46
 * Each member of the class Test should have @ExpectedSignature annotations
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    47
 * if it must have the Signature attribute. Anonymous class cannot be annotated.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    48
 * So its enclosing class should be annotated and method isAnonymous
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    49
 * of ExpectedSignature must return true.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    50
 */
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    51
public class Driver extends TestResult {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    52
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    53
    private final static String ACC_BRIDGE = "ACC_BRIDGE";
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    54
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    55
    private final String topLevelClassName;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    56
    private final File[] files;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    57
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    58
    public Driver(String topLevelClassName) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    59
        this.topLevelClassName = topLevelClassName;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    60
        // Get top level class and all inner classes.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    61
        FilenameFilter filter = (dir, file) ->
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    62
                file.equals(topLevelClassName + ".class")
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    63
                        || file.matches(topLevelClassName + "\\$.*\\.class");
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    64
        files = getClassDir().listFiles(filter);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    65
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    66
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    67
    private boolean isAnonymous(String className) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    68
        return className.matches(".*\\$\\d+$");
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    69
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    70
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    71
    private Class<?> getEnclosingClass(String className) throws ClassNotFoundException {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    72
        return Class.forName(className.replaceFirst("\\$\\d+$", ""));
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    73
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    74
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    75
    private ExpectedSignature getExpectedClassSignature(String className, Class<?> clazz)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    76
            throws ClassNotFoundException {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    77
        // anonymous class cannot be annotated, so information about anonymous class
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    78
        // is located in its enclosing class.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    79
        boolean isAnonymous = isAnonymous(className);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    80
        clazz = isAnonymous ? getEnclosingClass(className) : clazz;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    81
        return Stream.of(clazz.getAnnotationsByType(ExpectedSignature.class))
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    82
                .filter(s -> s.isAnonymous() == isAnonymous)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    83
                .collect(Collectors.toMap(ExpectedSignature::descriptor, Function.identity()))
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    84
                .get(className);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    85
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    86
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    87
    // Class.getName() cannot be used here, because the method can rely on signature attribute.
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    88
    private Map<String, ExpectedSignature> getClassExpectedSignature(String className, Class<?> clazz)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    89
            throws ClassNotFoundException {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    90
        Map<String, ExpectedSignature> classSignatures = new HashMap<>();
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    91
        ExpectedSignature classSignature = getExpectedClassSignature(className, clazz);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    92
        if (classSignature != null) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    93
            classSignatures.put(className, classSignature);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    94
        }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    95
        return classSignatures;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    96
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    97
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    98
    private Map<String, ExpectedSignature> getExpectedExecutableSignatures(Executable[] executables,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
    99
                                                                           Predicate<Executable> filterBridge) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   100
        return Stream.of(executables)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   101
                .filter(filterBridge)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   102
                .map(e -> e.getAnnotation(ExpectedSignature.class))
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   103
                .filter(Objects::nonNull)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   104
                .collect(Collectors.toMap(ExpectedSignature::descriptor, Function.identity()));
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   105
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   106
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   107
    private Map<String, ExpectedSignature> getExpectedMethodSignatures(Class<?> clazz) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   108
        Map<String, ExpectedSignature> methodSignatures =
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   109
                getExpectedExecutableSignatures(clazz.getDeclaredMethods(),
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   110
                        m -> !((java.lang.reflect.Method) m).isBridge());
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   111
        methodSignatures.putAll(
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   112
                getExpectedExecutableSignatures(clazz.getDeclaredConstructors(),
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   113
                        m -> true));
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   114
        return methodSignatures;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   115
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   116
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   117
    private Map<String, ExpectedSignature> getExpectedFieldSignatures(Class<?> clazz) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   118
        return Stream.of(clazz.getDeclaredFields())
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   119
                .map(f -> f.getAnnotation(ExpectedSignature.class))
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   120
                .filter(Objects::nonNull)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   121
                .collect(Collectors.toMap(ExpectedSignature::descriptor, Function.identity()));
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   122
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   123
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   124
    public void test() throws TestFailedException {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   125
        try {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   126
            addTestCase("Source is " + topLevelClassName + ".java");
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   127
            assertTrue(files.length > 0, "No class files found");
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   128
            for (File file : files) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   129
                try {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   130
                    String className = file.getName().replace(".class", "");
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   131
                    Class<?> clazz = Class.forName(className);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   132
                    printf("Testing class %s\n", className);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   133
                    ClassFile classFile = readClassFile(file);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   134
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   135
                    // test class signature
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   136
                    testAttribute(
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   137
                            className,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   138
                            classFile,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   139
                            () -> (Signature_attribute) classFile.getAttribute(Attribute.Signature),
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   140
                            getClassExpectedSignature(className, clazz).get(className));
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   141
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   142
                    testFields(getExpectedFieldSignatures(clazz), classFile);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   143
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   144
                    testMethods(getExpectedMethodSignatures(clazz), classFile);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   145
                } catch (Exception e) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   146
                    addFailure(e);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   147
                }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   148
            }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   149
        } catch (Exception e) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   150
            addFailure(e);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   151
        } finally {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   152
            checkStatus();
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   153
        }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   154
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   155
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   156
    private void checkAllMembersFound(Set<String> found, Map<String, ExpectedSignature> signatures, String message) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   157
        if (signatures != null) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   158
            checkContains(found,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   159
                    signatures.values().stream()
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   160
                            .map(ExpectedSignature::descriptor)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   161
                            .collect(Collectors.toSet()),
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   162
                    message);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   163
        }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   164
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   165
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   166
    private void testMethods(Map<String, ExpectedSignature> expectedSignatures, ClassFile classFile)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   167
            throws ConstantPoolException, Descriptor.InvalidDescriptor {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   168
        String className = classFile.getName();
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   169
        Set<String> foundMethods = new HashSet<>();
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   170
        for (Method method : classFile.methods) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   171
            String methodName = getMethodName(classFile, method);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   172
            printf("Testing method %s\n", methodName);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   173
            if (method.access_flags.getMethodFlags().contains(ACC_BRIDGE)) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   174
                printf("Bridge method is skipped : %s\n", methodName);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   175
                continue;
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   176
            }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   177
            testAttribute(
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   178
                    methodName,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   179
                    classFile,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   180
                    () -> (Signature_attribute) method.attributes.get(Attribute.Signature),
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   181
                    expectedSignatures.get(methodName));
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   182
            foundMethods.add(methodName);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   183
        }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   184
        checkAllMembersFound(foundMethods, expectedSignatures,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   185
                "Checking that all methods of class " + className + " with Signature attribute found");
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   186
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   187
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   188
    private String getMethodName(ClassFile classFile, Method method)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   189
            throws ConstantPoolException, Descriptor.InvalidDescriptor {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   190
        return String.format("%s%s",
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   191
                method.getName(classFile.constant_pool),
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   192
                method.descriptor.getParameterTypes(classFile.constant_pool));
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   193
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   194
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   195
    private void testFields(Map<String, ExpectedSignature> expectedSignatures, ClassFile classFile)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   196
            throws ConstantPoolException {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   197
        String className = classFile.getName();
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   198
        Set<String> foundFields = new HashSet<>();
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   199
        for (Field field : classFile.fields) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   200
            String fieldName = field.getName(classFile.constant_pool);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   201
            printf("Testing field %s\n", fieldName);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   202
            testAttribute(
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   203
                    fieldName,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   204
                    classFile,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   205
                    () -> (Signature_attribute) field.attributes.get(Attribute.Signature),
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   206
                    expectedSignatures.get(fieldName));
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   207
            foundFields.add(fieldName);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   208
        }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   209
        checkAllMembersFound(foundFields, expectedSignatures,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   210
                "Checking that all fields of class " + className + " with Signature attribute found");
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   211
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   212
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   213
    private void testAttribute(
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   214
            String memberName,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   215
            ClassFile classFile,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   216
            Supplier<Signature_attribute> sup,
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   217
            ExpectedSignature expectedSignature)
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   218
            throws ConstantPoolException {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   219
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   220
        Signature_attribute attribute = sup.get();
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   221
        if (expectedSignature != null && checkNotNull(attribute, memberName + " must have attribute")) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   222
            checkEquals(classFile.constant_pool.getUTF8Value(attribute.attribute_name_index),
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   223
                    "Signature", "Attribute's name : " + memberName);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   224
            checkEquals(attribute.attribute_length, 2, "Attribute's length : " + memberName);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   225
            checkEquals(attribute.getSignature(classFile.constant_pool),
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   226
                    expectedSignature.signature(),
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   227
                    "Testing signature of : " + memberName);
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   228
        } else {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   229
            checkNull(attribute, memberName + " must not have attribute");
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   230
        }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   231
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   232
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   233
    public static void main(String[] args) throws TestFailedException {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   234
        if (args.length != 1) {
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   235
            throw new IllegalArgumentException("Usage: Driver <class-name>");
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   236
        }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   237
        new Driver(args[0]).test();
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   238
    }
c03745b71c70 8049238: Implement classfile tests for Signature attribute
aeremeev
parents:
diff changeset
   239
}