Skip to content

Commit

Permalink
Refactor "getInheritedTypes" methods, providing ordered and inclusive…
Browse files Browse the repository at this point in the history
… versions.

	Change on 2016/03/02 by kstanger <kstanger@google.com>
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=116135545
  • Loading branch information
kstanger authored and tomball committed Mar 8, 2016
1 parent b79e5fb commit 75e4346
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 47 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -70,11 +69,11 @@ public class OuterReferenceResolver extends TreeVisitor {
// parameter in a constructor.
public static final IVariableBinding OUTER_PARAMETER = GeneratedVariableBinding.newPlaceholder();

private Map<ITypeBinding, IVariableBinding> outerVars = Maps.newHashMap();
private Set<ITypeBinding> usesOuterParam = Sets.newHashSet();
private Map<ITypeBinding, IVariableBinding> outerVars = new HashMap<>();
private Set<ITypeBinding> usesOuterParam = new HashSet<>();
private ListMultimap<ITypeBinding, Capture> captures = ArrayListMultimap.create();
private Map<TreeNode.Key, List<IVariableBinding>> outerPaths = Maps.newHashMap();
private ArrayList<Scope> scopeStack = Lists.newArrayList();
private Map<TreeNode.Key, List<IVariableBinding>> outerPaths = new HashMap<>();
private ArrayList<Scope> scopeStack = new ArrayList<>();

@Override
public void run(TreeNode node) {
Expand All @@ -96,7 +95,7 @@ public IVariableBinding getOuterField(ITypeBinding type) {

public List<IVariableBinding> getCapturedVars(ITypeBinding type) {
List<Capture> capturesForType = captures.get(type);
List<IVariableBinding> capturedVars = Lists.newArrayListWithCapacity(capturesForType.size());
List<IVariableBinding> capturedVars = new ArrayList<>(capturesForType.size());
for (Capture capture : capturesForType) {
capturedVars.add(capture.var);
}
Expand All @@ -105,7 +104,7 @@ public List<IVariableBinding> getCapturedVars(ITypeBinding type) {

public List<IVariableBinding> getInnerFields(ITypeBinding type) {
List<Capture> capturesForType = captures.get(type);
List<IVariableBinding> innerFields = Lists.newArrayListWithCapacity(capturesForType.size());
List<IVariableBinding> innerFields = new ArrayList<>(capturesForType.size());
for (Capture capture : capturesForType) {
innerFields.add(capture.field);
}
Expand All @@ -132,13 +131,12 @@ private static class Scope {
private final ITypeBinding type;
private final Set<ITypeBinding> inheritedScope;
private boolean initializingContext = true;
private Set<IVariableBinding> declaredVars = Sets.newHashSet();
private Set<IVariableBinding> declaredVars = new HashSet<>();

private Scope(ITypeBinding type) {
this.type = type;
ImmutableSet.Builder<ITypeBinding> 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();
Expand Down Expand Up @@ -206,7 +204,7 @@ private IVariableBinding getOrCreateInnerField(IVariableBinding var, ITypeBindin

private List<IVariableBinding> getOuterPath(ITypeBinding type) {
type = type.getTypeDeclaration();
List<IVariableBinding> path = Lists.newArrayList();
List<IVariableBinding> path = new ArrayList<>();
for (int i = scopeStack.size() - 1; i >= 0; i--) {
Scope scope = scopeStack.get(i);
if (type.equals(scope.type)) {
Expand All @@ -221,7 +219,7 @@ private List<IVariableBinding> getOuterPath(ITypeBinding type) {

private List<IVariableBinding> getOuterPathInherited(ITypeBinding type) {
type = type.getTypeDeclaration();
List<IVariableBinding> path = Lists.newArrayList();
List<IVariableBinding> path = new ArrayList<>();
for (int i = scopeStack.size() - 1; i >= 0; i--) {
Scope scope = scopeStack.get(i);
if (scope.inheritedScope.contains(type)) {
Expand All @@ -244,7 +242,7 @@ private List<IVariableBinding> getPathForField(IVariableBinding var) {

private List<IVariableBinding> getPathForLocalVar(IVariableBinding var) {
boolean isConstant = var.getConstantValue() != null;
List<IVariableBinding> path = Lists.newArrayList();
List<IVariableBinding> path = new ArrayList<>();
Scope lastScope = null;
for (int i = scopeStack.size() - 1; i >= 0; i--) {
Scope scope = scopeStack.get(i);
Expand Down Expand Up @@ -283,7 +281,7 @@ private void pushType(TreeNode node, ITypeBinding type) {
}
}

private void popType(ITypeBinding type) {
private void popType() {
scopeStack.remove(scopeStack.size() - 1);
}

Expand All @@ -295,7 +293,7 @@ public boolean visit(TypeDeclaration node) {

@Override
public void endVisit(TypeDeclaration node) {
popType(node.getTypeBinding());
popType();
}

@Override
Expand All @@ -306,7 +304,7 @@ public boolean visit(AnonymousClassDeclaration node) {

@Override
public void endVisit(AnonymousClassDeclaration node) {
popType(node.getTypeBinding());
popType();
}

@Override
Expand All @@ -317,7 +315,7 @@ public boolean visit(EnumDeclaration node) {

@Override
public void endVisit(EnumDeclaration node) {
popType(node.getTypeBinding());
popType();
}

@Override
Expand All @@ -328,7 +326,7 @@ public boolean visit(AnnotationTypeDeclaration node) {

@Override
public void endVisit(AnnotationTypeDeclaration node) {
popType(node.getTypeBinding());
popType();
}

@Override
Expand All @@ -339,7 +337,7 @@ public boolean visit(LambdaExpression node) {

@Override
public void endVisit(LambdaExpression node) {
popType(node.getTypeBinding());
popType();
}

@Override
Expand Down
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -192,35 +193,59 @@ 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<ITypeBinding> getAllInheritedTypes(ITypeBinding type) {
Set<ITypeBinding> inheritedTypes = Sets.newHashSet();
collectAllInheritedTypes(type, inheritedTypes);
public static Set<ITypeBinding> getInheritedTypes(ITypeBinding type) {
Set<ITypeBinding> inheritedTypes = getInheritedTypesInclusive(type);
inheritedTypes.remove(type);
return inheritedTypes;
}

public static void collectAllInheritedTypes(ITypeBinding type, Set<ITypeBinding> inheritedTypes) {
collectAllInterfaces(type, inheritedTypes);
while (true) {
type = type.getSuperclass();
if (type == null) {
break;
}
inheritedTypes.add(type);
public static Set<ITypeBinding> getInheritedTypesInclusive(ITypeBinding type) {
Set<ITypeBinding> 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<ITypeBinding> getOrderedInheritedTypes(ITypeBinding type) {
LinkedHashSet<ITypeBinding> inheritedTypes = getOrderedInheritedTypesInclusive(type);
inheritedTypes.remove(type);
return inheritedTypes;
}

public static LinkedHashSet<ITypeBinding> getOrderedInheritedTypesInclusive(ITypeBinding type) {
LinkedHashSet<ITypeBinding> inheritedTypes = new LinkedHashSet<>();
collectInheritedTypesInclusive(type, inheritedTypes);
return inheritedTypes;
}

private static void collectInheritedTypesInclusive(
ITypeBinding type, Set<ITypeBinding> inheritedTypes) {
if (type == null) {
return;
}
inheritedTypes.add(type);
for (ITypeBinding interfaze : type.getInterfaces()) {
collectInheritedTypesInclusive(interfaze, inheritedTypes);
}
collectInheritedTypesInclusive(type.getSuperclass(), inheritedTypes);
}

/**
* Returns a set containing bindings of all interfaces implemented by the
* given class, and all super-interfaces of those.
*/
public static Set<ITypeBinding> getAllInterfaces(ITypeBinding type) {
Set<ITypeBinding> interfaces = Sets.newHashSet();
Set<ITypeBinding> interfaces = new HashSet<>();
collectAllInterfaces(type, interfaces);
return interfaces;
}

public static void collectAllInterfaces(ITypeBinding type, Set<ITypeBinding> interfaces) {
Deque<ITypeBinding> typeQueue = Lists.newLinkedList();
Deque<ITypeBinding> typeQueue = new LinkedList<>();

while (type != null) {
typeQueue.add(type);
Expand Down Expand Up @@ -514,7 +539,7 @@ public int compare(IMemberValuePairBinding o1, IMemberValuePairBinding o2) {
public static Set<String> parseAttributeString(IAnnotationBinding propertyAnnotation) {
assert propertyAnnotation.getName().equals("Property");
String attributesStr = (String) getAnnotationValue(propertyAnnotation, "value");
Set<String> attributes = Sets.newHashSet();
Set<String> attributes = new HashSet<>();
attributes.addAll(Arrays.asList(attributesStr.split(",\\s*")));
attributes.remove(""); // Clear any empty strings.
return attributes;
Expand All @@ -524,7 +549,7 @@ public static Set<String> parseAttributeString(IAnnotationBinding propertyAnnota
* Returns all declared constructors for a specified type.
*/
public static Set<IMethodBinding> getDeclaredConstructors(ITypeBinding type) {
Set<IMethodBinding> constructors = Sets.newHashSet();
Set<IMethodBinding> constructors = new HashSet<>();
for (IMethodBinding m : type.getDeclaredMethods()) {
if (m.isConstructor()) {
constructors.add(m);
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -55,7 +57,7 @@
public class NameTable {

private final Types typeEnv;
private final Map<IVariableBinding, String> variableNames = Maps.newHashMap();
private final Map<IVariableBinding, String> variableNames = new HashMap<>();

public static final String INIT_NAME = "init";
public static final String ALLOC_METHOD = "alloc";
Expand Down Expand Up @@ -563,7 +565,7 @@ public List<String> getExtraSelectors(IMethodBinding method) {
return Collections.emptyList();
}
List<IMethodBinding> originalMethods = getOriginalMethodBindings(method);
List<String> extraSelectors = Lists.newArrayList();
List<String> extraSelectors = new ArrayList<>();
String actualSelector = selectorForOriginalBinding(originalMethods.get(0));
for (int i = 1; i < originalMethods.size(); i++) {
String selector = selectorForOriginalBinding(originalMethods.get(i));
Expand Down Expand Up @@ -679,16 +681,16 @@ public static String getMethodNameFromAnnotation(IMethodBinding method) {
private List<IMethodBinding> 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<IMethodBinding> originalBindings = Lists.newArrayList();
List<IMethodBinding> originalBindings = new ArrayList<>();
originalBindings.add(method);

// Collect all the inherited types.
// Predictable ordering is important, so we use a LinkedHashSet.
Set<ITypeBinding> inheritedTypes = Sets.newLinkedHashSet();
BindingUtil.collectAllInheritedTypes(declaringClass, inheritedTypes);
LinkedHashSet<ITypeBinding> inheritedTypes =
BindingUtil.getOrderedInheritedTypes(declaringClass);
if (declaringClass.isInterface()) {
inheritedTypes.add(typeEnv.resolveJavaType("java.lang.Object"));
}
Expand Down Expand Up @@ -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<ITypeBinding> bounds = Lists.newArrayList();
List<ITypeBinding> bounds = new ArrayList<>();
collectBounds(type, bounds);
objCType = constructObjCType(bounds);
} else {
Expand Down Expand Up @@ -824,7 +826,7 @@ private boolean collectBounds(ITypeBinding type, Collection<ITypeBinding> bounds

private String constructObjCType(Iterable<ITypeBinding> types) {
String classType = null;
List<String> interfaces = Lists.newArrayList();
List<String> interfaces = new ArrayList<>();
for (ITypeBinding type : types) {
type = type.getErasure();
if (typeEnv.isIdType(type) || typeEnv.isJavaVoidType(type)) {
Expand Down

0 comments on commit 75e4346

Please sign in to comment.