From 37f0261db80fadeb530fee7419dcabf233bbb48a Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Wed, 1 Mar 2023 13:51:51 -0800 Subject: [PATCH] Move `TreeUtils.isAutoGeneratedRecordMember(Element)` to `ElementUtils` (#5679) --- .../dataflow/util/PurityUtils.java | 2 +- docs/CHANGELOG.md | 4 +++- .../framework/type/AnnotatedTypeFactory.java | 2 +- .../javacutil/ElementUtils.java | 18 +++++++++++++++ .../checkerframework/javacutil/TreeUtils.java | 22 ++----------------- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/dataflow/src/main/java/org/checkerframework/dataflow/util/PurityUtils.java b/dataflow/src/main/java/org/checkerframework/dataflow/util/PurityUtils.java index edbea388f59..5e1a5cf52e0 100644 --- a/dataflow/src/main/java/org/checkerframework/dataflow/util/PurityUtils.java +++ b/dataflow/src/main/java/org/checkerframework/dataflow/util/PurityUtils.java @@ -139,7 +139,7 @@ public static EnumSet getPurityKinds( AnnotationProvider provider, ExecutableElement methodElement) { // Special case for record accessors if (ElementUtils.isRecordAccessor(methodElement) - && TreeUtils.isAutoGeneratedRecordMember(methodElement)) { + && ElementUtils.isAutoGeneratedRecordMember(methodElement)) { return EnumSet.of(Kind.DETERMINISTIC, Kind.SIDE_EFFECT_FREE); } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 0ce7cf295c2..56f61f90aac 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -15,7 +15,9 @@ This assumption is unsound in general, but it holds for most code. **Implementation details:** -Rename `TreeUtils.instanceOfGetPattern()` to `TreeUtils.instanceOfTreeGetPattern()`. +Moved `TreeUtils.isAutoGeneratedRecordMember(Element)` to `ElementUtils`. + +Renamed `TreeUtils.instanceOfGetPattern()` to `TreeUtils.instanceOfTreeGetPattern()`. Deprecated `AnnotatedTypes#isExplicitlySuperBounded` and `AnnotatedTypes#isExplicitlyExtendsBounded` because they are duplicates of `#hasExplicitSuperBound` and `#hasExplicitExtendsBound`. diff --git a/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java b/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java index c5cfa78718a..97f2cd988ac 100644 --- a/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java +++ b/framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeFactory.java @@ -5691,7 +5691,7 @@ public boolean isSideEffectFree(ExecutableElement methodElement) { return true; } if (ElementUtils.isRecordAccessor(methodElement) - && TreeUtils.isAutoGeneratedRecordMember(methodElement)) { + && ElementUtils.isAutoGeneratedRecordMember(methodElement)) { return true; } for (AnnotationMirror anno : getDeclAnnotations(methodElement)) { diff --git a/javacutil/src/main/java/org/checkerframework/javacutil/ElementUtils.java b/javacutil/src/main/java/org/checkerframework/javacutil/ElementUtils.java index be658b616aa..86f7ea5fe48 100644 --- a/javacutil/src/main/java/org/checkerframework/javacutil/ElementUtils.java +++ b/javacutil/src/main/java/org/checkerframework/javacutil/ElementUtils.java @@ -884,6 +884,24 @@ public static boolean isRecordAccessor(ExecutableElement methodElement) { return false; } + /** + * Returns true if the given {@link Element} is part of a record that has been automatically + * generated by the compiler. This can be a field that is derived from the record's header field + * list, or an automatically generated canonical constructor. + * + * @param e the {@link Element} for a member of a record + * @return true if the given element is generated by the compiler + */ + public static boolean isAutoGeneratedRecordMember(Element e) { + if (!(e instanceof Symbol)) { + return false; + } + + // Generated constructors seem to get GENERATEDCONSTR even though the documentation + // seems to imply they would get GENERATED_MEMBER like the fields do. + return (((Symbol) e).flags() & (TreeUtils.Flags_GENERATED_MEMBER | Flags.GENERATEDCONSTR)) != 0; + } + /** * Check that a method Element matches a signature. * diff --git a/javacutil/src/main/java/org/checkerframework/javacutil/TreeUtils.java b/javacutil/src/main/java/org/checkerframework/javacutil/TreeUtils.java index f5530943443..708a933fb8b 100644 --- a/javacutil/src/main/java/org/checkerframework/javacutil/TreeUtils.java +++ b/javacutil/src/main/java/org/checkerframework/javacutil/TreeUtils.java @@ -102,7 +102,7 @@ private TreeUtils() { public static final UniqueIdMap treeUids = new UniqueIdMap<>(); /** The value of Flags.GENERATED_MEMBER which does not exist in Java 9 or 11. */ - private static final long Flags_GENERATED_MEMBER = 16777216; + /*package-private*/ static final long Flags_GENERATED_MEMBER = 16777216; /** The value of Flags.RECORD which does not exist in Java 9 or 11. */ private static final long Flags_RECORD = 2305843009213693952L; @@ -1640,25 +1640,7 @@ public static boolean isCompactCanonicalRecordConstructor(final MethodTree metho */ public static boolean isAutoGeneratedRecordMember(final Tree member) { Element e = elementFromTree(member); - return e != null && isAutoGeneratedRecordMember(e); - } - - /** - * Returns true if the given {@link Element} is part of a record that has been automatically - * generated by the compiler. This can be a field that is derived from the record's header field - * list, or an automatically generated canonical constructor. - * - * @param e the {@link Element} for a member of a record - * @return true if the given element is generated by the compiler - */ - public static boolean isAutoGeneratedRecordMember(Element e) { - if (!(e instanceof Symbol)) { - return false; - } - - // Generated constructors seem to get GENERATEDCONSTR even though the documentation - // seems to imply they would get GENERATED_MEMBER like the fields do: - return (((Symbol) e).flags() & (Flags_GENERATED_MEMBER | Flags.GENERATEDCONSTR)) != 0; + return e != null && ElementUtils.isAutoGeneratedRecordMember(e); } /**