2626import com .fasterxml .jackson .databind .JsonMappingException ;
2727import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
2828import org .apache .commons .io .IOUtils ;
29- import org .kohsuke .github .exception .GHFileNotFoundException ;
30- import org .kohsuke .github .exception .GHIOException ;
3129
3230import java .io .FileNotFoundException ;
3331import java .io .IOException ;
@@ -274,7 +272,7 @@ private <T> T _to(String tailApiUrl, Class<T> type, T instance) throws IOExcepti
274272 if (nextLinkMatcher .find ()) {
275273 final String link = nextLinkMatcher .group (1 );
276274 T nextResult = _to (link , type , instance );
277- injectInResult (nextResult );
275+ setResponseHeaders (nextResult );
278276 final int resultLength = Array .getLength (result );
279277 final int nextResultLength = Array .getLength (nextResult );
280278 T concatResult = (T ) Array .newInstance (type .getComponentType (), resultLength + nextResultLength );
@@ -284,8 +282,7 @@ private <T> T _to(String tailApiUrl, Class<T> type, T instance) throws IOExcepti
284282 }
285283 }
286284 }
287- injectInResult (result );
288- return result ;
285+ return setResponseHeaders (result );
289286 } catch (IOException e ) {
290287 handleApiError (e );
291288 } finally {
@@ -605,16 +602,12 @@ private <T> T parse(Class<T> type, T instance) throws IOException {
605602 String data = IOUtils .toString (r );
606603 if (type !=null )
607604 try {
608- final T readValue = MAPPER .readValue (data , type );
609- injectInResult (readValue );
610- return readValue ;
605+ return setResponseHeaders (MAPPER .readValue (data , type ));
611606 } catch (JsonMappingException e ) {
612607 throw (IOException )new IOException ("Failed to deserialize " +data ).initCause (e );
613608 }
614609 if (instance !=null ) {
615- final T readValue = MAPPER .readerForUpdating (instance ).<T >readValue (data );
616- injectInResult (readValue );
617- return readValue ;
610+ return setResponseHeaders (MAPPER .readerForUpdating (instance ).<T >readValue (data ));
618611 }
619612 return null ;
620613 } catch (FileNotFoundException e ) {
@@ -628,24 +621,19 @@ private <T> T parse(Class<T> type, T instance) throws IOException {
628621 }
629622 }
630623
631- private <T > void injectInResult (T readValue ) {
624+ private <T > T setResponseHeaders (T readValue ) {
632625 if (readValue instanceof GHObject []) {
633626 for (GHObject ghObject : (GHObject []) readValue ) {
634- injectInResult (ghObject );
627+ setResponseHeaders (ghObject );
635628 }
636629 } else if (readValue instanceof GHObject ) {
637- injectInResult ((GHObject ) readValue );
630+ setResponseHeaders ((GHObject ) readValue );
638631 }
632+ return readValue ;
639633 }
640634
641- private void injectInResult (GHObject readValue ) {
642- try {
643- final Field field = GHObject .class .getDeclaredField ("responseHeaderFields" );
644- field .setAccessible (true );
645- field .set (readValue , uc .getHeaderFields ());
646- } catch (NoSuchFieldException ignore ) {
647- } catch (IllegalAccessException ignore ) {
648- }
635+ private void setResponseHeaders (GHObject readValue ) {
636+ readValue .responseHeaderFields = uc .getHeaderFields ();
649637 }
650638
651639 /**
@@ -700,7 +688,7 @@ private InputStream wrapStream(InputStream in) throws IOException {
700688 HttpException http = (HttpException ) e ;
701689 throw (IOException ) new HttpException (error , http .getResponseCode (), http .getResponseMessage (), http .getUrl (), e );
702690 } else {
703- throw (IOException ) new GHIOException (error ).withResponceHeaderFields (uc ).initCause (e );
691+ throw (IOException ) new GHIOException (error ).withResponseHeaderFields (uc ).initCause (e );
704692 }
705693 } else {
706694 throw e ;
0 commit comments