Skip to content

Commit

Permalink
More meaningful toString() method
Browse files Browse the repository at this point in the history
Produce toString without dilligently adding it to every single class.
Rely on heuristics to cut down the number of fields to show.
  • Loading branch information
kohsuke committed Jun 3, 2016
1 parent 3c5592c commit cde501a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/kohsuke/github/GHIssue.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class GHIssue extends GHObject {
protected int number;
protected String closed_at;
protected int comments;
@SkipFromToString
protected String body;
// for backward compatibility with < 1.63, this collection needs to hold instances of Label, not GHLabel
protected List<Label> labels;
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/org/kohsuke/github/GHObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.lang.reflect.FieldUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Date;

Expand Down Expand Up @@ -72,4 +77,39 @@ private Object intToString(int id, Class type) {
private Object urlToString(URL url, Class type) {
return url==null ? null : url.toString();
}

/**
* String representation to assist debugging and inspection. The output format of this string
* is not a committed part of the API and is subject to change.
*/
@Override
public String toString() {
return new ReflectionToStringBuilder(this, TOSTRING_STYLE, null, null, false, false) {
@Override
protected boolean accept(Field field) {
return super.accept(field) && !field.isAnnotationPresent(SkipFromToString.class);
}
}.toString();
}

private static final ToStringStyle TOSTRING_STYLE = new ToStringStyle() {
{
this.setUseShortClassName(true);
}

@Override
public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
// skip unimportant properties. '_' is a heuristics as important properties tend to have short names
if (fieldName.contains("_"))
return;
// avoid recursing other GHObject
if (value instanceof GHObject)
return;
// likewise no point in showing root
if (value instanceof GitHub)
return;

super.append(buffer,fieldName,value,fullDetail);
}
};
}
8 changes: 2 additions & 6 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class GHRepository extends GHObject {
private String default_branch,language;
private Map<String,GHCommit> commits = new HashMap<String, GHCommit>();

@SkipFromToString
private GHRepoPermission permissions;

private GHRepository source, parent;
Expand Down Expand Up @@ -1350,14 +1351,9 @@ public GHNotificationStream listNotifications() {
}


@Override
public String toString() {
return "Repository:"+owner.login+":"+name;
}

@Override
public int hashCode() {
return toString().hashCode();
return ("Repository:"+owner.login+":"+name).hashCode();
}

@Override
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/org/kohsuke/github/GHUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,6 @@ protected void wrapUp(GHGist[] page) {
};
}

@Override
public String toString() {
return "User:"+login;
}

@Override
public int hashCode() {
return login.hashCode();
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/kohsuke/github/SkipFromToString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.kohsuke.github;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Ignores this field for {@link GHObject#toString()}
*
* @author Kohsuke Kawaguchi
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface SkipFromToString {
}
13 changes: 13 additions & 0 deletions src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.net.URL;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -854,6 +855,18 @@ public void notifications() throws Exception {
gitHub.listNotifications().markAsRead();
}

/**
* Just basic code coverage to make sure toString() doesn't blow up
*/
@Test
public void checkToString() throws Exception {
GHUser u = gitHub.getUser("jenkinsci");
System.out.println(u);
GHRepository r = u.getRepository("jenkins");
System.out.println(r);
System.out.println(r.getIssue(1));
}

private void kohsuke() {
String login = getUser().getLogin();
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
Expand Down

0 comments on commit cde501a

Please sign in to comment.