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