3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio CodeのJavaで--enable-previewオプションを付ける

Last updated at Posted at 2019-11-07

随分前ですが、Visual Studio Code(以降VSCode)のJava Extension PackがJDK 13に対応しました。

JEP 355: Text Blocks (Preview)を試してみたところ「Text Blocks is a preview feature and disabled by default. Use --enable-preview to enable」というエラーがでました。
2019-11-07_22h30_01.png

そうだ、--enable-previewを付けないといけないんだったと思いつつ、あれVSCodeの場合どう指定するんだろう?と一瞬手が止まったので調べてみました。

上記のエラーがでたのはMavenを使ったプロジェクトだったので、VSCode関係なくMavenのCompiler Pluginに以下の記述を追記して解決できました。

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <compilerArgs>--enable-preview</compilerArgs>
    </configuration>
</plugin>

他に、Java Extension PackのLanguage Support for Javaに含まれるJDT(Java Development Tool)の設定でも解決できました。
参考: https://github.com/redhat-developer/vscode-java/issues/671#issuecomment-477379761

Javaプロジェクトに.settingsフォルダができているので、その中のorg.eclipse.jdt.core.prefsファイルを開いて「org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=enabled」を追記するのみです。
2019-11-08_00h05_59.png

Mavenを使わない場合はどうなるのか試したところ、特にエラーは出ずに実行できました。よくみると--enable-previewが付いた状態で実行されていました。
2019-11-08_00h46_20.png

Java Extension Packの中のDebugger for Javaで指定されているっぽい?のかな。

Githubのコードを探したところ、以下にありました。

 // Auto add '--enable-preview' vmArgs if the java project enables COMPILER_PB_ENABLE_PREVIEW_FEATURES flag.
if (await lsPlugin.detectPreviewFlag(config.mainClass, config.projectName)) {
    config.vmArgs = (config.vmArgs || "") + " --enable-preview";
}

COMPILER_PB_ENABLE_PREVIEW_FEATURESフラグによって制御されるようで、フラグは以下のとおりでした。デフォルトがenabledのようです。(あってるのかな?

const COMPILER_PB_ENABLE_PREVIEW_FEATURES: string = "org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures";
export async function detectPreviewFlag(className: string, projectName: string): Promise<boolean> {
    const expectedOptions = {
        [COMPILER_PB_ENABLE_PREVIEW_FEATURES]: "enabled",
    };
    return checkProjectSettings(className, projectName, true, expectedOptions);
}

最後に余談ですが、先日参加したOracle Code One 2019 報告会@bitter_foxさんのデモをみて「おぉ」と思ったのが、Text Blocksの先頭行にある空白は末尾の閉じ位置で決まるというものでした。試してみたところ無事にできました。地味だけどなんか印象的でした。

2019-11-08_00h55_40.png

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?