62 * @since 1.6 63 */ 64 public class ElementFilter { 65 private ElementFilter() {} // Do not instantiate. 66 67 private static final Set<ElementKind> CONSTRUCTOR_KIND = 68 Collections.unmodifiableSet(EnumSet.of(ElementKind.CONSTRUCTOR)); 69 70 private static final Set<ElementKind> FIELD_KINDS = 71 Collections.unmodifiableSet(EnumSet.of(ElementKind.FIELD, 72 ElementKind.ENUM_CONSTANT)); 73 private static final Set<ElementKind> METHOD_KIND = 74 Collections.unmodifiableSet(EnumSet.of(ElementKind.METHOD)); 75 76 private static final Set<ElementKind> PACKAGE_KIND = 77 Collections.unmodifiableSet(EnumSet.of(ElementKind.PACKAGE)); 78 79 private static final Set<ElementKind> MODULE_KIND = 80 Collections.unmodifiableSet(EnumSet.of(ElementKind.MODULE)); 81 82 @SuppressWarnings("removal") 83 private static final Set<ElementKind> TYPE_KINDS = 84 Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS, 85 ElementKind.ENUM, 86 ElementKind.INTERFACE, 87 ElementKind.RECORD, 88 ElementKind.ANNOTATION_TYPE)); 89 90 @SuppressWarnings("removal") 91 private static final Set<ElementKind> RECORD_COMPONENT_KIND = 92 Set.of(ElementKind.RECORD_COMPONENT); 93 94 /** 95 * Returns a list of fields in {@code elements}. 96 * @return a list of fields in {@code elements} 97 * @param elements the elements to filter 98 */ 99 public static List<VariableElement> 100 fieldsIn(Iterable<? extends Element> elements) { 101 return listFilter(elements, FIELD_KINDS, VariableElement.class); 102 } 103 104 /** 105 * Returns a set of fields in {@code elements}. 106 * @return a set of fields in {@code elements} 107 * @param elements the elements to filter 108 */ 109 public static Set<VariableElement> 110 fieldsIn(Set<? extends Element> elements) { 111 return setFilter(elements, FIELD_KINDS, VariableElement.class); 112 } 113 114 /** 115 * Returns a list of record components in {@code elements}. 116 * @return a list of record components in {@code elements} 117 * @param elements the elements to filter 118 * @since 14 119 * @deprecated This method is part of a preview feature and may be removed 120 * if the preview feature is removed. 121 */ 122 @Deprecated(forRemoval=true, since="14") 123 @SuppressWarnings("removal") 124 public static List<RecordComponentElement> 125 recordComponentsIn(Iterable<? extends Element> elements) { 126 return listFilter(elements, RECORD_COMPONENT_KIND, RecordComponentElement.class); 127 } 128 129 /** 130 * Returns a set of record components in {@code elements}. 131 * @return a set of record components in {@code elements} 132 * @param elements the elements to filter 133 * @since 14 134 * @deprecated This method is part of a preview feature and may be removed 135 * if the preview feature is removed. 136 */ 137 @Deprecated(forRemoval=true, since="14") 138 @SuppressWarnings("removal") 139 public static Set<RecordComponentElement> 140 recordComponentsIn(Set<? extends Element> elements) { 141 return setFilter(elements, RECORD_COMPONENT_KIND, RecordComponentElement.class); 142 } 143 144 /** 145 * Returns a list of constructors in {@code elements}. 146 * @return a list of constructors in {@code elements} 147 * @param elements the elements to filter 148 */ 149 public static List<ExecutableElement> 150 constructorsIn(Iterable<? extends Element> elements) { 151 return listFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class); 152 } 153 154 /** 155 * Returns a set of constructors in {@code elements}. 156 * @return a set of constructors in {@code elements} 157 * @param elements the elements to filter 158 */ | 62 * @since 1.6 63 */ 64 public class ElementFilter { 65 private ElementFilter() {} // Do not instantiate. 66 67 private static final Set<ElementKind> CONSTRUCTOR_KIND = 68 Collections.unmodifiableSet(EnumSet.of(ElementKind.CONSTRUCTOR)); 69 70 private static final Set<ElementKind> FIELD_KINDS = 71 Collections.unmodifiableSet(EnumSet.of(ElementKind.FIELD, 72 ElementKind.ENUM_CONSTANT)); 73 private static final Set<ElementKind> METHOD_KIND = 74 Collections.unmodifiableSet(EnumSet.of(ElementKind.METHOD)); 75 76 private static final Set<ElementKind> PACKAGE_KIND = 77 Collections.unmodifiableSet(EnumSet.of(ElementKind.PACKAGE)); 78 79 private static final Set<ElementKind> MODULE_KIND = 80 Collections.unmodifiableSet(EnumSet.of(ElementKind.MODULE)); 81 82 @SuppressWarnings("preview") 83 private static final Set<ElementKind> TYPE_KINDS = 84 Collections.unmodifiableSet(EnumSet.of(ElementKind.CLASS, 85 ElementKind.ENUM, 86 ElementKind.INTERFACE, 87 ElementKind.RECORD, 88 ElementKind.ANNOTATION_TYPE)); 89 90 @SuppressWarnings("preview") 91 private static final Set<ElementKind> RECORD_COMPONENT_KIND = 92 Set.of(ElementKind.RECORD_COMPONENT); 93 94 /** 95 * Returns a list of fields in {@code elements}. 96 * @return a list of fields in {@code elements} 97 * @param elements the elements to filter 98 */ 99 public static List<VariableElement> 100 fieldsIn(Iterable<? extends Element> elements) { 101 return listFilter(elements, FIELD_KINDS, VariableElement.class); 102 } 103 104 /** 105 * Returns a set of fields in {@code elements}. 106 * @return a set of fields in {@code elements} 107 * @param elements the elements to filter 108 */ 109 public static Set<VariableElement> 110 fieldsIn(Set<? extends Element> elements) { 111 return setFilter(elements, FIELD_KINDS, VariableElement.class); 112 } 113 114 /** 115 * {@preview Associated with records, a preview feature of the Java language. 116 * 117 * This method is associated with <i>records</i>, a preview 118 * feature of the Java language. Programs can only use this 119 * method when preview features are enabled. Preview features 120 * may be removed in a future release, or upgraded to permanent 121 * features of the Java language.} 122 * 123 * Returns a list of record components in {@code elements}. 124 * @return a list of record components in {@code elements} 125 * @param elements the elements to filter 126 * @since 14 127 */ 128 @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.RECORDS, 129 essentialAPI=false) 130 @SuppressWarnings("preview") 131 public static List<RecordComponentElement> 132 recordComponentsIn(Iterable<? extends Element> elements) { 133 return listFilter(elements, RECORD_COMPONENT_KIND, RecordComponentElement.class); 134 } 135 136 /** 137 * {@preview Associated with records, a preview feature of the Java language. 138 * 139 * This method is associated with <i>records</i>, a preview 140 * feature of the Java language. Programs can only use this 141 * method when preview features are enabled. Preview features 142 * may be removed in a future release, or upgraded to permanent 143 * features of the Java language.} 144 * 145 * Returns a set of record components in {@code elements}. 146 * @return a set of record components in {@code elements} 147 * @param elements the elements to filter 148 * @since 14 149 */ 150 @jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.RECORDS, 151 essentialAPI=false) 152 @SuppressWarnings("preview") 153 public static Set<RecordComponentElement> 154 recordComponentsIn(Set<? extends Element> elements) { 155 return setFilter(elements, RECORD_COMPONENT_KIND, RecordComponentElement.class); 156 } 157 158 /** 159 * Returns a list of constructors in {@code elements}. 160 * @return a list of constructors in {@code elements} 161 * @param elements the elements to filter 162 */ 163 public static List<ExecutableElement> 164 constructorsIn(Iterable<? extends Element> elements) { 165 return listFilter(elements, CONSTRUCTOR_KIND, ExecutableElement.class); 166 } 167 168 /** 169 * Returns a set of constructors in {@code elements}. 170 * @return a set of constructors in {@code elements} 171 * @param elements the elements to filter 172 */ |