Unable to run OOPS groovy with some abstraction
Answered
Hello,
I am unable to run some basic groovy code with abstraction. I have read thru this link "https://blog.jetbrains.com/datagrip/2018/02/13/generate-pojos/" and auto generated pojos and rest controllers from Data Grip. On DataGrip I right click, hit "Scripted Extensions" and select a Groovy file. If the file is written exactly as "Groovy Pojos.groovy" (see in link) then it works. If I want to use some basic abstraction and designs around reusability it fails to work. Cant even get to import classes in Groovy.
Simple code snippet of what I tried
class abstract AGenerate {
abstract def getPackageName() ;
abstract def getFileSuffix() ;
abstract def generateClassFiles()
abstract def generate(output, className, fields) ;
def typeMapping = [
(~/(?i)int/) : "Integer",
(~/(?i)bigint/) : "Long",
(~/(?i)float/): "Double", : "Float",
(~/(?i)double|decimal|real/) : "Double",
(~/(?i)datetime|timestamp/) : "java.sql.Timestamp",
(~/(?i)date/) : "java.sql.Date",
(~/(?i)time/) : "java.sql.Time",
(~/(?i)/) : "String"
]
...
package com.squats.autogenerate ;
import com.intellij.database.model.DasTable
import com.intellij.database.model.ObjectKind
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instanceof DasTable && it.getKind() == ObjectKind.TABLE }.each { generateFolderFromTable(it, dir) }
}
class GenerateEntityClass extends AGenerate {
def getFileSuffix() {
return "Entity.java"
}
def getPackageName() {
return "com.sample;" ;
}
...
import com.intellij.database.model.DasTable
import com.intellij.database.model.ObjectKind
import com.squats.autogenerate.GenerateEntityClass
def GenerateEntityClass entityClass = newGenerateEntityClass();
FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
SELECTION.filter { it instance of DasTable && it.getKind() == ObjectKind.TABLE }.each { entityClass.generateFolderFromTable(it, dir) }
}
I get a error
12:06 AM AutoGenerate.groovy: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script24.groovy: 4: unable to resolve class com.squats.autogenerate.GenerateEntityClass
@ line 4, column 1.
import com.squats.autogenerate.GenerateEntityClass;
What am I missing here? Doesnt this run with OOPS?
Please sign in to leave a comment.
could you please share full code of your script?
AGenerate (abstract class)
GenerateEntityClass extends AGenerate
AutoGenerate I call from DataGrip
Hello Jsiddharth,
Unfortunately you need to place all classes in one file
I found a typo in your script `class absract` should be `abstract class`
These scripts can be quite complicated. It's better to use an editor that'll help you to find errors e.g. IntelliJ IDEA Community, it's free