Skip to content

Commit

Permalink
Move TreeUtils.isAutoGeneratedRecordMember(Element) to `ElementUtil…
Browse files Browse the repository at this point in the history
…s` (#5679)
  • Loading branch information
mernst authored Mar 1, 2023
1 parent e84e29e commit 37f0261
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static EnumSet<Pure.Kind> 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);
}

Expand Down
4 changes: 3 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private TreeUtils() {
public static final UniqueIdMap<Tree> 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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 37f0261

Please sign in to comment.