A JetBrains plugin that refactors Java code to use the idiomatic var
keyword introduced in Java 10. This plugin helps modernize your codebase by automatically replacing explicit type declarations with the var
keyword where appropriate.
- Automatically identifies eligible variables for conversion to
var
- Handles local variable declarations in Java 10+ projects
- Supports batch refactoring for multiple files
- Configurable rules for when to apply
var
conversions - Works via action in the refactoring menu and as intention actions
- In your IDE, go to
Settings
>Plugins
>Marketplace
- Search for "Java Var Refactoring"
- Click
Install
and restart the IDE
- Download the latest release JAR file from the Releases page
- In your IDE, go to
Settings
>Plugins
>⚙️
>Install Plugin from Disk...
- Select the downloaded JAR file and restart the IDE
- Select a Java file, class, or package in the Project view
- Right-click and select
Refactor
>Convert to 'var'
- Review the changes in the preview and apply them
- Place the cursor on a variable declaration
- Press
Alt+Enter
(orOption+Enter
on macOS) - Select
Convert to 'var'
from the menu
Configure the plugin behavior at Settings
> Tools
> Java Var Refactoring
:
- Replace primitive types with 'var' - Convert
int
,boolean
, etc. tovar
- Replace for-loop variables with 'var' - Apply to variables in for loops
- Replace diamond operator types with 'var' - Replace
List<>
usage withvar
- Replace when declared type differs from initializer type - Apply even when explicit type provides additional information
- Refactor variables initialized with anonymous classes - Apply to variables with anonymous class initializers
- Refactor variables initialized with lambda expressions - Apply to variables with lambda initializers
The project is organized into several modules to promote cross-platform compatibility:
- core - Contains the core refactoring logic, independent of any IDE
- common - Shared utilities and interfaces
- intellij-plugin - IntelliJ-specific implementation
- lsp-server - Language Server Protocol implementation for other editors
This plugin is designed with cross-platform compatibility in mind. Here's how it can be extended to other IDEs and editors:
The lsp-server
module provides a Language Server Protocol implementation that can be used with VS Code:
- Build the LSP server:
./gradlew :lsp-server:shadowJar
- Create a VS Code extension that launches the server JAR
- Configure the extension to handle Java files and provide the appropriate commands
Example package.json
for a VS Code extension:
{
"name": "java-var-refactoring-plugin",
"displayName": "Java Var Refactoring",
"description": "Refactors Java code to use the var keyword",
"version": "1.0.0",
"engines": {
"vscode": "^1.65.0"
},
"categories": [
"Programming Languages"
],
"activationEvents": [
"onLanguage:java"
],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "java.var.refactor",
"title": "Convert to 'var'"
}
],
"menus": {
"editor/context": [
{
"when": "editorLangId == java",
"command": "java.var.refactor",
"group": "1_modification"
}
]
}
}
}
For Eclipse integration:
- Build a bundle using the core module
- Implement an Eclipse plugin that bridges between Eclipse's JDT and the core refactoring logic
- Provide appropriate extension points for refactoring actions
For NetBeans integration:
- Use the core module in a NetBeans module
- Implement the NetBeans-specific UI and action handlers
- Connect to the NetBeans Java source tree API
- JDK 17 or higher
- Gradle 7.4 or higher
-
Clone the repository
git clone https://github.com/rlogman/java-var-refactoring-plugin.git cd java-var-refactoring-plugin
-
Build the project
./gradlew build
-
Run the plugin in a development IDE instance
./gradlew runIde
The project is set up with the following structure:
java-var-refactoring-plugin/
├── core/ # Core refactoring logic
├── common/ # Shared utilities
├── intellij-plugin/ # IntelliJ plugin implementation
├── lsp-server/ # Language Server Protocol implementation
├── docs/ # Documentation
├── build.gradle.kts # Main build configuration
├── settings.gradle.kts # Project settings
└── README.md # Project documentation
Run the tests with:
./gradlew test
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.