[9] RFR of 8065556: (bf) Buffer.position and other methods should include detail in IAE

Martin Buchholz martinrb at google.com
Mon Mar 23 22:12:00 UTC 2015


Previous maintainers were nervous about improving the error messages
because of the risk of performance loss in these performance critical
methods.

Notice that the below method contains both cold code and hot code - you
want to segregate them as I suggested in my previous message.

Because this is a critical low-level core library:
I would not have a checkPositionBounds method at all and save a branch by
using | instead of || and optimize for the current hotspot implementation:

if ((newPosition > limit) | (newPosition < 0))
  throw new IllegalArgumentException(positionOutOfBoundsMsg(newPosition));

Also, if we're going to this much trouble to craft a high quality error
message, we can figure out the "or" for the user.

 249      * Verify that {@code 0 < newPosition <= limit} 250      *
251      * @param newPosition 252      *        The new position value
253      * 254      * @throws IllegalArgumentException 255      *
   If the specified position is out of bounds. 256      */ 257
private void checkPositionBounds(int newPosition) { 258         if
((newPosition > limit) || (newPosition < 0)) { 259             throw
new IllegalArgumentException("newPosition > limit: (" 260
   + newPosition + " > " + limit + ")" 261                 + " or
newPosition < 0: (" + newPosition + " < 0)"); 262         } 263     }



More information about the core-libs-dev mailing list