1 /* 2 * Copyright 2018 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.turbine.testing; 18 19 import static com.google.common.collect.ImmutableList.toImmutableList; 20 21 import com.google.common.base.Splitter; 22 import com.google.common.collect.ImmutableList; 23 import com.google.common.collect.Streams; 24 import com.google.turbine.binder.ClassPath; 25 import com.google.turbine.binder.ClassPathBinder; 26 import com.google.turbine.binder.CtSymClassBinder; 27 import com.google.turbine.options.TurbineOptions; 28 import java.io.File; 29 import java.io.IOException; 30 import java.io.UncheckedIOException; 31 import java.nio.file.Files; 32 import java.nio.file.Path; 33 import java.nio.file.Paths; 34 import java.util.Optional; 35 36 public class TestClassPaths { 37 38 private static final Splitter CLASS_PATH_SPLITTER = 39 Splitter.on(File.pathSeparatorChar).omitEmptyStrings(); 40 41 private static final ImmutableList<Path> BOOTCLASSPATH = 42 Streams.stream( 43 CLASS_PATH_SPLITTER.split( 44 Optional.ofNullable(System.getProperty("sun.boot.class.path")).orElse(""))) 45 .map(Paths::get) 46 .filter(Files::exists) 47 .collect(toImmutableList()); 48 49 public static final ClassPath TURBINE_BOOTCLASSPATH = getTurbineBootclasspath(); 50 getTurbineBootclasspath()51 private static ClassPath getTurbineBootclasspath() { 52 try { 53 if (!BOOTCLASSPATH.isEmpty()) { 54 return ClassPathBinder.bindClasspath(BOOTCLASSPATH); 55 } 56 return CtSymClassBinder.bind("8"); 57 } catch (IOException e) { 58 e.printStackTrace(); 59 throw new UncheckedIOException(e); 60 } 61 } 62 63 /** 64 * Return an {@link TurbineOptions} builder, with either {@code --bootclasspath} or {@link 65 * --release} set to a JDK 8 equivalent. 66 */ optionsWithBootclasspath()67 public static TurbineOptions.Builder optionsWithBootclasspath() { 68 TurbineOptions.Builder options = TurbineOptions.builder(); 69 if (!BOOTCLASSPATH.isEmpty()) { 70 options.setBootClassPath( 71 BOOTCLASSPATH.stream().map(Path::toString).collect(toImmutableList())); 72 } else { 73 options.setRelease("8"); 74 } 75 return options; 76 } 77 } 78