Groovy File Name and Class Name different - grails

I am developing application using grails which uses groovy.I changed one of the domain class name say class A to class B.However this does not change the file name unlike the case with Java.Why is it so.My guess would be the A.groovy file is treated as groovy script and class name is a Java class?Please clarify..thanks

If you rename a class, you need to need to rename the source file in which that class is defined. If you are using an IDE's refactoring support, the IDE should do that for you. Regardless, the file name should be changed either by you or by your tools.

Related

grails 2.5+ Duplicate class definition error when implementing serializable on a controller

Upgrading a legacy system of grails. One of the controllers implements Serializable. This is throwing the following error in newer versions of grails:
Invalid duplicate class definition of class com.regional.ScheduleController :
The source contains at least two definitions of the class.
One of the classes is an explicit generated class using the class statement,
the other is a class generated from the script body based on the file name.
Solutions are to change the file name or to change the class name.
The solution mentioned would break (previous) grails convention. Anyone know how to handle this in grails 2.5+?
EDIT
Serializable is not the issue. I tried removing it and got the same error.
I found this explanation from another question:
IN groovy.. class B{} is a class structure and defines a class B.
Scripts are classes too.
Now you may create a B.groovy, with the content "class B{}; def b = new B()".
There would be one class named B, and a script with the very same name.
This is a conflict.
However this does not explain why it runs fine below grails 2.5 and not above it. And I can't find a def conflict like the one mentioned above in that controller.
ANSWER:
One of the imports was what was actually failing- in a way that caused groovy to generate a class definition based on the current file name. When it hit the class definition, there was already an auto generated class name to collide with.

swagger-inflector and its use of x-swagger-router-controller and x-swagger-router-model

I am using swagger-inflector to design an API. I am trying to use x-swagger-router-controller and x-swagger-router-model as specified in the docs, but it is not working as specified.
The way I read the docs, these vendor extensions are supposed to create the class if it does not exist and then create a method named with the "operationId". However, they do not work as specified. (I looked at the code that I think is supposed to process this vendor extension, and it looks like the extension should be processed, but the classes are not created as expected. Well, the x-swagger-router-model generates the desired class name, but not the desired package).
If I use the inflector.yaml file and specify modelPackage and controllerPackage, the classes get created in those packages, but I need more granular control over what package is used for the generated classes.
Am i doing something wrong, or is this broken?
Here is an example:
definitions:
SomeObject:
type: object
x-swagger-router-model: com.example.api.dto.SomeObjectDTO
...
paths:
/mypath:
x-swagger-router-controller: com.example.api.controller.subpkg1.MyController
get:
...
From the above example
I do not get a model class named SomeObjectDTO created in com/example/api/dto. If no modelPackage is specified in inflector.yaml, I get a model class named SomeObject created in the default package (io/swagger/...). The model class name that is generated in either case is SomeObject.java
I do not get a controller class named MyController created in com/example/api/controller/subpkg1. If no controllerPackage is specified in inflector.yaml, I get a controller class named "MyPathController" created in the default package (io/swagger/...). The controller class that is generated in either case is MyPathController.java.
It looks like this is a bug, or that I am missing something really obvious. Any pointers here?
the swagger-inflector code will not generate models for you--it will attempt to connect the routing information (via x-swagger-router-model) or via modelPackage + schema name. If that model cannot be loaded in the class loader, it will treat this model as a jackson JsonNode for all input (put/post) methods.

What its the first, the annotated class (egg) or used class (chicken)?

