• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.turbine.binder.ClassPath;
24 import com.google.turbine.binder.ClassPathBinder;
25 import com.google.turbine.binder.JimageClassBinder;
26 import com.google.turbine.options.LanguageVersion;
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 final class TestClassPaths {
37 
38   private static final Splitter CLASS_PATH_SPLITTER =
39       Splitter.on(File.pathSeparatorChar).omitEmptyStrings();
40 
41   public static final ImmutableList<Path> BOOTCLASSPATH =
42       CLASS_PATH_SPLITTER
43           .splitToStream(Optional.ofNullable(System.getProperty("sun.boot.class.path")).orElse(""))
44           .map(Paths::get)
45           .filter(Files::exists)
46           .collect(toImmutableList());
47 
48   public static final ClassPath TURBINE_BOOTCLASSPATH = getTurbineBootclasspath();
49 
getTurbineBootclasspath()50   private static ClassPath getTurbineBootclasspath() {
51     try {
52       if (!BOOTCLASSPATH.isEmpty()) {
53         return ClassPathBinder.bindClasspath(BOOTCLASSPATH);
54       }
55       return JimageClassBinder.bindDefault();
56     } catch (IOException e) {
57       e.printStackTrace();
58       throw new UncheckedIOException(e);
59     }
60   }
61 
62   /**
63    * Return an {@link TurbineOptions} builder, with either {@code --bootclasspath} or {@link
64    * --release} set to a JDK 8 equivalent.
65    */
optionsWithBootclasspath()66   public static TurbineOptions.Builder optionsWithBootclasspath() {
67     TurbineOptions.Builder options = TurbineOptions.builder();
68     if (!BOOTCLASSPATH.isEmpty()) {
69       options.setBootClassPath(
70           BOOTCLASSPATH.stream().map(Path::toString).collect(toImmutableList()));
71     } else {
72       options.setLanguageVersion(LanguageVersion.fromJavacopts(ImmutableList.of("--release", "8")));
73     }
74     return options;
75   }
76 
TestClassPaths()77   private TestClassPaths() {}
78 }
79