Property file dot separated key names - java

I just noticed that almost all the key values in property files are in dot separated names.
eg -some.key=some value
Does anyone know why? Just asked out of curiosity.

It's basically a convention that makes it easier to see what properties are related.
For example:
person.title="Title"
person.surname="Surname"
job.description="Some description"
It's easy to see which properties are related and, using a smart editor, you can then chose to only see the properties for person. A few IDE's and editors can even use this to add code completion for you, as you know you want to work with person, but might not remember the exact property.
We sometimes underestimate the power of readability, yet a convention like this makes it easy for anyone to add, edit or maintain these properties. This becomes very important when working in teams or when on-boarding juniors.

There is no any specific reason for that but it's just for better understanding of programmers nothing else. you can also directly write key=value without using any kind of "some" :-)

Related

Save values to file(e.g yml) in java

First of all this might be a dumb question and I searched for some days but didn't find an answer. So if there is an existing answer concerning my question, I would be grateful for a link.
I don't know if anyone of you ever coded Spigot, Paper or Bukkit, but there was a class called YamlConfiguration which had the following methods:
public FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
cfg.set(path.path2, "hello");
cfg.getInt/String/...(path.path2); (which obviously returns "hello")
cfg.save(file);
The produced file then looks like this:
path:
path2: "hello"
So you could basically save any value in those files and reuse them even if your program has been restarted.
I know have moved forward from Spigot/Paper to native Java and I'm missing something like that Yaml-thing. The only thing I found was a kind of a config file, where every time the whole file is overwritten, when I try to add values.
Can you show me a proper way of saving values to a file? (would be nice without libraries)
I'm missing sth like that Yaml-thing
SnakeYAML should have you covered. Without knowing anything about your use-case, it makes no sense to discuss its usage here since its documentation already does cover the general topics.
The only thing I found was a kind of a config file, where everytime the whole file is overwritten, when I try to add values.
Saving as YAML will always overwrite the complete file as well. Serialization does not really work with append-only. (Serialization is the term to search for when you want functionality like this, by the way.)
If you mean that previous values were deleted, that probably was because you didn't load the file's content before or some other coding error, but since you don't show your code, we can only speculate.
Can you show me a proper way of saving values to a file?
People will have quite different opinions on what would be a proper way and therefore it is not a good question to ask here. It also heavily depends on your use-case.
would be nice without libraries
So you're basically saying „previously I used a library which had a nice feature but I want to have that feature without using a library“. This stance won't get you far in today's increasingly modular software world.
For example, JAXB which offers (de)serialization from/to XML was previously part of Java SE, but has been removed as of Java SE 11 and is a separate library now.

How to automatically find similarities in Java bytecode?

Not sure if the title is the most descriptive way of putting it, but it's about as descriptive as I could think of.
Anyway, onto the question. I want to know how I can find similarities in bytecode. What I mean by this is rather difficult to properly explain (at least for me), so I will give an example instead.
I have aba.class, and nhf.class. These classes are obfuscated classes from a game I made. I offer a modified version of this game which simply has some small code changes in some places, but because the game is for sale it gets reobfuscated every time there is a new update. I want to be able to tell what class has changed to what in the reobfuscation by checking how similar the bytecode is for the classes. I know this is possible, but I have no idea how to check how to do this.
Is there a library, program or something that can parse bytecode and check how similar it is, or would I have to write this myself? If I would have to write it myself, I would appreciate someone to point me in the right direction (or link me to something that might help, etc).
Also, I'm looking at doing this with code, rather than manually, in case that wasn't apparent.
There can be a simpler solution:
I don't know what obfuscator you use (maybe Proguard), but it probably generates a map that maps obfuscated classnames to non-obfuscated classnames. (If not, you can switch to Produard, which generates such map.)
So, you can translate obfuscated classnames to original classnames (and vice versa) provided that you have the map for the version.
So, you can make such map from these two maps by matching original classnames.

Intellij IDEA String handling features and retriving messages from properties files

I have recently stumbled upon a neat feature in Intellij IDEA that has let me to question one of my practices. It's the String auto-complete. Basically, if I define a key-value pair in a properties file, and then begin typing a String in java code that has the save value as the key in the properties files, IDEA can auto-complete it. More, I can navigate to it with ctrl+click and can refactor it!
The practice that I was talking about is related to displaying a value from the properties file. I am currently using an enum for this, whose types have the same name as the keys in the properties file. I was doing this because I gained type checking and refactoring. But it seems that I can have the same benefits just by using strings in IDEA (well, it doesn't really give me type checking, but it's kind of close).
I was wondering if any of you are using simple String values for retrieving messages. Is this a good practice?
I don't think it's a good practice. You shouldn't depend on your IDE when you're developing application. If somebody elses uses e.g. eclipse he has a chance to mess up all this.I like solution with enums more than string only because it gives compile time checks. You can even build you enum so it also reads properties file and every item in enum contains both key and value from property file.
A compromise would be to have an enum with properties where enum value is the property string. This way you get type safety and IDEA will recognize that enum value comes from property and let you easily navigate to it.
Its probably better to use an Enum and to make everyone you work with use intellij, if they complain show them this question (and many others).
Also worth bearing in mind that some string values require compile time constants , which makes things a bit more complex.

Can we refactor the following scenario using Eclipse?

I want to change the type of a variable from String to int, can we use Eclipse to refactor?
There's no out of the box refactoring tool that does it as far as I know. The reason probably is that strictly speaking this isn't refactoring: refactoring is a change that doesn't affect the behaviour of the code, but this change definitely does.
Unless you're using reflection, the easiest way to make this change is to change the field first, then watch the bits that turn red, and work your way through them. (You'll get a cascade of errors, pieces that you fix will cause other pieces to go wrong, but eventually you'll get o the end of it.)
I know this isn't really the answer you wanted but if you follow this pattern (deliberately break the code first, then correct errors that arise), it doesn't take long.
If you do have reflection in your code though, then you have no other option than to go through every single file that uses reflection and check whether it would be affected by your change.

How to organize Java properties entries for internationalization?

In our app we have a messages.properties file which contains all of the strings that will be shown to the UI. We have a small app with a few screens and it's already getting unwieldy with duplicate string values throughout.
Right now we have it organized with page specific strings separated out with whitespace and comments, with a section for each jsp with the property name having a prefix of the page name. We also have sections for entities, for instance, anywhere we show the user's email address, we would reference the property user.email for the label for that input or output field. We have another section for error and status messages, and finally one last section with global messages like "Submit" and "Cancel"
There's got to be a better way, and I'm wondering if you know what it is.
I don't think there is a universal "better way". I tried Googling for "best practice" advice on this, and found nothing that talked about how best to structure the property namespace for i18n.
(This I found somewhat surprising. There's usually someone out there who is prepared to put forward their ill-considered opinions on something like this as "best practice". Or perhaps, I'm too cynical.)
FWIW, my general advice would be:
be systematic and consistent about the property names and the property file structure,.
don't be afraid to use resource bundle inheritance if there is a lot of duplication,
if the property files or resource bundles get too large, partition them.
But I expect you already know and do all of that.
Finally, don't get too hung up about getting this "just right". There is no perfect solution, and what you are currently doing is probably good enough ... according to the criteria of whoever is paying you to do this work.

Categories