Eclipse Java code Format - java

I am not that much aware of Eclipse Shortcuts.
I copied code from some link and I pasted in Eclipse Indigo but it is coming like
"public String doLogin() throws ApplicationException{ long executionStartTime = System.cu... }"
I want to format it in java style like
public String doLogin() throws ApplicationException{
long executionStartTime = System.cu...
}
I google it and found few shortcuts like,
"Shift + Tab" , "Ctrl + I", "Ctrl + Shift + F". but is not giving me the behavior I want.
is there I need to add custom formatter or I am expecting more.

You can format text using the Ctrl+Shift+F shortcuts.
You can use Ctrl+A to select all text or you can format only several lines (which you have selected).
There is a caveat however: If your code does not compile the formatting does not work. I don't know whether this is intentional or a bug.
You may want to enable auto-formatting in Eclipse it is a handy feature I usually use:
You can find the formatter settings here:
I also recommend the Eclipse Color Theme plugin!

With default key mapping Ctrl+Shift+F should format your code (current class or selection if any). Of course syntax has to be valid.
You should be able to access that feature using the menu Source > Format where shortcut is displayed if existing.

Type Ctrl+Shift+L to get list of shortcuts in Eclipse.... and to format Ctrl+Shift+F

You can create your own formatter:
Window -> Preferences -> Java -> Code Style -> Formatter.

"Ctrl + Shift + F" should work but make sure you remove " from start and end of the code you copied.

select all text using ctrl + a
and press
Ctrl + Shift + F
to format text

Related

Using Selenium Java get special characters (Chinese, Japanese) but gives?

