Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
prettier-plugin-java
Advanced tools
The prettier-plugin-java npm package is a plugin for Prettier that formats Java code. It ensures that Java code adheres to a consistent style, making it easier to read and maintain.
Code Formatting
This feature automatically formats Java code to follow a consistent style. For example, it ensures proper indentation, spacing, and line breaks.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Consistent Style
This feature enforces a consistent coding style across the entire codebase, making it easier to read and maintain. It handles things like method spacing, bracket placement, and more.
public class Example {
private int value;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
Integration with Prettier
This feature allows the plugin to be easily integrated with Prettier, enabling seamless formatting of Java code alongside other languages supported by Prettier.
{
"prettier": {
"plugins": ["prettier-plugin-java"]
}
}
google-java-format is a program that reformats Java source code to comply with Google Java Style. It is similar to prettier-plugin-java in that it enforces a consistent style, but it specifically adheres to Google's style guide.
Spotless is a general-purpose code formatter that supports multiple languages, including Java. It can be configured to use different formatters, such as google-java-format or prettier-plugin-java, providing flexibility in formatting rules.
Prettier is an opinionated code formatter which forces a certain coding style. It makes the code consistent through an entire project.
This plugin allows the support of Java on Prettier.
The plugin implementation is pretty straightforward as it uses java-parser (thanks to Chevrotain) visitor to traverse the Concrete Syntax Tree and apply the format processing on each node (it uses Prettier API).
Since the plugin is meant to be used with Prettier, you need to install it:
npm install --save-dev --save-exact prettier
or
yarn add prettier --dev --exact
npm install prettier-plugin-java --save-dev
or
yarn add prettier-plugin-java --dev
If you installed Prettier globally and want to format java code via the CLI, run the following command:
npm install -g prettier-plugin-java
The plugin will be automatically loaded, check here for more.
prettier --write MyJavaFile.java
If the plugin is not automatically loaded:
# Example where the plugin is locate in node_modules
prettier --write MyJavaFile.java --plugin=./node_modules/prettier-plugin-java
const prettier = require("prettier");
const javaText = `
public class HelloWorldExample{
public static void main(String args[]){
System.out.println("Hello World !");
}
}
`;
const formattedText = prettier.format(javaText, {
parser: "java",
tabWidth: 2
});
public class HelloWorld {
public static void main(String[] args) {System.out.println("Hello World!");;;;;}
@Override
public String toString() {
return "Hello World";
}
public int sum(int argument1,int argument2,int argument3,int argument4,int argument5
) {
return argument1+argument2+ argument3 +argument4 + argument5;
}
}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
@Override
public String toString() {
return "Hello World";
}
public int sum(
int argument1,
int argument2,
int argument3,
int argument4,
int argument5
) {
return argument1 + argument2 + argument3 + argument4 + argument5;
}
}
We added a custom option entrypoint
in order to run prettier on code snippet.
prettier --write MyJava.java --entrypoint compilationUnit
Here is the exhaustive list of all entrypoints.
MyJavaCode.java content:
public void myfunction() {
mymethod.is().very().very().very().very().very().very().very().very().very().very().very().very().very().very().big();
}
Run:
prettier --write MyJavaCode.java --entrypoint classBodyDeclaration
Result:
public void myfunction() {
mymethod
.is()
.very()
.very()
.very()
.very()
.very()
.very()
.very()
.very()
.very()
.very()
.very()
.very()
.very()
.very()
.big();
}
FAQs
Prettier Java Plugin
The npm package prettier-plugin-java receives a total of 120,441 weekly downloads. As such, prettier-plugin-java popularity was classified as popular.
We found that prettier-plugin-java demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.