I have a very simple function which I define as follows:
def mySimpleFunction(Map myMap) {
// Function logic here...
}
However, when I try to compile this, I get a warning message and build exception which says that: The [mySimpleFunction] action accepts a parameter of type [java.util.Map] which has not been marked with Validateable.
How can I mark this function as Validateable? I imported the org.codehaus.groovy.grails.validation.Validateable
and have marked my class as #Validateable .
What should I be doing differently in order to get my application to build?
Thank you in advance!
If it is a helper method, make it private. In Grails 2.0+ public controller methods are assumed to be actions, and arguments are assumed to be bindable. That means they need to be number types, boolean, String, etc., or a command object class.
Command object classes are automatically made validateable if they're defined in the controller class file, and if they're defined elsewhere they need to be annotated as #Validateable.
Since this is a helper method and not an action, just make it private (especially since it can't be called from another class anyway):
private mySimpleFunction(Map myMap) {
// Function logic here...
}
Related
I have a variable handler whose value can be flight_gds, flight_commission etc.
There are classes in my application which has names like FlightGds, FlightCommission etc. under module FlightManager.
I want to execute the function of class based on the value of the handler.
The function name is same in all classes, only the class name is different which is dependent on
the handler.
For example:
If handler name is 'flight_gds', then the function is FlightManager::FlightGds.calculate()
I am trying something like this:
FlightManager::handler.camelize.calculate()
But I am getting undefined method handler error.
Is there any way to do this or I should go with if/else loop?
You should use constantize to get a class based on it's name in string:
"FlightManager::#{handler.camelize}".constantize.calculate())
I'm trying to annotate fields over my domain classes but I'm not able to get them at runtime by reflection as usual Java fields.
My annotated domain class looks like:
class MyDomainClass {
#MyAnnotation
String myField
}
The Java way to access myField by reflection doesn't work:
MyDomainClass.class.getField("myField") //throws java.lang.NoSuchFieldException
The Grails way to inspect domain classes doesn't expose field annotations:
new DefaultGrailsDomainClass(MyDomainClass).getPersistentProperty("myField").??? //there is nothing similar to getAnnotations()
How can I retrieve the annotations associated with a domain field?
That's not a field, it's a property. When you declare a class-scope variable like that with no scope modifier (public, protected, etc.) in a Groovy class, the Groovy compiler converts it to a private field with the same type and name, and a getter and setter method, essentially
class MyDomainClass {
#MyAnnotation
private String myField
public String getMyField() {
return myField
}
public void setMyField(String s) {
myField = s
}
}
It won't overwrite an existing getter or setter though.
When you access the property you end up calling the getter or setter, which you can see if you add those methods and include println statements. This way Java classes can access the property via those methods, and Groovy pretends that you're directly reading or writing a field but under the hood you end up calling the same methods that you would from Java.
One benefit of this is that if you decide later that you want to add some logic when setting or getting the value, you can define the corresponding method(s) and add what you want without needing to change the code that accesses the property since in Groovy you can still treat it like a field and your custom methods will be called, and Java classes will have been calling the setter and getter all along.
getFields() returns only public fields, but getDeclaredFields() returns all of them, so since the actual field is private that's why getDeclaredFields() (or the property access form declaredFields) is necessary.
Grails doesn't use annotations because Graeme (and others) feel that they're ugly and bulky, not because they're not accessible.
Why "this" behaves differently when used inside a class function/method as compared to when it is used inside an anonymous function.
For example
public MyClass
{
function myfun()
{
output(this) // << will show the instance of this class but not myfun() function
abc = function ()
{
output ( this ) // << will show abc function
}
abc()
}
}
So, why "this" outputs the instance of MyClass but doesnot output myfun() . What makes it different from anonymous functions ?
While the specific will vary between languages, the general idea behind an anonymous inline method such as that is that the compiler will create a new class (possibly given some random name, just for it's own use), inside that class will be a method (again, probably given some auto-generated name for it's own use) and that method will do the work of the anonymous method. Then in the original call site of the anonymous method it creates an instance of this compiler generated class and calls the appropriate method within that class.
As such, the definition of that anonymous method is going to actually be called from within another type, not from within the type that declared the anonymous method.
public function setAlbumTable(AlbumTable $albumTable)
{
$this->albumTable = $albumTable;
return $this;
}
I am talking about first parameter ( it's not parameter btw) looks like datatype ? what is it ? constant ? I encounter this when trying to develop app in zend framework 2
This is PHP's type hinting. It means that the first parameter to this function - $albumTable - must be an instance of the AlbumTable class or a class that inherits from it.
class Car {}
class BlueCar extends Car {}
function drive_the_car(Car $car) {}
drive_the_car(42); // error, first parameter must be an instance of Car
drive_the_car(new stdClass()); // error
drive_the_car(new Car()); // works
drive_the_car(new BlueCar()); // works
The piece of code you're showing is an example of dependency injection via setter method. The setter is passed an instance of AlbumTable and assignes this instance to a class field.
Your method is passed ONLY ONE parameter: $albumTable.
The AlbumTable before the parameter is a type hint and makes sure that only a instance of AlbumTable or a deriving class can be passed to the setter.
It forces the actual parameter ($albumTable) to be an instance of AlbumTable class. PHP will give a fatal error if anything else is passed to the function.
This is useful so you don't have to check what type of variable/object you received in order to make use of it's functions and properties.
How to declare a method inside a closure.Or which is better to use method or closure.
I have a closure and in that closure i have a method to call and i defined method as
def getBindedGenes(Long colId) {
........
}
But when i used codenarc plugin for code review it is showing the rule as GrailsPublicControllerMethod and the message as The Grails controller has a public method getBindedGenes. This should be a closure property or moved
What is the cause and what is happening exactly.
Thanks in advance
I think CodeNarc is warning you that your controller actions must be public closures, not public methods. Given that you can't use a public controller method as an action, there's probably no good reason to have one.
Grails 2.0 Update
Since Grails 2.0, public methods of controllers can be used as actions, and if fact, it is now recommended to use methods instead of closures.