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 26 package com.sun.tools.javac.platform; 27 28 import java.io.IOException; 29 import java.nio.file.DirectoryStream; 30 import java.nio.file.FileSystem; 31 import java.nio.file.FileSystems; 32 import java.nio.file.Files; 33 import java.nio.file.Path; 34 import java.nio.file.Paths; 35 import java.nio.file.ProviderNotFoundException; 36 import java.util.ArrayList; 37 import java.util.Collection; 38 import java.util.Collections; 39 import java.util.HashMap; 40 import java.util.List; 41 import java.util.Map; 42 import java.util.Set; 43 import java.util.TreeSet; 44 45 import javax.annotation.processing.Processor; 46 47 import com.sun.source.util.Plugin; 48 import com.sun.tools.javac.jvm.Target; 49 50 /** PlatformProvider for JDK N. 51 * 52 * <p><b>This is NOT part of any supported API. 53 * If you write code that depends on this, you do so at your own risk. 54 * This code and its internal interfaces are subject to change or 55 * deletion without notice.</b> 56 */ 57 public class JDKPlatformProvider implements PlatformProvider { 58 59 @Override 60 public Iterable<String> getSupportedPlatformNames() { 61 return SUPPORTED_JAVA_PLATFORM_VERSIONS; 62 } 63 64 @Override 65 public PlatformDescription getPlatform(String platformName, String options) { 66 return new PlatformDescriptionImpl(platformName); 67 } 68 69 private static final String[] symbolFileLocation = { "lib", "ct.sym" }; 70 71 private static final Set<String> SUPPORTED_JAVA_PLATFORM_VERSIONS; 72 73 static { 74 SUPPORTED_JAVA_PLATFORM_VERSIONS = new TreeSet<>(); 75 Path ctSymFile = findCtSym(); 76 if (Files.exists(ctSymFile)) { 77 try (FileSystem fs = FileSystems.newFileSystem(ctSymFile, null); 78 DirectoryStream<Path> dir = 79 Files.newDirectoryStream(fs.getRootDirectories().iterator().next())) { 80 for (Path section : dir) { 81 for (char ver : section.getFileName().toString().toCharArray()) { 82 String verString = Character.toString(ver); 83 Target t = Target.lookup(verString); 84 85 if (t != null) { 86 SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(t)); 87 } 88 } 89 } 90 } catch (IOException | ProviderNotFoundException ex) { 91 } 92 } 93 // Workaround until full support for --release 9 distinct from --release 10 94 SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(Target.JDK1_9)); 95 SUPPORTED_JAVA_PLATFORM_VERSIONS.add(targetNumericVersion(Target.DEFAULT)); 96 } 97 98 private static String targetNumericVersion(Target target) { 99 return Integer.toString(target.ordinal() - Target.JDK1_1.ordinal() + 1); 100 } 101 102 static class PlatformDescriptionImpl implements PlatformDescription { 103 104 private final Map<Path, FileSystem> ctSym2FileSystem = new HashMap<>(); 105 private final String version; 106 107 PlatformDescriptionImpl(String version) { 108 this.version = version; 109 } 110 111 @Override 112 public Collection<Path> getPlatformPath() { 113 // Comparison should be == Target.DEFAULT once --release 9 114 // is distinct from 10 115 if (Target.lookup(version).compareTo(Target.JDK1_9) >= 0) { 116 return null; 117 } 118 119 List<Path> paths = new ArrayList<>(); 120 Path file = findCtSym(); 121 // file == ${jdk.home}/lib/ct.sym 122 if (Files.exists(file)) { 123 FileSystem fs = ctSym2FileSystem.get(file); 124 if (fs == null) { 125 try { 126 ctSym2FileSystem.put(file, fs = FileSystems.newFileSystem(file, null)); 127 } catch (IOException ex) { 128 throw new IllegalStateException(ex); 129 } 130 } 131 Path root = fs.getRootDirectories().iterator().next(); 132 try (DirectoryStream<Path> dir = Files.newDirectoryStream(root)) { 133 for (Path section : dir) { 134 if (section.getFileName().toString().contains(version)) { 135 paths.add(section); 136 } 137 } 138 } catch (IOException ex) { 139 throw new IllegalStateException(ex); 140 } 141 } else { 142 throw new IllegalStateException("Cannot find ct.sym!"); 143 } 144 return paths; 145 } 146 147 @Override 148 public String getSourceVersion() { 149 return version; 150 } 151 152 @Override 153 public String getTargetVersion() { 154 return version; 155 } 156 157 @Override 158 public List<PluginInfo<Processor>> getAnnotationProcessors() { 159 return Collections.emptyList(); 160 } 161 162 @Override 163 public List<PluginInfo<Plugin>> getPlugins() { 164 return Collections.emptyList(); 165 } 166 167 @Override 168 public List<String> getAdditionalOptions() { 169 return Collections.emptyList(); 170 } 171 172 @Override 173 public void close() throws IOException { 174 for (FileSystem fs : ctSym2FileSystem.values()) { 175 fs.close(); 176 } 177 ctSym2FileSystem.clear(); 178 } 179 180 } 181 182 static Path findCtSym() { 183 String javaHome = System.getProperty("java.home"); 184 Path file = Paths.get(javaHome); 185 // file == ${jdk.home} 186 for (String name : symbolFileLocation) 187 file = file.resolve(name); 188 return file; 189 } 190 191 }