• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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.binder;
18 
19 import com.google.common.base.Supplier;
20 import com.google.common.collect.ImmutableMap;
21 import com.google.turbine.binder.bound.ModuleInfo;
22 import com.google.turbine.binder.bytecode.BytecodeBinder;
23 import com.google.turbine.binder.bytecode.BytecodeBoundClass;
24 import com.google.turbine.binder.env.Env;
25 import com.google.turbine.binder.env.SimpleEnv;
26 import com.google.turbine.binder.lookup.SimpleTopLevelIndex;
27 import com.google.turbine.binder.lookup.TopLevelIndex;
28 import com.google.turbine.binder.sym.ClassSymbol;
29 import com.google.turbine.binder.sym.ModuleSymbol;
30 import com.google.turbine.zip.Zip;
31 import java.io.IOException;
32 import java.nio.file.Path;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.LinkedHashMap;
36 import java.util.Map;
37 import org.jspecify.annotations.Nullable;
38 
39 /** Sets up an environment for symbols on the classpath. */
40 public final class ClassPathBinder {
41 
42   /**
43    * The prefix for repackaged transitive dependencies; see {@link
44    * com.google.turbine.deps.Transitive}.
45    */
46   public static final String TRANSITIVE_PREFIX = "META-INF/TRANSITIVE/";
47 
48   /**
49    * The suffix for repackaged transitive dependencies; see {@link
50    * com.google.turbine.deps.Transitive}.
51    */
52   public static final String TRANSITIVE_SUFFIX = ".turbine";
53 
54   /** Creates an environment containing symbols in the given classpath. */
bindClasspath(Collection<Path> paths)55   public static ClassPath bindClasspath(Collection<Path> paths) throws IOException {
56     Map<ClassSymbol, Supplier<BytecodeBoundClass>> transitive = new LinkedHashMap<>();
57     Map<ClassSymbol, Supplier<BytecodeBoundClass>> map = new HashMap<>();
58     Map<ModuleSymbol, ModuleInfo> modules = new HashMap<>();
59     Map<String, Supplier<byte[]>> resources = new HashMap<>();
60     Env<ClassSymbol, BytecodeBoundClass> env =
61         new Env<ClassSymbol, BytecodeBoundClass>() {
62           @Override
63           public @Nullable BytecodeBoundClass get(ClassSymbol sym) {
64             Supplier<BytecodeBoundClass> supplier = map.get(sym);
65             return supplier == null ? null : supplier.get();
66           }
67         };
68     for (Path path : paths) {
69       try {
70         bindJar(path, map, modules, env, transitive, resources);
71       } catch (IOException e) {
72         throw new IOException("error reading " + path, e);
73       }
74     }
75     for (Map.Entry<ClassSymbol, Supplier<BytecodeBoundClass>> entry : transitive.entrySet()) {
76       ClassSymbol symbol = entry.getKey();
77       map.putIfAbsent(symbol, entry.getValue());
78     }
79     SimpleEnv<ModuleSymbol, ModuleInfo> moduleEnv = new SimpleEnv<>(ImmutableMap.copyOf(modules));
80     TopLevelIndex index = SimpleTopLevelIndex.of(map.keySet());
81     return new ClassPath() {
82       @Override
83       public Env<ClassSymbol, BytecodeBoundClass> env() {
84         return env;
85       }
86 
87       @Override
88       public Env<ModuleSymbol, ModuleInfo> moduleEnv() {
89         return moduleEnv;
90       }
91 
92       @Override
93       public TopLevelIndex index() {
94         return index;
95       }
96 
97       @Override
98       public @Nullable Supplier<byte[]> resource(String path) {
99         return resources.get(path);
100       }
101     };
102   }
103 
104   private static void bindJar(
105       Path path,
106       Map<ClassSymbol, Supplier<BytecodeBoundClass>> env,
107       Map<ModuleSymbol, ModuleInfo> modules,
108       Env<ClassSymbol, BytecodeBoundClass> benv,
109       Map<ClassSymbol, Supplier<BytecodeBoundClass>> transitive,
110       Map<String, Supplier<byte[]>> resources)
111       throws IOException {
112     // TODO(cushon): don't leak file descriptors
113     for (Zip.Entry ze : new Zip.ZipIterable(path)) {
114       String name = ze.name();
115       if (name.startsWith(TRANSITIVE_PREFIX)) {
116         if (!name.endsWith(TRANSITIVE_SUFFIX)) {
117           continue;
118         }
119         ClassSymbol sym =
120             new ClassSymbol(
121                 name.substring(
122                     TRANSITIVE_PREFIX.length(), name.length() - TRANSITIVE_SUFFIX.length()));
123         transitive.putIfAbsent(sym, BytecodeBoundClass.lazy(sym, ze, benv, path));
124         continue;
125       }
126       if (!name.endsWith(".class")) {
127         resources.put(name, ze);
128         continue;
129       }
130       if (name.substring(name.lastIndexOf('/') + 1).equals("module-info.class")) {
131         ModuleInfo moduleInfo = BytecodeBinder.bindModuleInfo(path.toString(), ze);
132         modules.put(new ModuleSymbol(moduleInfo.name()), moduleInfo);
133         continue;
134       }
135       ClassSymbol sym = new ClassSymbol(name.substring(0, name.length() - ".class".length()));
136       env.putIfAbsent(sym, BytecodeBoundClass.lazy(sym, ze, benv, path));
137     }
138   }
139 
140   private ClassPathBinder() {}
141 }
142