Customize hashCode() and equals()?

Does anyone know of a way to customize the hashCode() and equals() methods on the Generate menu? Our standard is to use the commons-lang HashCodeBuilder and EqualsBuilder but Intellij's implementation is slightly different.

Alternatively I could write a plugin but am having trouble finding out how to get the member variables from a class. Anyone know how to do that either?

Thanks.

6 comments
Comment actions Permalink

Hello Max E.,

Does anyone know of a way to customize the hashCode() and equals()
methods on the Generate menu? Our standard is to use the commons-lang
HashCodeBuilder and EqualsBuilder but Intellij's implementation is
slightly different.


No, the equals() and hashCode() methods are currently not customizable.

Alternatively I could write a plugin but am having trouble finding out
how to get the member variables from a class. Anyone know how to do
that either?


PsiClass.getFields()

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thanks Dmitry - that's exactly what I need. Following the tutorial on how to make a plugin, how do I get to the PsiClass interface for the current file within my actionPerformed method? I can't seem to find the link. Is ApplicationManager.getApplication on the right track?

Also, when I go to test the plugin a new instance of IDEA is launched but it always hangs when I try to create a new project so I can have a class to test this on. Broad question - but is there any other way to test a plugin?

Thanks again.

0
Comment actions Permalink

Hello Max E.,

Thanks Dmitry - that's exactly what I need. Following the tutorial on
how to make a plugin, how do I get to the PsiClass interface for the
current file within my actionPerformed method? I can't seem to find
the link. Is ApplicationManager.getApplication on the right track?


As described in the tutorials, any data that you need to access from your
action methods is available though AnActionEvent.getData() method. In particular,
to get the PsiClass, you need to do the following:

PsiElement element = e.getData(DataKeys.PSI_ELEMENT);
PsiClass psiClass = element != null ? PsiTreeUtil.getParentOfType(element,
PsiClass.class) : null;

Also, when I go to test the plugin a new instance of IDEA is launched
but it always hangs when I try to create a new project so I can have a
class to test this on. Broad question - but is there any other way to
test a plugin?


It's definitely not supposed to hang. Are there any exceptions thrown from
the new instance? What have you specified as the sandbox directory in the
IDEA SDK configuration?

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Hi Dmitry,

Thank you again. The hanging problem was caused by OutOfMemoryErrors. I bumped my max up to 1024m in my idea.vmoptions file but it would just keep sucking up memory until it ran out. Where it ran out varied but I could never get to the point where I could test the plugin. I got around it by installing IntelliJ on Windows (was running on Linux) and now its works fine. Let me know if you want any more info about that one but it's no longer blocking me.

Regarding the code you sent, the element variable is null when I run the plugin from a simple little class so I still can't get the PsiFields. Here is the code;

public class HashEqualsStringBuilder extends AnAction {
public void actionPerformed(AnActionEvent e) {
PsiElement element = e.getData(DataKeys.PSI_ELEMENT);
if (element != null)
Messages.showInfoMessage("Got Element=" + element.getText(), "Element");

PsiClass psiClass = element != null ? PsiTreeUtil.getParentOfType(element, PsiClass.class) : null;

StringBuilder buff = new StringBuilder();
if (psiClass != null) {
PsiField[] psiFields = psiClass.getFields();
for (PsiField psiField : psiFields) {
if (buff.length() > 0) buff.append(", ");
buff.append("Field=").append(psiField.getName());
}
}

Messages.showInfoMessage(buff.toString(), "PsiFields"); }
}


Thanks once again for your help.

0
Comment actions Permalink

Hello Max E.,

How exactly do you run the plugin? Where's the focus when you invoke this
action?

Regarding the code you sent, the element variable is null when I run
the plugin from a simple little class so I still can't get the
PsiFields. Here is the code;

public class HashEqualsStringBuilder extends AnAction {
public void actionPerformed(AnActionEvent e) {
PsiElement element = e.getData(DataKeys.PSI_ELEMENT);
if (element != null)
Messages.showInfoMessage("Got Element=" +
element.getText(), "Element");
PsiClass psiClass = element != null ?
PsiTreeUtil.getParentOfType(element, PsiClass.class) : null;

StringBuilder buff = new StringBuilder();
if (psiClass != null) {
PsiField[] psiFields = psiClass.getFields();
for (PsiField psiField : psiFields) {
if (buff.length() > 0) buff.append(", ");
buff.append("Field=").append(psiField.getName());
}
}
Messages.showInfoMessage(buff.toString(), "PsiFields"); } }

Thanks once again for your help.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

I am editing a class and have linked the action to the Generate (shortcut Alt + Ins). Focus is inside the class which just has two variables like so

public class Foobar {
public String foo;
public String bar;
}

When I invoke the plugin, given the code above I'd expect my popup to say "foo, bar";

Thanks, Max.

0

Please sign in to leave a comment.