kidok / protobuf

Automatically exported from code.google.com/p/protobuf
0 stars 0 forks source link

Java: initializing bitFields variables #630

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Generate Java class

What is the expected output? What do you see instead?
In the message class in the init method there are all the variables initialized 
but not the bitFields. This issue does not propagate itself when normally used 
because the bitFields are populate by JVM automatically to zeros when new 
instance is created. However, I thought I would report it since it does not 
seem to me the way it was intended to be. I discovered it when I was 
implementing changes to the generated class to have it behave more in "SAX" 
style than "DOM". So I was reusing one message object multiple times and I 
needed to reset the variables before every usage.

Please use labels and text to provide additional information.
Java, initMethod, bitfields

Original issue reported on code.google.com by kral.voj...@gmail.com on 15 Apr 2014 at 7:47

GoogleCodeExporter commented 9 years ago
This is working as intended. bitFields should be initialized to all zeros and 
it's reasonable to not do it again in the init method when JVM already does 
that.

The init method is a private method. If you intend to call it by yourself (or 
set data members directly), you are probably doing it wrong.

Original comment by xiaof...@google.com on 15 Apr 2014 at 8:49

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Ok, I needed different behavior from the parsing so I had to call it by myself 
and I noticed this handling of bitFields and since I was not sure if it was 
intended or not rather brought it up. Thank you for such a quick answer.

Original comment by kral.voj...@gmail.com on 15 Apr 2014 at 9:00

GoogleCodeExporter commented 9 years ago
If you are implementing your own parsing methods, it's better to only use 
public methods (e.g., clear() when you want to reset a mesasge). Internal 
methods may change without notice so depending on them is not a very good idea.

Original comment by xiaof...@google.com on 15 Apr 2014 at 9:13

GoogleCodeExporter commented 9 years ago
And just quick wondering why are the other number types properties not set as 
bitFields so by JVM only without the setting in initialization method?

Original comment by kral.voj...@gmail.com on 15 Apr 2014 at 9:13

GoogleCodeExporter commented 9 years ago
Re #5:
JVM will only set the value to 0 but an integral protobuf field may have a 
non-zero default value (set by field option [default = 12345]). When generating 
the init method it's easier to just generate an assignment statement for every 
field no matter whether the default value is 0 or not. This is an 
implementation detail though. We could change it to not initialize integral 
fields whose default value is 0.

Original comment by xiaof...@google.com on 15 Apr 2014 at 9:21

GoogleCodeExporter commented 9 years ago
I see. That is what confuse me the difference between attributes with zeros and 
the bitFields.

Original comment by kral.voj...@gmail.com on 15 Apr 2014 at 9:51