Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-816

Introduce CONST() methods to prevent Javac from inlining constants

    XMLWordPrintableJSON

    Details

    • Type: Wish
    • Status: Closed
    • Priority: Trivial
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.2
    • Component/s: lang.*
    • Labels:
      None

      Description

      ConstantUtils could contain a set of methods to ensure constants are not inlined by javac.

      For example, typically a developer might declare a constant like

        public final static int MAGIC_NUMBER = 5;
      

      Should a different jar file refer to this, and the MAGIC_NUMBER is changed a later date (e.g., MAGIC_NUMBER = 6), the different jar file will need to be recompiled. This is because javac typically inlines the primitive or String constant directly into the bytecode, and removes the reference to the MAGIC_NUMBER field.

      To help the other jar (so that it does not need to recompile when constants are changed) the original developer can declare their constant using one of the CONST() utility methods, instead:

        public final static int MAGIC_NUMBER = CONST(5);
      

      I imagine the code would look something like this:

      public static boolean CONST(boolean v) { return v; }
      public static byte CONST(byte v) { return v; }
      public static char CONST(char v) { return v; }
      public static short CONST(short v) { return v; }
      public static int CONST(int v) { return v; }
      public static long CONST(long v) { return v; }
      public static float CONST(float v) { return v; }
      public static double CONST(double v) { return v; }
      public static <T> T CONST(T t) { return t; }
      

        Attachments

          Activity

            People

            • Assignee:
              juliusdavies Julius Davies
              Reporter:
              juliusdavies Julius Davies
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Due:
                Created:
                Updated:
                Resolved: