Error Proneとは
Google の バグチェックツール。
FindBugsみたいなもんといえば、Javaエンジニアなら想像しやすいはず。
環境情報
必要なものは、JavaとGradleだけあれば、とりあえず大丈夫
Java
Java9の調査をしてたので。。。
切り替えるの面倒だったから、Java9で試したが、Java8でもイケるハズ!
試すの面倒くさいからしないけど。
>java -version java version "9-ea" Java(TM) SE Runtime Environment (build 9-ea+171) Java HotSpot(TM) 64-Bit Server VM (build 9-ea+171, mixed mode)
Gradle
gradle -v ------------------------------------------------------------ Gradle 3.5 ------------------------------------------------------------ Build time: 2017-04-10 13:37:25 UTC Revision: b762622a185d59ce0cfc9cbc6ab5dd22469e18a6 Groovy: 2.4.10 Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015 JVM: 9-ea (Oracle Corporation 9-ea+171) OS: Windows 10 10.0 amd64
eclise
Eclipse Java EE IDE for Web Developers. Version: Neon.2 Release (4.6.2) Build id: 20161208-0600
とりあえず開発はコイツを使う。
コイツにGradleのプラグイン(たぶんBuildship Gradle Integrationだったと思う)を入れてテスト
試す
とりあえず、今回の目標は、exampleで提供されているものを実行してエラーになることを試す。
Gradleプロジェクト作成
eclipseから新規Gradleプロジェクトを作成
build.gradle を変更
下記リンク先のソースに置き換えれば大丈夫のハズ。
error-prone/build.gradle at master · google/error-prone · GitHub
念のため、自分が使った内容を晒しておく
// See https://github.com/tbroyer/gradle-errorprone-plugin buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath 'net.ltgt.gradle:gradle-errorprone-plugin:0.0.8' } } apply plugin: 'java' apply plugin: 'net.ltgt.errorprone' repositories { mavenCentral() } configurations.errorprone { resolutionStrategy.force 'com.google.errorprone:error_prone_core:2.0.19' }
サンプルソースの配置
Gradleでプロジェクト作ったら、Library.java
みたいなソースがあったので、それは消した。
そんでもって、下記のファイルを配置した。
どこでもいいと思うが、例にならって、src\main\java\Main.java
に配置した。
error-prone/Main.java at master · google/error-prone · GitHub
/* * Copyright 2013 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { public static void main(String[] args) { // Dead exception new Exception(); } }
FindBugsやってなくても、コレは「変」って分かるはず。
Exceptionをthrowしてないって。
実行
Gradleを clean & build !
そうすると、下記のエラーが出てくるはず。
..src\main\java\Main.java:20: エラー: [DeadException] Exception created but not thrown new Exception(); ^ (see http://errorprone.info/bugpattern/DeadException) Did you mean 'throw new Exception();'? エラー1個 FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileJava'. > Compilation failed with exit code 1; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 0.841 secs
たぶん、意味的に「new Exception();
っておかしくね? throw new Exception();
がやりたかったんじゃね?」ってことを行っていると思う。(銀魂風に翻訳してみた!)
感想
なんとかサンプルを動かすまでできた。
Gradleの存在は知っていたけど、使い方は知らなかったので、かなり手間取った。
あと、日本語の情報が異様に少ない。
みんな、FindBugsを使っているのかな?
コンパイル時に弾かれるから、直さないとUnitテストもできないので、かなり強制力が強い気がする。
※もしかしたら回避できるかも知れないが、俺にはここまでの実力しかないようだ。。。
ルールをカスタマイズしたりできるらしいが、ぶっちゃけよく分からん。
英語に対する苦手意識が強い。
FindBugsが本格的にダメになってきたら、ガンバってドキュメント読んで見る。
もしくは、誰か翻訳してぇ~
Google先生に翻訳お願いするしかなくなる。
今後のTODO
ルールのカスタマイズ