IntelliJ IDEA 2019.3.4 利用 Generate POJOs.groovy 生成 POJO 异常

daxuan 发布于 03/20 15:55
阅读 932
收藏 0

我将 IDEA 版本升级到  2019.3.4  之后,利用 Generate POJOs.groovy 生成 POJO 时出现以下异常,不知大家有遇到过没?或者有什么解决思路可以分享下?

异常信息:

Generate POJOs.groovy: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script10.groovy: 3: unable to resolve class com.intellij.database.util.DasUtil @ line 3, column 1. import com.intellij.database.util.DasUtil ^ Script10.groovy: 1: unable to resolve class com.intellij.database.model.DasTable @ line 1, column 1. import com.intellij.database.model.DasTable ^ Script10.groovy: 2: unable to resolve class com.intellij.database.util.Case @ line 2, column 1. import com.intellij.database.util.Case ^ 3 errors at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:981) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:626) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:575) at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:323) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:293) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:279) at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:236) at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:335) at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:157) at java.scripting/javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264) in IdeScriptEngineManagerImpl$EngineImpl.lambda$eval$0(IdeScriptEngineManagerImpl.java:220)

 

Generate POJOs.groovy 源代码(默认文件,未进行改动):

import com.intellij.database.model.DasTable
import com.intellij.database.util.Case
import com.intellij.database.util.DasUtil

/*
 * Available context bindings:
 *   SELECTION   Iterable<DasObject>
 *   PROJECT     project
 *   FILES       files helper
 */

packageName = "com.sample;"
typeMapping = [
  (~/(?i)int/)                      : "long",
  (~/(?i)float|double|decimal|real/): "double",
  (~/(?i)datetime|timestamp/)       : "java.sql.Timestamp",
  (~/(?i)date/)                     : "java.sql.Date",
  (~/(?i)time/)                     : "java.sql.Time",
  (~/(?i)/)                         : "String"
]

FILES.chooseDirectoryAndSave("Choose directory", "Choose where to store generated files") { dir ->
  SELECTION.filter { it instanceof DasTable }.each { generate(it, dir) }
}

def generate(table, dir) {
  def className = javaName(table.getName(), true)
  def fields = calcFields(table)
  new File(dir, className + ".java").withPrintWriter { out -> generate(out, className, fields) }
}

def generate(out, className, fields) {
  out.println "package $packageName"
  out.println ""
  out.println ""
  out.println "public class $className {"
  out.println ""
  fields.each() {
    if (it.annos != "") out.println "  ${it.annos}"
    out.println "  private ${it.type} ${it.name};"
  }
  out.println ""
  fields.each() {
    out.println ""
    out.println "  public ${it.type} get${it.name.capitalize()}() {"
    out.println "    return ${it.name};"
    out.println "  }"
    out.println ""
    out.println "  public void set${it.name.capitalize()}(${it.type} ${it.name}) {"
    out.println "    this.${it.name} = ${it.name};"
    out.println "  }"
    out.println ""
  }
  out.println "}"
}

def calcFields(table) {
  DasUtil.getColumns(table).reduce([]) { fields, col ->
    def spec = Case.LOWER.apply(col.getDataType().getSpecification())
    def typeStr = typeMapping.find { p, t -> p.matcher(spec).find() }.value
    fields += [[
                 name : javaName(col.getName(), false),
                 type : typeStr,
                 annos: ""]]
  }
}

def javaName(str, capitalize) {
  def s = com.intellij.psi.codeStyle.NameUtil.splitNameIntoWords(str)
    .collect { Case.LOWER.apply(it).capitalize() }
    .join("")
    .replaceAll(/[^\p{javaJavaIdentifierPart}[_]]/, "_")
  capitalize || s.length() == 1? s : Case.LOWER.apply(s[0]) + s[1..-1]
}

 

 

加载中
0
菜鸟雷
菜鸟雷

我也遇到这种情况,求大佬jie'da

daxuan
daxuan
我还未解决呢,如果解决了我会附上解决方案的。:smiley:
0
lfork
lfork

可能是IDEA2019.3的BUG ,老版本就没问题,可以先用老版本。

有个哥们儿反馈上去了,但是没人回复

https://youtrack.jetbrains.com/issues?q=unable%20to%20resolve%20class%20com.intellij.database.util.DasUtil

0
qjh123
qjh123

我刚也遇到了。

0
故木
故木

有大佬解决了的吗?   同wen

0
eastfog
eastfog

降版本可以用,2019.3不能用的脚本我换成2019.2就能用了

返回顶部
顶部