Is there a tool for the conversion of duplicate literals into final Strings/magic numbers in Java?

StackOverflow https://stackoverflow.com/questions/18391843

سؤال

I have some Java files that use the same String values up to 20 times each, and I want a tool that replaces those Strings with a private static final String STRING_TO_CAPS_AS_CONSTANT or something along those lines. If it can also convert numbers into magic numbers, that would be great. I'm using Eclipse but if it's a standalone tool I can still make use of it.

هل كانت مفيدة؟

المحلول

I think Eclipse provides that already. If you select the whole String and press CTRL+1 it should give you the opportunity to choose: "Extract to constant". This will implicitly replace all occurrencies of your String with the new constant.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top