I was wondering what is the best way of finding out what dynamic methods are added to certain variables. I don't see it documented in the documentation or the javadoc of the controller class.
An example is the params object in a grails controller:
params.int('max')
The int methods is injected and I'd like to know what extra methods are available for this particular map.
I got my copy of The Definitive Guide to Grails 2 several days ago and as it is stated here, there are 8 converting methods for primitive types:
... Grails provides convenience methods for doing this type of
conversion on request parameters. Those methods are available on the
params object and have names that correspond to all eight of the
primitive types defined by Java (Boolean, byte, char, short,
int, long, float and double). The methods each accept one or
two arguments. The first argument is the name of the request parameter
that is to be converted, and the optional second argument is a default
value that will be returned if a corresponding request parameter
cannot be found or if an error occurs during the conversion ...
And there is also mentioned a converting method named list:
... Another type of converting method is named list. The list method is useful
when dealing with multiple request parameters of the same name ...
Brown, J. S., Rocher, G., (2013). The Definitive Guide to Grails 2. New York: Apress.
ISBN:978-1-4302-4377-9
Amazon
I hope it's not against any copyright to cite from it...
And yes, I consider books a great source for questions that go a little further and I definitely would recommend The Definitive Guide to Grails 2 to you or any other programmer who wants to gain a deeper understanding on Grails. Also, the Grails Mailing Lists and the Groovy Zone over at dzone.com are good resources for any Grails related questions and/or news. The Groovy JDK documentation is also of great help, as #doelleri mentioned in his answer.
Unfortunately, those methods are poorly documented. The best documentation there is on them is in the Simple Type Converters section of the docs. They are also briefly mentioned in the Release Notes for Grails 1.2, which is perhaps even more unhelpful.
The Groovy JDK is also a good resource to see what methods Groovy adds to the standard Java classes.
Related
I'm having problems using any types created in an assembly for an F# Generative Type Provider. I created a YouTube video that demonstrates this.
The error messages I get are:
The module/namespace 'tutorial' from compilation unit 'Addressbook1' did not contain the namespace, module or type 'Person'
A reference to the type 'tutorial.Person' in assembly 'Addressbook1' was found, but the type could not be found in that assembly
I don't understand because the type is definitely in the assembly. For troubleshooting this, the assembly is a very basic C# dll. The code in the video is available via git:
git url: https://code.google.com/p/froto/
git branch: help
Any troubleshooting ideas would be appreciated. I'm hoping to make more progress on an F# Type Provider for .proto files, but I'm stuck on this.
I've taken a quick look at your code - as I mentioned in a comment I think you would be much better served by using the ProvidedTypes API that is defined by the F# 3.0 Sample Pack and documented (a bit) on MSDN.
Basically, the raw type provider API has a lot of assumptions baked in which will be hard for you to maintain by hand. I think that the specific problem you have is that the compiler expects to see a type named tutorial.Person in your assembly (since it's the return type of a method on tutorial.AddressbookProto, which you are exposing as a generated type), but it isn't ever embedded into your assembly.
However, this is really only one of several problems - as you've probably realized, your will see additional errors if the type that you're defining is called anything other than tutorial.AddressbookProto. That's because you're using a concrete type as the return from ApplyStaticArguments, but you would typically want to use a synthetic System.Type instance that accurately reflects the namespace and type name that the user used (e.g. in the ProvidedTypes API the ProvidedTypeDefinition class inherits from System.Type and handles this bookkeeping).
I am trying to create a class implementing Blackberry's Comparator so I can easily sort 2D arrays.
import net.rim.device.api.util.Comparator;
class ArrayComparator implements Comparator<Object[]> {
...
}
This gives me the error:
The Type Comparator is not generic; it can not be parameterized with
arguments <Object[]>
This error goes away if I include the normal JRE library and import java.util.Comparator, but this won't compile because it is a mobile device, the library is not preverified, etc, etc, etc.
How can I resolve this issue?
net.rim.device.api.util.Comparator doesn't have Generic capabilities. You need to implement Comparator without any type information and compare all the items of the array. if the items of the array are objets, you need to implements the Comparator interface on this Objets too.
you can get more implementation information on this link
BlackBerry JRE is 1.3 (like CLDC 1.1) and don't support generics, non-synchronized collections and other things from modern java world.
Sicne you're defining a comparator for a particular class you don't need (and can't use) generics. Your array comparator should cast the Object type parameters to their corresponding type before comparing in the compare method you override.
For example, your ArrayComparator should cast o1 and o2 to an array. In the future if you make a, say, UserDataComparator the compare method should cast the parameters to your UserData class type.
FYI: Blackberry's compiler (RAPC) supports up to java 1.4 AFAIK (check this) so Generics, Enums and other Java 1.5 and above features are not supported.
EDIT As other persons pointed (and for the sake of a complete answer) I was not specific enough on my anwer. I have edited this answer to reflect some crucial points related to your issue.
I'm reading Struts 2 In Action, and, on the chapter five, a conversion.properties file is created to demonstrate data transfer for multivalued parameters. It includes this line:
Element_weights=java.lang.Double
And there's a list that does not use generics in the Action class:
List weights;
I replaced all this with just
List<Double> weights;
and the type conversion seemed to work just fine. Are there any drawbacks to using generics, any reason for what the authors of the book are doing?
edit: I kept reading and, in fact, generics work, and the authors even recommend it. Why they haven't used it in the first place still puzzles me, nevertheless.
Those types of conversion entries are only needed in pre-generics environments. Definitely use generics if at all possible.
I am trying to make a taglib to represent an object (to read and display at the UI). When creating an object (save method in the controller), I see the domain class and association are created by the auto assignment of parameter
def Book = new Book(params)
It also maps complex types (for eg: joda time). I wonder about the naming convention necessary to facilitate this mapping. Out of curiosity, can someone also point where in the grails source code I could see how grails handles this mapping. I'm still learning Spring and probably this would be a good exercise.
Thanks,
Babu.
AFAIK the naming conventions are rather straightforward. If there's a field params.foo and the object you are binding to has a field foo, it will bind the value, assuming the type conversion works properly. If there's a params.bar.id set with an Long value and your object has a complex property of type Bar, it will lookup this instance and inject it.
If you need more control over the binding process, you might want to use bindData.
If you are interested into the details of the binding process, have a look at Java's PropertyEditor as this is what is being used in the background. I wrote a blog post on how to create and register PropertyEditors a while ago, maybe it helps you getting started with that stuff.
I'm running Pharo and I'm just in a use case that sort of screams for Dependency Injection à la Guice. Is there something similar for Smalltalk?
I understand that you can sort of do it all by foot, by just passing in your dependencies explicitly. But that feels awkward and verbose to me.
There is a Smalltalk dialect with strong emphasis on dependency injection. It extends the language such that not only method names but also class names use a dynamic lookup. The novel lookup of class names is most similar to that of methods, except that bubbles up through a series of nested classes rather than along an inheritance chain. Thus you can change the injected classes by changing the nesting environment.
To learn more about the dialect, follow this link.
With Guice, it looks like you define your classes to take certain interfaces as constructor parameters. Then you tell Guice "this interface maps to that class implementing said interface".
That sort've thing is completely unnecessary in Smalltalk, because Smalltalk classes only care about protocols.
If we translated the example into Smalltalk, we could pass any object we liked into the RealBillingService's constructor, as long as that object responded to #logChargeResult: and #logConnectException:, i.e., as long as that object implemented the protocol required of a TransactionLog.
Here's a link to a similar answer to the above.
I am not really an expert but I found this article on google: http://codebetter.com/blogs/jeremy.miller/archive/2006/05/05/144172.aspx
I hope this will lead you in the right direction.
:)