langtools/src/java.compiler/share/classes/javax/lang/model/util/ElementFilter.java
--- a/langtools/src/java.compiler/share/classes/javax/lang/model/util/ElementFilter.java Tue Mar 15 13:48:30 2016 -0700
+++ b/langtools/src/java.compiler/share/classes/javax/lang/model/util/ElementFilter.java Thu Mar 17 19:04:28 2016 +0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -33,6 +33,12 @@
import java.util.LinkedHashSet;
import javax.lang.model.element.*;
+import javax.lang.model.element.ModuleElement.Directive;
+import javax.lang.model.element.ModuleElement.DirectiveKind;
+import javax.lang.model.element.ModuleElement.ExportsDirective;
+import javax.lang.model.element.ModuleElement.ProvidesDirective;
+import javax.lang.model.element.ModuleElement.RequiresDirective;
+import javax.lang.model.element.ModuleElement.UsesDirective;
/**
@@ -78,6 +84,9 @@
private static final Set<ElementKind> PACKAGE_KIND =
Collections.unmodifiableSet(EnumSet.of(ElementKind.PACKAGE));
+ private static final Set<ElementKind> MODULE_KIND =
+ Collections.unmodifiableSet(EnumSet.of(ElementKind.MODULE));
+
private static final Set<ElementKind> TYPE_KINDS =
Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS,
ElementKind.ENUM,
@@ -183,6 +192,28 @@
return setFilter(elements, PACKAGE_KIND, PackageElement.class);
}
+ /**
+ * Returns a list of modules in {@code elements}.
+ * @return a list of modules in {@code elements}
+ * @param elements the elements to filter
+ * @since 9
+ */
+ public static List<ModuleElement>
+ modulesIn(Iterable<? extends Element> elements) {
+ return listFilter(elements, MODULE_KIND, ModuleElement.class);
+ }
+
+ /**
+ * Returns a set of modules in {@code elements}.
+ * @return a set of modules in {@code elements}
+ * @param elements the elements to filter
+ * @since 9
+ */
+ public static Set<ModuleElement>
+ modulesIn(Set<? extends Element> elements) {
+ return setFilter(elements, MODULE_KIND, ModuleElement.class);
+ }
+
// Assumes targetKinds and E are sensible.
private static <E extends Element> List<E> listFilter(Iterable<? extends Element> elements,
Set<ElementKind> targetKinds,
@@ -207,4 +238,62 @@
}
return set;
}
+
+
+
+ /**
+ * Returns a list of export directives in {@code directives}.
+ * @return a list of export directives in {@code directives}
+ * @param directives the directives to filter
+ * @since 9
+ */
+ public static List<ExportsDirective>
+ exportsIn(Iterable<? extends Directive> directives) {
+ return listFilter(directives, DirectiveKind.EXPORTS, ExportsDirective.class);
+ }
+
+ /**
+ * Returns a list of provides directives in {@code directives}.
+ * @return a list of provides directives in {@code directives}
+ * @param directives the directives to filter
+ * @since 9
+ */
+ public static List<ProvidesDirective>
+ providesIn(Iterable<? extends Directive> directives) {
+ return listFilter(directives, DirectiveKind.PROVIDES, ProvidesDirective.class);
+ }
+
+ /**
+ * Returns a list of requires directives in {@code directives}.
+ * @return a list of requires directives in {@code directives}
+ * @param directives the directives to filter
+ * @since 9
+ */
+ public static List<RequiresDirective>
+ requiresIn(Iterable<? extends Directive> directives) {
+ return listFilter(directives, DirectiveKind.REQUIRES, RequiresDirective.class);
+ }
+
+ /**
+ * Returns a list of uses directives in {@code directives}.
+ * @return a list of uses directives in {@code directives}
+ * @param directives the directives to filter
+ * @since 9
+ */
+ public static List<UsesDirective>
+ usesIn(Iterable<? extends Directive> directives) {
+ return listFilter(directives, DirectiveKind.USES, UsesDirective.class);
+ }
+
+ // Assumes directiveKind and D are sensible.
+ private static <D extends Directive> List<D> listFilter(Iterable<? extends Directive> directives,
+ DirectiveKind directiveKind,
+ Class<D> clazz) {
+ List<D> list = new ArrayList<>();
+ for (Directive d : directives) {
+ if (d.getKind() == directiveKind)
+ list.add(clazz.cast(d));
+ }
+ return list;
+ }
}