Skip to content
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

core: forward toString for forwarding classes #4337

Merged
merged 1 commit into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/src/main/java/io/grpc/ForwardingChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc;

import com.google.common.base.MoreObjects;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -219,6 +220,11 @@ public ManagedChannel build() {
return delegate().build();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString();
}

/**
* Returns the correctly typed version of the builder.
*/
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/io/grpc/PartialForwardingClientCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc;

import com.google.common.base.MoreObjects;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -57,4 +58,9 @@ public boolean isReady() {
public Attributes getAttributes() {
return delegate().getAttributes();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.grpc;

import com.google.common.base.MoreObjects;

/**
* A {@link ClientCall.Listener} which forwards all of its methods to another {@link
* ClientCall.Listener} which may have a different parameterized type than the
Expand All @@ -41,4 +43,9 @@ public void onClose(Status status, Metadata trailers) {
public void onReady() {
delegate().onReady();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString();
}
}
7 changes: 7 additions & 0 deletions core/src/main/java/io/grpc/PartialForwardingServerCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.grpc;

import com.google.common.base.MoreObjects;

/**
* A {@link ServerCall} which forwards all of it's methods to another {@link ServerCall} which
* may have a different onMessage() message type.
Expand Down Expand Up @@ -73,4 +75,9 @@ public Attributes getAttributes() {
public String getAuthority() {
return delegate().getAuthority();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.grpc;

import com.google.common.base.MoreObjects;

/**
* A {@link ServerCall.Listener} which forwards all of its methods to another {@link
* ServerCall.Listener} which may have a different parameterized type than the
Expand Down Expand Up @@ -47,4 +49,9 @@ public void onComplete() {
public void onReady() {
delegate().onReady();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc.internal;

import com.google.common.base.MoreObjects;
import io.grpc.Attributes;
import io.grpc.Compressor;
import io.grpc.DecompressorRegistry;
Expand Down Expand Up @@ -99,4 +100,9 @@ public void setMaxOutboundMessageSize(int maxSize) {
public Attributes getAttributes() {
return delegate().getAttributes();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc.internal;

import com.google.common.base.MoreObjects;
import io.grpc.Metadata;
import io.grpc.Status;

Expand Down Expand Up @@ -47,4 +48,9 @@ public void messagesAvailable(MessageProducer producer) {
public void onReady() {
delegate().onReady();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc.internal;

import com.google.common.base.MoreObjects;
import com.google.common.util.concurrent.ListenableFuture;
import io.grpc.Attributes;
import io.grpc.CallOptions;
Expand Down Expand Up @@ -64,7 +65,7 @@ public Attributes getAttributes() {

@Override
public String toString() {
return getClass().getSimpleName() + "[" + delegate().toString() + "]";
return MoreObjects.toStringHelper(this).add("delegate", delegate()).toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc.internal;

import com.google.common.base.MoreObjects;
import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.ConnectivityState;
Expand Down Expand Up @@ -86,4 +87,9 @@ public void resetConnectBackoff() {
public void enterIdle() {
delegate.enterIdle();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("delegate", delegate).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import io.grpc.NameResolver;

/**
Expand Down Expand Up @@ -50,4 +51,9 @@ public void shutdown() {
public void refresh() {
delegate.refresh();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("delegate", delegate).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc.internal;

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -99,4 +100,9 @@ public int arrayOffset() {
public void close() {
buf.close();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("delegate", buf).toString();
}
}
16 changes: 16 additions & 0 deletions core/src/test/java/io/grpc/ForwardingTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package io.grpc;

import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.verify;

import com.google.common.base.Defaults;
import com.google.common.base.MoreObjects;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -68,5 +70,19 @@ public static <T> void testMethodsForwarded(
throw new AssertionError(String.format("Method was not forwarded: %s", method));
}
}

boolean skipToString = false;
for (Method method : skippedMethods) {
if (method.getName().equals("toString")) {
skipToString = true;
break;
}
}
if (!skipToString) {
String actual = forwarder.toString();
String expected =
MoreObjects.toStringHelper(forwarder).add("delegate", mockDelegate).toString();
assertEquals("Method toString() was not forwarded properly", expected, actual);
}
}
}