Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cde501a

Browse filesBrowse files
committed
More meaningful toString() method
Produce toString without dilligently adding it to every single class. Rely on heuristics to cut down the number of fields to show.
1 parent 3c5592c commit cde501a
Copy full SHA for cde501a

File tree

Expand file treeCollapse file tree

6 files changed

+72
-11
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+72
-11
lines changed

‎src/main/java/org/kohsuke/github/GHIssue.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHIssue.java
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class GHIssue extends GHObject {
5454
protected int number;
5555
protected String closed_at;
5656
protected int comments;
57+
@SkipFromToString
5758
protected String body;
5859
// for backward compatibility with < 1.63, this collection needs to hold instances of Label, not GHLabel
5960
protected List<Label> labels;

‎src/main/java/org/kohsuke/github/GHObject.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHObject.java
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
44
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
5+
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
6+
import org.apache.commons.lang.builder.ToStringBuilder;
7+
import org.apache.commons.lang.builder.ToStringStyle;
8+
import org.apache.commons.lang.reflect.FieldUtils;
59

610
import java.io.IOException;
11+
import java.lang.reflect.Field;
712
import java.net.URL;
813
import java.util.Date;
914

@@ -72,4 +77,39 @@ private Object intToString(int id, Class type) {
7277
private Object urlToString(URL url, Class type) {
7378
return url==null ? null : url.toString();
7479
}
80+
81+
/**
82+
* String representation to assist debugging and inspection. The output format of this string
83+
* is not a committed part of the API and is subject to change.
84+
*/
85+
@Override
86+
public String toString() {
87+
return new ReflectionToStringBuilder(this, TOSTRING_STYLE, null, null, false, false) {
88+
@Override
89+
protected boolean accept(Field field) {
90+
return super.accept(field) && !field.isAnnotationPresent(SkipFromToString.class);
91+
}
92+
}.toString();
93+
}
94+
95+
private static final ToStringStyle TOSTRING_STYLE = new ToStringStyle() {
96+
{
97+
this.setUseShortClassName(true);
98+
}
99+
100+
@Override
101+
public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
102+
// skip unimportant properties. '_' is a heuristics as important properties tend to have short names
103+
if (fieldName.contains("_"))
104+
return;
105+
// avoid recursing other GHObject
106+
if (value instanceof GHObject)
107+
return;
108+
// likewise no point in showing root
109+
if (value instanceof GitHub)
110+
return;
111+
112+
super.append(buffer,fieldName,value,fullDetail);
113+
}
114+
};
75115
}

‎src/main/java/org/kohsuke/github/GHRepository.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHRepository.java
+2-6Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public class GHRepository extends GHObject {
7777
private String default_branch,language;
7878
private Map<String,GHCommit> commits = new HashMap<String, GHCommit>();
7979

80+
@SkipFromToString
8081
private GHRepoPermission permissions;
8182

8283
private GHRepository source, parent;
@@ -1350,14 +1351,9 @@ public GHNotificationStream listNotifications() {
13501351
}
13511352

13521353

1353-
@Override
1354-
public String toString() {
1355-
return "Repository:"+owner.login+":"+name;
1356-
}
1357-
13581354
@Override
13591355
public int hashCode() {
1360-
return toString().hashCode();
1356+
return ("Repository:"+owner.login+":"+name).hashCode();
13611357
}
13621358

13631359
@Override

‎src/main/java/org/kohsuke/github/GHUser.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHUser.java
-5Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,6 @@ protected void wrapUp(GHGist[] page) {
196196
};
197197
}
198198

199-
@Override
200-
public String toString() {
201-
return "User:"+login;
202-
}
203-
204199
@Override
205200
public int hashCode() {
206201
return login.hashCode();
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.kohsuke.github;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Ignores this field for {@link GHObject#toString()}
10+
*
11+
* @author Kohsuke Kawaguchi
12+
*/
13+
@Target(ElementType.FIELD)
14+
@Retention(RetentionPolicy.RUNTIME)
15+
@interface SkipFromToString {
16+
}

‎src/test/java/org/kohsuke/github/AppTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/AppTest.java
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.net.URL;
1515
import java.util.*;
1616
import java.util.Map.Entry;
17+
import java.util.concurrent.ExecutionException;
1718
import java.util.regex.Pattern;
1819

1920
/**
@@ -854,6 +855,18 @@ public void notifications() throws Exception {
854855
gitHub.listNotifications().markAsRead();
855856
}
856857

858+
/**
859+
* Just basic code coverage to make sure toString() doesn't blow up
860+
*/
861+
@Test
862+
public void checkToString() throws Exception {
863+
GHUser u = gitHub.getUser("jenkinsci");
864+
System.out.println(u);
865+
GHRepository r = u.getRepository("jenkins");
866+
System.out.println(r);
867+
System.out.println(r.getIssue(1));
868+
}
869+
857870
private void kohsuke() {
858871
String login = getUser().getLogin();
859872
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.