diff options
author | dvfeinblum <dvfeinblum@gmail.com> | 2018-04-24 21:48:51 -0400 |
---|---|---|
committer | sebright <sebright@google.com> | 2018-04-24 18:48:51 -0700 |
commit | 610ff80ebec2b831a7ea6cc73ad4614152a36ad9 (patch) | |
tree | e526ab6877484ef02cc49f4beae61868af681a56 /api/src/main | |
parent | 8ff7ff9abec6b7abe95391fb450d99866c6a2c3a (diff) | |
download | opencensus-java-610ff80ebec2b831a7ea6cc73ad4614152a36ad9.tar.gz |
Added null checking to Span implementations (#1150)
This PR adds null checking to classes that implement Span. Specifically,
- BlankSpan
- NoopSpan
- Span
- SpanImpl
For the latter, I had to use Preconditions.checkNotNull because io.opencensus.internal shouldn't be imported into ImplCore.
Diffstat (limited to 'api/src/main')
-rw-r--r-- | api/src/main/java/io/opencensus/trace/BlankSpan.java | 31 | ||||
-rw-r--r-- | api/src/main/java/io/opencensus/trace/Span.java | 6 |
2 files changed, 30 insertions, 7 deletions
diff --git a/api/src/main/java/io/opencensus/trace/BlankSpan.java b/api/src/main/java/io/opencensus/trace/BlankSpan.java index 641bad7e..3109f013 100644 --- a/api/src/main/java/io/opencensus/trace/BlankSpan.java +++ b/api/src/main/java/io/opencensus/trace/BlankSpan.java @@ -16,6 +16,7 @@ package io.opencensus.trace; +import io.opencensus.internal.Utils; import java.util.Map; import javax.annotation.concurrent.Immutable; @@ -42,19 +43,29 @@ public final class BlankSpan extends Span { /** No-op implementation of the {@link Span#putAttribute(String, AttributeValue)} method. */ @Override - public void putAttribute(String key, AttributeValue value) {} + public void putAttribute(String key, AttributeValue value) { + Utils.checkNotNull(key, "key"); + Utils.checkNotNull(value, "value"); + } /** No-op implementation of the {@link Span#putAttributes(Map)} method. */ @Override - public void putAttributes(Map<String, AttributeValue> attributes) {} + public void putAttributes(Map<String, AttributeValue> attributes) { + Utils.checkNotNull(attributes, "attributes"); + } /** No-op implementation of the {@link Span#addAnnotation(String, Map)} method. */ @Override - public void addAnnotation(String description, Map<String, AttributeValue> attributes) {} + public void addAnnotation(String description, Map<String, AttributeValue> attributes) { + Utils.checkNotNull(description, "description"); + Utils.checkNotNull(attributes, "attributes"); + } /** No-op implementation of the {@link Span#addAnnotation(Annotation)} method. */ @Override - public void addAnnotation(Annotation annotation) {} + public void addAnnotation(Annotation annotation) { + Utils.checkNotNull(annotation, "annotation"); + } /** No-op implementation of the {@link Span#addNetworkEvent(NetworkEvent)} method. */ @Override @@ -63,14 +74,20 @@ public final class BlankSpan extends Span { /** No-op implementation of the {@link Span#addMessageEvent(MessageEvent)} method. */ @Override - public void addMessageEvent(MessageEvent messageEvent) {} + public void addMessageEvent(MessageEvent messageEvent) { + Utils.checkNotNull(messageEvent, "messageEvent"); + } /** No-op implementation of the {@link Span#addLink(Link)} method. */ @Override - public void addLink(Link link) {} + public void addLink(Link link) { + Utils.checkNotNull(link, "link"); + } @Override - public void setStatus(Status status) {} + public void setStatus(Status status) { + Utils.checkNotNull(status, "status"); + } /** No-op implementation of the {@link Span#end(EndSpanOptions)} method. */ @Override diff --git a/api/src/main/java/io/opencensus/trace/Span.java b/api/src/main/java/io/opencensus/trace/Span.java index 4a8c6f04..cb82038d 100644 --- a/api/src/main/java/io/opencensus/trace/Span.java +++ b/api/src/main/java/io/opencensus/trace/Span.java @@ -96,6 +96,8 @@ public abstract class Span { // Not final because for performance reasons we want to override this in the implementation. // Also a default implementation is needed to not break the compatibility (users may extend this // for testing). + Utils.checkNotNull(key, "key"); + Utils.checkNotNull(value, "value"); putAttributes(Collections.singletonMap(key, value)); } @@ -110,6 +112,7 @@ public abstract class Span { public void putAttributes(Map<String, AttributeValue> attributes) { // Not final because we want to start overriding this method from the beginning, this will // allow us to remove the addAttributes faster. All implementations MUST override this method. + Utils.checkNotNull(attributes, "attributes"); addAttributes(attributes); } @@ -130,6 +133,7 @@ public abstract class Span { * @since 0.5 */ public final void addAnnotation(String description) { + Utils.checkNotNull(description, "description"); addAnnotation(description, EMPTY_ATTRIBUTES); } @@ -180,6 +184,7 @@ public abstract class Span { public void addMessageEvent(MessageEvent messageEvent) { // Default implementation by invoking addNetworkEvent() so that any existing derived classes, // including implementation and the mocked ones, do not need to override this method explicitly. + Utils.checkNotNull(messageEvent, "messageEvent"); addNetworkEvent(BaseMessageEventUtils.asNetworkEvent(messageEvent)); } @@ -209,6 +214,7 @@ public abstract class Span { public void setStatus(Status status) { // Implemented as no-op for backwards compatibility (for example gRPC extends Span in tests). // Implementation must override this method. + Utils.checkNotNull(status, "status"); } /** |