IntelliJ IDEA doesn't like a HashMap as delegate - intellij-idea

Let's consider the following code:
class MyClass { //Here:IJ says method putAll not implemented
#Delegate
HashMap<String, Integer> map = new HashMap<String, Integer>()
}
hello = new MyClass()
hello.put("x", 1)
println "x=" + hello.get("x")
It works fine when called with Groovy directly on the command line, but IntelliJ IDEA complains about the first line with the following error message:
Method putAll() is not implemented
Any idea why and how to fix it?

I have experienced the same behavior.
What I found was in my IntelliJ project, the class (MyClass) was showing with the .groovy file extension in the Project view. By doing a refactor-->Rename and removing the .groovy extension, the class would then show without the extension in the Project view and the issue was resolved.
The explanation for the issue came when I tried to run the class, which is really a Groovy Script and not just a plain Class. When it compiles, it would encounter an error:
Invalid duplicate class definition, which gave me a hint that I had created what IntelliJ saw as a Class definition, but was really a Groovy script, so IntelliJ, behind the scenes processed the script and created a second, synthetic class for the script.

Related

How can I navigate to Lombok generated implementations

I'm wondering if it would be possible to navigate directly to the methods generated by Lombok using Intellij IDEA.
For instance, for this given example:
#Builder
public class AClass {
private String body;
}
trying to go to the implementation of AClass#builder in an instruction like AClass.builder().build() results in Intellij navigating to AClass, instead of taking me to the real compiled method, which is generated under target directory
Please feel free to create an issue here: https://youtrack.jetbrains.com/issues/IDEA
Currently you can't change the behavior.

Intelllij IDEA Ultimate 2018.1 groovy code completion does not work

I can create a Groovy project and even run it. But when I type main or print there are no auto-complete / code completion suggestions at all. I have groovy-2.4.1 in the external library folder along with Java 1.8. I find Intellij frustrating and non-intuitive to use.
In a groovy script main does show correct code completion but when I click enter it puts "main()" without the String[] args that I can see in the suggestion.
You have created a Groovy class where code has to be embedded into a class to be executable and where only class member declarations are expected (e.g. functions, fields). Select to create a Groovy script (or just delete class declaration) to be able to write executable code in top level file. See also Scripts versus classes.
I was having the same problem when I was in a rush and forgot to add a method in which to write the code. Just type 'psvm + Enter' inside the class. This will create a main method in which code completion will work. Also, you can remove the public keyword as classes and methods in Groovy are by default public.

Geb/Groovy : Page object class considered as a property in another when refering to with "to" option

I am new to Groovy programing and I am currently trying to test a web app with Geb and Spock on IntelliJ IDE. I wanted to try a simple script first without using Spock, that is why I use a simple main class for creating Browser and so on and run some basic tests (understand: verifying if I am at the correct page).
Everything was working well but I had a lot of page class that were melt in the same folder as my main class, modules and so on. So I decided to clean up by using packages. Here is my project folders:
src
---main
------groovy
------------app
---------------module
---------------pages
---------------templates
module folder contains the modules used in my pages
pages folder contains the actual page the browser will browse
templates folder contains some super pages class for not repeating content through pages instances.
My class Main with main method is in app folder.
So I re-run the code that was previously working well (when every source files were in the same folder) and I get an error Exception in thread "main" groovy.lang.MissingPropertyException: No such property: homePage for class: app.pages.loginPage
The line that seems to be the problem is this one (in loginPage.groovy) :
loginButton(to: homePage){$("input", id: "loginButton_submit")}
Which is in the static content of loginPage class.
I don't understand why I get this error as loginPage and homePage are in the same package. I guess I don't understand some groovy stuff here or compiling mechanics.
Here is the error messages I got :
The package is the correct one both in homePage and loginPage (they are in the same one) so the class seems to be resolved. But when running, `homePage` is considered as a static property of `loginPage`I suppose and as it is not declared in `loginPage` properties it cannot work. Here is my log :
Exception in thread "main" groovy.lang.MissingPropertyException: No such property: homePage for class: app.pages.loginPage
at groovy.lang.MetaClassImpl.invokeStaticMissingProperty(MetaClassImpl.java:1004)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1859)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1835)
at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3735)
at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:175)
at groovy.lang.Closure.getPropertyTryThese(Closure.java:312)
at groovy.lang.Closure.getPropertyOwnerFirst(Closure.java:306)
at groovy.lang.Closure.getProperty(Closure.java:295)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:50)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
at app.pages.loginPage$__clinit__closure4.doCall(loginPage.groovy:28)...
Do you have any ideas ?
Check to see if the package your homePage belongs to was updated when you moved it from "main" to "pages":
package app.pages
class homePage extends Page {
}
If the package is incorrect or undeclared on the homePage class your loginPage will not be able to resolve it
Finally solved it thanks to this comment:
Note that by convention class names in Java and Groovy usually start with a capital letter.
All my classes where considered as variable. I still don't get why the behavior is different in default package so if anyone has a clue...
Thanks all for your help.

