src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/OptionsVerifierTest.java
changeset 58299 6df94ce3ab2f
parent 55509 d58442b8abc1
child 58679 9c3209ff7550
equal deleted inserted replaced
58298:0152ad7b38b8 58299:6df94ce3ab2f
     1 /*
     1 /*
     2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    24 
    24 
    25 package org.graalvm.compiler.core.test;
    25 package org.graalvm.compiler.core.test;
    26 
    26 
    27 import static java.lang.String.format;
    27 import static java.lang.String.format;
    28 
    28 
    29 import java.io.ByteArrayOutputStream;
       
    30 import java.io.File;
       
    31 import java.io.IOException;
    29 import java.io.IOException;
    32 import java.io.InputStream;
       
    33 import java.lang.reflect.Constructor;
    30 import java.lang.reflect.Constructor;
    34 import java.lang.reflect.Executable;
    31 import java.lang.reflect.Executable;
    35 import java.lang.reflect.Method;
    32 import java.lang.reflect.Method;
    36 import java.net.URL;
       
    37 import java.net.URLClassLoader;
       
    38 import java.nio.file.Files;
       
    39 import java.util.ArrayList;
       
    40 import java.util.Arrays;
    33 import java.util.Arrays;
    41 import java.util.HashSet;
    34 import java.util.HashSet;
    42 import java.util.LinkedHashMap;
       
    43 import java.util.List;
       
    44 import java.util.Map;
       
    45 import java.util.Objects;
       
    46 import java.util.Set;
    35 import java.util.Set;
    47 
    36 
    48 import org.graalvm.compiler.options.OptionDescriptor;
    37 import org.graalvm.compiler.options.OptionDescriptor;
    49 import org.graalvm.compiler.options.OptionDescriptors;
    38 import org.graalvm.compiler.options.OptionDescriptors;
    50 import org.graalvm.compiler.options.OptionKey;
    39 import org.graalvm.compiler.options.OptionKey;
    51 import org.graalvm.compiler.options.OptionsParser;
    40 import org.graalvm.compiler.options.OptionsParser;
    52 import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
    41 import org.graalvm.compiler.serviceprovider.GraalServices;
    53 import org.junit.Test;
    42 import org.junit.Test;
    54 import org.objectweb.asm.ClassReader;
    43 import org.objectweb.asm.ClassReader;
    55 import org.objectweb.asm.ClassVisitor;
    44 import org.objectweb.asm.ClassVisitor;
    56 import org.objectweb.asm.Label;
    45 import org.objectweb.asm.Label;
    57 import org.objectweb.asm.MethodVisitor;
    46 import org.objectweb.asm.MethodVisitor;
    65  */
    54  */
    66 public class OptionsVerifierTest {
    55 public class OptionsVerifierTest {
    67 
    56 
    68     @Test
    57     @Test
    69     public void verifyOptions() throws IOException {
    58     public void verifyOptions() throws IOException {
    70         try (Classpath cp = new Classpath()) {
    59         HashSet<Class<?>> checked = new HashSet<>();
    71             HashSet<Class<?>> checked = new HashSet<>();
    60         for (OptionDescriptors opts : OptionsParser.getOptionsLoader()) {
    72             for (OptionDescriptors opts : OptionsParser.getOptionsLoader()) {
    61             for (OptionDescriptor desc : opts) {
    73                 for (OptionDescriptor desc : opts) {
    62                 OptionsVerifier.checkClass(desc.getDeclaringClass(), desc, checked);
    74                     OptionsVerifier.checkClass(desc.getDeclaringClass(), desc, checked, cp);
       
    75                 }
       
    76             }
    63             }
    77         }
    64         }
    78     }
    65     }
    79 
    66 
    80     static class Classpath implements AutoCloseable {
       
    81         private final Map<String, Object> entries = new LinkedHashMap<>();
       
    82 
       
    83         Classpath() throws IOException {
       
    84             List<String> names = new ArrayList<>(Arrays.asList(System.getProperty("java.class.path").split(File.pathSeparator)));
       
    85             if (JavaVersionUtil.JAVA_SPEC <= 8) {
       
    86                 names.addAll(Arrays.asList(System.getProperty("sun.boot.class.path").split(File.pathSeparator)));
       
    87             } else {
       
    88                 names.addAll(Arrays.asList(System.getProperty("jdk.module.path").split(File.pathSeparator)));
       
    89             }
       
    90             for (String n : names) {
       
    91                 File path = new File(n);
       
    92                 if (path.exists()) {
       
    93                     if (path.isDirectory()) {
       
    94                         entries.put(n, path);
       
    95                     } else if (n.endsWith(".jar") || n.endsWith(".zip")) {
       
    96                         URL url = new URL("jar", "", "file:" + n + "!/");
       
    97                         entries.put(n, new URLClassLoader(new URL[]{url}));
       
    98                     }
       
    99                 }
       
   100             }
       
   101         }
       
   102 
       
   103         @Override
       
   104         public void close() throws IOException {
       
   105             for (Object e : entries.values()) {
       
   106                 if (e instanceof URLClassLoader) {
       
   107                     ((URLClassLoader) e).close();
       
   108                 }
       
   109             }
       
   110         }
       
   111 
       
   112         public byte[] getInputStream(String classFilePath) throws IOException {
       
   113             for (Object e : entries.values()) {
       
   114                 if (e instanceof File) {
       
   115                     File path = new File((File) e, classFilePath.replace('/', File.separatorChar));
       
   116                     if (path.exists()) {
       
   117                         return Files.readAllBytes(path.toPath());
       
   118                     }
       
   119                 } else {
       
   120                     assert e instanceof URLClassLoader;
       
   121                     URLClassLoader ucl = (URLClassLoader) e;
       
   122                     try (InputStream in = ucl.getResourceAsStream(classFilePath)) {
       
   123                         if (in != null) {
       
   124                             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
       
   125                             int nRead;
       
   126                             byte[] data = new byte[1024];
       
   127                             while ((nRead = in.read(data, 0, data.length)) != -1) {
       
   128                                 buffer.write(data, 0, nRead);
       
   129                             }
       
   130                             return buffer.toByteArray();
       
   131                         }
       
   132                     }
       
   133                 }
       
   134             }
       
   135             return null;
       
   136         }
       
   137     }
       
   138 
       
   139     static final class OptionsVerifier extends ClassVisitor {
    67     static final class OptionsVerifier extends ClassVisitor {
   140 
    68 
   141         public static void checkClass(Class<?> cls, OptionDescriptor option, Set<Class<?>> checked, Classpath cp) throws IOException {
    69         public static void checkClass(Class<?> cls, OptionDescriptor option, Set<Class<?>> checked) throws IOException {
   142             if (!checked.contains(cls)) {
    70             if (!checked.contains(cls)) {
   143                 checked.add(cls);
    71                 checked.add(cls);
   144                 Class<?> superclass = cls.getSuperclass();
    72                 Class<?> superclass = cls.getSuperclass();
   145                 if (superclass != null && !superclass.equals(Object.class)) {
    73                 if (superclass != null && !superclass.equals(Object.class)) {
   146                     checkClass(superclass, option, checked, cp);
    74                     checkClass(superclass, option, checked);
   147                 }
    75                 }
   148 
    76 
   149                 String classFilePath = cls.getName().replace('.', '/') + ".class";
    77                 GraalServices.getClassfileAsStream(cls);
   150                 ClassReader cr = new ClassReader(Objects.requireNonNull(cp.getInputStream(classFilePath), "Could not find class file for " + cls.getName()));
    78                 ClassReader cr = new ClassReader(GraalServices.getClassfileAsStream(cls));
   151 
    79 
   152                 ClassVisitor cv = new OptionsVerifier(cls, option);
    80                 ClassVisitor cv = new OptionsVerifier(cls, option);
   153                 cr.accept(cv, 0);
    81                 cr.accept(cv, 0);
   154             }
    82             }
   155         }
    83         }