text: On Using the Right Tool for the Job
  On Using the Right Tool for the Job
  Join the DZone community and get the full member experience.Join For Free
  Disclaimer: The code samples in the post below are for brain teasing only. Do not, I repeat, do not ever do such things for any other purpose!
  A friend of mine is a big fan of puzzlers. Any puzzlers, including programming ones. Here’s his latest and greatest:
  Write some code in the static initializer to make the assert pass:
  public class Test { static { //Write some code here } public static void main(String[] args) { Integer a = 20; Integer b = 20; assert (a + b == 60); } }
  Don’t forget to enable assertions if you want to solve this one (-ea as VM parameter).
  If you solved it, or don’t feel like puzzlers, read on for the solution and the punchline.
  First, the solution: You need to know refection and two facts about Integers to solve this one:
  - Integers have cache for all values in range -128 to 127.
  - Integers use that cache during auto-boxing (as opposite to using Integer’s constructor, for example).
  Well, you also need to know the exact implementation of the Integer class to figure out where the cache is and what’s the name, but rt.jar sources are here for the rescue. Guess what? the cache is a private variable inside a private inner class. Joy-joy.
  Here’s the plan:
  - Get Integer’s class object.
  - Get the list of its inner classes.
  - Find the right one (what a relief, it only has one. Or not – it is subject to change in future versions since we are digging in highly encapsulated stuff . Duh.)
  - Get the cache field from it.
  - It’s private, set it to accessible in order to get the value of the field.
  - Change the value of “20″ to be “30″
  Here’s the code of the static initializer:
  static { try { Class<?>[] declaredClasses = Integer.class.getDeclaredClasses(); Field cacheField = declaredClasses[0].getDeclaredField("cache"); cacheField.setAccessible(true); ((Integer[]) cacheField.get(null))[20 + 128] = 30; } catch (Exception e) { e.printStackTrace(); } }
  Personally, I hate it. It’s ugly, it messes with very low-level encapuslated stuff that one even doesn’t suppose to know, and it is fragile (e.g. it won’t work if Integer is created with constructor).
  And fairly, we just got lucky. If Integers did not have the cache then we would not have any way to change what “+” does!
  So, why this simple trick is so difficult to impossible to implement? Simple – Java is not intended for those tricks. Java is static, and it is good! It means that you can rely on it. 20+20 will (almost always) be 40. That is Good.
  If you want to do dynamic stuff, like changing the behavior of a “+” operator, you’d better use the right tool for the job. E.g. – Groovy.
  Here’s the Groovy version of the puzzler:
  //Write some code here Integer a = 20 Integer b = 20 assert 60 == a + b
  Looks almost the same (without the main() mess, some ridiculous semicolons and the need to include -ea in VM parameters), but the difference in the way to achieve the desired is huge. Since Groovy is dynamic it is intended to do stuff like changing the behavior of the plus sign. Here’s the facts about Groovy that you need to know to solve this one:
  - Groovy operates with wrappers, always.
  - Groovy implements operators though methods. “+” is implemented in plus() method.
  - Using Groovy’s MetaClasses you can easily replace method with any closure.
  Here’s the plan:
  - Get Integer’s MetaClass.
  - Replace plus() method with implementation that returns 60.
  That’s all. Really. Here’s the code:
  Integer.metaClass.plus = {int i -> 60 }
  Nuff said. Now you only have 2 options. Stop doing those ugly tricks, or at least do it with the right tools for the job.
  Opinions expressed by DZone contributors are their own.
  {{ parent.title || parent.header.title}}
  {{ parent.tldr }}
  {{ parent.linkDescription }}{{ parent.urlSource.name }}