test/jdk/tools/jimage/VerifyJimage.java
author herrick
Fri, 21 Jun 2019 08:29:09 -0400
branchJDK-8200758-branch
changeset 57419 44c2481650ce
parent 47216 71c04702a3d5
permissions -rw-r--r--
8225092: Several jpackage tests failes when run with jcov enabled Submitted-by: almatvee Reviewed-by: herrick, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.nio.file.DirectoryStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.nio.file.attribute.BasicFileAttributes;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.ArrayList;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Deque;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.concurrent.ConcurrentLinkedDeque;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.concurrent.ExecutorService;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.concurrent.Executors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.concurrent.TimeUnit;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.concurrent.atomic.AtomicInteger;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.stream.Collectors;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.internal.jimage.BasicImageReader;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import jdk.internal.jimage.ImageLocation;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 * @summary Verify jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 * @modules java.base/jdk.internal.jimage
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
    52
 * @run main/othervm --add-modules ALL-SYSTEM VerifyJimage
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 * This test runs in two modes:
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
 * (1) No argument: it verifies the jimage by loading all classes in the runtime
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
 * (2) path of exploded modules: it compares bytes of each file in the exploded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
 *     module with the entry in jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
 * FIXME: exception thrown when findLocation from jimage by multiple threads
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
 * -Djdk.test.threads=<n> to specify the number of threads.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
public class VerifyJimage {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    private static final String MODULE_INFO = "module-info.class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    private static final Deque<String> failed = new ConcurrentLinkedDeque<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    public static void main(String... args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        String home = System.getProperty("java.home");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        Path bootimagePath = Paths.get(home, "lib", "modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        if (Files.notExists(bootimagePath)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
             System.out.println("Test skipped, not an images build");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
             return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        long start = System.nanoTime();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        int numThreads = Integer.getInteger("jdk.test.threads", 1);
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
    79
        JImageReader reader = newJImageReader();
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
    80
        VerifyJimage verify = new VerifyJimage(reader, numThreads);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        if (args.length == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            // load classes from jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            verify.loadClasses();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
            Path dir = Paths.get(args[0]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            if (Files.notExists(dir) || !Files.isDirectory(dir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                throw new RuntimeException("Invalid argument: " + dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
            verify.compareExplodedModules(dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        verify.waitForCompletion();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        long end = System.nanoTime();
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
    93
        int entries = reader.entries();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        System.out.format("%d entries %d files verified: %d ms %d errors%n",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                          entries, verify.count.get(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                          TimeUnit.NANOSECONDS.toMillis(end - start), failed.size());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        for (String f : failed) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            System.err.println(f);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        if (!failed.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            throw new AssertionError("Test failed");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    private final AtomicInteger count = new AtomicInteger(0);
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   106
    private final JImageReader reader;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    private final ExecutorService pool;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   109
    VerifyJimage(JImageReader reader, int numThreads) {
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   110
        this.reader = reader;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        this.pool = Executors.newFixedThreadPool(numThreads);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    private void waitForCompletion() throws InterruptedException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
        pool.shutdown();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        pool.awaitTermination(20, TimeUnit.SECONDS);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
    private void compareExplodedModules(Path dir) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        System.out.println("comparing jimage with " + dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            for (Path mdir : stream) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                if (Files.isDirectory(mdir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                    pool.execute(new Runnable() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                        public void run() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
                            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                                Files.find(mdir, Integer.MAX_VALUE, (Path p, BasicFileAttributes attr)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                                           -> !Files.isDirectory(p) &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                                              !mdir.relativize(p).toString().startsWith("_") &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                                              !p.getFileName().toString().equals("MANIFEST.MF"))
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   133
                                     .forEach(p -> compare(mdir, p, reader));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
                            } catch (IOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                                throw new UncheckedIOException(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                    });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    private final List<String> BOOT_RESOURCES = Arrays.asList(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        "java.base/META-INF/services/java.nio.file.spi.FileSystemProvider"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
    private final List<String> EXT_RESOURCES = Arrays.asList(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        "jdk.zipfs/META-INF/services/java.nio.file.spi.FileSystemProvider"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
    );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
    private final List<String> APP_RESOURCES = Arrays.asList(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        "jdk.hotspot.agent/META-INF/services/com.sun.jdi.connect.Connector",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        "jdk.jdi/META-INF/services/com.sun.jdi.connect.Connector"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   155
    private void compare(Path mdir, Path p, JImageReader reader) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        String entry = p.getFileName().toString().equals(MODULE_INFO)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                ? mdir.getFileName().toString() + "/" + MODULE_INFO
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                : mdir.relativize(p).toString().replace(File.separatorChar, '/');
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        count.incrementAndGet();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        String file = mdir.getFileName().toString() + "/" + entry;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        if (APP_RESOURCES.contains(file)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
            // skip until the service config file is merged
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            System.out.println("Skipped " + file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   168
        if (reader.findLocation(entry) != null) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
            reader.compare(entry, p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
    private void loadClasses() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        ClassLoader loader = ClassLoader.getSystemClassLoader();
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   175
        Stream.of(reader.getEntryNames())
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   176
              .filter(this::accept)
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   177
              .map(this::toClassName)
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   178
              .forEach(cn -> {
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   179
                  count.incrementAndGet();
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   180
                  try {
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   181
                      System.out.println("Loading " + cn);
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   182
                      Class.forName(cn, false, loader);
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   183
                  } catch (VerifyError ve) {
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   184
                      System.err.println("VerifyError for " + cn);
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   185
                      failed.add(reader.imageName() + ": " + cn + " not verified: " + ve.getMessage());
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   186
                  } catch (ClassNotFoundException e) {
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   187
                      failed.add(reader.imageName() + ": " + cn + " not found");
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   188
                  }
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   189
              });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   192
    private String toClassName(String entry) {
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   193
        int index = entry.indexOf('/', 1);
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   194
        return entry.substring(index + 1, entry.length())
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   195
                    .replaceAll("\\.class$", "").replace('/', '.');
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
44852
82cacb64e969 8177845: Need a mechanism to load Graal
dnsimon
parents: 44840
diff changeset
   198
    private static Set<String> EXCLUDED_MODULES =
82cacb64e969 8177845: Need a mechanism to load Graal
dnsimon
parents: 44840
diff changeset
   199
        Set.of("javafx.deploy", "jdk.deploy", "jdk.plugin", "jdk.javaws",
82cacb64e969 8177845: Need a mechanism to load Graal
dnsimon
parents: 44840
diff changeset
   200
            // All JVMCI packages other than jdk.vm.ci.services are dynamically
82cacb64e969 8177845: Need a mechanism to load Graal
dnsimon
parents: 44840
diff changeset
   201
            // exported to jdk.internal.vm.compiler and jdk.aot
82cacb64e969 8177845: Need a mechanism to load Graal
dnsimon
parents: 44840
diff changeset
   202
            "jdk.internal.vm.compiler", "jdk.aot"
82cacb64e969 8177845: Need a mechanism to load Graal
dnsimon
parents: 44840
diff changeset
   203
        );
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   204
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   205
    private boolean accept(String entry) {
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   206
        int index = entry.indexOf('/', 1);
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   207
        String mn = index > 1 ? entry.substring(1, index) : "";
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   208
        // filter deployment modules
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   209
44852
82cacb64e969 8177845: Need a mechanism to load Graal
dnsimon
parents: 44840
diff changeset
   210
        if (mn.isEmpty() || EXCLUDED_MODULES.contains(mn)) {
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   211
            return false;
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   212
        }
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   213
        return entry.endsWith(".class") && !entry.endsWith(MODULE_INFO);
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   214
    }
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   215
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   216
    private static JImageReader newJImageReader() throws IOException {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        String home = System.getProperty("java.home");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        Path jimage = Paths.get(home, "lib", "modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
        System.out.println("opened " + jimage);
44840
2761d60c363f 8179025: Exclude deployment modules from FieldSetAccessibleTest.java and VerifyJimage.java
mchung
parents: 42703
diff changeset
   220
        return new JImageReader(jimage);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
    static class JImageReader extends BasicImageReader {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
        final Path jimage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
        JImageReader(Path p) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
            super(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
            this.jimage = p;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
        String imageName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
            return jimage.getFileName().toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
        int entries() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            return getHeader().getTableLength();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
        void compare(String entry, Path p) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
                byte[] bytes = Files.readAllBytes(p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                byte[] imagebytes = getResource(entry);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
                if (!Arrays.equals(bytes, imagebytes)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
                    failed.add(imageName() + ": bytes differs than " + p.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
            } catch (IOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
                throw new UncheckedIOException(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
}