{"id":1673,"date":"2017-06-07T18:26:34","date_gmt":"2017-06-07T18:26:34","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=1673"},"modified":"2017-06-07T18:26:34","modified_gmt":"2017-06-07T18:26:34","slug":"interrogating-your-code-dipping-your-toes-into-javas-reflection","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/06\/07\/interrogating-your-code-dipping-your-toes-into-javas-reflection\/","title":{"rendered":"interrogating your code &#8211; dipping your toes into java&#8217;s reflection"},"content":{"rendered":"<p>Perhaps the coolest programming language ability that I was never allowed to use was reflection in Java. Why? Well, thats another story.<\/p>\n<p>What exactly is reflection? Well, reflection is a set of low level methods that can be used to inspect code while the program that is doing the inspecting is running.<\/p>\n<p>This puts the program in a very unique situation where it can interrogate objects that it is working with. Thus the program can then make decisions based on what information it finds on the class that it is inspecting.<\/p>\n<p>This makes it possible to inspect an object to see if it contains a certain method. This allows your program to make the decision which method to use if multiple valid methods exist. One, perhaps somewhat contrived, example of this would be that your program could check and see if the new 2.0 version of your method existed.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nMethod method = null;\r\ntry {\r\n    method = financialCalcObj.getClass().getMethod(&quot;calcBreakEvenV2&quot;, null);\r\n}\r\ncatch (Exceptione ex)\r\n{\r\n    \/\/ not really an error, use standard method\r\n}\r\ntry {\r\n    if (method == null)\r\n        method = financialCalcObj.getClass().getMethod(&quot;calcBreakEvenV1&quot;, null);\r\n}\r\ncatch (Exceptione ex)\r\n{\r\n    \/\/ wow, thats bad\r\n    System.exit(1);\r\n}\r\nmethod.invoke(financialCalcObj, null);\r\n<\/pre>\n<p>If this doesn&#8217;t seem pretty amazing just remember that you cannot do the following unless you already have all methods defined at compile time.\u00a0 (which does defeat the idea of trying to future proof your code)<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntry {\r\n    financialCalcObj.calcBreakEvenV2();\r\n}\r\ncatch (Exception ex)\r\n{\r\n    financialCalcObj.calcBreakEvenV1();\r\n}\r\n<\/pre>\n<p>The difference between the two sets of code is that in the reflection example was compiled before version 2 of this method existed.\u00a0 We just knew that it would show up eventually. \u00a0This could be extended to either use the highest version that existed or to use the version of a method based on a command line argument.<\/p>\n<h2>No magic just a bit of forethought required<\/h2>\n<p>A program that uses reflection in this manner might not make much of a difference if the same set of class files or jar files are used each time. \u00a0It does allow some flexibility but if the code base never really changes it is not necessary to go to these extremes just to change how the program behaves at runtime.<\/p>\n<p>The program that I had been envisioning \u00a0was going to use this technology to dynamically support multiple language text. \u00a0The dynamic part was the program would convert numbers into text of the desired language. \u00a0The program would run every few minutes so it would be possible to exchange jar files between runs, however I think there is an even better solution.<\/p>\n<pre>ie.\r\n\r\n    numberToTextGerman\r\n    numberToTextEnglish\r\n    numberToTextFrench<\/pre>\n<p>Instead of continuously replacing the language jar &#8211; each time with more languages I would take a slightly different route.\u00a0 I would structure the program in such a way that each new language with its methods would exist in its own jar file.<\/p>\n<p>The actual program itself would retrieve the data which needs to be processed and this would include the form language. \u00a0Most of the form text might be read from a database table or a possibly a file but the conversion between numbers and their textual descriptions would need to be some sort of program.<\/p>\n<p>Because each language would exist in its own jar file and because the program would be receiving the target language in the input data, it would be possible to support new target languages whenever they were ready.<\/p>\n<p>Simply copy the new jar file to the program directory and change the list of supported languages in the application.\u00a0 The program would do the rest.\u00a0 Well, the script or batch file that calls the program does need to ensure that it will include the new jar file.<\/p>\n<p>I simply create the classpath from the list of jar files available and the script which runs the program would be called periodically from the crontab or other scheduler.\u00a0 So the simple act of adding the jar file to the programs library directory automatically adds support to the program for this new language each time the script is run.<\/p>\n<h2>Reflection classes and methods<\/h2>\n<p>The Java API has the Method class which can be\u00a0used to retrieve and inspect methods. \u00a0It is possible to query the name, which parameters types it expects, the return type and of course the ability to call the method.<\/p>\n<h3>class Method<\/h3>\n<ul>\n<li><code>String getName()<\/code><br \/>\nReturns the name of the method.<\/li>\n<li><code>Class[] getParameterTypes()<\/code><br \/>\nReturns an array of Class objects which represent the parameters, in the order of declaration.<\/li>\n<li><code>Class getReturnType()<\/code><br \/>\nReturns a Class object that represents the return type of the method.<\/li>\n<li><code>Object invoke(Object obj, Object... args)<\/code><br \/>\nInvokes the underlying method represented by this Method object with the specified parameters.<\/li>\n<\/ul>\n<h3>class\u00a0Field<\/h3>\n<p>The Method class allows you to interrogate a method to see exactly what parameters it is expecting and so forth but it is also possible to perform similar queries but on the class level for defined variables.<\/p>\n<p>Once you have retrieved your variable from the class it is it can be used to then query information about the fields. \u00a0It is possible to retrieve the name, the type or even the value.<\/p>\n<ul>\n<li><code>String getName()<\/code><br \/>\nReturns the name of the field.<\/li>\n<li><code>Class\u00a0getType()<\/code><br \/>\nReturns the type of the variable.<code><\/code><\/li>\n<li><code>Object\u00a0get(Object obj)<\/code><br \/>\nReturns a Class object that represents the return type of the method.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Multilingual support example<\/h2>\n<p>For this example, it is expected that every language support class must have a certain core set of methods.\u00a0 The easiest way to ensure that this happens is to create an interface of those methods.<\/p>\n<p>To demonstrate this rather interesting idea using reflection, I am not going to have complete language functionality just enough to demonstrate how it works.\u00a0 The language classes will each have to support two numberToText methods.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.acmesoft.support;\r\n\r\npublic interface languageInterface {\r\n\r\n\tpublic String numberToText(long value);\r\n\tpublic String numberToText(double value);\r\n}\r\n<\/pre>\n<p>The actual reflection magic happens in the Numbers class. This class essentially is a small dispatcher by looking up the method based on the language that is passed in.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.acmesoft.support;\r\n\r\nimport java.lang.reflect.Method;\r\n\r\npublic class Numbers \r\n{\r\n\tpublic static String number2TextClass = &quot;Numbers&quot;;\r\n\tpublic static String number2TextMethod = &quot;numberToText&quot;;\r\n\r\n\tpublic String numberToText(String language, Integer val) throws Exception \r\n\t{\r\n\t\tString outputtext = &quot;&quot;;\r\n\t\t\r\n\t\ttry{\r\n\t\t\tString searchObject = &quot;com.acmesoft.support.&quot; + language ;\r\n\t\t\tClass cls = Class.forName(searchObject);\r\n\t\t\tObject obj = cls.newInstance();\r\n\t\t\tMethod method = null;\r\n\r\n\t\t\t\/* for methods with other parameters \r\n\t\t\t\/\/no paramater\r\n\t\t\tClass noparams[] = {};\r\n\r\n\t\t\t\/\/String parameter\r\n\t\t\tClass[] paramString = new Class[1];\t\r\n\t\t\tparamString[0] = String.class;\r\n\r\n\t\t\t\/\/Double parameter\r\n\t\t\tClass[] paramDouble = new Class[1];\t\r\n\t\t\tparamDouble[0] = Double.TYPE;\r\n\t\t\t*\/\r\n\r\n\t\t\t\/\/Long parameter\r\n\t\t\tClass[] paramLong = new Class[1];\t\r\n\t\t\tparamLong[0] = Long.TYPE;\r\n\t\t\t\r\n\t\t\t\/\/ lets find out what the number is in words\r\n\t\t\tmethod = cls.getDeclaredMethod(number2TextMethod, paramLong);\r\n\t\t\toutputtext = (String)method.invoke(obj, val);\r\n\t\t}\r\n\t\tcatch(Exception ex)\r\n\t\t{\r\n\t\t\t\/*\r\n\t\t\tex.printStackTrace();\r\n\t\t\tSystem.out.println(ex.toString());\r\n\t\t\tSystem.out.println(ex.getLocalizedMessage());\r\n\t\t\t*\/\r\n\t\t\tthrow ex;\r\n\t\t}\r\n\t\t\r\n\t\treturn outputtext;\r\n\t}\r\n}\r\n<\/pre>\n<p>Lines 15-17 actually lookup the class that we will need for this method.\u00a0 This will be the actual language class (ie English).\u00a0 Lines 34-35 prepare the type of parameter that will be used by this method as well as performing the lookup of the method with the getDeclareMethod call.\u00a0 Lines 38-39 simply calls this method with our parameter.<\/p>\n<p>The actual language implementations are actually rather sparse.\u00a0 Having the code to convert 73626.82 into either either German or English although interesting doesn&#8217;t actually reinforce anything related to reflection so it has been simplified.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.acmesoft.support;\r\n\r\npublic class en implements languageInterface \r\n{\r\n\tpublic String numberToText(long val)\r\n\t{\r\n\t\tString retval = &quot;&quot;;\r\n\t\t\r\n\t\tswitch ((int)val)\r\n\t\t{\r\n\t\tcase 1:\r\n\t\t\tretval = &quot;one&quot;;\r\n\t\t\tbreak;\r\n\t\t\t\r\n\t\tcase 2:\r\n\t\t\tretval = &quot;two&quot;;\r\n\t\t\tbreak;\r\n\t\t\t\r\n\t\tcase 3:\r\n\t\t\tretval = &quot;three&quot;;\r\n\t\t\tbreak;\r\n\t\t\t\r\n\t\tdefault:\r\n\t\t\tretval = &quot;unknown&quot;;\r\n\t\t}\r\n\t\treturn retval;\r\n\t}\r\n\t\r\n\tpublic String numberToText(double val)\r\n\t{\r\n\t\treturn &quot;not yet&quot;;\r\n\t}\r\n\t\r\n\tpublic void testPgm(String[] args)\r\n\t{\r\n\t\tint val = 2;\r\n\t\tString valText = &quot;&quot;;\r\n\t\t\r\n\t\tif (args.length != 0)\r\n\t\t\tval = Integer.parseInt(args[0]);\r\n\t\t\r\n\t\ttry \r\n\t\t{\r\n\t\t\tvalText = numberToText(val);\r\n\t\t} \r\n\t\tcatch (Exception e) \r\n\t\t{\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t\t\r\n\t\tSystem.out.println(val + &quot; is equal to &quot; + valText);\r\n\r\n\t}\r\n\tpublic static void main(String[] args)\r\n\t{\r\n\t\ten pgm = new en();\r\n\t\tpgm.testPgm(args);\r\n\t\treturn ;\r\n\t}\r\n}\r\n<\/pre>\n<p>There is nothing in this class &#8220;en&#8221; sample code that you won&#8217;t be able to recreate in perhaps in the first few days of introduction to Java.\u00a0 It is only a few lines of reflection code in our Number class that provides the flexibility.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n\r\nCP=base\/final\/internationalBase.jar:german\/final\/internationalDE.jar:english\/final\/internationalEN.jar:.\r\n\r\njava -cp $CP com.acmesoft.support.TestPgm  1 en de fr\r\necho &quot; &quot;\r\njava -cp $CP com.acmesoft.support.TestPgm  2 en de xx\r\necho &quot; &quot;\r\njava -cp $CP com.acmesoft.support.TestPgm  3 en de\r\n\r\n<\/pre>\n<p><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2016\/11\/testrun-1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1694 size-full\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2016\/11\/testrun-1.png\" alt=\"testrun\" width=\"795\" height=\"462\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2016\/11\/testrun-1.png 795w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2016\/11\/testrun-1-300x174.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2016\/11\/testrun-1-768x446.png 768w\" sizes=\"(max-width: 795px) 100vw, 795px\" \/><\/a><\/p>\n<p>Download complete source for this language example<\/p>\n<div class=\"\"><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/downloads\/languages.zip\" target=\"_self\" class=\"emd_dl_blue\">Download files<\/a><\/div>        <style type=\"text\/css\">\r\n    .emd_dl_blue {\r\n        -moz-box-shadow:inset 0px 1px 0px 0px #bbdaf7;\r\n        -webkit-box-shadow:inset 0px 1px 0px 0px #bbdaf7;\r\n        box-shadow:inset 0px 1px 0px 0px #bbdaf7;\r\n        background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #79bbff), color-stop(1, #378de5) );\r\n        background:-moz-linear-gradient( center top, #79bbff 5%, #378de5 100% );\r\n        filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#79bbff', endColorstr='#378de5');\r\n        background-color:#79bbff;\r\n        -webkit-border-top-left-radius:0px;\r\n        -moz-border-radius-topleft:0px;\r\n        border-top-left-radius:0px;\r\n        -webkit-border-top-right-radius:0px;\r\n        -moz-border-radius-topright:0px;\r\n        border-top-right-radius:0px;\r\n        -webkit-border-bottom-right-radius:0px;\r\n        -moz-border-radius-bottomright:0px;\r\n        border-bottom-right-radius:0px;\r\n        -webkit-border-bottom-left-radius:0px;\r\n        -moz-border-radius-bottomleft:0px;\r\n        border-bottom-left-radius:0px;\r\n        text-indent:0;\r\n        border:1px solid #84bbf3;\r\n        display:inline-block;\r\n        color:#ffffff !important;\r\n        font-family:Georgia;\r\n        font-size:15px;\r\n        font-weight:bold;\r\n        font-style:normal;\r\n        height:41px;\r\n        line-height:41px;\r\n        width:153px;\r\n        text-decoration:none;\r\n        text-align:center;\r\n        text-shadow:1px 1px 0px #528ecc;\r\n    }\r\n    .emd_dl_blue:hover {\r\n        background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #378de5), color-stop(1, #79bbff) );\r\n        background:-moz-linear-gradient( center top, #378de5 5%, #79bbff 100% );\r\n        filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#378de5', endColorstr='#79bbff');\r\n        background-color:#378de5;\r\n    }.emd_dl_blue:active {\r\n        position:relative;\r\n        top:1px;\r\n    }\r\n    <\/style>\n<h2>The darker side of reflection<\/h2>\n<p>The ability to get either the value or method directly is much more powerful than it appears at the first glance. \u00a0It is actually possible to retrieve values from private variables or call private methods that you normally wouldn&#8217;t have access to.<\/p>\n<p>The neat thing about this is you only need to add one additional method call to enable the actual access to the value or method.<\/p>\n<p>For Private methods<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nMethod method = yourObject.getClass().getDeclaredMethod(yourMethod, argumentTypes);\r\nmethod.setAccessible(true);\r\nreturn method.invoke(yourObject, yourParameters);\r\n<\/pre>\n<p>For private fields<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nField field = yourObject.getClass().getDeclaredField(yourField);\r\nfield.setAccessible(true);\r\nfield.set(yourObject, value);\r\n<\/pre>\n<h2>Ignoring access permissions example<\/h2>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npackage payroll;\r\n\r\nimport java.lang.reflect.*;\r\n\r\npublic class Interrogate {\r\n\r\n\u00a0\u00a0 \u00a0public void showIncome(PayrollDept myobject)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Field privateIncomeField = null;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0try {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0privateIncomeField = myobject.getClass().getDeclaredField(&quot;income&quot;);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (NoSuchFieldException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (SecurityException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0double income = 0;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0try {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0privateIncomeField.setAccessible(true);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0income = privateIncomeField.getDouble(myobject);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (IllegalArgumentException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (IllegalAccessException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(&quot;Income &quot; + income);\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0public void hackIncome(PayrollDept myobject)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Field privateIncomeField = null;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0try {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0privateIncomeField = myobject.getClass().getDeclaredField(&quot;income&quot;);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (NoSuchFieldException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (SecurityException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0double income = 0;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0try {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0privateIncomeField.setAccessible(true);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0privateIncomeField.set(myobject, 1000000.0);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (IllegalArgumentException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (IllegalAccessException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0public Interrogate()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0PayrollDept myobject = new PayrollDept();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(&quot;Name\u00a0\u00a0 &quot; + myobject.firstName + &quot; &quot;+ myobject.lastName + &quot;, &quot; + myobject.title);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(&quot;Eyes\u00a0\u00a0 &quot; + myobject.eyeColor);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(&quot;Height &quot; + myobject.height);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(&quot;Weight &quot; + myobject.weight);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0showIncome(myobject);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0hackIncome(myobject);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0showIncome(myobject);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Method increaseIncomeMethod = null;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Class[] paramDouble = new Class[1];\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0paramDouble[0] = Double.TYPE;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0try {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0increaseIncomeMethod = myobject.getClass().getDeclaredMethod(&quot;setIncome&quot;, paramDouble);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (NoSuchMethodException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (SecurityException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0try {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0increaseIncomeMethod.setAccessible(true);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0increaseIncomeMethod.invoke(myobject,2000000.0);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (IllegalAccessException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (IllegalArgumentException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (InvocationTargetException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(&quot;Income &quot; + myobject.verifyIncome());\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0public static void main(String[] args) \r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0new Interrogate();\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n<\/pre>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npackage payroll;\r\n\r\npublic class PayrollDept extends Object\r\n{\r\n\u00a0\u00a0 \u00a0public String title ;\r\n\u00a0\u00a0 \u00a0public int height;\r\n\u00a0\u00a0 \u00a0public String eyeColor;\r\n\u00a0\u00a0 \u00a0public double weight;\r\n\u00a0\u00a0 \u00a0public String firstName;\r\n\u00a0\u00a0 \u00a0public String lastName;\r\n\u00a0\u00a0 \u00a0private double income ;\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0public PayrollDept()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0title = &quot;CEO&quot;;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0height = 204;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0eyeColor = &quot;hazel&quot;;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0weight = 90;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0income = 1000;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0firstName = &quot;Max&quot;;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0lastName = &quot;Musterman&quot;;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0private void setIncome(double val)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0income = val;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0private double getIncome()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return income;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public double verifyIncome()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return income;\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n<\/pre>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n\r\nCP=final\/darkside.jar\r\n\r\njava -cp $CP payroll.Interrogate\r\n<\/pre>\n<p><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2016\/11\/darksidetest.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1698 size-full\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2016\/11\/darksidetest.png\" alt=\"darksidetest\" width=\"829\" height=\"534\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2016\/11\/darksidetest.png 829w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2016\/11\/darksidetest-300x193.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2016\/11\/darksidetest-768x495.png 768w\" sizes=\"(max-width: 829px) 100vw, 829px\" \/><\/a><\/p>\n<p>Download complete source for this &#8220;darkside&#8221;\u00a0example<\/p>\n<div class=\"\"><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/downloads\/darkside.zip\" target=\"_self\" class=\"emd_dl_blue\">Download files<\/a><\/div>        <style type=\"text\/css\">\r\n    .emd_dl_blue {\r\n        -moz-box-shadow:inset 0px 1px 0px 0px #bbdaf7;\r\n        -webkit-box-shadow:inset 0px 1px 0px 0px #bbdaf7;\r\n        box-shadow:inset 0px 1px 0px 0px #bbdaf7;\r\n        background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #79bbff), color-stop(1, #378de5) );\r\n        background:-moz-linear-gradient( center top, #79bbff 5%, #378de5 100% );\r\n        filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#79bbff', endColorstr='#378de5');\r\n        background-color:#79bbff;\r\n        -webkit-border-top-left-radius:0px;\r\n        -moz-border-radius-topleft:0px;\r\n        border-top-left-radius:0px;\r\n        -webkit-border-top-right-radius:0px;\r\n        -moz-border-radius-topright:0px;\r\n        border-top-right-radius:0px;\r\n        -webkit-border-bottom-right-radius:0px;\r\n        -moz-border-radius-bottomright:0px;\r\n        border-bottom-right-radius:0px;\r\n        -webkit-border-bottom-left-radius:0px;\r\n        -moz-border-radius-bottomleft:0px;\r\n        border-bottom-left-radius:0px;\r\n        text-indent:0;\r\n        border:1px solid #84bbf3;\r\n        display:inline-block;\r\n        color:#ffffff !important;\r\n        font-family:Georgia;\r\n        font-size:15px;\r\n        font-weight:bold;\r\n        font-style:normal;\r\n        height:41px;\r\n        line-height:41px;\r\n        width:153px;\r\n        text-decoration:none;\r\n        text-align:center;\r\n        text-shadow:1px 1px 0px #528ecc;\r\n    }\r\n    .emd_dl_blue:hover {\r\n        background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #378de5), color-stop(1, #79bbff) );\r\n        background:-moz-linear-gradient( center top, #378de5 5%, #79bbff 100% );\r\n        filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#378de5', endColorstr='#79bbff');\r\n        background-color:#378de5;\r\n    }.emd_dl_blue:active {\r\n        position:relative;\r\n        top:1px;\r\n    }\r\n    <\/style>\n","protected":false},"excerpt":{"rendered":"<p>Perhaps the coolest programming language ability that I was never allowed to use was reflection in Java. Why? Well, thats another story. What exactly is reflection? Well, reflection is a set of low level methods that can be used to &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/06\/07\/interrogating-your-code-dipping-your-toes-into-javas-reflection\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[12],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1673"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=1673"}],"version-history":[{"count":18,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1673\/revisions"}],"predecessor-version":[{"id":1699,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/1673\/revisions\/1699"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=1673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=1673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=1673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}