Skip to content
Permalink
Browse files
8282411: Add useful predicates to ElementKind
Reviewed-by: prappo
  • Loading branch information
jddarcy committed Mar 1, 2022
1 parent eac8021 commit 732d891f9f8987edfc188c049eb19de6ba790c42
Showing 2 changed files with 102 additions and 5 deletions.
@@ -147,6 +147,17 @@ public boolean isInterface() {
return this == INTERFACE || this == ANNOTATION_TYPE;
}

/**
* {@return {@code true} if this is a kind of declared type, a
* {@linkplain #isClass() class} or an {@linkplain #isInterface()
* interface}, and {@code false} otherwise}
*
* @since 19
*/
public boolean isDeclaredType() {
return isClass() || isInterface();
}

/**
* Returns {@code true} if this is a kind of field:
* either {@code FIELD} or {@code ENUM_CONSTANT}.
@@ -156,4 +167,50 @@ public boolean isInterface() {
public boolean isField() {
return this == FIELD || this == ENUM_CONSTANT;
}

/**
* Returns {@code true} if this is a kind of executable: either
* {@code METHOD} or {@code CONSTRUCTOR} or {@code STATIC_INIT} or
* {@code INSTANCE_INIT}.
*
* @return {@code true} if this is a kind of executable
* @since 19
*/
public boolean isExecutable() {
return switch(this) {
case METHOD, CONSTRUCTOR, STATIC_INIT, INSTANCE_INIT -> true;
default -> false;
};
}

/**
* Returns {@code true} if this is a kind of initializer: either
* {@code STATIC_INIT} or {@code INSTANCE_INIT}.
*
* @return {@code true} if this is a kind of initializer
* @since 19
*/
public boolean isInitializer() {
return switch(this) {
case STATIC_INIT, INSTANCE_INIT -> true;
default -> false;
};
}
/**
* Returns {@code true} if this is a kind of variable: including
* {@code ENUM_CONSTANT}, {@code FIELD}, {@code PARAMETER},
* {@code LOCAL_VARIABLE}, {@code EXCEPTION_PARAMETER},
* {@code RESOURCE_VARIABLE}, and {@code BINDING_VARIABLE}.
*
* @return {@code true} if this is a kind of variable
* @since 19
*/
public boolean isVariable() {
return switch(this) {
case ENUM_CONSTANT, FIELD, PARAMETER,
LOCAL_VARIABLE, EXCEPTION_PARAMETER, RESOURCE_VARIABLE,
BINDING_VARIABLE -> true;
default -> false;
};
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, 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
@@ -23,7 +23,7 @@

/*
* @test
* @bug 8222430
* @bug 8222430 8282411
* @summary Test various predicates of ElementKind.
*/

@@ -38,24 +38,64 @@ public class TestElementKindPredicates {
public static void main(String... args) {
Set<ElementKind> ALL_KINDS = Set.of(ElementKind.values());

// isClass: Returns true if this is a kind of class: either CLASS or ENUM.
// isClass: Returns true if this is a kind of class:CLASS, ENUM, or RECORD.
test(ALL_KINDS,
(ElementKind k) -> Set.of(ElementKind.CLASS,
ElementKind.ENUM,
ElementKind.RECORD).contains(k),
(ElementKind k) -> k.isClass(), "isClass");

// isField: Returns true if this is a kind of field: either FIELD or ENUM_CONSTANT.
// isField: Returns true if this is a kind of field: one of
// FIELD or ENUM_CONSTANT.
test(ALL_KINDS,
(ElementKind k) -> Set.of(ElementKind.FIELD,
ElementKind.ENUM_CONSTANT).contains(k),
(ElementKind k) -> k.isField(), "isField");

// isInterface: Returns true if this is a kind of interface: either INTERFACE or ANNOTATION_TYPE.
// isInterface: Returns true if this is a kind of interface:
// either INTERFACE or ANNOTATION_TYPE.
test(ALL_KINDS,
(ElementKind k) -> Set.of(ElementKind.INTERFACE,
ElementKind.ANNOTATION_TYPE).contains(k),
(ElementKind k) -> k.isInterface(), "isInterface");

// isDeclaredType: Returns true if this is a kind of class or interface
test(ALL_KINDS,
(ElementKind k) -> Set.of(ElementKind.CLASS,
ElementKind.ENUM,
ElementKind.RECORD,
ElementKind.INTERFACE,
ElementKind.ANNOTATION_TYPE).contains(k),
(ElementKind k) -> k.isDeclaredType(), "isDeclaredType");

// isExecutable: Returns true if this is a kind of executable: one of
// METHOD, CONSTRUCTOR, STATIC_INIT, INSTANCE_INIT
test(ALL_KINDS,
(ElementKind k) -> Set.of(ElementKind.METHOD,
ElementKind.CONSTRUCTOR,
ElementKind.STATIC_INIT,
ElementKind.INSTANCE_INIT).contains(k),
(ElementKind k) -> k.isExecutable(), "isExecutable");

// isInitializer: Returns true if this is a kind of initializer: one of
// STATIC_INIT, INSTANCE_INIT
test(ALL_KINDS,
(ElementKind k) -> Set.of(ElementKind.STATIC_INIT,
ElementKind.INSTANCE_INIT).contains(k),
(ElementKind k) -> k.isInitializer(), "isInitializer");

// isVariable: Returns true if this is a kind of variable: one of
// ENUM_CONSTANT, FIELD, PARAMETER, LOCAL_VARIABLE, EXCEPTION_PARAMETER
// RESOURCE_VARIABLE, BINDING_VARIABLE
test(ALL_KINDS,
(ElementKind k) -> Set.of(ElementKind.ENUM_CONSTANT,
ElementKind.FIELD,
ElementKind.PARAMETER,
ElementKind.LOCAL_VARIABLE,
ElementKind.EXCEPTION_PARAMETER,
ElementKind.RESOURCE_VARIABLE,
ElementKind.BINDING_VARIABLE).contains(k),
(ElementKind k) -> k.isVariable(), "isVariable");
}

private static void test(Set<ElementKind> kinds,

1 comment on commit 732d891

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 732d891 Mar 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.