0

Hi guys can anyone help me with this.

Scenario:

For context please look at my older post. How to patch a Java program?

So basically in my company if I have to redeploy a program I am only allowed to update specific classes. Meaning I have to supply the compiled class to the administrator and tell them where those files should be placed.

Question:

Now my question is I have a class named ClassA that is extended by ClassB, ClassC, and ClassD. ClassA has a method named genericMethodA() whose internal implementation I would like to change.

Example

// old implementation in ClassA
public void int genericMethodA()
{
   return 50;
}

// change to this implementation in ClassA
public void int genericMethodA()
{
   return 100;
}

// Method in ClassB, ClassC, and ClassD
public void logB()
{
     log.info(genericMethodA());
}

Will the byte code for ClassB, ClassC, and ClassD change? So if I placed the newly compiled ClassA.class in my old JAR/WAR when ClassB, ClassC, and ClassD calls genericMethodA() will it return 50 or will it return 100.

Also can anyone point out a documentation how java compiles code. For example when a class is extended does the compiler place the instructions on the subclass byte code or it just points back to the base class.

Guys please do not hesitate to comment if you need more information or clarification about my question.

Community
  • 1
  • 1
Jefrey Valencia
  • 713
  • 3
  • 13
  • 30
  • NO, it won't. Each class has its own byte code. – Dawood ibn Kareem Nov 20 '13 at 02:49
  • Hi David, just to be clear you are saying that the byte code will NOT CHANGE. So does that also means that if I do not update ClassB, ClassC, and ClassD their genericMethodA() will still return 50. – Jefrey Valencia Nov 20 '13 at 03:02
  • 2
    The method doesn't exist in the bytecode for B. So when you change the bytecode for A, then call the method from B, you'll get the new version. The bytecode for a method only ever exists in the class where that method was actually defined. – Dawood ibn Kareem Nov 20 '13 at 03:11
  • 1
    One easy way to convince yourself that this will work is to try it! However, I think it's still smart that you asked because it might have been the case that it happened to work on your system, but might not work in general. Luckily for you, swapping out a classfile should work fine on any JVM as long as you don't try to deserialize a serialized copy of the older version of an instance of the class. – DaoWen Nov 20 '13 at 03:13
  • 1
    I'd try throwing the .class files into a bytecode reading program like jclasslib if you're curious what is / is not in the bytecode of different classes :) – Charlie Nov 20 '13 at 03:20
  • @David thanks for the info but that is nice to know. I know how to use the java api and syntax but not exactly how the compiler creats the byte code again thank you. – Jefrey Valencia Nov 20 '13 at 03:23
  • @DaoWen yes that is exactly why I asked as no one in my department has actually tried it before and I am not sure it will work for all cases. thank you for your comment. – Jefrey Valencia Nov 20 '13 at 03:26
  • @charlie thanks I will also try that. – Jefrey Valencia Nov 20 '13 at 03:27
  • 1
    I'm not sure how I failed to answer your question. You change A, and you'll be able to see your changes from within B, C and D. Isn't that what you were asking? – Dawood ibn Kareem Nov 20 '13 at 03:34
  • @David Hi, actually you did answer my question might have phrased my last comments wrong. All I was saying is that I know how to use the Java API and do not know much how the compiler works. As I would just normally redeploy the recompiled JAR/WAR. If not for the new company policy I really would not have cared. Again you did answer my question and thank you. Though I do not think a comment can be accepted as an answer. :) – Jefrey Valencia Nov 20 '13 at 03:43
  • You need to get your company policy changed. Patching a class at a time is a recipe for nightmares. I do it occasionally but I'm a sole developer and owner. I wouldn't consider it in a company where there are enough developers for them to be told what to do like this. – user207421 Nov 20 '13 at 04:27
  • @EJP thanks for your comment, I would if I could @_@ – Jefrey Valencia Nov 20 '13 at 04:45

1 Answers1

1

No it does not.

The main things you have to watch out for when updating classes are things that are determined during compile time. This includes method overloading, which is chosen at compile time and hardcoded, and static final primitive constants, which are inlined at compile time.

But overall, Java is actually designed to support modifying some classes while leaving others untouched. There's in fact developer guides which tell you what is safe to change and what will require recompilation of dependencies.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • Hi Antimony, just to be clear based on your answer and David comment that the byte code of the subclass is will not change. So does that also means that if I do not update ClassB, ClassC, and ClassD their genericMethodA() will still return 50. Also, if you do not mind do you have the links to the document you are saying. Or even the keywords you used to find it all i keep getting is tutorials on how to compile and not how the compiler works . – Jefrey Valencia Nov 20 '13 at 03:17
  • 1
    @JefreyValencia No. They don't *have* a `genericMethodA().` They have the one in class `A,` that you changed to return 100. So they will get 100. – user207421 Nov 20 '13 at 04:26
  • @EJP thanks for completing the answer. As the answer given by Antimony and comment by EJP is already ok for me, I will be accepting this answer. But I would also like to note the helpful comments of David, DaoWen, and charlie above. I would have accepted David as the first correct answer but it was just written as a comment. – Jefrey Valencia Nov 20 '13 at 04:48
  • 1
    @Jefrey look in chapter 13 of the Java Language Specification, Binary Compatibility. – Antimony Nov 20 '13 at 06:49
  • Why should every reader search for it? It’s http://docs.oracle.com/javase/specs/jls/se7/html/jls-13.html – Holger Nov 21 '13 at 09:55
  • Another good reading regarding the question is the JVM spec [Chapter 3. Compiling for the Java Virtual Machine](http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html) – Holger Nov 21 '13 at 09:58