Reserved Class Names in Groovy - groovy

If I have a class named Character.groovy (with no explicit constructors) and try to instantiate it, I get a message that says:
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.lang.Character()
But if I change the class name to Characterr.groovy, then I am able to instantiate an object and use it as expected. So are there reserved words I can't use in Groovy classes? If so, why is Character one of them?

It's not a reserved class name, but there already is a class with that name (java.lang.Character) imported as the package java.lang gets imported automatically in java.
This can happen all the time, especially if you are a java developer and not used to get e.g. java.io package etc. autoimported for you by groovy (e.g. File) (see also What packages does 1) Java and 2) Groovy automatically import?)
There are three ways around it:
the java way: address your class with the full name, that is package and class name. e.g. org.myrpg.Character.
the groovy way: import the class with a new name. e.g. import org.myrgp.Character as RPGChar and use then RPGChar instead.
the zen way: more often than not, it is not worth the hastle and easier to just rename your class. if you tripped over this once, then the chance very high you will trip over this again and only things like #CompileStatic or an IDE may make notice you this at compile time or while writing it.

http://groovy.codehaus.org/Reserved+Words
Those are the reserved keywords
http://docs.oracle.com/javase/8/docs/api/java/lang/Character.html
Character I believe is a object wrapper class in java which is why you can't use it. You can't use any name of java classes that are autoincluded in java

Related

Why should I import "java.util.*" in top of my code?

my question is that i want to use Scanner object or Arrays object for example for usage of Arrays.copyof, but before importing java.util.* or java.util.Scanner and java.util.Arrays there is now object of them to use !
why this happens to me??enter image description here
It is a little unclear what you want to achieve. Are you asking why you should do imports in Java?
import statements allow you to refer to classes which are declared in other packages without referring to their full package name. This is standard java practice. Inside your main(String args[] you can use java.util.Scanner myobject = new .. if you want, but that is too cumbersome and often you have multiple classes in a package that you want to use. So adding a simple import java.util.* is considered better.
Another tip is to use one among the large number of available IDEs (eclipse, sublime, IntelliJ) which will add the imports automatically for you.

Creating libraries that can be imported and used in Groovy

Currently, I am working on a project to transpile from my company's in house scripting language, which is Object Orientated and takes quite a few features from other languages, into Groovy, which has many similar features.
To keep code as close to original as possible, I am trying to leave certain function names and parameters the same. To cater for this, I would like to write a set of libraries that can be imported.
For example, say I have an inbuilt method in the original scripting language,
I would like to be able to write the definition for this method in a groovy file, that can then be imported when needed, and the method may be called.
Tools.groovy
// filename: Tools.groovy
public String foo(String bar) {
return bar;
}
and in another file
Main.groovy
// filename: Main.groovy
import Tools;
String bat = foo("bar")
I know you can can compile class files into jars and put them into the class path, but a lot of the methods I will need to implement will either require meta programming or won't be associated with an object.
Sorry if it's either a bad question or not clear enough. I'm not sure whether its even possible.
Cheers
I believe you should be able to create libraries and reuse them when needed.
All you need to do is create class and add the static methods if you do not have to create instances, non static methods otherwise. Then it looks like you already aware how to proceed later.
For instance, you can create utilities classes for String, List, etc based on your description.
By the way, even if you do not create libraries, it is even possible to write one lines in groovy achieve what you may needed most of the cases.

Groovy - extensions structure

I'd like to extend String's asType method to handle LocalDateTime. I know how to override this method, however I've no idea where should I put it in project structure to work globally - for all strings in my project. Is it enough to put such extension wherever in the classpath? I know that there's a special convention for extensions (META-INF/services), how does it work for method overriding?
All documentation regarding this topic can be found here. And here exactly the relevant part can be found.
Module extension and module descriptor
For Groovy to be able to load your extension methods, you must declare
your extension helper classes. You must create a file named
org.codehaus.groovy.runtime.ExtensionModule into the META-INF/services
directory:
org.codehaus.groovy.runtime.ExtensionModule moduleName=Test module for
specifications moduleVersion=1.0-test
extensionClasses=support.MaxRetriesExtension
staticExtensionClasses=support.StaticStringExtension The module
descriptor requires 4 keys:
moduleName : the name of your module
moduleVersion: the version of your module. Note that version number is
only used to check that you don’t load the same module in two
different versions.
extensionClasses: the list of extension helper classes for instance
methods. You can provide several classes, given that they are comma
separated.
staticExtensionClasses: the list of extension helper classes for
static methods. You can provide several classes, given that they are
comma separated.
Note that it is not required for a module to define both static
helpers and instance helpers, and that you may add several classes to
a single module. You can also extend different classes in a single
module without problem. It is even possible to use different classes
in a single extension class, but it is recommended to group extension
methods into classes by feature set.
Module extension and classpath
It’s worth noting that you can’t use an extension which is compiled at
the same time as code using it. That means that to use an extension,
it has to be available on classpath, as compiled classes, before the
code using it gets compiled. Usually, this means that you can’t have
the test classes in the same source unit as the extension class
itself. Since in general, test sources are separated from normal
sources and executed in another step of the build, this is not an
issue.

How to understand Groovy's classes as first-class citizens

I am an experienced Java developer, but a novice Groovy programmer, which I am learning at the moment (and it's great so far). As a reference I am reading this document:
http://groovy.codehaus.org/Groovy+style+and+language+feature+guidelines+for+Java+developers
That is all fine, except one thing I do not fully understand. The documentation says that classes are first-class citizens and the .class suffix is not needed in Groovy. But if that is the case how can I then refer to an object's type (i.e. class) in Groovy?
Consider the following example:
def o = new Object()
println("$o, ${o.class}")
Which gives me the following output:
java.lang.Object#da479dd, class java.lang.Object
This output is expected and makes sense. But what is the Groovy documentation than referring to when they say that the .class suffix is not needed?
In Groovy and many other dynamic languages everything is an object, including class itself.
Say you have a class Circle in java. You need to call Circle.getClass() for an class object do deal with. In many dynamic languages, class itself does not need to be specified. Say you have a class
class Miu {}
and each later reference to Miu will be referencing to the class object itself
Miu.class
Miu
will both evaluate to the very same object
In other words, Java and earlier in C++ had no eval(), so class definition itself cannot be made into class object directly. OO model in them is more like class oriented programming rather than true object oriented, as classes are subtly not objects. In more recent interpreted dynamic languages classes themselves are directly objects.
You're confusing two different things. You don't need .class when you're referring to some particular class, so if you have a class Foo and you want to refer to it you don't have to type Foo.class, you just type Foo. (That's what the article you're linking to describes.) But when you have some object and you want to know its class you would still use .class (where .class is actually short for invoking the getClass method). Note if you have a map and you want to know its class you have to type out getClass() so it won't think you aren't referring to a map key called 'class'.

Make groovy metaclass methods available globally

I have a groovy script, Bootstrap.groovy, where I have defined several metaclass methods on the String class. I have my test cases in another file Test.groovy. How do I make the metaclass methods available in the Test.groovy, when manipulating String? (Iow, how do I make the metaclass methods globally available in other scripts/programs)?
(I did search and find some related questions, but they did not answer this specifically. Im using Groovy 1.8.4)
Use the DelegatingMetaClass to make these additions to the String class globally visible.
I think I found a way to do this: just call evaluate(new File("ch8/Bootstrap.groovy")) in Test.groovy. Previously I was trying new GroovyShell().evaluate(), but thats not required. I could directly call the evaluate() method.

Resources