I have a requirement where I would like to read combination of English and non English characters from a dropdown on Web UI
Example- abcd , efgh, 你好, こんにちは
public void test(){
dropdown.click();
Select newValues = new Select(dropdown);
List<WebElement> listNewValues = countryValues.getOptions();
ArrayList<String> actualDropDownItems = new ArrayList();
for(WebElement value : listNewValues){
actualDropDownItems.add(value.getText());
System.out.println(value.getText().toString());
}
If I try to run and print this, I get ?? for Chinese and Japanese values.
Please note I have read some of the previous suggestions and in eclipse at project> properties level> Resource> Other is set to UTF-8.
Everywhere it is suggested to use UTF-8. But what else can I do If I have already set this at project properties level.
Is there any other easier way of getting this non-English characters? Any help is appreciated.
P.S. I had also tried using BufferedWriter approach, but it gives me ­å›½æ—¥æœ¬ëŒ€í•œë¯¼êµ­ instead of 你好, こんにちは
Running simple System.out.println("你好"); was not giving me correct output on Eclipse console.
Looks like setting eclipse at project level was not doing anything to help me.
project> properties level> Resource> Other is set to UTF-8
Did right click on test, Coverage As> Coverage configuration > Common and then set Other to UTF-8, solved my problem.

IntelliJ: Is there an Intention Action to convert old style Log4J calls to new style SLF4J calls?

I am working on a project with legacy code that had used Log4J in the past, and now uses SLF4J. Most of the old style Log4J logging statements remain, and I convert the log statements slowly to the new SLF4J style as I come across them.
I've just realised that pressing Alt+Enter when in the old style log statements gives me the following options (unfortunately I cannot upload a screenshot):
Copy String concatenation text to the clipboard
Inject language or reference
Replace '+' with 'String.format()'
Replace '+' with 'StringBuilder.append()'
Replace '+' with 'java.test.MessageFormat.format()'
The option Replace '+' with 'String.format()' is very close to what I need, although I don't need the String.format( bit.
Is there something that would give me an Intention Action of: Replace Log4J style log statement with SLF4J style log statement?
As an example of old logging style (e.g. Log4J), I mean:
LOGGER.debug("User " + user.getUserId() + " has performed a search with the criteria " + dto);
And by new style I mean:
LOGGER.debug("User {} has performed a search with the criteria {}", user.getUserId(), dto);
Have you tried the Java | Logging issues | Non-constant string concatenation as argument to logging call inspection? It has a quickfix to automatically convert a string concatenation to a parameterized log message.
Thanks to Wim Deblauwe's comment about SSR I have discovered Edit | Find | Structural Replace and using the following to fix simple cases where two arguments are used in a logging statement:
Search template:
LOGGER.debug("$str1$" + $arg1$ + "$str2$" + $arg2$)
Replacement template:
LOGGER.debug("$str1${}$str2${}", $arg1$, $arg2$)
I doubt I am using the Structural Search and Replace feature to its maximum capability, and will have to do a few sweeps to get all the logging statements, but this is great progress. Thank you Wim.

How to set part of text to bold when using AlertDialog.setMessage() in Android?

How to set part of text to Bold when using AlertDialog's setMessage()? Adding <b> and </b> to my String doesn't work.
You need to use Html.fromHtml() too. For example:
AlertDialog.setMessage(Html.fromHtml("Hello "+"<b>"+"World"+"</b>"));
Update:
Looks like Html.fromHtml(String source) has been deprecated in the Latest Android Nougat version. Although deprecation doesn't mean that you need to change your code now, but it's a good practice to remove deprecated code from your app as soon as possible.
The replacement is Html.fromHtml(String source, int flags). You just need to add an additional parameter mentioning a flag.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
AlertDialog.setMessage(Html.fromHtml("Hello "+"<b>"+"World"+"</b>", Html.FROM_HTML_MODE_LEGACY));
} else {
#Suppress("DEPRECATION")
AlertDialog.setMessage(Html.fromHtml("Hello "+"<b>"+"World"+"</b>"));
}
For more details have a look at this answer.
This page describes how to add HTML formatting to resource strings.
<resources>
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.
</string>
</resources>
And do not forget to use: Html.fromHtml
AlertDialog.setMessage(Html.fromHtml(getString(R.string.welcome_messages)));
This works for me
In case if anyone wants to add only a single string:
<string name="abouttxt"><b>Enter license key</b></string>
Add this line in your Alertdialog code.
dialog.setTitle(Html.fromHtml(getString(R.string.abouttxt)))
None of these solutions worked for me, but I am required to use an older version of the API so I could not use Html.fromHtml. To bold part of the text for an AlertDialog I had to use a SpannableString.
String msgPart1 = getString(R.string.PartOneOfMessage);
SpannableString msg = new SpannableString(msgPart1 + " " + boldTextString + " " + getString(R.string.PartTwoOfMessage));
msg.setSpan(new StyleSpan(Typeface.BOLD), msgPart1.length() + 1, msgPart1.length() + datumName.length() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
AlertDialog.setMessage(msg);
I am not saying this is the best way, but it was the way that worked for me.
<string name="abouttxt">"<b>Info</b>\ntexttxtxtxtxt"</string>
this works for me in xml

Eclipse formatter: can it ignore annotations?

Sometimes I want annotations on fields be in a single line and sometimes in a line each. Is there any way to make Eclipse formatter just ignore these annotations and leave the lines breaks just as I did?
Not quite sure what you mean, but you break up lines for field this way:
String text =
"cake" +
"more cake" +
"alot more cake";
This is also an option:
You can go to Properties -> Java code style -> Formatter -> Edit: Then there should be some tags to on/off.
Also include this line in your code:
/* #formatter:on */
I think so. In Preferences go to Java - Code Style - Wrapper. Then edit your active profile, click "Line Wrapping" and then "Annotations - Element-value pairs". Then set the Line wrapping policy to "Do not wrap".

Pasting a very long string in eclipse editor

I am trying to paste a very long string in eclipse like:
String str = "verrrrrrrrry long string that hass 9000 characters";
I want it to appear as
String str = "verrrrrrrrry long"+
"string that hass "+
"9000 characters";
I tried the option mentioned here : Paste a multi-line Java String in Eclipse , but that gives me a bunch of new lines inserted in the string which I dont want.
What I am getting currently is a long string that just wraps over itself on the same line.
Any pointers ?
Go in your Eclipse preferences:
under Java--> editor --> save actions --> Check off the "Format Srouce code" and "All Lines"
That will wrap it for you, when you save it.
You can make a macro on Notepad++ for instance and prepare the string there.
After that you just need to paste it to Eclipse.
None of the settings worked for me , though they might be correct and I might have some other issue with my workspace.
Anyways, I was able to work through my problem with this expression:
cat Data.txt | grep -o -E '.{1,70}' | sed -r 's/(.*)/ "\1"+/'
P.S:
If anyone else confirms that one of the answers given by anyone in this post works, I will accept it on their behalf , since I think my answer is not a direct answer to my question.

Categories

Resources