1 // Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file 2 // for details. All rights reserved. Use of this source code is governed by a 3 // BSD-style license that can be found in the LICENSE file. 4 package com.android.tools.r8.ir.code; 5 6 import com.android.tools.r8.graph.DexItemFactory; 7 import com.android.tools.r8.graph.DexType; 8 import com.google.common.collect.ImmutableList; 9 import com.google.common.collect.ImmutableSet; 10 import java.util.List; 11 import java.util.Set; 12 13 public class CatchHandlers<T> { 14 15 private final List<DexType> guards; 16 private final List<T> targets; 17 private Set<T> uniqueTargets; 18 19 public static final CatchHandlers<Integer> EMPTY_INDICES = new CatchHandlers<>(); 20 public static final CatchHandlers<BasicBlock> EMPTY_BASIC_BLOCK = new CatchHandlers<>(); 21 CatchHandlers()22 private CatchHandlers() { 23 guards = ImmutableList.of(); 24 targets = ImmutableList.of(); 25 } 26 CatchHandlers(List<DexType> guards, List<T> targets)27 public CatchHandlers(List<DexType> guards, List<T> targets) { 28 assert !guards.isEmpty(); 29 assert guards.size() == targets.size(); 30 // Guava ImmutableList does not support null elements. 31 this.guards = ImmutableList.copyOf(guards); 32 this.targets = ImmutableList.copyOf(targets); 33 } 34 isEmpty()35 public boolean isEmpty() { 36 return size() == 0; 37 } 38 size()39 public int size() { 40 assert guards.size() == targets.size(); 41 return guards.size(); 42 } 43 getGuards()44 public List<DexType> getGuards() { 45 return guards; 46 } 47 getAllTargets()48 public List<T> getAllTargets() { 49 return targets; 50 } 51 getUniqueTargets()52 public Set<T> getUniqueTargets() { 53 if (uniqueTargets == null) { 54 uniqueTargets = ImmutableSet.copyOf(targets); 55 } 56 return uniqueTargets; 57 } 58 hasCatchAll()59 public boolean hasCatchAll() { 60 return getGuards().size() > 0 && 61 getGuards().get(getGuards().size() - 1) == DexItemFactory.catchAllType; 62 } 63 64 @Override equals(Object o)65 public boolean equals(Object o) { 66 if (this == o) { 67 return true; 68 } 69 if (!(o instanceof CatchHandlers)) { 70 return false; 71 } 72 CatchHandlers that = (CatchHandlers) o; 73 return guards.equals(that.guards) && targets.equals(that.targets); 74 } 75 76 @Override hashCode()77 public int hashCode() { 78 return 31 * guards.hashCode() + targets.hashCode(); 79 } 80 } 81