How to use #GrailsTypeChecked and #GrailsCompileStatic - grails

I was interested to read about the new annotations in Grails which should in theory provide runtime performance improvements whilst still allowing dynamic dispatch however it seems to have no effect. What am I missing?
class MyService {
#GrailsTypeChecked // or #GrailsCompileStatic
def doSomething() {
String name = missingVariable
}
}
grails> clean
| Application cleaned.
grails> compile
| Compiling 1 source files.....
Everything compiles but blows up at runtime? I must be missing something because I don't really see how these new annotations could work. How will the compiler be able to work out what is a bad call and what is a bad call that can be ignored (i.e. a dynamic finder)?
I'm using Grails 2.5.0

That code shouldn't compile. I have built a simple app and pasted your code directly into that app and the code doesn't compile for me. See https://github.com/jeffbrown/grailscompilestatic.
| Compiling 8 source files
[groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
[groovyc] /Users/jeff/t/grailscompilestatic/grails- app/services/demo/MyService.groovy: 9: [Static type checking] - The variable [missingVariable] is undeclared.
[groovyc] # line 9, column 19.
[groovyc] String name = missingVariable
[groovyc] ^
[groovyc]
| Compiling 8 source files.
| Error Compilation error: startup failed:
/Users/jeff/t/grailscompilestatic/grails-app/services/demo/MyService.groovy: 9: [Static type checking] - The variable [missingVariable] is undeclared.
# line 9, column 19.
String name = missingVariable
^
1 error
How will the compiler be able to work out what is a bad call and what
is a bad call that can be ignored (i.e. a dynamic finder)?
We have type checking extensions that collaborate with the type checker so when the type checker thinks it sees invalid code our extension is engaged and the extension gets the opportunity to turn the call into a dynamic call if the extension can recognize that it is something like a dynamic finder, which cannot be statically dispatched but is in fact valid at runtime.

Following on from Jeff's comments it seems there was a bug which prevented the #GrailsCompileStatic and #GrailsTypeChecked annotations from working when a class level #Transactional annotation was also present. I believe this is now fixed in grails 3.0.1 (Thanks guys!)

Related

How we can use A template profiler plugin in grails 2.4.3

Forgive me for my English. Actually I want to use A template profiler plugin in grails 2.4.3. But it is unable to install in this grails version. It is available for the grails version 1.3.2. And I want to use it for grails 2.4.3. When I am trying to install. It shows an error
| Error Compilation error: startup failed:
C:\Project\target\work\plugins\profile-template-0.1\grails-app\services\profile\te
mplate\ProfileTemplateService.groovy: 5: unable to resolve class
org.codehaus.groovy.grails.commons.Configurat ionHolder # line 5,
column 1. import
org.codehaus.groovy.grails.commons.ConfigurationHolder ^
C:\Projects\target\work\plugins\profile-template-0.1\grails-app\services\profile\te
mplate\ProfileTemplateService.groovy: 8: unable to resolve class
org.codehaus.groovy.grails.commons.Configurat ionHolder # line 8,
column 26.
boolean isRecording = !(ConfigurationHolder.config?.profile?.template?.disabled as Boolean)
^
2 errors
Can anybody help me to get me out of this. Or You can suggest me another alternate option for this.
Thanks in advance.
If you have access to the source code of the plugin you could follow this:
In Grails 2 we no longer use ConfigurationHolder we use GrailsApplication.getConfig() instead. In the ProfileTemplateService add field GrailsApplication grailsApplication (Spring will inject it during app boot up) and change the:
ConfigurationHolder.config?.profile....
to
grailsApplication.config.profile...
Your service class should look like:
class ProfileTemplateService {
GrailsApplication grailsApplication
def method() {
a = grailsApplication.config.profile
}
}
Obviously there can more hoops to jump through before you actually make the plugin work.

Grails ehcache plugin compile error

I'm trying to use the latest version of the grails ehcache plugin (1.0.4) in my grails application, however when I add the plugin as a dependency in BuildConfig.groovy I get a compile error on startup:
| Error Compilation error: startup failed:
Compile error during compilation with javac.
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:202: error: ReloadableCacheManager.ProxyEhcache is not abstract and does not override abstract method getSearchesPerSecond() in Ehcache
protected class ProxyEhcache implements Ehcache {
^
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:805: error: getStatistics() in ReloadableCacheManager.ProxyEhcache cannot implement getStatistics() in Ehcache
public StatisticsGateway getStatistics()
^
return type StatisticsGateway is not compatible with Statistics
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:663: error: cannot find symbol
return getUnderlyingEhcache(name).getSearchAttributes();
^
symbol: method getSearchAttributes()
location: interface Ehcache
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:660: error: method does not override or implement a method from a supertype
#Override
^
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:758: error: cannot find symbol
return getUnderlyingEhcache(name).calculateOnDiskSize();
^
symbol: method calculateOnDiskSize()
location: interface Ehcache
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:755: error: method does not override or implement a method from a supertype
#Override
^
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:765: error: cannot find symbol
return getUnderlyingEhcache(name).getAll(arg0);
^
symbol: method getAll(Collection)
location: interface Ehcache
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:761: error: method does not override or implement a method from a supertype
#Override
^
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:770: error: cannot find symbol
return getUnderlyingEhcache(name).hasAbortedSizeOf();
^
symbol: method hasAbortedSizeOf()
location: interface Ehcache
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:768: error: method does not override or implement a method from a supertype
#Override
^
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:777: error: cannot find symbol
getUnderlyingEhcache(name).putAll(arg0);
^
symbol: method putAll(Collection)
location: interface Ehcache
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:773: error: method does not override or implement a method from a supertype
#Override
^
/Users/rcgeorge23/Documents/workspace/grails-cache-ehcache/src/java/grails/plugin/cache/ehcache/GrailsEhCacheManagerFactoryBean.java:783: error: method putIfAbsent in interface Ehcache cannot be applied to given types;
return getUnderlyingEhcache(name).putIfAbsent(arg0, arg1);
^
required: Element
found: Element,boolean
Has anyone else had this problem? I notice the ehcache plugin is still being maintained, so I guess it works with current versions of grails, however I can't seem to get it to work for me. I've also tried cloning the latest commit from the grails-ehcache project in github and referencing this as a local plugin project, but I still get the same compile error.
I'm using Grails 2.3.7.
EDIT 1:
I was able to get my app to start up using version 1.0.0 of the ehcache plugin, although I imagine I'm probably missing out on some important enhancements if I use this version...
EDIT 2:
Ok for now, I've just cloned version 1.0.0 from github and cherry picked the fix for the TTL defect (https://jira.grails.org/browse/GPCACHEEHCACHE-6). It'd be good to find out why I can't get 1.0.4 working, but I have a workaround for now.
It works for me with a basic 2.3.11 app, so I'm guessing that something else is bringing in an older version of the ehcache jar. You'll probably see that if you run grails dependency-report.
The 1.0.4 plugin depends on net.sf.ehcache:ehcache:2.9.0 but most other plugins that depend on Ehcache haven't been updated to the most recent versions, so you'll probably see that you also have a dependency for net.sf.ehcache:ehcache-core:2.4.8, but the version might be off a bit. Notice that they dropped the "-core" from the name, which could also be contributing to this because unless the group and name are exactly the same, one jar cannot evict another. So it's possible for two people to have both jars in the classpath, but they end up in the opposite order, and it works fine for one because the newer jar's classes are loaded, but not the other because the the older classes are loaded.
You'll get a better dependency report if you switch temporarily from
grails.project.dependency.resolver = "maven"
to
grails.project.dependency.resolver = "ivy"
in BuildConfig.groovy - the maven resolver only generates output to the console, but the Ivy output also includes HTML output.
If you can't resolve this, create a small separate test app and configure it so it fails the same way - you probably just need to copy BuildConfig.groovy from your app. Create an issue at https://jira.grails.org/browse/GPCACHEEHCACHE and I'll take a look. If all you need to do is change BuildConfig.groovy, attach that to the issue, otherwise run grails bug-report and attach the zip it creates.

Producing a Webbapp War file - Error that parameter will be ignored

I’m trying to get a Project running on Tomcat7 that requires some Plugins and is written in Grails.
Creating a -war with the command grails prod war results in an Error that tells me a parameter of type [java.util.List] is not supported.
Where and how can i see which parameter are Supported and why List isn’t ?
the logfile with the errors looks like this:
| Loading Grails 2.4.1
| Configuring classpath
| Configuring classpath.
| Environment set to production
| Environment set to production.
| Environment set to production..
| Environment set to production...
| Environment set to production....
| Environment set to production.....
| Packaging Grails application
| Packaging Grails application.
| Packaging Grails application..
| Packaging Grails application...
| Packaging Grails application....
| Packaging Grails application.....
| Compiling 70 source files
| Warning The [listFullAccessResources] action in [VsGrantController] accepts a parameter of type [java.util.List]. Interface types and abstract class types are not supported as command objects. This parameter will be ignored.
List listFullAccessResources(VsUser loggedInUser, List resultsGoups) {
^
[groovyc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
[groovyc] General error during class generation: java.lang.NoClassDefFoundError: Unable to load class org.jets3t.service.impl.rest.httpclient.RestS3Service due to missing dependency org/apache/http/client/methods/HttpHead
[groovyc]
[groovyc] java.lang.RuntimeException: java.lang.NoClassDefFoundError: Unable to load class org.jets3t.service.impl.rest.httpclient.RestS3Service due to missing dependency org/apache/http/client/methods/HttpHead
[groovyc] at org.codehaus.groovy.control.CompilationUnit.convertUncaughtExceptionToCompilationError(CompilationUnit.java:1083)
The VsGrantController.groovy looks like this
import org.apache.commons.lang.StringUtils
class VsGrantController {
def vsAuthenticateService
def vsGrantService
def index = { redirect(action:list,params:params) }
// the delete, save and update actions only accept POST requests
static allowedMethods = [delete:'POST', save:'POST', update:'POST']
List listFullAccessResources(VsUser loggedInUser, List resultsGoups) {
// labels user has full access to
def cLabels = VsLabel.createCriteria()
def resultsLabelsFullAccess = cLabels.listDistinct {
grants {
and {
permission {
eq("name", 'Full')
}
or {
eq("accessor", loggedInUser)
if(resultsGoups) {
or {
for (g in resultsGoups) {
eq("accessor", g)
}
}
}
}
}
}
order("name")
}
I'm kind of new to the Grails/Groovy Tomcat Webbapp Topic
so i hope this question is not to bad
The warning "Interface types and abstract class types are not supported as command objects." complains because your controller action has a List resultsGoups parameter. This is not supported.
You can extract the parameters from the implicit 'params' map or by using a command object.
Something like this...
def listFullAccessResources(AccessResourcesCommand cmd) {
}
class AccessResourcesCommand {
Bar loggedInUser
List resultsGroups
}
Your actual error is
Unable to load class org.jets3t.service.impl.rest.httpclient.RestS3Service due to missing dependency org/apache/http/client/methods/HttpHead
You need to include httpclient.jar in the classpath
Change it to:
def List listFullAccessResources(VsUser loggedInUser, List resultsGoups)
and your warning will go away...

Repetitive method name/signature for method compilation error when using #I18nFields

I get repetitive method name/Signature compilation failure when i try to use i18nFields in my domain class to support multiple languages.
Grails Version : 2.3.7 ( I tried with 2.3.4 and got the same issue and upgraded)
Documentation from Grails followed for this was http://grails.org/plugin/i18n-fields
My Domain class looks like
package com.sampleapp.domain
import i18nfields.I18nFields;
#I18nFields
class Products {
def name
static constraints = {}
static i18nFields = ['name']
}
My Config.groovy has the below line included to specify the locale
// internationalization support - testing
i18nFields {
locales = ['en','es']
}
BuildConfig.groovy plugin definition
plugins {
// plugins for the build system only
build ":tomcat:7.0.47"
// plugins for the compile step
compile ":scaffolding:2.0.1"
compile ':cache:1.1.1'
// plugins needed at runtime but not for compilation
runtime ":hibernate:3.6.10.6" // or":hibernate4:4.1.11"//
runtime ":database-migration:1.3.8"
runtime ":jquery:1.10.2.2"
// compile ":jquery-ui:1.10.2.2"
runtime ":resources:1.2.1"
// Uncomment these (or add new ones) to enable additional resources capabilities
runtime ":zipped-resources:1.0.1"
runtime ":cached-resources:1.1"
//runtime ":yui-minify-resources:0.1.5"
compile ':platform-core:1.0.RC6'
compile ":cache-headers:1.1.5"
runtime ':spring-security-core:2.0-RC2'
// internationalization
compile ":i18n-fields:0.8.1"
}
The compilation error is
grails-workspace\Test\grails-app\domain\com\sampleapp\domain\Products.groovy: -1: Repetitive method name/signature for method 'void setName_es(java.lang.String)' in class 'com.sampleapp.domain.Products'.
# line -1, column -1.
The Error is repeated for the name property for both en and es locales twice.
There is no error if i remove the i18nFields annotation and the sample app worked fine before this. I verified GGTS repetitive method name/signature error in controllers post for similar error in controller. I have also verified to ensure that groovy version is correct and in my case it is 2.1
Can somebody please give me any pointers on where i should look to resolve this issue.
This problem shows up when you are trying to use Java > v7 with any version of Grails < 2.3.7. Either downgrade your jvm or upgrade your grails.
Thanks for trying (and for making me know via github ;) )
The issue was known but hadn't been tackled yet.
The previous answer (commenting out methods) is not exact, event though it follows the right track, because the problem comes from new changes in Grails that will cause a collision in getters and setters.
The solution I've found is to separately create the property and the getter/setter, and it seems to work.
I'm releasing a new version as soon as it can be fully tested in a project, but code is already available in https://github.com/jorgeuriarte/grails-i18n-fields-plugin/tree/redis_integration (version 0.9-redis-SNAPSHOT) in case you want to use it.
Probably it has something to do with the new Binding mechanism in grails 2.3. Maybe the getters and setters are set automatic now?
When I comment out these two lines in ClassI18nalizator.groovy the error disappears. It seems to work at least partly. I can use the fields in scaffolding. It's not a real solution but maybe a hint for someone who understands grails better than me.
private def getSetterMethod(field) {
// setter should return void. that's why the return statement.
//return new AstBuilder().buildFromString("i18nfields.I18nFieldsHelper.setValue(this, '${field}', value); return;").pop();
}
private def getGetterMethod(field) {
//new AstBuilder().buildFromString("i18nfields.I18nFieldsHelper.getValueOrDefault(this, '${field[0..-7]}', '${field[-5..-1]}')").pop();
}
After that I run into a second issue:
No signature of method: groovy.util.ConfigObject.contain() is
applicable for argument types: (java.lang.String) values: [en_US]
I solved it by adding redisLocale to Config.groovy
i18nFields {
locales = ['de_DE', 'en_US']
defaultLocale = "en_US"
redisLocales = ['de_DE', 'en_US']
}

Error with spring security plugin in grails 2.4.0.M1

i'm running into a compilation issue, using grails 2.4.0.M1 and spring-security-core:2.0-RC2
this is the error:
..../target/work/plugins/spring-security-core-2.0-RC2/src/groovy/grails/plugin/springsecurity/ReflectionUtils.groovy:
205: Apparent variable 'org' was found in a static scope but doesn't
refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance
variable from a static context. You misspelled a classname or
statically imported field. Please check the spelling. You attempted
to use a method 'org' but left out brackets in a place not allowed by
the grammar. # line 205, column 18.
application = org.codehaus.groovy.grails.commons.ApplicationHolder.application
^
the problem seems to be around this method
private static GrailsApplication getApplication() {
if (!application) {
application = org.codehaus.groovy.grails.commons.ApplicationHolder.application
}
application
}
on the class ReflectionUtils.groovy,
does anyone else as ran into something like this? if so how do you fixed it?
I fixed this today - https://github.com/grails-plugins/grails-spring-security-core/commit/ef3aab05bfb0eb2f2cbb2c5945f4fc9ca2f0697d
You can make the change that #Bubuntux showed as a temporary workaround, and I'll be releasing 2.0 final in a couple of weeks with this fixed. Hopefully you're not planning on using a Grails M1 release in production, so the delay shouldn't be too much of an issue.
Seems like the ApplicationHolder class was deprecated a long time ago, and now removed on grals 2.4
so i just change the line
application = org.codehaus.groovy.grails.commons.ApplicationHolder.application
to
application = Holders.grailsApplication

Resources