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.naming; 5 6 import com.android.tools.r8.naming.MemberNaming.Signature; 7 import java.io.IOException; 8 import java.io.StringWriter; 9 import java.io.Writer; 10 import java.util.LinkedHashMap; 11 import java.util.Map; 12 import java.util.function.Consumer; 13 14 /** 15 * Stores name information for a class. 16 * <p> 17 * This includes how the class was renamed and information on the classes members. 18 */ 19 public class ClassNaming { 20 21 public final String originalName; 22 final String renamedName; 23 24 /** 25 * Mapping from the renamed signature to the naming information for a member. 26 * <p> 27 * A renamed signature is a signature where the member's name has been obfuscated but not the type 28 * information. 29 **/ 30 final Map<Signature, MemberNaming> members = new LinkedHashMap<>(); 31 ClassNaming(String renamedName, String originalName)32 ClassNaming(String renamedName, String originalName) { 33 this.renamedName = renamedName; 34 this.originalName = originalName; 35 } 36 addMemberEntry(MemberNaming entry)37 void addMemberEntry(MemberNaming entry) { 38 Signature renamedSignature = entry.renamedSignature; 39 members.put(renamedSignature, entry); 40 } 41 lookup(Signature renamedSignature)42 public MemberNaming lookup(Signature renamedSignature) { 43 return members.get(renamedSignature); 44 } 45 lookupByOriginalSignature(Signature original)46 public MemberNaming lookupByOriginalSignature(Signature original) { 47 for (MemberNaming naming : members.values()) { 48 if (naming.signature.equals(original)) { 49 return naming; 50 } 51 } 52 return null; 53 } 54 forAllMemberNaming(Consumer<MemberNaming> consumer)55 public void forAllMemberNaming(Consumer<MemberNaming> consumer) { 56 members.values().forEach(consumer); 57 } 58 write(Writer writer, boolean collapseRanges)59 void write(Writer writer, boolean collapseRanges) throws IOException { 60 writer.append(originalName); 61 writer.append(" -> "); 62 writer.append(renamedName); 63 writer.append(":\n"); 64 for (MemberNaming member : members.values()) { 65 member.write(writer, collapseRanges, true); 66 } 67 } 68 69 @Override toString()70 public String toString() { 71 try { 72 StringWriter writer = new StringWriter(); 73 write(writer, false); 74 return writer.toString(); 75 } catch (IOException e) { 76 return e.toString(); 77 } 78 } 79 80 @Override equals(Object o)81 public boolean equals(Object o) { 82 if (this == o) { 83 return true; 84 } 85 if (!(o instanceof ClassNaming)) { 86 return false; 87 } 88 89 ClassNaming that = (ClassNaming) o; 90 91 return originalName.equals(that.originalName) 92 && renamedName.equals(that.renamedName) 93 && members.equals(that.members); 94 95 } 96 97 @Override hashCode()98 public int hashCode() { 99 int result = originalName.hashCode(); 100 result = 31 * result + renamedName.hashCode(); 101 result = 31 * result + members.hashCode(); 102 return result; 103 } 104 } 105 106