intellij cannot create class-file when creating test class with maven project - intellij-idea

I created a maven project, and my hierarchy is :
MyPro:
.ideas
src
-main
-java
-mypackage
-MyClass.java
-test
-java
-mypackage
- (PLACEHOLDER)
When I try to create MyClassTest.java at PLACEHOLDER i.e. in test/java/mypackage, I got unable to parse template class, cannot create class-file.
But if I create TestMyClass.java file, it works. I wonder why is that?

Do you have MyClassTest.java or something similar listed under:
Settings → Editor → File Types→ Text files ?
If so, try removing that pattern and check if you can create the class.
I was able to reproduce this error in a standard maven project, after adding the following file type:
And then trying to create the MyClassTest.java:
And after removing the file type there were no problems creating the Test class.

Related

How to create a Kotlin console application with Gradle in Intellij IDEA

I found this tutorial from JetBrains: https://www.jetbrains.com/help/idea/create-your-first-kotlin-app.html?section=Gradle%20Kotlin
It is dated August 19th, 2020.
I am using the same IntelliJ IDEA version as in their documentation: 2020.2.
However, my project creation wizard looks quite different from theirs.
They provide this screenshot:
But for me, it looks like this:
and when I click on Next:
I don't see where I can choose the Console Application template, or Gradle.
I found a second tutorial - https://kotlinlang.org/docs/tutorials/jvm-get-started.html , which shows yet a third variation of the New Project wizard:
Are the tutorials out of date? Am I doing anything wrong? How do I create a Kotlin project, based on a console application template, with Gradle?
The wizard you have seems to be obsolete now. There was a brand-new one, released as a part of Kotlin 1.4 recently(see here). Most probably, the problem is caused by the Kotlin IDE plugin being outdated or something. Please try to delete in and re-install using the Preferences -> Plugins menu. Comment here with the results, please. I'd like to know if this would help.
Indeed it's quite weird, I've never seen the dialog to look like yours (mine looks like the one in the tutorials). However, choosing the template doesn't do anything special - it simply creates the main file, which you can do so yourself.
So create a new project with the "JVM/IDEA" option. If it already opens up a main.kt file, you don't need to do anything else. If it didn't, look in the src folder - you should see a folder named main with a folder named kotlin (with a blue icon instead of grey) inside - here's where you wanna create your main file (right click -> new kotlin file/class -> main.kt and make it a file, not a class). Finally, put this in the file:
fun main(args: Array<String>) {
println("Hello world!")
}
Note: if you don't have a kotlin folder, create the file in the folder with the blue icon (might even be src). Also, if this doesn't use Gradle (for some reason), create a Gradle project instead, and at the "Additional libraries and frameworks" option, uncheck Java and check Kotlin, then continue with creating main.kt if it isn't created.
You may create a Kotlin + gradle project from the terminal:
$mkdir myProject; cd myProject; gradle init
follow the tutorial.
And then start the Intellij & open the dir
You're good to go
Same process like this
The same happened to me but I figured out how to fix it:
Just disable Material Theme UI.

IntelliJ macOS Mojave - Moving Java Class into another Package does not consider JSON Files

I am using IntelliJ 2018.3.4 with default settings on macOS Mojave 10.14 for a spring boot project containing JSON files for data population. Refactoring Package names for Java-Classes (move a class into existing package) does not consider JSON files.
IntellJ Moving Java Class in another package
I've already tried to change the file extension into *.txt or *.html, after this change the refactoring seems to work. Changed package name occurs in those files.
Java class: com.test.MyClass
Json file (src/main/resources/file.json):
{
"name" : "com.test.MyClass"
}
After changing the Package name into "com.test2.MyClass", the "name" property in src/main/resources/file.json is expected to be changed into the same value "com.test2.MyClass".
When you rename a variable/property/class/method/package in IntelliJ IDEA, you're given a choice to search over strings and text occurrences. This will do the trick
To change the scope, use the Preview button.
To move a class to another package, use the Move refactoring action.

Creating class inside package A of A.B in IntelliJ

I have a maven project in IntelliJ IDE. I have created a package with name event.handlers. After which I created multiple classes inside this package.
There are no classes inside event everything is inside event.handlers.
Now I want to create a java class inside package event.
Is there any way from the IDE I can do that?
Currently, I am manually creating the java file inside the event folder in my code repository.
In the left Project structure pane, there is Settings button, in that please Uncheck, Compact Empty Middle Packages.
The settings will be like
.
Now you will have a tree structure, where you can right-click or (ctrl + enter/ alt + insert) on the package and create the file.
For example,
Try creating a Java Class at src folder named event.NewClass. This will create a NewClass.java inside src/event.
You can create folder with the dot symbol . at creating a Java Class or a Package, or Kotlin File/Class when Type is not File. For example create a Package with name com.company.example at src folder will automatically generate the folders recursively, src/com/company/example, create a Java Class with name com.company.example.AClass will automatically generate a file AClass.java inside the automatically generated folder tree src/com/company/example.
It might be easier to just create the class inside package AB and Refactor > Move to place it inside of A.
It seems that all other answers here are workarounds anyway, so if it's just a one-off, this might be the easiest option.
However, in my case, after doing this I had to "Invalidate Caches and Restart" before IDEA updated the UI properly.

Intellij IDEA plugin development. Action "create kotlin class"

I want to create a plugin for Intellij IDEA. I need to add an action (AnAction) that will create a class in Kotlin (not Java) in a custom package. I have two questions:
How to create files with the desired extension?
Or how to create a file with Kotlin class(from some base class) in a custom package?
One possible way to accomplish this is to use PsiFileFactory.createFileFromText() to create an in-memory Kotlin file and then to pass that file as a parameter to PsiDirectory.add() to save it to the filesystem.
Although yole's answer is correct, I would like to see more details about mistery PsiDirectory class.
//get directory by VirtualFile
PsiDirectory directory = PsiManager.getInstance(project).findDirectory((getKotlinSourceDir(project))
Get kotlin source dir:
private VirtualFile getKotlinSourceDir(Project project) {
return project.getBaseDir().findChild("src").findChild("main").findChild("kotlin");
}
And than you can create subdirectories:
//someDirectoryName it is simple name
//(i.e if you post "com.some.package" - it does not create com/some/package folder)
PsiDirectory newDirectory = psiDirectory.createSubdirectory(someDirectoryName);

Xtend in IntelliJ IDEA not generate file in src-gen

I combine Xtext and Xtend. In project, where I write code for my DSL, Xtext work but XTend not generate file in src-gen (this is sources root on: <project-root>/src-gen/).
I have this code in function doGenerate
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
fsa.generateFile("a.txt",'a')
}
Src-gen is always empty. In eclipse this work. How can I generate file in IntelliJ IDEA or how can I fix this problem. I run in gradle.
I run gradle task runIdea. I create Java project without any SDK.
When project is created, I add "file.mydsl" in src folder. Then I need change facet settings. I open project structure dialog (Ctrl+Shift+Alt+S), in the left-hand panel, click Facets, choose + icon and add DSL facet. Then apply and code is work. In src-gen create a file "a.txt".