3131public class ClippyJsonReportReader {
3232 private final JSONParser jsonParser = new JSONParser ();
3333 private final Consumer <ClippyIssue > consumer ;
34- private static final String BEGINJSON = "{\" results\" : [" ;
34+ private static final String RESULTS ="results" ;
35+ private static final String BEGINJSON = "{\" " + RESULTS + "\" : [" ;
3536 private static final String ENDJSON = "]}" ;
37+ private static final String VISIT_MSG ="for further information visit" ;
38+ private static final String MESSAGE ="message" ;
3639
3740 public static class ClippyIssue {
3841 @ Nullable
@@ -63,19 +66,19 @@ static void read(InputStream in, Consumer<ClippyIssue> consumer) throws IOExcept
6366
6467 private void read (InputStream in ) throws IOException , ParseException {
6568 JSONObject rootObject = (JSONObject ) jsonParser .parse (new InputStreamReader (in , UTF_8 ));
66- JSONArray results = (JSONArray ) rootObject .get ("results" );
69+ JSONArray results = (JSONArray ) rootObject .get (RESULTS );
6770 if (results != null ) {
6871 ((Stream <JSONObject >) results .stream ()).forEach (this ::onResult );
6972 }
7073 }
7174
7275 private void onResult (JSONObject result ) {
73- ClippyIssue clippyIssue = new ClippyIssue ();
7476
7577
78+ ClippyIssue clippyIssue = new ClippyIssue ();
7679 //Exit silently when JSON is not compliant
7780
78- JSONObject message = (JSONObject ) result .get ("message" );
81+ JSONObject message = (JSONObject ) result .get (MESSAGE );
7982 if (message == null ) return ;
8083 JSONObject code = (JSONObject ) message .get ("code" );
8184 if (code == null ) return ;
@@ -84,7 +87,11 @@ private void onResult(JSONObject result) {
8487 if ((spans == null ) || spans .isEmpty ()) return ;
8588 JSONObject span = (JSONObject ) spans .get (0 );
8689 clippyIssue .filePath = (String ) span .get ("file_name" );
87- clippyIssue .message = (String ) message .get ("message" );
90+ clippyIssue .message = (String ) message .get (MESSAGE );
91+ JSONArray children = (JSONArray ) message .get ("children" );
92+ if ((children != null ) && !children .isEmpty ()){
93+ addHelpDetails (clippyIssue , children );
94+ }
8895 clippyIssue .lineNumberStart = toInteger (span .get ("line_start" ));
8996 clippyIssue .lineNumberEnd = toInteger (span .get ("line_end" ));
9097 clippyIssue .colNumberStart = toInteger (span .get ("column_start" ));
@@ -94,6 +101,37 @@ private void onResult(JSONObject result) {
94101 consumer .accept (clippyIssue );
95102 }
96103
104+ private void addHelpDetails (ClippyIssue clippyIssue , JSONArray children ) {
105+ int sz = children .size ();
106+ StringBuilder sb = new StringBuilder (clippyIssue .message );
107+ for (int i = 0 ;i < sz ;i ++){
108+ JSONObject child = (JSONObject )children .get (i );
109+ String level = (String )child .get ("level" );
110+ String childMsg = (String )child .get (MESSAGE );
111+
112+ //ignore some of the children
113+ boolean isNote = level .equalsIgnoreCase ("note" );
114+ boolean isVisitLink = childMsg .startsWith (VISIT_MSG );
115+ if (isNote || isVisitLink ) continue ;
116+
117+ sb .append ("\n " ).append (childMsg );
118+
119+ //Are there any suggested replacement ?
120+ String replStr = suggestedMessage (child );
121+ if (replStr != null ) sb .append ("\n " ).append (replStr );
122+
123+ }
124+ clippyIssue .message = sb .toString ();
125+ }
126+
127+ private static String suggestedMessage (JSONObject obj ){
128+ if (obj == null ) return null ;
129+ JSONArray spans = (JSONArray )obj .get ("spans" );
130+ if ((spans == null ) || spans .isEmpty ()) return null ;
131+ JSONObject span = (JSONObject )spans .get (0 );
132+ return (String )span .get ("suggested_replacement" );
133+ }
134+
97135 private static Integer toInteger (Object value ) {
98136 if (value instanceof Number ) {
99137 return ((Number ) value ).intValue ();
0 commit comments