Intellij auto-generate java method at the end of the file - java

Is there any way to ask the auto-generate methods created by intellij to be generated at the end of the class?
currently when i generate a method (⌥⌘M) it is created right below the function I extracted the method from, and I would like it to be at the end of the file.
Does intellij offer that option?

You can re-arrange the code in IntelliJ after code generation manually even though this does not guarantee that the code will go to the bottom of the file. You can do that by clicking on the "Rearrange" menu item in the "Code" menu:
Another option to re-arrange the code without having to use the menus would be via the "Reformat" file dialogue, by pressing on Windows Ctrl+Shift+Alt+L and by activating in it the "Re-format code" checkbox.
You can change the pattern of code arrangement in the settings in the following dialogue:

There is no such option in Intellij, Once you extract method you can move method up and down using Press Up( ⌥ ⇧ Up) or Down( ⌥ ⇧ Down).

Related

Create a shortcut for system.out.println in visual studio code

I am using visual studio code IDE for my Java project and I want to create a shortcut for:
System.out.println();
That is if I write sysout or something like that it will automatically changed to System.out.println(); with cursor inside parenthesis.
Is there any way it can be achieved in VScode?
There is no need to define the snippet yourself. You can install https://marketplace.visualstudio.com/items?itemName=redhat.java, which contains the predefined snippet for your case (and more than that), here are some screenshot:
sysout:
syserr:
and even systrace:
If you want to developing your Java application in VS Code, you can also install https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack, which provides debugging, testing, project managing capabilities to you.
As mentioned by #vaibhavsahu in the comments you can create user defined snippets in VSCode: https://code.visualstudio.com/docs/editor/userdefinedsnippets
Here's a relevant excerpt from that document which shows where to locate the user defined snippets file:
You can easily define your own snippets without any extension. To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the language (by language identifier) for which the snippets should appear, or the New Global Snippets file option if they should appear for all languages. VS Code manages the creation and refreshing of the underlying snippets file(s) for you.
So in your case you'd go to File (or Code on macOS) > Preferences > User Defined Snippets and then type in Java, this should open a java.json file (which contains an example user defined snippet commented out).
I think this snippet would do it for what you're trying to accomplish:
"System.out.println(placeholder)": {
"prefix": "sysout",
"body" : ["System.out.println(${1:string})"],
"description" : "System out println with placeholder in parens"
}
Then when you start typing sysout in a .java file IntelliSense should show the recommendation for the code snippet, when you enter enter it should be substituted in with your placeholder in the parens.
Edit:
The System.out.println(placeholder) from the java.json file is just what displays in the IntelliSense dropdown, you could make it System print or whatever you'd like so that you'll recognize it. The actual code which gets substituted is in the "body"
this extension or plugin can meet your need
https://marketplace.visualstudio.com/items?itemName=ricardo-emerson.java-sysout

Show source code for selected class name in editor in IntelliJ 2018.1

Through Maven, I have the source code and JavaDoc both downloaded for libraries used by my code.
I want to select a class name called in my code in the IntelliJ code editor, and then ask IntelliJ to show me the source code for that particular class.
Bonus: Show the JavaDoc for that class.
I thought F1 key would do this, but no so.
If a keyboard shortcut is required, please indicate the macOS version as well as the PC version if known.
Hold Command key, hover mouse, and click
On a Mac, hold down the Command key. Notice as you move the mouse pointer, the class names change to blue underscored text like an old-school web page link. Click that retro-link while holding down Command.
I am guessing, per comment by Justice, that on a PC you would hold down Control key while hovering/clicking.
No need to select the class name.
Before, no keys pressed
After, pressing Command while hovering mouse pointer
Click the apparent link to open the local copy of class source code.
This approach worked on IntelliJ 2018.1 on macOS Sierra.
Pressing CTRL+N will open a text box where you can search for the class by name, and then open it, regardless of whether it's a class that's part of your project's sources or a library you use. If you have the library's source downloaded, IntelliJ IDEA will show you that instead of the decompiled binary of the class.
COMMAND+B is used to show source code after selecting the class or interface for IntelliJ Users.

Running Projects in IntelliJ

I've got a project from my teacher, in which i should add my implementation for a certain aspect.
The Project already got a main method but still the "Run" and "Debug" buttons in the top right corner are greyed out.
I've also tried right clicking the main method and java file, but it doesnt show me run or debug.
Also tried using the "Make Project" Button (top right corner), but it doesnt let me run it either.
How can i run the project?
Help would be much appreciated cause I cant seem to find a solution...
the "run" and "debug"-button in the top run the currently selected main-method. As long as you haven't selected one, you can't run it. In the current version on the left side of the editor-pane, there should be a green arrow right next to the main-button. Run it once via clicking that arrow and from that point on you can run/debug the program using the buttons on the top-window.
In this image you can see the green arrows (version 2016.2.4).
Alternatively, you can use the following more complicated, but also more versatile option:
Directly from the run- and debug-buttons is a button that toggles a list. In the initial configuration of the project, this list contains exactly one Item: "Edit Configurations...". Use it to create a custom-target - go for Application, if your using a java-class as entry-point - and set up the configuration as you wish. Clicking the arrow mentioned earlier does basically the same with a bit of automatic configuration.
Third alternative:
Open the context-menu on the main-method. It as well contains options to run and debug the program with the marked method as entry-point. Works for the context-menu of the respective class as well.
You have to add a run configuration. Do this with clicking the grey button and then clicking at sth with configurations (i am not in front of my PC), then add there the config
Does your main method have the proper syntax for arguments? I found that in Eclipse, the program will not let you run the main method if it is not in the format of public static void main(String[] args). Also, does your code compile properly and your IDE just doesn't let you run it?

