1 // Copyright 2017 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 static com.google.common.base.Preconditions.checkNotNull; 17 import static com.google.common.base.Preconditions.checkState; 18 19 import com.google.devtools.build.android.desugar.io.BitFlags; 20 import java.util.HashMap; 21 import javax.annotation.Nullable; 22 import org.objectweb.asm.ClassReader; 23 24 /** 25 * Simple memoizer for whether types are classes or interfaces. 26 */ 27 class ClassVsInterface { 28 /** Map from internal names to whether they are an interface ({@code false} thus means class). */ 29 private final HashMap<String, Boolean> known = new HashMap<>(); 30 private final ClassReaderFactory classpath; 31 ClassVsInterface(ClassReaderFactory classpath)32 public ClassVsInterface(ClassReaderFactory classpath) { 33 this.classpath = classpath; 34 } 35 addKnownClass(@ullable String internalName)36 public ClassVsInterface addKnownClass(@Nullable String internalName) { 37 if (internalName != null) { 38 Boolean previous = known.put(internalName, false); 39 checkState(previous == null || !previous, "Already recorded as interface: %s", internalName); 40 } 41 return this; 42 } 43 addKnownInterfaces(String... internalNames)44 public ClassVsInterface addKnownInterfaces(String... internalNames) { 45 for (String internalName : internalNames) { 46 Boolean previous = known.put(internalName, true); 47 checkState(previous == null || previous, "Already recorded as class: %s", internalName); 48 } 49 return this; 50 } 51 isOuterInterface(String outerName, String innerName)52 public boolean isOuterInterface(String outerName, String innerName) { 53 Boolean result = known.get(outerName); 54 if (result == null) { 55 // We could just load the outer class here, but this tolerates incomplete classpaths better. 56 // Note the outer class should be in the Jar we're desugaring, so it should always be there. 57 ClassReader outerClass = checkNotNull(classpath.readIfKnown(outerName), 58 "Couldn't find outer class %s of %s", outerName, innerName); 59 result = BitFlags.isInterface(outerClass.getAccess()); 60 known.put(outerName, result); 61 } 62 return result; 63 } 64 } 65