• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.checkState;
17 
18 import com.google.common.collect.ImmutableMap;
19 import java.util.LinkedHashMap;
20 import java.util.Map;
21 import org.objectweb.asm.ClassVisitor;
22 import org.objectweb.asm.tree.ClassNode;
23 
24 /**
25  * Simple wrapper around a map that holds generated classes so they can be processed later.
26  */
27 class GeneratedClassStore {
28 
29   /** Map from internal names to generated classes with deterministic iteration order. */
30   private final Map<String, ClassNode> classes = new LinkedHashMap<>();
31 
32   /**
33    * Adds a class for the given internal name. It's the caller's responsibility to {@link
34    * ClassVisitor#visit} the returned object to initialize the desired class, and to avoid
35    * confusion, this method throws if the class had already been present.
36    */
add(String internalClassName)37   public ClassVisitor add(String internalClassName) {
38     ClassNode result = new ClassNode();
39     checkState(
40         classes.put(internalClassName, result) == null, "Already present: %s", internalClassName);
41     return result;
42   }
43 
drain()44   public ImmutableMap<String, ClassNode> drain() {
45     ImmutableMap<String, ClassNode> result = ImmutableMap.copyOf(classes);
46     classes.clear();
47     return result;
48   }
49 }
50