How to select a different module to run when you click the Run button in IntelliJ IDEA?

In my IntelliJ IDEA project, I have 3 modules written in Kotlin:
An HTTP Servlet one.
A desktop swing application; and
A library that contains contracts that the above two listed projects share to talk to each other.
When I click the Run button, it starts the Tomcat server and loads up my servlet project. That is because, and I am guessing here, the new project creation template inside of the IDE created a new Run Configuration for the entire project and it is defined in this run configuration that it must start the module that has the servlet inside it.
Now that the servlet runs fine, I'd like to also run the desktop application written using Swing.
How do I do that? I've done this once before but I have forgotten how I did it.
Do I have to define a new Run Configuation? I tried that this way:
I selected Kotlin from the left pane titled Add New Configuration and specified the name of the class that had the main function, and also the name of the module that had this class.
Here is the source code of my main class.
package bookyard.client;
import javax.swing.SwingUtilities;
public class Program {
public fun main(args : Array<String>) {
SwingUtilities.invokeLater(LoginDialogEventLoop());
}
}
But when I click the Run button after choosing that run configuration's name, the process reports an error that suggests that the class name I specified as having the main function actually does not have the main function, which I am not sure why that is.
The main method needs to be static, and the method you have declared is not. In Kotlin, you can either declare main as a top-level function (outside of a class), or, if you want to keep it inside the class, use the following syntax:
class Program {
companion object {
#JvmStatic fun main(args: Array<String) { ... }
}
}

Xcode pluginDidLoad not getting called when adding objective-c file in swift plugin

I'm working on a plugin for Xcode. It is supposed to be written in Swift.
When I start with a fresh plugin project (I'm using this Xcode Plugin template which is also available via Alcatraz) the project compiles and runs fine.
The pluginDidLoad method is getting called right after Xcode starts. As soon as I add any Objective-C file (and a bridging header of course) the pluginDidLoad method is not getting called anymore.
The Objective-C file might be as simple as an empty class that is a subclass of NSObject.
Removing the target-memberbership (for the plugin-target) from the newly created Objective-C (.m) file the aforementioned mentioned method is getting called again.
Has anyone developed a Xcode plugin in Swift that also uses Objective-C files before and got this working?
Update
It seems that my original solution only works with Swift only projects because Xcode always takes the objective c class if you have one.
So here is another trick: Extend the NSObject class by the function class func pluginDidLoad(bundle: NSBundle) {} and initialize your plugin there. Then it doesn't matter on which class it is called. You might have to check that also all Swift classes subclass NSObject. I pushed it to my repository that you can have a look
Original Post
I think I could reproduce the problem now. To simplify the problem, let's say that we have only two swift classes PluginMain and PluginHelper.
As you said, sometimes the plugin isn't getting called for some mysterious reason. I was struggling with the problem again and I was wondering how Xcode knows which class is the main class. So I came up with the idea to put the following initializer in both classes PluginMain and PluginHelper
class func pluginDidLoad(bundle: NSBundle) {
let appName = NSBundle.mainBundle().infoDictionary?["CFBundleName"] as? NSString
if appName == "Xcode" {
//sharedPlugin = SwiftySafe(bundle: bundle)
//initialize your shared plugin
}
}
By putting a breakpoint or log message in pluginDidLoad in both classes, I notices that Xcode isn't ignoring the plugin, it is just loading the wrong class (e.g. PluginHelper instead of PluginMain).
The Solution
It turns out that Xcode uses the class that is compiled first as the main class and calls pluginDidLoad only on that. So you can change that by reordering the "Compiled Sources" under your target settings->Build Phases. Move your main class so that it is on top. In the following image you find an example from my project. SwiftySafe is my main class.
My example
You will find my project here https://github.com/creinders/SwiftySafe if you want to compare the settings.
The pluginDidLoad method is called on the principal class. When the principal class is a Swift class, you have to include the module name in the NSPrincipalClass Info.plist key.
So if your target name is MyPlugin and your principal class is MyClass, set NSPrincipalClass to MyPlugin.MyClass.
Also make sure that MyClass inherits from NSObject.
Sometimes you need to tell Xcode to reload bundle. Run this and restart Xcode
defaults delete com.apple.dt.Xcode DVTPlugInManagerNonApplePlugIns-Xcode-7.3