diff options
author | bcorso <bcorso@google.com> | 2019-11-08 12:39:54 -0800 |
---|---|---|
committer | Chris Povirk <beigetangerine@gmail.com> | 2019-11-11 14:28:21 -0500 |
commit | 3adcc79d23396d247cc2841eec3506be17955878 (patch) | |
tree | 15816970ebac014ca78db1b47bf93d72bf2947ba /common | |
parent | 107694bb74c7a8ba4259b4ca01e2e368329bf761 (diff) | |
download | auto-3adcc79d23396d247cc2841eec3506be17955878.tar.gz |
Roll forward of CL 264613939: Use ElementVisitor rather than Element#getKind() in MoreElements#isType()
*** Original change description ***
Use ElementVisitor rather than Element#getKind() in MoreElements#isType()
Element#getKind() contains more information than is needed here (we just need to know if the element is an instance of TypeElement, not its kind). Element#getKind() forces symbol completion, which requires parsing the class, so we shouldn't use it unless we have...
***
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=279369564
Diffstat (limited to 'common')
-rw-r--r-- | common/src/main/java/com/google/auto/common/MoreElements.java | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/common/src/main/java/com/google/auto/common/MoreElements.java b/common/src/main/java/com/google/auto/common/MoreElements.java index 5e8e3541..3a607743 100644 --- a/common/src/main/java/com/google/auto/common/MoreElements.java +++ b/common/src/main/java/com/google/auto/common/MoreElements.java @@ -106,6 +106,19 @@ public final class MoreElements { } } + private static final class IsTypeVisitor extends SimpleElementVisitor8<Boolean, Void> { + private static final IsTypeVisitor INSTANCE = new IsTypeVisitor(); + + IsTypeVisitor() { + super(false); + } + + @Override + public Boolean visitType(TypeElement e, Void unused) { + return true; + } + } + /** * Returns true if the given {@link Element} instance is a {@link TypeElement}. * @@ -115,7 +128,9 @@ public final class MoreElements { * @throws NullPointerException if {@code element} is {@code null} */ public static boolean isType(Element element) { - return element.getKind().isClass() || element.getKind().isInterface(); + // Use a visitor rather than Element#getKind(). Element#getKind() contains more information + // than is needed here. It also requires symbol completion, which can be slow. + return element.accept(IsTypeVisitor.INSTANCE, null); } /** |