certainly I have not read something fundamental, and it seems very strange, but I wonder.
Suppose you use
#SharedPref
public interface SharedPreferencesInterface {
#DefaultBoolean(true)
boolean showDeviceName();
I have the IDE (idea) configured with Gradle, and I generated the SharedPreferencesInterface_ class that I can use in another class as
#Pref
SharedPreferencesInterface_ prefs;
But suppose someone now download the project, how can the use? Because the class where used SharedPreferencesInterface_ not compile because the class does not exist, and the class does not exist because compilation errors ...
How it's made? Surely there is a way ... configured to compile certain classes first?
Help is appreciated.
A greeting.
But suppose someone now download the project, how can the use? Because
the class where used SharedPreferencesInterface_ not compile because
the class does not exist, and the class does not exist because
compilation errors ...
This is the same situation when you compile a project in a full build (when no classes are generated yet). Actually Gradle always does a full build currently in Android projects. No configuration is needed at all in addition to the standard AndroidAnnotaions config.
Actually this works because the compiler does not fully compiles your class before passing it to annotations processing. It is clear it should not to, because the class may reference generated classes, which are only available after the processing. So first the compiler creates a model of the classes, only parses the structure of the them (fields, methods, return types, parameter types, etc), but not the implementations. Also it allows missing types even on fields. If it finds a missing type, it assigns to TypeKind.ERROR, but the name of the type is still available for the annotation processor. After the processor is done, it generates the missing class, so the kind of the class is no longer TypeKind.ERROR, and the compilation can succeed.

Grails Logging - Exclude One Class

I have a project consisting of hundreds of various classes. One of these extends a class located in a JAR library and produces tons of log info. I would like to exclude this one class from producing all this logging information.
Here's my config.groovy logging section:
trace('grails.plugin.springsecurity.web.filter.DebugFilter',
'grails.app.conf.com.myrootpackage',
'grails.app.controllers.com.myrootpackage',
'grails.app.domain.com.myrootpackage',
'grails.app.filters.com.myrootpackage',
'grails.app.services.com.myrootpackage',
'grails.app.taglib.com.myrootpackage',
'com.myrootpackage')
Since all of my classes are located in the com.myrootpackage package or sub packages of that, I'm not sure how to exclude the one class. As far as I can tell, the logging setup in config.groovy only allows specifying the beginning of class names so I would have to specify by name all other classes and omit the one I want to omit, or move the one I want to omit to a separate root package. Both of these seem silly to have to do to just omit one class from producing log output.
As far as I can tell, the logging setup in config.groovy only allows specifying the beginning of class names
No, you can use the full class name to configure an individual class, so the following should work
trace('grails.plugin.springsecurity.web.filter.DebugFilter',
'grails.app.conf.com.myrootpackage',
'grails.app.controllers.com.myrootpackage',
'grails.app.domain.com.myrootpackage',
'grails.app.filters.com.myrootpackage',
'grails.app.services.com.myrootpackage',
'grails.app.taglib.com.myrootpackage',
'com.myrootpackage')
error 'com.myrootpackage.ExcludeMe'
I've assumed
com.myrootpackage.ExcludeMe is the fully-qualified name of the class in question
this class should log at the error level instead of trace

Reduce number of class files produced by BlackBerry app

I have developed an order processing application for BlackBerry. When I look at the bin folder I see more than 100 .class files.
I have created a main screen class for adding new clients. The screen has 7 LabelField objects and 7 corresponding TextField objects. This screen also creates a VerticalFieldManager and adds all these fields to it and then adds the VerticalFieldManager to the screen.
For this screen, I have 14 .class files in the bin folder. It seems there is one class file for every field in the progam.
For example:
NewClient.class
NewClient$1.class
...
NewClient$14.class
How do I design the UI in order to reduce the number of compiled classes?
Building a Java-ME app for BlackBerry is a two step process. First the java source code is compiled to class files, then those class files are compiled again into a .cod file, which can be deployed onto a simulator or a device.
'rapc' is the RIM compiler that takes java programs and turns them into a cod or alx file for deployment. 'rapc' can take either java source code, or compiled java classes. Either way, it can produce output suitable for a device.
When starting with Java source files, you can explicitly compile them to class files and hand those class files to rapc or you can pass the Java source to rapc and it will compile the source directly. rapc just defers to the JDK javac compiler when presented with java source code. This means a standard java JDK compiler is always used as the first step of compiling a BlackBerry app, and we can look at standard java behavior to understand what is happening.
In Java, every class that is instantiated has exactly one .class file. For normal classes with a declared name, like this:
public class Foo extends Bar {
}
The .class file is assigned a name that matches the declared class name. However, Java also allows anonymous classes. These take the form of a new Foo() followed by a curly brace which turns this into an anonymous class. This presents a problem, as this anonymous class must be assigned a name at the VM level, despite having none at the Java source level. The solution is to use a character that is invalid in Java source, but valid in the VM, namely $. The anonymous classes are assigned a name based on the enclosing Java class, followed by $, then an integer index based on the number of anonymous classes ahead of this one. In your case, that is NewClient, followed by 14 distinct integers.
To see the behavior you describe, your fields must all actually be anonymous implementations of those classes you mention. To reduce the number of classes, try reusing explicit classes, instead of writing custom behavior with each instantiation.
Set your jdk bin folder path on the environment variable path on right clicking on the myComputer icon
Then Restart the pc
Other way is to don't use overwrite method on your code such as
btmSave.setChangeListner(new FieldChangeListner()
{
private void fieldChange()
{
}
}
);
Avoid This type of writing code it create your no of class Files on project bin folder

Resources