Eclipse, Easily remove/fix all #Override due to Java version change

I am aware of 'Must Override a Superclass Method' Errors after importing a project into Eclipse (my issue is the opposite, I need to remove the #Overrides) and I understand the reason the error occurs.
My question
Is there an easy way to remove all of those errors, or remove all the errors of the same kind in one swoop? Kind of like solving all imports...
Thanks for any tips to help save time.
I do not know better way than find and replace all #Override by empty string. Eclipse can do it globally: press Cnrtl/H, write text #Override and leave the target empty. Then press "replace all" and pray... :(
Solution:
If you imported a Java 6 project and have Eclipse complaining about #Override annotations because you are compiling with Java 5...
Make sure you have a file from the project open and selected in your workspace
CTRL+H
Change the Tab at the top from Java Search to File Search
In Containing Text enter #Override
In File name patterns enter *
Select Enclosing Projects for your Scope
Press Search to do a dry run to see what happens and what it finds
Then, if all looks good, proceed with:
CTRL+H
Change the Tab at the top from Java Search to File Search
All your values should be remembered, if not follow the steps above from the dry run
Press Replace
Example Replace
Replacing 92 matches in 18 files
Replace: field should have carried over #Override
Remove anything/everything from the With: field
Press OK
Awesome...no laborious removing of #Overrides ever again.
Please NOTE: Occasionally, for me at least, when pressing CTRL+H the File Search wont allow me to choose Enclosing Projects as a Scope or it will automatically search the entire Workspace as opposed to the project. This is resolved by ensuring a file from your project is open and focused in Eclipse.
Hope this helps others. Thanks
After removing all instances of "#Override", you can also add back the non-interface overrides using "Clean Up".
Steps (search/replace, then add back):
Select the projects or folders containing your source files.
Go to "Search > Search..." (Ctrl-H) to bring up the Search dialog.
Go to the "File Search" tab.
Enter "#Override" in "Containing text" and "*.java" in "File name patterns". Click "Replace...", then "OK", to remove all instances of "#Override".
Go to "Window > Preferences > Java > Code Style > Clean Up" and create a new profile.
Edit the profile, and uncheck everything except "Missing Code > Add missing Annotations > #Override". Make sure "Implementations of interface methods" is unchecked.
Select the projects or folders containing your source files.
Select "Source > Clean Up..." (Alt+Shift+s, then u), then "Finish" to add back the non-interface overrides.
Eclipse->Window->Preferences->Java->Compiler->Compiler compliance level ----> choose 1.6.
JAVA JRE 1.5 override is only superclass

How do I modify Eclipse code formatting?

When I reformat code with Eclipse, it turns method headers like this:
public void myMethod(String param) {
into method headers like this:
public void myMethod(
String param) {
When I was brought on here I'd never used Eclipse before, and I imported project settings provided by someone else. I have seen that on small new projects I've worked on Eclipse does not do this, so it must be in the settings I've imported. But I've gone through every panel I can find, as well as every hidden file I can find in the workspace, and I can't figure out what is causing this.
How do I turn it off? I don't want a newline before parameters in my method signatures, and I can't imagine why anyone would!
Have a look at Window>Preferences>Java>Code Style>Formatter.
There you can configure almost everything. Your case is found at
Line Wrapping>Method Declarations>Parameters.
In my version of Eclipse, I found the option under Window->Preferences->Java->Code Style->Formatter, then clicked the Edit.. button.
In the new window, go to the Line Wrapping tab and find Method Declarations. You want to change the Line wrapping policy. The Force split check box seems to do the same.
Go into preferences: Java -> Code Style -> Formatter
Restore Defaults or edit what's there.
Additionally, Code Style -> Code Templates will allow you to define generate code formatting.
Note that this is for Eclipse Workspace as a whole, the same Preferences can be accessed under the project preferences if you want to get more fine grained at a per project level.
In eclipse you can define your code formatting use Code Style ,
for MAC System :--> Eclipse --> Preference -- > java--> code Style
for Window System :--> Window->Preferences->Java->Code Style->Formatter
The best way to change your format is to go to Window-> Preferences-> Java-> Code Style-> Formatter. Inside the formatter window, eclipse has a couple of built in styles, but the best way is create a new profile of your own by pressing on New. You can change the format as you please on the edit tab
You can change parentheses and curly braces positioning along with other stuff.

Categories