Showing posts with label Grails. Show all posts
Showing posts with label Grails. Show all posts

Monday, February 6, 2012

Performance : Parameterized Constructor in Grails for Domain Objects

Don't use parametereized constructor in Grails for domain objects. This is slower than empty constructor.

This performance issue become significant when creating large number of domain objects in loop.

Wednesday, February 1, 2012

Overriding equals in Groovy

I was overriding equals method in for a Groovy class and got into StackOverflow error.




class ObjKey {
long id
String value

public boolean equals(Object obj) {
if (!(obj instanceof ObjKey)) {
return false;
}
if (this == obj) {
return true;
}
ObjKey rhs = (ObjKey) obj;
return new EqualsBuilder()
.append(id, rhs?.id)
.append(value, rhs?.value)
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(id)
.append(value)
.toHashCode();
}
}



I was getting stackoverflow at line: if (this == obj). Groovy's equals calls the equals method instead of comparing the reference.

I fixed it using bellow syntax: if (this.is(obj)). The corrected code looks like:


class ObjKey {
long id
String value

public boolean equals(Object obj) {
if (!(obj instanceof ObjKey)) {
return false;
}
if (this.is(obj)) {
return true;
}
ObjKey rhs = (ObjKey) obj;
return new EqualsBuilder()
.append(id, rhs?.id)
.append(value, rhs?.value)
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(id)
.append(value)
.toHashCode();
}
}

Wednesday, November 10, 2010

log.debug not working in grails unit test

Use the following method call to enable debug level messages to be printed.

mockLogging(grails_artifact_class, true)

where, grails_artifact_class is domain/controller/service class.

Friday, November 5, 2010

Exception while starting grails from STS

Problem: Getting java.lang.ClassNotFoundException: sun.tools.native2ascii.Main while starting grails application from STS.

Solution: Go to build path. Change your default system JRE to JDK. You are good!