1/* 2 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.tools.javac.platform; 27 28import java.io.IOException; 29import java.net.URI; 30import java.nio.charset.Charset; 31import java.nio.file.DirectoryStream; 32import java.nio.file.FileSystem; 33import java.nio.file.FileSystems; 34import java.nio.file.Files; 35import java.nio.file.Path; 36import java.nio.file.Paths; 37import java.nio.file.ProviderNotFoundException; 38import java.util.ArrayList; 39import java.util.Collection; 40import java.util.Collections; 41import java.util.HashMap; 42import java.util.List; 43import java.util.Map; 44import java.util.Set; 45import java.util.TreeSet; 46import java.util.stream.Stream; 47 48import javax.annotation.processing.Processor; 49 50import com.sun.source.util.Plugin; 51import com.sun.tools.javac.jvm.Target; 52 53/** PlatformProvider for JDK N. 54 * 55 * <p><b>This is NOT part of any supported API. 56 * If you write code that depends on this, you do so at your own risk. 57 * This code and its internal interfaces are subject to change or 58 * deletion without notice.</b> 59 */ 60public class JDKPlatformProvider implements PlatformProvider { 61 62 @Override 63 public Iterable<String> getSupportedPlatformNames() { 64 return SUPPORTED_JAVA_PLATFORM_VERSIONS; 65 } 66 67 @Override 68 public PlatformDescription getPlatform(String platformName, String options) { 69 return new PlatformDescriptionImpl(platformName.equals("10") ? "9" : platformName); 70 } 71 72 private static final String[] symbolFileLocation = { "lib", "ct.sym" }; 73 74 private static final Set<String> SUPPORTED_JAVA_PLATFORM_VERSIONS; 75 76 static { 77 SUPPORTED_JAVA_PLATFORM_VERSIONS = new TreeSet<>(); 78 Path ctSymFile = findCtSym(); 79 if (Files.exists(ctSymFile)) { 80 try (FileSystem fs = FileSystems.newFileSystem(ctSymFile, null); 81 DirectoryStream<Path> dir = 82 Files.newDirectoryStream(fs.getRootDirectories().iterator().next())) { 83 for (Path section : dir) { 84 for (char ver : section.getFileName().toString().toCharArray()) { 85 String verString = Character.toString(ver); 86 Target t = Target.lookup(verString); 87 88 if (t != null) { 89 SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(t)); 90 } 91 } 92 } 93 } catch (IOException | ProviderNotFoundException ex) { 94 } 95 } 96 97 if (SUPPORTED_JAVA_PLATFORM_VERSIONS.contains("9")) { 98 SUPPORTED_JAVA_PLATFORM_VERSIONS.add("10"); 99 } 100 } 101 102 private static String targetNumericVersion(Target target) { 103 return Integer.toString(target.ordinal() - Target.JDK1_1.ordinal() + 1); 104 } 105 106 static class PlatformDescriptionImpl implements PlatformDescription { 107 108 private final Map<Path, FileSystem> ctSym2FileSystem = new HashMap<>(); 109 private final String version; 110 111 PlatformDescriptionImpl(String version) { 112 this.version = version; 113 } 114 115 @Override 116 public Collection<Path> getPlatformPath() { 117 List<Path> paths = new ArrayList<>(); 118 Path file = findCtSym(); 119 // file == ${jdk.home}/lib/ct.sym 120 if (Files.exists(file)) { 121 FileSystem fs = ctSym2FileSystem.get(file); 122 if (fs == null) { 123 try { 124 ctSym2FileSystem.put(file, fs = FileSystems.newFileSystem(file, null)); 125 } catch (IOException ex) { 126 throw new IllegalStateException(ex); 127 } 128 } 129 Path root = fs.getRootDirectories().iterator().next(); 130 try (DirectoryStream<Path> dir = Files.newDirectoryStream(root)) { 131 for (Path section : dir) { 132 if (section.getFileName().toString().contains(version)) { 133 Path systemModules = section.resolve("system-modules"); 134 135 if (Files.isRegularFile(systemModules)) { 136 Path modules = 137 FileSystems.getFileSystem(URI.create("jrt:/")) 138 .getPath("modules"); 139 try (Stream<String> lines = 140 Files.lines(systemModules, Charset.forName("UTF-8"))) { 141 lines.map(line -> modules.resolve(line)) 142 .filter(mod -> Files.exists(mod)) 143 .forEach(mod -> paths.add(mod)); 144 } 145 } else { 146 paths.add(section); 147 } 148 } 149 } 150 } catch (IOException ex) { 151 throw new IllegalStateException(ex); 152 } 153 } else { 154 throw new IllegalStateException("Cannot find ct.sym!"); 155 } 156 return paths; 157 } 158 159 @Override 160 public String getSourceVersion() { 161 return version; 162 } 163 164 @Override 165 public String getTargetVersion() { 166 return version; 167 } 168 169 @Override 170 public List<PluginInfo<Processor>> getAnnotationProcessors() { 171 return Collections.emptyList(); 172 } 173 174 @Override 175 public List<PluginInfo<Plugin>> getPlugins() { 176 return Collections.emptyList(); 177 } 178 179 @Override 180 public List<String> getAdditionalOptions() { 181 return Collections.emptyList(); 182 } 183 184 @Override 185 public void close() throws IOException { 186 for (FileSystem fs : ctSym2FileSystem.values()) { 187 fs.close(); 188 } 189 ctSym2FileSystem.clear(); 190 } 191 192 } 193 194 static Path findCtSym() { 195 String javaHome = System.getProperty("java.home"); 196 Path file = Paths.get(javaHome); 197 // file == ${jdk.home} 198 for (String name : symbolFileLocation) 199 file = file.resolve(name); 200 return file; 201 } 202 203} 204