Search in sources :

Example 21 with SetMultimap

use of com.google.common.collect.SetMultimap in project auto by google.

the class MoreElements method getAllMethods.

private static ImmutableSet<ExecutableElement> getAllMethods(TypeElement type, Overrides overrides) {
    SetMultimap<String, ExecutableElement> methodMap = LinkedHashMultimap.create();
    getAllMethods(type, methodMap);
    // Find methods that are overridden. We do this using `Elements.overrides`, which means
    // that it is inherently a quadratic operation, since we have to compare every method against
    // every other method. We reduce the performance impact by (a) grouping methods by name, since
    // a method cannot override another method with a different name, and (b) making sure that
    // methods in ancestor types precede those in descendant types, which means we only have to
    // check a method against the ones that follow it in that order.
    Set<ExecutableElement> overridden = new LinkedHashSet<ExecutableElement>();
    for (Collection<ExecutableElement> methods : methodMap.asMap().values()) {
        List<ExecutableElement> methodList = ImmutableList.copyOf(methods);
        for (int i = 0; i < methodList.size(); i++) {
            ExecutableElement methodI = methodList.get(i);
            for (int j = i + 1; j < methodList.size(); j++) {
                ExecutableElement methodJ = methodList.get(j);
                if (overrides.overrides(methodJ, methodI, type)) {
                    overridden.add(methodI);
                    break;
                }
            }
        }
    }
    return methodMap.values().stream().filter(m -> !overridden.contains(m)).collect(toImmutableSet());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PackageElement(javax.lang.model.element.PackageElement) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) MoreStreams.toImmutableSet(com.google.auto.common.MoreStreams.toImmutableSet) ImmutableList(com.google.common.collect.ImmutableList) Optional(com.google.common.base.Optional) ElementFilter(javax.lang.model.util.ElementFilter) SimpleElementVisitor8(javax.lang.model.util.SimpleElementVisitor8) LinkedHashMultimap(com.google.common.collect.LinkedHashMultimap) LinkedHashSet(java.util.LinkedHashSet) ExplicitOverrides(com.google.auto.common.Overrides.ExplicitOverrides) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) ExecutableElement(javax.lang.model.element.ExecutableElement) Set(java.util.Set) Element(javax.lang.model.element.Element) PACKAGE(javax.lang.model.element.ElementKind.PACKAGE) Types(javax.lang.model.util.Types) AnnotationMirror(javax.lang.model.element.AnnotationMirror) SetMultimap(com.google.common.collect.SetMultimap) Beta(com.google.common.annotations.Beta) TypeParameterElement(javax.lang.model.element.TypeParameterElement) TypeKind(javax.lang.model.type.TypeKind) STATIC(javax.lang.model.element.Modifier.STATIC) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) Predicate(com.google.common.base.Predicate) Annotation(java.lang.annotation.Annotation) ExecutableElement(javax.lang.model.element.ExecutableElement)

Aggregations

SetMultimap (com.google.common.collect.SetMultimap)21 List (java.util.List)11 Map (java.util.Map)11 Set (java.util.Set)11 HashSet (java.util.HashSet)9 Optional (java.util.Optional)9 ImmutableSet (com.google.common.collect.ImmutableSet)8 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 Collections (java.util.Collections)7 HashMultimap (com.google.common.collect.HashMultimap)6 Sets (com.google.common.collect.Sets)6 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableSetMultimap (com.google.common.collect.ImmutableSetMultimap)5 Collection (java.util.Collection)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 Preconditions.checkState (com.google.common.base.Preconditions.checkState)3 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)3 MultimapBuilder (com.google.common.collect.MultimapBuilder)3