From 75e4346b008466ecef1321d05094acae959ee896 Mon Sep 17 00:00:00 2001 From: kstanger Date: Wed, 2 Mar 2016 07:19:30 -0800 Subject: [PATCH] Refactor "getInheritedTypes" methods, providing ordered and inclusive versions. Change on 2016/03/02 by kstanger ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=116135545 --- .../translate/OuterReferenceResolver.java | 40 ++++++------- .../devtools/j2objc/util/BindingUtil.java | 59 +++++++++++++------ .../devtools/j2objc/util/NameTable.java | 20 ++++--- 3 files changed, 72 insertions(+), 47 deletions(-) diff --git a/translator/src/main/java/com/google/devtools/j2objc/translate/OuterReferenceResolver.java b/translator/src/main/java/com/google/devtools/j2objc/translate/OuterReferenceResolver.java index 5020ba1cd7..ac29bb6fbb 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/translate/OuterReferenceResolver.java +++ b/translator/src/main/java/com/google/devtools/j2objc/translate/OuterReferenceResolver.java @@ -17,9 +17,6 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ListMultimap; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; import com.google.devtools.j2objc.ast.AnnotationTypeDeclaration; import com.google.devtools.j2objc.ast.AnonymousClassDeclaration; import com.google.devtools.j2objc.ast.ClassInstanceCreation; @@ -49,6 +46,8 @@ import org.eclipse.jdt.core.dom.Modifier; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -70,11 +69,11 @@ public class OuterReferenceResolver extends TreeVisitor { // parameter in a constructor. public static final IVariableBinding OUTER_PARAMETER = GeneratedVariableBinding.newPlaceholder(); - private Map outerVars = Maps.newHashMap(); - private Set usesOuterParam = Sets.newHashSet(); + private Map outerVars = new HashMap<>(); + private Set usesOuterParam = new HashSet<>(); private ListMultimap captures = ArrayListMultimap.create(); - private Map> outerPaths = Maps.newHashMap(); - private ArrayList scopeStack = Lists.newArrayList(); + private Map> outerPaths = new HashMap<>(); + private ArrayList scopeStack = new ArrayList<>(); @Override public void run(TreeNode node) { @@ -96,7 +95,7 @@ public IVariableBinding getOuterField(ITypeBinding type) { public List getCapturedVars(ITypeBinding type) { List capturesForType = captures.get(type); - List capturedVars = Lists.newArrayListWithCapacity(capturesForType.size()); + List capturedVars = new ArrayList<>(capturesForType.size()); for (Capture capture : capturesForType) { capturedVars.add(capture.var); } @@ -105,7 +104,7 @@ public List getCapturedVars(ITypeBinding type) { public List getInnerFields(ITypeBinding type) { List capturesForType = captures.get(type); - List innerFields = Lists.newArrayListWithCapacity(capturesForType.size()); + List innerFields = new ArrayList<>(capturesForType.size()); for (Capture capture : capturesForType) { innerFields.add(capture.field); } @@ -132,13 +131,12 @@ private static class Scope { private final ITypeBinding type; private final Set inheritedScope; private boolean initializingContext = true; - private Set declaredVars = Sets.newHashSet(); + private Set declaredVars = new HashSet<>(); private Scope(ITypeBinding type) { this.type = type; ImmutableSet.Builder inheritedScopeBuilder = ImmutableSet.builder(); - inheritedScopeBuilder.add(type.getTypeDeclaration()); - for (ITypeBinding inheritedType : BindingUtil.getAllInheritedTypes(type)) { + for (ITypeBinding inheritedType : BindingUtil.getInheritedTypesInclusive(type)) { inheritedScopeBuilder.add(inheritedType.getTypeDeclaration()); } this.inheritedScope = inheritedScopeBuilder.build(); @@ -206,7 +204,7 @@ private IVariableBinding getOrCreateInnerField(IVariableBinding var, ITypeBindin private List getOuterPath(ITypeBinding type) { type = type.getTypeDeclaration(); - List path = Lists.newArrayList(); + List path = new ArrayList<>(); for (int i = scopeStack.size() - 1; i >= 0; i--) { Scope scope = scopeStack.get(i); if (type.equals(scope.type)) { @@ -221,7 +219,7 @@ private List getOuterPath(ITypeBinding type) { private List getOuterPathInherited(ITypeBinding type) { type = type.getTypeDeclaration(); - List path = Lists.newArrayList(); + List path = new ArrayList<>(); for (int i = scopeStack.size() - 1; i >= 0; i--) { Scope scope = scopeStack.get(i); if (scope.inheritedScope.contains(type)) { @@ -244,7 +242,7 @@ private List getPathForField(IVariableBinding var) { private List getPathForLocalVar(IVariableBinding var) { boolean isConstant = var.getConstantValue() != null; - List path = Lists.newArrayList(); + List path = new ArrayList<>(); Scope lastScope = null; for (int i = scopeStack.size() - 1; i >= 0; i--) { Scope scope = scopeStack.get(i); @@ -283,7 +281,7 @@ private void pushType(TreeNode node, ITypeBinding type) { } } - private void popType(ITypeBinding type) { + private void popType() { scopeStack.remove(scopeStack.size() - 1); } @@ -295,7 +293,7 @@ public boolean visit(TypeDeclaration node) { @Override public void endVisit(TypeDeclaration node) { - popType(node.getTypeBinding()); + popType(); } @Override @@ -306,7 +304,7 @@ public boolean visit(AnonymousClassDeclaration node) { @Override public void endVisit(AnonymousClassDeclaration node) { - popType(node.getTypeBinding()); + popType(); } @Override @@ -317,7 +315,7 @@ public boolean visit(EnumDeclaration node) { @Override public void endVisit(EnumDeclaration node) { - popType(node.getTypeBinding()); + popType(); } @Override @@ -328,7 +326,7 @@ public boolean visit(AnnotationTypeDeclaration node) { @Override public void endVisit(AnnotationTypeDeclaration node) { - popType(node.getTypeBinding()); + popType(); } @Override @@ -339,7 +337,7 @@ public boolean visit(LambdaExpression node) { @Override public void endVisit(LambdaExpression node) { - popType(node.getTypeBinding()); + popType(); } @Override diff --git a/translator/src/main/java/com/google/devtools/j2objc/util/BindingUtil.java b/translator/src/main/java/com/google/devtools/j2objc/util/BindingUtil.java index b12bb920a5..303eca07d0 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/util/BindingUtil.java +++ b/translator/src/main/java/com/google/devtools/j2objc/util/BindingUtil.java @@ -14,8 +14,6 @@ package com.google.devtools.j2objc.util; -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; import com.google.devtools.j2objc.types.LambdaTypeBinding; import com.google.j2objc.annotations.Property; @@ -31,6 +29,9 @@ import java.util.Arrays; import java.util.Comparator; import java.util.Deque; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.regex.Pattern; @@ -192,21 +193,45 @@ public static ITypeBinding toTypeBinding(IBinding binding) { * Returns a set containing the bindings of all classes and interfaces that * are inherited by the given type. */ - public static Set getAllInheritedTypes(ITypeBinding type) { - Set inheritedTypes = Sets.newHashSet(); - collectAllInheritedTypes(type, inheritedTypes); + public static Set getInheritedTypes(ITypeBinding type) { + Set inheritedTypes = getInheritedTypesInclusive(type); + inheritedTypes.remove(type); return inheritedTypes; } - public static void collectAllInheritedTypes(ITypeBinding type, Set inheritedTypes) { - collectAllInterfaces(type, inheritedTypes); - while (true) { - type = type.getSuperclass(); - if (type == null) { - break; - } - inheritedTypes.add(type); + public static Set getInheritedTypesInclusive(ITypeBinding type) { + Set inheritedTypes = new HashSet<>(); + collectInheritedTypesInclusive(type, inheritedTypes); + return inheritedTypes; + } + + /** + * Returns a set containing the bindings of all classes and interfaces that + * are inherited by the given type in the same order that they would be + * searched by the ObjC compiler. + */ + public static LinkedHashSet getOrderedInheritedTypes(ITypeBinding type) { + LinkedHashSet inheritedTypes = getOrderedInheritedTypesInclusive(type); + inheritedTypes.remove(type); + return inheritedTypes; + } + + public static LinkedHashSet getOrderedInheritedTypesInclusive(ITypeBinding type) { + LinkedHashSet inheritedTypes = new LinkedHashSet<>(); + collectInheritedTypesInclusive(type, inheritedTypes); + return inheritedTypes; + } + + private static void collectInheritedTypesInclusive( + ITypeBinding type, Set inheritedTypes) { + if (type == null) { + return; + } + inheritedTypes.add(type); + for (ITypeBinding interfaze : type.getInterfaces()) { + collectInheritedTypesInclusive(interfaze, inheritedTypes); } + collectInheritedTypesInclusive(type.getSuperclass(), inheritedTypes); } /** @@ -214,13 +239,13 @@ public static void collectAllInheritedTypes(ITypeBinding type, Set * given class, and all super-interfaces of those. */ public static Set getAllInterfaces(ITypeBinding type) { - Set interfaces = Sets.newHashSet(); + Set interfaces = new HashSet<>(); collectAllInterfaces(type, interfaces); return interfaces; } public static void collectAllInterfaces(ITypeBinding type, Set interfaces) { - Deque typeQueue = Lists.newLinkedList(); + Deque typeQueue = new LinkedList<>(); while (type != null) { typeQueue.add(type); @@ -514,7 +539,7 @@ public int compare(IMemberValuePairBinding o1, IMemberValuePairBinding o2) { public static Set parseAttributeString(IAnnotationBinding propertyAnnotation) { assert propertyAnnotation.getName().equals("Property"); String attributesStr = (String) getAnnotationValue(propertyAnnotation, "value"); - Set attributes = Sets.newHashSet(); + Set attributes = new HashSet<>(); attributes.addAll(Arrays.asList(attributesStr.split(",\\s*"))); attributes.remove(""); // Clear any empty strings. return attributes; @@ -524,7 +549,7 @@ public static Set parseAttributeString(IAnnotationBinding propertyAnnota * Returns all declared constructors for a specified type. */ public static Set getDeclaredConstructors(ITypeBinding type) { - Set constructors = Sets.newHashSet(); + Set constructors = new HashSet<>(); for (IMethodBinding m : type.getDeclaredMethods()) { if (m.isConstructor()) { constructors.add(m); diff --git a/translator/src/main/java/com/google/devtools/j2objc/util/NameTable.java b/translator/src/main/java/com/google/devtools/j2objc/util/NameTable.java index 5dae926786..91ac259d0f 100644 --- a/translator/src/main/java/com/google/devtools/j2objc/util/NameTable.java +++ b/translator/src/main/java/com/google/devtools/j2objc/util/NameTable.java @@ -19,7 +19,6 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.google.devtools.j2objc.Options; import com.google.devtools.j2objc.ast.CompilationUnit; @@ -37,10 +36,13 @@ import org.eclipse.jdt.core.dom.VariableDeclarationFragment; import java.io.File; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -55,7 +57,7 @@ public class NameTable { private final Types typeEnv; - private final Map variableNames = Maps.newHashMap(); + private final Map variableNames = new HashMap<>(); public static final String INIT_NAME = "init"; public static final String ALLOC_METHOD = "alloc"; @@ -563,7 +565,7 @@ public List getExtraSelectors(IMethodBinding method) { return Collections.emptyList(); } List originalMethods = getOriginalMethodBindings(method); - List extraSelectors = Lists.newArrayList(); + List extraSelectors = new ArrayList<>(); String actualSelector = selectorForOriginalBinding(originalMethods.get(0)); for (int i = 1; i < originalMethods.size(); i++) { String selector = selectorForOriginalBinding(originalMethods.get(i)); @@ -679,16 +681,16 @@ public static String getMethodNameFromAnnotation(IMethodBinding method) { private List getOriginalMethodBindings(IMethodBinding method) { method = method.getMethodDeclaration(); if (method.isConstructor() || BindingUtil.isStatic(method)) { - return Lists.newArrayList(method); + return Collections.singletonList(method); } ITypeBinding declaringClass = method.getDeclaringClass(); - List originalBindings = Lists.newArrayList(); + List originalBindings = new ArrayList<>(); originalBindings.add(method); // Collect all the inherited types. // Predictable ordering is important, so we use a LinkedHashSet. - Set inheritedTypes = Sets.newLinkedHashSet(); - BindingUtil.collectAllInheritedTypes(declaringClass, inheritedTypes); + LinkedHashSet inheritedTypes = + BindingUtil.getOrderedInheritedTypes(declaringClass); if (declaringClass.isInterface()) { inheritedTypes.add(typeEnv.resolveJavaType("java.lang.Object")); } @@ -787,7 +789,7 @@ private String getObjCTypeInner(ITypeBinding type, String qualifiers, boolean ex objCType = objCType.endsWith("*") ? objCType + "*" : objCType + " *"; } else if (type.isTypeVariable() || type.isCapture() || type.isWildcardType()) { if (expandBounds) { - List bounds = Lists.newArrayList(); + List bounds = new ArrayList<>(); collectBounds(type, bounds); objCType = constructObjCType(bounds); } else { @@ -824,7 +826,7 @@ private boolean collectBounds(ITypeBinding type, Collection bounds private String constructObjCType(Iterable types) { String classType = null; - List interfaces = Lists.newArrayList(); + List interfaces = new ArrayList<>(); for (ITypeBinding type : types) { type = type.getErasure(); if (typeEnv.isIdType(type) || typeEnv.isJavaVoidType(type)) {