Casual source code analysis starting today announced at JJUG CCC 2019 Spring -ccc-2019-spring) is a detailed version of the use case explained in the content of the announcement.
Starting with Java 12, Switch expressions have been added as a preview feature. Since Switch can be treated as an expression, it can be written in the rvalue of the assignment statement.
For details, this article is very easy to understand.
When new syntax is added to a programming language, I often wonder, "So, how useful is it?"
This time, in order to solve such a question and satisfy intellectual curiosity, I would like to search the actual source code for a Switch statement that can be converted into a Switch expression.
Source code analysis technology is used to search the source code.
Specifically, it uses a library called JavaParser to perform parsing and perform a search process on the generated abstract syntax tree.
This time, I thought about "Switch statement that can be converted to Switch expression" as follows, and searched for Switch statements that match the conditions.
--A two-statement CASE must always be a assignment statement + return
or a assignment statement + break
.
--There must be at least one CASE consisting of two sentences.
--A single statement CASE must always be return
or break
.
--There can be any number of CASEs that have no statement.
--There must be no CASE with more than two statements.
--All variables on the left side of the assignment statement must be the same variable.
The source code used for the analysis is here.
I got 100 Java projects on GitHub in descending order of stars and targeted them. The search query is as follows.
curl -o github_java_top100.json "https://api.github.com/search/repositories?q=stars:>0+language:Java&sort=stars&order=desc&page=1&per_page=100"
The survey target was acquired around February 2019, so it may be different from the current star order. Also, it doesn't take into account what the project looks like or whether it is archived.
--Java files: 120,996 files --Processing time: Approximately 20 minutes --Finded Switch statements: 639
The summary of the search results in markdown format is here.
I haven't been able to confirm all the search results, but a quick glance seems to indicate that the search was as intended. Below are two examples of good search results.
From PullToRefreshBase.java of the Android-PullToRefresh project
switch(getPullToRefreshScrollDirection()) {
case HORIZONTAL:
oldScrollValue = getScrollX();
break;
case VERTICAL:
default:
oldScrollValue = getScrollY();
break;
}
From RunParameterDefinition.java of the jenkins project
switch(getFilter()) {
case COMPLETED:
lastBuild = project.getLastCompletedBuild();
break;
case SUCCESSFUL:
lastBuild = project.getLastSuccessfulBuild();
break;
case STABLE:
lastBuild = project.getLastStableBuild();
break;
default:
lastBuild = project.getLastBuild();
break;
}
This time, by searching for a Switch statement that can be converted to a Switch expression, I actually executed a search process on the source code of the project on GitHub.
Personally, I feel that there are more places that can be converted than I expected.
In the future, let's utilize the Switch type where it can be used.
Recommended Posts