使用jOOQ 3.6 +,纯SQL和javac编译器进行慢速编译

时间:2015-12-11 12:10:11

标签: java compilation java-8 jooq

jOOQ user group报告了以下错误。它似乎是javac编译器中的一个错误,它与编译器在使用像jOOQ这样的内部DSL的环境中完成的相当“复杂”的类型推断工作有关。

鉴于该bug的一般性质,我在Stack Overflow上记录它,以便其他人帮助应用变通方法(如果遇到它们)。在较高的层面上,它似乎是由于JEP 101: Generalized Target-Type Inference导致的编译器性能回归,这是在Java 8中引入的,并且在过去已经引起了1-2个问题。

以下相对无害的类使用Maven和jOOQ 3.7在Windows上使用jdk 1.8.0_60或1.8.0_66进行编译大约需要20秒:

import static org.jooq.impl.DSL.field;

import org.jooq.SQLDialect;
import org.jooq.impl.DSL;

public class Test {
    public void method() {
        DSL.using(SQLDialect.MYSQL)
           .select()
           .where(DSL.trueCondition())
           .and(field("client.id").eq(field("client_id")))
           .and(field("client.id").eq(field("client_id")))
           .and(field("client.id").eq(field("client_id")))
           .and(field("client.id").eq(field("client_id")))
           .and(field("client.id").eq(field("client_id")))
           .and(field("client.id").eq(field("client_id")))
           .and(field("client.id").eq(field("client_id")))
           .and(field("client.id").eq(field("client_id")))
           .and(field("client.id").eq(field("client_id")))
           .and(field("client.id").eq(field("client_id")))
        ;
    }
}

的pom.xml:

<project 
        xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                            http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>compilation-issues</groupId>
    <artifactId>compilation-issues</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq</artifactId>
            <version>3.7.1</version>
        </dependency>
    </dependencies>
</project>

未出现此问题时的配置:

  • 使用jOOQ 3.5(3.6.0之前的任何内容)
  • 将jOOQ用于生成的类而不是上面的“普通SQL”API
  • 使用Java 7
  • 使用Eclipse编译器

1 个答案:

答案 0 :(得分:5)

解释

在jOOQ 3.6中(首次出现此问题时),DSL.field() API看到了22个新的重载,它们将不同的Row类型作为参数:

看来上面这个特定的API用法,当javac编译器试图在所有可能的重载中找到最具体的重载时,新的重载会导致很多麻烦。以下解决方法立即编译:

修复

正在针对发布3.9.03.8.13.7.43.6.5进行修复,再次从公共API中删除这些方法,并提供重命名的替代方法不会导致任何超载问题。

的变通方法

1。帮助编译器选择最具体的DSL.field()重载

import static org.jooq.impl.DSL.field;

import org.jooq.Field;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;

public class Test {
    public void method() {
        Field<Object> f1 = field("client.id");
        Field<Object> f2 = field("client_id");
        DSL.using(SQLDialect.MYSQL)
           .select()
           .where(DSL.trueCondition())
           .and(f1.eq(f2))
           .and(f1.eq(f2))
           .and(f1.eq(f2))
           .and(f1.eq(f2))
           .and(f1.eq(f2))
           .and(f1.eq(f2))
           .and(f1.eq(f2))
           .and(f1.eq(f2))
           .and(f1.eq(f2))
           .and(f1.eq(f2))
        ;
    }
}

2。完全在and()方法

的上下文中防止目标类型推断
import static org.jooq.impl.DSL.field;

import org.jooq.Condition;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;

public class Test {
    public void method() {
        Condition condition = field("client.id").eq(field("client_id"));
        DSL.using(SQLDialect.MYSQL)
           .select()
           .where(DSL.trueCondition())
           .and(condition)
           .and(condition)
           .and(condition)
           .and(condition)
           .and(condition)
           .and(condition)
           .and(condition)
           .and(condition)
           .and(condition)
           .and(condition)
        ;
    }
}

更多信息

实际上已经在Stack Overflow上报告过:

在jOOQ用户组中也讨论过它: