[JAVA] Find a Switch statement that can be converted to a Switch expression

What is this article

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.

Overview of search process

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.

Search target

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.

search results

--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();
    case VERTICAL:
        oldScrollValue = getScrollY();

From RunParameterDefinition.java of the jenkins project

switch(getFilter()) {
    case COMPLETED:
        lastBuild = project.getLastCompletedBuild();
    case SUCCESSFUL:
        lastBuild = project.getLastSuccessfulBuild();
    case STABLE:
        lastBuild = project.getLastStableBuild();
        lastBuild = project.getLastBuild();


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

Find a Switch statement that can be converted to a Switch expression
[Android] I want to create a ViewPager that can be used for tutorials
Write a class that can be ordered in Java
Check if a string can be converted to an int without using Integer # parseInt
Create a page control that can be used with RecyclerView
Create a jar file that can be executed in Gradle
Programming can be a god.
Introduction to Rakefile that can be done in about 10 minutes
Convert an array that may be null to a stream
Object-oriented design that can be used when you want to return a response in form format
How to make a key pair of ecdsa in a format that can be read by Java
When you want to check whether the contents of a property can be converted to a specific type
I made a question that can be used for a technical interview
Mecab installation that can be done almost by typing a command
Introduction to Java that can be understood even with Krillin (Part 1)
I met a great switch statement
To not be a static uncle
How to override in a model unit test so that Faker can be used to generate random values
A memo that enabled VS Code + JUnit 5 to be used on Windows 10
Object-oriented that can be understood by fairies
Log out a CSV file that can be read in Excel using logback
I made a THETA API client that can be used for plug-in development
Try to save the data that can be read by JavaFX as PNG
[Swift5] extension that allows UIImage to be specified by a String type URL