1 2 Previous Next 21 Replies Latest reply on Jan 31, 2010 6:13 PM by jschellSomeoneStoleMyAlias

    Constants in Java Compiler

    843810
      Hi,

      I am trying to figure out dependency between constant defined in an iterface, which is being used by class. This dependency exists at compile but not at runtime, as javac optimises the byte code and replaces it with value. Now I wish to replace this constant value in constant pool with the reference to valriable in interface.

      For this I looked at the compliler code but was not able to figure which class is responsible for this optimisation.
      public class DepClass{
      private int i = Depend.a;
      }
      
      public interface Depend{
      int a = 5;
      }
      When Class DepClass is compiled the value for variable i is replaced with 5 rather that it points to Depend.a

      I wish to keep this reference in compiled class. Any idea how should I do this.
        • 1. Re: Constants in Java Compiler
          jschellSomeoneStoleMyAlias
          If you can add a static initializer to the interface then you can initialize 'a' that way.

          Or make the interface a class and do the same thing.
          • 2. Re: Constants in Java Compiler
            843810
            Thanks for your reply.

            I think I have not explained my question properly.

            In the code Class Dep1 depends on interface Dep2 at compile time.
            public class Dep1{
            private int b = Dep2.a;
            }
            public interface Dep2{
            int a = 2;
            }
            If I compile these two I will get two files Dep1.class and Dep2.class.
            But at runtime if I delete Dep2.class file, Dep1.class will still work. This is happening bcoz java compiler is replacing constats with its value. I want to modify the java compiler code such that it replace constants with its fully qualified name instead of value.

            I hope this makes clear, Any help will be appreciated.

            At the moment I am not able to find out where this optimisation is taking place

            -Regards
            • 3. Re: Constants in Java Compiler
              jschellSomeoneStoleMyAlias
              raghavx wrote:
              I think I have not explained my question properly.
              I understood your question.
              What part of my answer did you not understand?
              • 4. Re: Constants in Java Compiler
                843810
                You mean I should replace Interface with class in this code
                 JCStatement classOrInterfaceOrEnumDeclaration(JCModifiers mods, String dc) {
                        if (S.token() == CLASS) {
                            return classDeclaration(mods, dc);
                        } else if (S.token() == INTERFACE) {
                            return interfaceDeclaration(mods, dc);
                        } else if (allowEnums) {
                ...........
                • 5. Re: Constants in Java Compiler
                  jschellSomeoneStoleMyAlias
                  I am guessing you don't understand my answer. The following class examples has a class with the problem and one without.

                  // Has the problem
                  public class MyClass1
                  {
                  public final static int a = 2;
                  }
                  
                  // Does NOT have the problem
                  public class MyClass2
                  {
                  public final static int a;
                  
                  static 
                       {
                       a = 2;
                       }
                  }
                  Now I don't put constants in interfaces so I don't know if you can use a static initializer in one. But that is the only solution if you want to use the constant.
                  • 6. Re: Constants in Java Compiler
                    EJP
                    If you can add a static initializer to the interface
                    But you can't. Interfaces cannot contain code.
                    • 7. Re: Constants in Java Compiler
                      jschellSomeoneStoleMyAlias
                      ejp wrote:
                      If you can add a static initializer to the interface
                      But you can't. Interfaces cannot contain code.
                      As a interesting thought though it means that that the variable only exists as a compile time value. Without an initializer block there is no way for the compiler to initialize it at runtime. So it just disappears.

                      And no JNI nor reflection access either.
                      • 8. Re: Constants in Java Compiler
                        EJP
                        it means that that the variable only exists as a compile time value
                        Untrue.
                        Without an initializer block there is no way for the compiler to initialize it at runtime.
                        Untrue. It doesn't have to be initialized at run time. It can be in the constant pool. And just because you can't write an initializer block doesn't imply that the compiler can't.
                        So it just disappears.
                        Untrue. It is embedded into the object file. Otherwise how can javac and javap find it at all?
                        And no JNI nor reflection access either.
                        Untrue.
                        • 9. Re: Constants in Java Compiler
                          843810
                          Thanks ejp.

                          How should I proceed to modify constant pool? As I have looked at compiler code it is going above my head.

                          The modification required is such that constant pool stores the fully qualified name of constant instead its value.

                          Any help will be great.
                          • 10. Re: Constants in Java Compiler
                            EJP
                            Just declare the constant in a class instead of an interface, and initialize it in an initializer block rather than inline. As recommended above.
                            • 11. Re: Constants in Java Compiler
                              843810
                              >
                              Just declare the constant in a class instead of an interface, and initialize it in an initializer block rather than inline. As recommended above.
                              I cannot modify my source code. The interface is in 3rd party API. Also code base is huge more than 80K LOC
                              • 12. Re: Constants in Java Compiler
                                jschellSomeoneStoleMyAlias
                                raghavx wrote:
                                >
                                Just declare the constant in a class instead of an interface, and initialize it in an initializer block rather than inline. As recommended above.
                                I cannot modify my source code. The interface is in 3rd party API.
                                Then you should have stated that when you started.

                                The compiler replaces that at compile time. If you modified to the code as suggested then the compiler would pick it up.

                                Simple as that.

                                If you are asking how to take class files, not source, and find and replace a single constant throughout then myself I would say it is impossible. Without other information there is no way to determine where that value (not variable) appears.

                                If you have source code, then the ONLY choice is to modify it in some way.

                                Not to mention of course that the license might forbid any modifications.
                                Also code base is huge more than 80K LOC
                                That isn't huge. It isn't even big. It is either small or even tiny.
                                • 13. Re: Constants in Java Compiler
                                  EJP
                                  I wish to keep this reference in compiled class.
                                  OK. Why?

                                  The only problem this causes is when you change the value in the interface. Solutions:

                                  (a) don't
                                  (b) recompile everything.
                                  • 14. Re: Constants in Java Compiler
                                    843810
                                    >
                                    OK. Why?
                                    This dependency is at compile time only. I wish to see this at run time as well.
                                    1 2 Previous Next