-
Notifications
You must be signed in to change notification settings - Fork 3.9k
core: remove potential this-escapes by extracting constructions of StatusException and StatusRuntimeException to builders #10892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…atusException and StatusRuntimeException to builders
I think it'd probably be better to remove the What is the |
* | ||
* @since 1.0.0 | ||
*/ | ||
public StatusException(Status status) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no way we can delete this constructor. This is stable API.
@tharakadesilva, are you still interested in this change? |
Hi @ejona86 thanks for the feedback and sorry for the delay in response.
Even if we move to an unconditional class we would have the same issue if we call the function from the constructor unless we make that class final.
It is only a possibility (or this might be a false positive). Here's my understanding (please bear with me in case this is wrong): Let's take a sub-class like this:
What would you recommend?
I am. For now, I have a Bazel patch override with |
I think we can use the super constructor with 4 parameters which was introduced in java 1.7 diff --git a/api/src/main/java/io/grpc/StatusRuntimeException.java b/api/src/main/java/io/grpc/StatusRuntimeException.java
index 68b816fc7..70c4d10f0 100644
--- a/api/src/main/java/io/grpc/StatusRuntimeException.java
+++ b/api/src/main/java/io/grpc/StatusRuntimeException.java
@@ -29,8 +29,6 @@ public class StatusRuntimeException extends RuntimeException {
private final Status status;
private final Metadata trailers;
- private final boolean fillInStackTrace;
-
/**
* Constructs the exception with both a status. See also {@link Status#asRuntimeException()}.
*
@@ -51,21 +49,10 @@ public class StatusRuntimeException extends RuntimeException {
}
StatusRuntimeException(Status status, @Nullable Metadata trailers, boolean fillInStackTrace) {
- super(Status.formatThrowableMessage(status), status.getCause());
+ super(Status.formatThrowableMessage(status), status.getCause(),
+ /* enableSuppression */ true, /* writableStackTrace */ fillInStackTrace);
this.status = status;
this.trailers = trailers;
- this.fillInStackTrace = fillInStackTrace;
- fillInStackTrace();
- }
-
- @Override
- public synchronized Throwable fillInStackTrace() {
- // Let's observe final variables in two states! This works because Throwable will invoke this
- // method before fillInStackTrace is set, thus doing nothing. After the constructor has set
- // fillInStackTrace, this method will properly fill it in. Additionally, sub classes may call
- // this normally, because fillInStackTrace will either be set, or this method will be
- // overriden.
- return fillInStackTrace ? super.fillInStackTrace() : this;
} |
created as #11064 |
Closing in lieu of @panchenko's MR, thank you both!! |
With JDK 21,
this-escape
issues are now being flagged by the compiler: https://bugs.openjdk.org/browse/JDK-8299995 by default and we started seeing these errors in our project:This solution is changing the bean creation to a builder pattern and moving the
fillInStackTrace
invocation happening in the constructor to thebuild
function.