Cleanup of access to methods/fields of DexClass in Minifiers.

Inspired by http://go/r8g/2760

Bug:
Change-Id: I66fc48b75932237951b80d621f01c820343e7b75
diff --git a/src/main/java/com/android/tools/r8/naming/ClassNameMinifier.java b/src/main/java/com/android/tools/r8/naming/ClassNameMinifier.java
index 10c89df..d8d3b8d 100644
--- a/src/main/java/com/android/tools/r8/naming/ClassNameMinifier.java
+++ b/src/main/java/com/android/tools/r8/naming/ClassNameMinifier.java
@@ -30,7 +30,7 @@
 import java.util.Set;
 import java.util.function.Consumer;
 
-public class ClassNameMinifier {
+class ClassNameMinifier {
 
   private final AppInfoWithLiveness appInfo;
   private final RootSet rootSet;
@@ -47,7 +47,7 @@
   private GenericSignatureParser<DexType> genericSignatureParser =
       new GenericSignatureParser<>(genericSignatureRewriter);
 
-  public ClassNameMinifier(AppInfoWithLiveness appInfo, RootSet rootSet, String packagePrefix,
+  ClassNameMinifier(AppInfoWithLiveness appInfo, RootSet rootSet, String packagePrefix,
       List<String> dictionary, boolean keepInnerClassStructure) {
     this.appInfo = appInfo;
     this.rootSet = rootSet;
@@ -56,7 +56,7 @@
     this.keepInnerClassStructure = keepInnerClassStructure;
   }
 
-  public Map<DexType, DexString> computeRenaming() {
+  Map<DexType, DexString> computeRenaming() {
     Iterable<DexProgramClass> classes = appInfo.classes();
     // Collect names we have to keep.
     for (DexClass clazz : classes) {
@@ -83,22 +83,10 @@
     for (DexClass clazz : appInfo.classes()) {
       rewriteGenericSignatures(clazz.annotations.annotations,
           genericSignatureParser::parseClassSignature);
-      for (DexEncodedField field : clazz.staticFields) {
-        rewriteGenericSignatures(field.annotations.annotations,
-            genericSignatureParser::parseFieldSignature);
-      }
-      for (DexEncodedField field : clazz.instanceFields) {
-        rewriteGenericSignatures(field.annotations.annotations,
-            genericSignatureParser::parseFieldSignature);
-      }
-      for (DexEncodedMethod method : clazz.directMethods) {
-        rewriteGenericSignatures(method.annotations.annotations,
-            genericSignatureParser::parseMethodSignature);
-      }
-      for (DexEncodedMethod method : clazz.virtualMethods) {
-        rewriteGenericSignatures(method.annotations.annotations,
-            genericSignatureParser::parseMethodSignature);
-      }
+      clazz.forEachField(field -> rewriteGenericSignatures(
+          field.annotations.annotations, genericSignatureParser::parseFieldSignature));
+      clazz.forEachMethod(method -> rewriteGenericSignatures(
+          method.annotations.annotations, genericSignatureParser::parseMethodSignature));
     }
   }
 
diff --git a/src/main/java/com/android/tools/r8/naming/FieldNameMinifier.java b/src/main/java/com/android/tools/r8/naming/FieldNameMinifier.java
index 3a72512..8177e89 100644
--- a/src/main/java/com/android/tools/r8/naming/FieldNameMinifier.java
+++ b/src/main/java/com/android/tools/r8/naming/FieldNameMinifier.java
@@ -14,7 +14,7 @@
 import java.util.List;
 import java.util.Map;
 
-public class FieldNameMinifier {
+class FieldNameMinifier {
 
   private final AppInfoWithSubtyping appInfo;
   private final RootSet rootSet;
@@ -22,7 +22,7 @@
   private final List<String> dictionary;
   private final Map<DexType, NamingState<DexType>> states = new IdentityHashMap<>();
 
-  public FieldNameMinifier(AppInfoWithSubtyping appInfo, RootSet rootSet, List<String> dictionary) {
+  FieldNameMinifier(AppInfoWithSubtyping appInfo, RootSet rootSet, List<String> dictionary) {
     this.appInfo = appInfo;
     this.rootSet = rootSet;
     this.dictionary = dictionary;
@@ -50,18 +50,17 @@
       return;
     }
     NamingState<DexType> newState = states.computeIfAbsent(type, t -> state.createChild());
-    reserveFieldNames(newState, holder.instanceFields(), holder.isLibraryClass());
-    reserveFieldNames(newState, holder.staticFields(), holder.isLibraryClass());
+    holder.forEachField(field -> reserveFieldName(field, newState, holder.isLibraryClass()));
     type.forAllExtendsSubtypes(subtype -> reserveNamesInSubtypes(subtype, newState));
   }
 
-  private void reserveFieldNames(NamingState<DexType> state, DexEncodedField[] fields,
+  private void reserveFieldName(
+      DexEncodedField encodedField,
+      NamingState<DexType> state,
       boolean isLibrary) {
-    for (DexEncodedField encodedField : fields) {
-      if (isLibrary || rootSet.noObfuscation.contains(encodedField)) {
-        DexField field = encodedField.field;
-        state.reserveName(field.name, field.type);
-      }
+    if (isLibrary || rootSet.noObfuscation.contains(encodedField)) {
+      DexField field = encodedField.field;
+      state.reserveName(field.name, field.type);
     }
   }
 
@@ -72,17 +71,14 @@
     }
     NamingState<DexType> state = states.get(clazz.type);
     assert state != null;
-    renameFields(clazz.instanceFields(), state);
-    renameFields(clazz.staticFields(), state);
+    clazz.forEachField(field -> renameField(field, state));
     type.forAllExtendsSubtypes(this::renameFieldsInSubtypes);
   }
 
-  private void renameFields(DexEncodedField[] fields, NamingState<DexType> state) {
-    for (DexEncodedField encodedField : fields) {
-      DexField field = encodedField.field;
-      if (!state.isReserved(field.name, field.type)) {
-        renaming.put(field, state.assignNewNameFor(field.name, field.type, false));
-      }
+  private void renameField(DexEncodedField encodedField, NamingState<DexType> state) {
+    DexField field = encodedField.field;
+    if (!state.isReserved(field.name, field.type)) {
+      renaming.put(field, state.assignNewNameFor(field.name, field.type, false));
     }
   }
 }
diff --git a/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java b/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java
index ec7fa46..4aca460 100644
--- a/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java
+++ b/src/main/java/com/android/tools/r8/naming/MethodNameMinifier.java
@@ -84,7 +84,7 @@
  * TODO(herhut): Currently, we do not minify members of annotation interfaces, as this would require
  * parsing and minification of the string arguments to annotations.
  */
-public class MethodNameMinifier {
+class MethodNameMinifier {
 
   private final AppInfoWithSubtyping appInfo;
   private final RootSet rootSet;
@@ -93,7 +93,7 @@
   private MethodSignatureEquivalence equivalence = MethodSignatureEquivalence.get();
   private final List<String> dictionary;
 
-  public MethodNameMinifier(AppInfoWithSubtyping appInfo, RootSet rootSet,
+  MethodNameMinifier(AppInfoWithSubtyping appInfo, RootSet rootSet,
       List<String> dictionary) {
     this.appInfo = appInfo;
     this.rootSet = rootSet;
@@ -101,7 +101,7 @@
     this.globalState = NamingState.createRoot(appInfo.dexItemFactory, dictionary);
   }
 
-  public Map<DexMethod, DexString> computeRenaming(Timing timing) {
+  Map<DexMethod, DexString> computeRenaming(Timing timing) {
     // Phase 1: Reserve all the names that need to be kept and allocate linked state in the
     //          library part.
     timing.begin("Phase 1");
@@ -198,10 +198,8 @@
       DexClass clazz = appInfo.definitionFor(iface);
       if (clazz != null) {
         Set<NamingState<DexProto>> collectedStates = getReachableStates(iface, frontierMap);
-        addStatesToGlobalMapForMethods(clazz.directMethods(), collectedStates, globalStateMap,
-            sourceMethodsMap, originStates, iface);
-        addStatesToGlobalMapForMethods(clazz.virtualMethods(), collectedStates, globalStateMap,
-            sourceMethodsMap, originStates, iface);
+        clazz.forEachMethod(method -> addStatesToGlobalMapForMethod(
+            method, collectedStates, globalStateMap, sourceMethodsMap, originStates, iface));
       }
     });
     timing.end();
@@ -237,7 +235,6 @@
 
 
   private void collectSubInterfaces(DexType iface, Set<DexType> interfaces) {
-    DexClass clazz = appInfo.definitionFor(iface);
     iface.forAllExtendsSubtypes(subtype -> {
       assert subtype.isInterface();
       if (interfaces.add(subtype)) {
@@ -246,19 +243,17 @@
     });
   }
 
-  private void addStatesToGlobalMapForMethods(
-      DexEncodedMethod[] methods, Set<NamingState<DexProto>> collectedStates,
+  private void addStatesToGlobalMapForMethod(
+      DexEncodedMethod method, Set<NamingState<DexProto>> collectedStates,
       Map<Wrapper<DexMethod>, Set<NamingState<DexProto>>> globalStateMap,
       Map<Wrapper<DexMethod>, Set<DexMethod>> sourceMethodsMap,
       Map<Wrapper<DexMethod>, NamingState<DexProto>> originStates, DexType originInterface) {
-    for (DexEncodedMethod method : methods) {
-      Wrapper<DexMethod> key = equivalence.wrap(method.method);
-      Set<NamingState<DexProto>> stateSet = globalStateMap
-          .computeIfAbsent(key, k -> new HashSet<>());
-      stateSet.addAll(collectedStates);
-      sourceMethodsMap.computeIfAbsent(key, k -> new HashSet<>()).add(method.method);
-      originStates.putIfAbsent(key, states.get(originInterface));
-    }
+    Wrapper<DexMethod> key = equivalence.wrap(method.method);
+    Set<NamingState<DexProto>> stateSet =
+        globalStateMap.computeIfAbsent(key, k -> new HashSet<>());
+    stateSet.addAll(collectedStates);
+    sourceMethodsMap.computeIfAbsent(key, k -> new HashSet<>()).add(method.method);
+    originStates.putIfAbsent(key, states.get(originInterface));
   }
 
   private void assignNameForInterfaceMethodInAllStates(DexMethod method,