Skip to content

Commit

Permalink
Treat all com.google.protobuf.ProtocolMessageEnums as immutable
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 653806506
  • Loading branch information
cushon authored and Error Prone Team committed Jul 19, 2024
1 parent 526aa72 commit 8e1bd10
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,12 @@ public static Violation create(ConsPStack<String> path) {
return new AutoValue_ThreadSafety_Violation(path);
}

/**
* @return true if a violation was found
*/
/** Returns true if a violation was found. */
public boolean isPresent() {
return !path().isEmpty();
}

/**
* @return the explanation
*/
/** Returns the explanation. */
public String message() {
return Joiner.on(", ").join(path());
}
Expand Down Expand Up @@ -658,6 +654,9 @@ public Violation visitType(Type type, Void s) {
}
return Violation.absent();
}
if (WellKnownMutability.isProtoEnum(state, type)) {
return Violation.absent();
}
return Violation.of(
String.format(
"the declaration of type '%s' is not annotated with %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ private static ImmutableSet<String> buildMutableClasses(List<String> knownMutabl
private static final Supplier<Type> PROTOCOL_MESSAGE_TYPE =
Suppliers.typeFromString("com.google.io.protocol.ProtocolMessage");

private static final Supplier<Type> PROTOCOL_MESSAGE_ENUM =
Suppliers.typeFromString("com.google.protobuf.ProtocolMessageEnum");

private static boolean isAssignableTo(Type type, Supplier<Type> supplier, VisitorState state) {
Type to = supplier.get(state);
if (to == null) {
Expand Down Expand Up @@ -371,6 +374,11 @@ public static boolean isProto2MutableMessageClass(VisitorState state, Type type)
&& !isAssignableTo(type, PROTOCOL_MESSAGE_TYPE, state);
}

public static boolean isProtoEnum(VisitorState state, Type type) {
checkNotNull(type);
return isAssignableTo(type, PROTOCOL_MESSAGE_ENUM, state);
}

/** Returns true if the type is an annotation. */
public static boolean isAnnotation(VisitorState state, Type type) {
return isAssignableTo(type, Suppliers.ANNOTATION_TYPE, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1084,4 +1084,21 @@ public void threadSafeRecursiveUpperBound_notThreadsafe() {
"}")
.doTest();
}

@Test
public void protoEnum() {
compilationHelper
.addSourceLines(
"E.java",
"import com.google.protobuf.ProtocolMessageEnum;",
"abstract class E implements ProtocolMessageEnum {",
"}")
.addSourceLines(
"Test.java",
"import com.google.errorprone.annotations.ThreadSafe;",
"@ThreadSafe class Test {",
" final E x = null;",
"}")
.doTest();
}
}

0 comments on commit 8e1bd10

Please sign in to comment.