Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming a <code>WebViewCore</code> with a <code>private String s</code> field:</p> <pre><code>public class WebViewCore { private String s; public WebViewCore(String s) { this.s = s; } @Override public String toString() { return "WebViewCore{" + "s='" + s + '\'' + '}'; } } </code></pre> <p>Assuming a <code>WebView</code> with a <code>private WebViewCore webViewCore</code> field:</p> <pre><code>public class WebView { private WebViewCore webViewCore; public WebView() { webViewCore = new WebViewCore("ohai"); } } </code></pre> <p>Now we reflect:</p> <p>public class Main {</p> <pre><code> public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { // First get the private WebViewCore field... Field fWvc = WebView.class.getDeclaredField("webViewCore"); fWvc.setAccessible(true); System.out.println(fWvc); // Then get an instance of a WebView; you already have one, // I'm constructing one... WebView wv = new WebView(); WebViewCore wvc = (WebViewCore) fWvc.get(wv); System.out.println(wvc); // Now get the private String field from the WebViewCore class... Field fS = WebViewCore.class.getDeclaredField("s"); fS.setAccessible(true); System.out.println(fS); // Now get the value of the private String field from the instance // of the WebViewCore we retrieved above... String s = (String) fS.get(wvc); System.out.println(s); } } </code></pre> <p>So, here's my rant: reflection is a relatively advanced technique, although it's pretty straight-forward. (With the caveat I've been doing this for a really long time, with languages that have better reflective abilities than Java.)</p> <p>This seems to still be a bit out-of-reach--that being the case, I'd be really, <em>really</em> careful about using it, and would avoid it at essentially any cost. I question your need to do whatever it is you're trying to do, and after that, I'd question the <em>wisdom</em> of doing whatever it is you're trying to, until stuff like the toy example we're mucking with here causes zero conceptual issues.</p> <p>Here there be dragons, and they will cook and eat you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload