• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Bazel Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 package com.google.devtools.build.android.desugar;
15 
16 import com.google.devtools.build.android.desugar.io.CoreLibraryRewriter;
17 import com.google.devtools.build.android.desugar.io.IndexedInputs;
18 import com.google.devtools.build.android.desugar.io.InputFileProvider;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import javax.annotation.Nullable;
22 import org.objectweb.asm.ClassReader;
23 
24 class ClassReaderFactory {
25   private final IndexedInputs indexedInputs;
26   private final CoreLibraryRewriter rewriter;
27 
ClassReaderFactory(IndexedInputs indexedInputs, CoreLibraryRewriter rewriter)28   public ClassReaderFactory(IndexedInputs indexedInputs, CoreLibraryRewriter rewriter) {
29     this.rewriter = rewriter;
30     this.indexedInputs = indexedInputs;
31   }
32 
33   /**
34    * Returns a reader for the given/internal/Class$Name if the class is defined in the wrapped input
35    * and {@code null} otherwise.  For simplicity this method turns checked into runtime exceptions
36    * under the assumption that all classes have already been read once when this method is called.
37    */
38   @Nullable
readIfKnown(String internalClassName)39   public ClassReader readIfKnown(String internalClassName) {
40     String filename = rewriter.unprefix(internalClassName) + ".class";
41     InputFileProvider inputFileProvider = indexedInputs.getInputFileProvider(filename);
42 
43     if (inputFileProvider != null) {
44       try (InputStream bytecode = inputFileProvider.getInputStream(filename)) {
45         // ClassReader doesn't take ownership and instead eagerly reads the stream's contents
46         return rewriter.reader(bytecode);
47       } catch (IOException e) {
48         // We should've already read through all files in the Jar once at this point, so we don't
49         // expect failures reading some files a second time.
50         throw new IllegalStateException("Couldn't load " + internalClassName, e);
51       }
52     }
53 
54     return null;
55   }
56 
57   /**
58    * Returns {@code true} if the given given/internal/Class$Name is defined in the wrapped input.
59    */
isKnown(String internalClassName)60   public boolean isKnown(String internalClassName) {
61     String filename = rewriter.unprefix(internalClassName) + ".class";
62     return indexedInputs.getInputFileProvider(filename) != null;
63   }
64 }
65