I'm trying to use an overloaded method to append XML in a controller in Grails 2.3.4.
I have the following overloaded methods in my ReportController.
String makePhotoXml(StringBuilder sb, Report r, String url, String desc) {
sb.append("<photo>")
sb.append(Utilities.makeElementCdata("url", url))
sb.append(Utilities.makeElementCdata("caseId", r.caseId))
sb.append(Utilities.makeElementCdata("type", r.type))
sb.append(Utilities.makeElementCdata("date", r.dateCreated.format('MM/dd/yy')))
sb.append(Utilities.makeElementCdata("address", r.address))
sb.append("<extra>extra</extra>")
sb.append(Utilities.makeElementCdata("description", desc))
sb.append("</photo>")
}
String makePhotoXml(List<Report> reports) {
StringBuilder sb = new StringBuilder()
sb.append("<photos>")
sb.append("<title>Photos</title>")
for (Report r : reports) {
for (Photo photo : r.photos) {
makePhotoXml(sb, r, photo.url(), photo.description)
}
for (Document doc : r.photoDocuments) {
makePhotoXml(sb, r, doc.url(-1), doc.getDescription())
}
}
sb.append("</photos>")
}
When running the application I get this compiler error:
| Error Compilation error: startup failed:
/Users/Anthony/GrailsApps/AppOrderWeb/grails-app/controllers/com/apporder/ReportController.groovy: 1360: Controller actions may not be overloaded. The [makePhotoXml] action has been overloaded in [com.apporder.ReportController]. # line 1360, column 5.
String makePhotoXml(StringBuilder sb, Report r, String url, String desc) {
I thought that Groovy and Grails supported method overloading. Any ideas on how to work around this and make this overloaded method work?
Groovy in general does allow method overloading, but Grails forbids it for the specific case of controller actions. If you want utility methods in your controller, you need to make them private or protected so Grails does not try and treat them as web-visible actions.
Alternatively, it would be more Grails-y to move the helper methods into a service instead of having them in the controller.
Related
I'm reasonably proficient with Groovy insofar as my job requires, but not having a background in OOP means that some things still elude me, so apologies if some of the wording is a little off here (feel free to edit if you can make the question clearer).
I'm trying to create an overloaded method where the signature (ideally) differs only in the return type of the single Closure parameter. The Closure contains a method call that returns either an ItemResponse or ListResponse object, both of which could contain an object/objects of any type (which is the type I would like to infer).
The following code is a simplified version of what I'm trying to implement - an error handling method which takes a reference to a service call, safely attempts to resolve it, and returns the item/items from the response as appropriate:
public <T> T testMethod(Closure<ItemResponse<T>> testCall) {
testCall.call().item as T
}
public <T> List<T> testMethod(Closure<ListResponse<T>> testCall) {
testCall.call().items as T
}
Obviously this doesn't work, but is there any alternate approach/workaround that would achieve the desired outcome?
I'm trying to create an overloaded method where the signature
(ideally) differs only in the return type of the single Closure
parameter.
You cannot do that because the return type is not part of the method signature. For example, the following is not valid:
class Demo {
int doit() {}
String doit() {}
}
As mentioned by yourself and #jeffscottbrown, you can't have two methods with the same parameters but different return value. The workaround I can see here is to use a call-back closure. The return value of your testMethod would default to Object and you would provide an "unwrapper" that would the bit after the closure call (extract item or items). Try this out in your GroovyConsole:
class ValueHolder <T> {
T value
}
Closure<List<Integer>> c = {
[1]
}
Closure<ValueHolder<String>> d = {
new ValueHolder(value:'hello world')
}
Closure liu = {List l ->
l.first()
}
Closure vhsu = {ValueHolder vh ->
vh.value
}
// this is the generic method
public <T> Object testMethod(Closure<T> testCall, Closure<T> unwrapper) {
unwrapper(testCall.call()) as T
}
println testMethod(c, liu)
println testMethod(d, vhsu)
It works with both a list or a value holder.
I'm starting with Grails (using mainly the Eclipse plugin) and have been having trouble with Grails reading Java src files - whether it's a jar in the bin folder, or a Java file in the src/java folder.
I've created an example of how I call from my controller, a Java static method, and for some reason there is a problem with the argument type. Here's the controller:
def attempt = {
int counter = 20
int val1 = 1
int val2 = 1
def list = Replicate.create(counter, val1, val2)
render list.size()
}
And here is the Java file in the src/Java folder:
public class Replicate {
public static LinkedList<Pair<Integer, Integer>> create (int count, int val1, int val2){
LinkedList<Pair<Integer, Integer>> list = new LinkedList<Pair<Integer,Integer>>();
for (int i = 0; i < count; i++){
list.add(new Pair<Integer, Integer>(val1, val2));
}
return list;
}
}
When I try and load the page for the Controller's method, I get:
URI:
/clive-toolbox/cliveServices/attempt
Class:
groovy.lang.MissingMethodException
Message:
No signature of method: static tst.Replicate.create() is applicable for argument types: (java.lang.Integer, java.lang.Integer, java.lang.Integer) values: [20, 1, 1] Possible solutions: grep(), iterator()
The line that is highlighted as the error is this (in the controller):
def list = Replicate.create(counter, val1, val2)
And in other cases, working the same way, Grails manages to compile the project but just doesn't execute the method. I assume I'm doing something wrong. Any ideas here?
Your exception correctly states that the method Replicate.create() is not available to the controller.
Check the following,
1. You have imported the class right.
2. Package name is correct.
3. You have cleaned your application.
Try all the three mentioned above, it might help if the method is working on other places.
I received the same error when trying to call a service method from a controller.
What I did wrong was that I had not typed: "servicename" at the top of the method in the controller where I called the service from.
I have been trying to use a path variable in grails controller but I am not able to achieve it.
The intention behind is to validate the parameter submitted to the url which I need to make mandatory. I could not achieve it through RequestParam so I switched to PathVariable so that the url submitted without the required param should be filtered off by grails controller itself rather than me adding if/else checks for validity.
So, I can illustrate as below:
My URL is something as below:-
'<appcontext>/<controller>/<action>?<paramName>=<something>'
Now, to make 'paramName' mandatory I am not finding any way in Grails(Spring MVC provides #RequestParam annotation which can enable me for 'required' as true).
Another alternative I thought was to use path variables so that 'paramName' can be included in URL itself. So I tried like following:
'<appcontext>/<controller>/<action>/$paramName'
For validating the above URL I wrote specific mapping but some how it does not work too..
Following is the specific mapping I wrote:-
"/<controllerName>/<action>/$paramName" {
controller:<controller to take request>
action:<action to do task>
constraints {
paramName(nullable: false,empty:false, blank: false)
}
}
I tried to use spring annotation like #PathVariable and #RequestParam in controller as given below:-
def action(#PathVariable("paramName") String param){
//code goes here
}
If you name the method argument the same as the request parameter rename, Grails will take care of it for you...
// In UrlMappings.groovy
"/foo/$someVariable/$someOtherVariable" {
controller = 'demo'
action = 'magic'
}
Then in your controller:
// grails-app/controllers/com/demo/DemoController.groovy
class DemoController {
def magic(String someOtherVariable, String someVariable) {
// a request to /foo/jeff/brown will result in
// this action being invoked, someOtherVariable will be
// "brown" and someVariable will be "jeff"
}
}
I hope that helps.
EDIT:
Another option...
If for some reason you want different names for the method arguments you can explicitly map a method argument to a request parameter like this...
import grails.web.RequestParameter
class DemoController {
def magic(#RequestParameter('someVariable') String s1,
#RequestParameter('someOtherVariable') String s2) {
// a request to /foo/jeff/brown will result in
// this action being invoked, s2 will be
// "brown" and s1 will be "jeff"
}
}
I need a list of all methods and their arguments at run time from all controllers in any project.
I have not find a way or an example of retrieving the arguments. For example in the method:
def login(String username, String password) {
...
}
I need the arguments username and password with their type.
Many thanks for your help.
During compilation, an AST transformation adds an empty method for action methods with arguments. This is annotated with the grails.web.Action annotation which has a commandObjects attribute containing a Class[] array of the classes of command objects and regular method argument types.
So you can loop through all of the controllers in the application, and find all annotated methods:
import grails.web.Action
for (cc in grailsApplication.controllerClasses) {
for (m in cc.clazz.methods) {
def ann = m.getAnnotation(Action)
if (ann) {
String controller = cc.logicalPropertyName
String action = m.name
Class[] argTypes = ann.commandObjects()
println "${controller}.$action(${argTypes*.name.join(', ')})"
}
}
}
I am building a framework that I don't want to couple to a particular IOC container so have created a layer on top of Ninject / structuremap etc.
I have a binding class that accepts a Func to allow binding to a method.
For example
public class Binding
{
public Type Source { get; set; }
public Func<object> Method {get; set; }
public Scope { get; set; }
}
If I have a binding like...
var binding = new Binding() {
Source = typeof(IRepository),
Method = () => new Repository(new LinqToSqlRepository(connectionString)),
Scope = Scope.HttpRequest
};
The framework wrapping Ninject creates Ninject bindings for my generic binding like this
Module :NinjectModule
{
IList<Binding> _Bindings;
public Module(IList<Binding> bindings)
{
_Bindings = bindings;
}
public override void Load() {
foreach (var binding in _Bindings) {
switch(binding.Scope) {
case IocScope.HttpRequest:
Bind(binding.Source).ToMethod(c => binding.Method()).InRequestScope();
break;
// ... omitted for brevity
}
}
}
}
This works fine when there is only one binding being bound to a method. When there are multiple bindings being bound within the same module to methods however the incorrect type is returned. From debugging, it looks as if the last binding is always used.
Thus the problem with an example;
var binding1 = new Binding() {
Source = typeof(IRepository),
Method = () => new Repository(new LinqToSqlRepository(connectionString)),
Scope = Scope.HttpRequest
};
var binding2 = new Binding() {
Source = typeof(ICalendar),
Method = () => new MvcCalendar( ..... )
Scope = Scope.HttpRequest
};
At runtime when Ninject is requested to new up an MVC Controller which takes in an IRepository and an ICalendar, I receive a type conversion error saying that a MvcCalendar cannot be converted to an IRepository. I have discovered that for some reason the last binding is always being returned for the first requested type.
This is a highly simplified version of what is really going on to try and highlight the actual issue, the wrong method being bound to a requested type when there are multiple method bindings. I hope this still explains the issue though.
This appears to be related to some sort of closure scoping issue. I also wonder whether Ninject is getting is getting confused by the Func instead of Func usage.
Unit Test Example
Here is a test module I load into my custom IOC container. This does not depend on any particular IOC framework. When I instantiate a NinjectIocContainer to handle the DI, the internal binding of this in Ninject occurs as example further up (see NinjectModule)
public class MultipleMethodBoundTypesModule : IocModule
{
public override void Load()
{
Bind<IPerson>().To(() => new Person()).In(IocScope.Transient);
Bind<IRobot>().To(() => new Robot(new Person())).In(IocScope.Transient);
}
}
Here is a simple test that tries to retrieve each of the types.
[Test]
public void Expect_That_Multiple_Method_Bound_Types_Can_Exist_Within_The_Same_Module()
{
// arrange
var container = Get_Container_With_Module(new MultipleMethodBoundTypesModule());
// act
var person = container.Get<IPerson>();
var robot = container.Get<IRobot>();
// assert
Assert.IsNotNull(person);
Assert.IsNotNull(robot);
}
As explained eariler, this throws a type conversion where the last closure (for the robot) is being bound to a person.
TestCase 'Ioc.Test.NinjectContainerTest.Expect_That_Multiple_Method_Bound_Types_Can_Exist_Within_The_Same_Module'
failed: System.InvalidCastException : Unable to cast object of type 'Ioc.Test.Robot' to type 'Ioc.Test.IPerson'.
at System.Linq.Enumerable.d__b11.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable1 source)
at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters)
NinjectIocContainer.cs(40,0): at Ioc.Ninject.NinjectIocContainer.GetTInstance
IocTestBase.cs(149,0): at Ioc.Test.IocTestBase.Expect_That_Multiple_Method_Bound_Types_Can_Exist_Within_The_Same_Module()
In the snippet:
Bind(binding.Source).ToMethod(binding.Method()).InRequestScope();
You're dereferencing the Method bit. You want to be doing that as either binding.Method or ()=>binding.Method() (the former may not unambiguously be inferrable based on the C# type inference rules).
You mentioned this is heavily stripped down from your real code. As a result, this may not be the actual issue. I'd still be betting on some form of closure confusion though (see the section Comparing capture strategies: complexity vs power in this CSID excerpt for a nice walkthrough).
You also probably meant to use .InScope(binding.Scope) rather than .InRequestScope() too,.