prettier-plugin-java
Advanced tools
Changelog
v1.2.0
// Input
if (o instanceof Integer i || p instanceof Point || q instanceof Circle c || r instanceof Square) {
formatted = String.format("int %d", i);
} else if (o instanceof Long l) {
formatted = String.format("long %d", l);
} else if (o instanceof Double d) {
formatted = String.format("double %f", d);
} else if (o instanceof String s) {
formatted = String.format("String %s", s);
}
// Output
if (
o instanceof Integer i ||
p instanceof Point ||
q instanceof Circle c ||
r instanceof Square
) {
formatted = String.format("int %d", i);
} else if (o instanceof Long l) {
formatted = String.format("long %d", l);
} else if (o instanceof Double d) {
formatted = String.format("double %f", d);
} else if (o instanceof String s) {
formatted = String.format("String %s", s);
}
// Input
public sealed class Rectangle implements Shape permits Square {
private final double length;
private final double height;
public Rectangle(double length, double height) {
this.length = length;
this.height = height;
}
@Override
public double area() {
return length * height;
}
}
// Output
public sealed class Rectangle implements Shape permits Square {
private final double length;
private final double height;
public Rectangle(double length, double height) {
this.length = length;
this.height = height;
}
@Override
public double area() {
return length * height;
}
}
Changelog
v1.1.0
// Input
public record Pet(
@NotNull String name,
int age,
String... others,
Object @Nullable... errorMessageArgs
) {
public Pet {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
public void test() {}
}
// Output
public record Pet(
@NotNull String name,
int age,
String... others,
Object @Nullable... errorMessageArgs
) {
public Pet {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("Name cannot be blank");
}
}
public void test() {}
}
Changelog
v1.0.2
// Input
class T {
SomeClass.@Valid SomeInnerClass someInnerClass = someClass.getInteractions().get(0);
void process(
Map.@NonNull Entry<String, SkeletonConfiguration> entry,
@NonNull Map<String, Object> context
) {}
}
// Prettier 1.0.1
class T {
SomeClass.@ValidSomeInnerClass someInnerClass = someClass
.getInteractions()
.get(0);
void process(
Map.@NonNullEntry<String, SkeletonConfiguration> entry,
@NonNull Map<String, Object> context
) {}
}
// Prettier 1.0.2
class T {
SomeClass.@Valid SomeInnerClass someInnerClass = someClass
.getInteractions()
.get(0);
void process(
Map.@NonNull Entry<String, SkeletonConfiguration> entry,
@NonNull Map<String, Object> context
) {}
}
// Input
class T {
public void method() {
new Foo(stuff, thing, "auaaaaaaaaa some very long stuff", "some more")
.bar(10);
foo(stuff, thing, "some very longuuuuuuuuuuuuuu stuff", "some more")
.bar(10);
}
}
// Prettier 1.0.1
class T {
public void method() {
new Foo(stuff, thing, "auaaaaaaaaa some very long stuff", "some more")
.bar(10);
foo(stuff, thing, "some very longuuuuuuuuuuuuuu stuff", "some more")
.bar(10);
}
}
// Prettier 1.0.2
class T {
public void method() {
new Foo(stuff, thing, "auaaaaaaaaa some very long stuff", "some more")
.bar(10);
foo(stuff, thing, "some very longuuuuuuuuuuuuuu stuff", "some more")
.bar(10);
}
}
// Input
class T {
public void method() {
Arrays.asList( // a
// b
// c
// d
);
}
}
// Prettier 1.0.1
class T {
public void method() {
Arrays.asList( // b // a
// c
// d
);
}
}
// Prettier 1.0.2
class T {
public void method() {
Arrays.asList( // a
// b
// c
// d
);
}
}
Changelog
v1.0.1
// Input
public class Test {
public void test(TestEnum testEnum) {
switch (testEnum) {
case FOO -> System.out.println("Foo!");
case BAR, BAZ -> System.out.println("Not Foo!");
}
}
}
// Prettier 1.0.0
public class Test {
public void test(TestEnum testEnum) {
switch (testEnum) {
case FOO -> System.out.println("Foo!");
case BAR, BAZ -> System.out.println("Not Foo!");
}
}
}
// Prettier 1.0.1
public class Test {
public void test(TestEnum testEnum) {
switch (testEnum) {
case FOO -> System.out.println("Foo!");
case BAR, BAZ -> System.out.println("Not Foo!");
}
}
}
Changelog
v1.0.0
Support of Java 15 ! 🚀
Support of Text blocks
Support of new switch rules and yield statements
Improve throws rendering (From @jhaber: #384)
// Input
void throwException7(String string1, String string2, String string3, String string4) throws RuntimeException {
throw new RuntimeException();
}
// Prettier 0.8.3
void throwException7(
String string1,
String string2,
String string3,
String string4
)
throws RuntimeException {
throw new RuntimeException();
}
// Prettier 1.0.0
void throwException7(
String string1,
String string2,
String string3,
String string4
) throws RuntimeException {
throw new RuntimeException();
}
Changelog
v0.8.1
// Input
class Indent {
void indetMethod() {
assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
);
assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo();
assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo()
.anotherInvocation(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
);
myInstanceObject
.assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo();
}
}
// Output
class Indent {
void indetMethod() {
assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
);
assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo();
assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo()
.anotherInvocation(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa,
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
);
myInstanceObject
.assertThat(
useraaaaaaaaaaojzapjzpozjapjzpoajzpozaaaaaaaaaaaMapperlaaaaaaaaaaaaaaaaaaaaaaaa
)
.isEqualTo();
}
}
// Throws in Prettier 0.8.0
double[][]::new
// Prettier 0.9.0
double[][]::new
Changelog
v0.8.0
[
"variableInitializerList",
"block",
"blockStatements",
"blockStatement",
"localVariableDeclarationStatement",
"localVariableDeclaration",
"localVariableType",
"statement",
"statementWithoutTrailingSubstatement",
"emptyStatement",
"labeledStatement",
"expressionStatement",
"statementExpression",
"ifStatement",
"assertStatement",
"switchStatement",
"switchBlock",
"switchCase",
"switchLabel",
"enumConstantName",
"whileStatement",
"doStatement",
"forStatement",
"basicForStatement",
"forInit",
"forUpdate",
"statementExpressionList",
"enhancedForStatement",
"breakStatement",
"continueStatement",
"returnStatement",
"throwStatement",
"synchronizedStatement",
"tryStatement",
"catches",
"catchClause",
"catchFormalParameter",
"catchType",
"finally",
"tryWithResourcesStatement",
"resourceSpecification",
"resourceList",
"resource",
"resourceInit",
"variableAccess",
"isBasicForStatement",
"isLocalVariableDeclaration",
"classDeclaration",
"normalClassDeclaration",
"classModifier",
"typeParameters",
"typeParameterList",
"superclass",
"superinterfaces",
"interfaceTypeList",
"classBody",
"classBodyDeclaration",
"classMemberDeclaration",
"fieldDeclaration",
"fieldModifier",
"variableDeclaratorList",
"variableDeclarator",
"variableDeclaratorId",
"variableInitializer",
"unannType",
"unannPrimitiveType",
"unannReferenceType",
"unannClassOrInterfaceType",
"unannClassType",
"unannInterfaceType",
"unannTypeVariable",
"methodDeclaration",
"methodModifier",
"methodHeader",
"result",
"methodDeclarator",
"receiverParameter",
"formalParameterList",
"formalParameter",
"variableParaRegularParameter",
"variableArityParameter",
"variableModifier",
"throws",
"exceptionTypeList",
"exceptionType",
"methodBody",
"instanceInitializer",
"staticInitializer",
"constructorDeclaration",
"constructorModifier",
"constructorDeclarator",
"simpleTypeName",
"constructorBody",
"explicitConstructorInvocation",
"unqualifiedExplicitConstructorInvocation",
"qualifiedExplicitConstructorInvocation",
"enumDeclaration",
"enumBody",
"enumConstantList",
"enumConstant",
"enumConstantModifier",
"enumBodyDeclarations",
"isClassDeclaration",
"identifyClassBodyDeclarationType",
"isDims",
"constantExpression",
"expression",
"lambdaExpression",
"lambdaParameters",
"lambdaParametersWithBraces",
"lambdaParameterList",
"inferredLambdaParameterList",
"explicitLambdaParameterList",
"lambdaParameter",
"regularLambdaParameter",
"lambdaParameterType",
"lambdaBody",
"ternaryExpression",
"binaryExpression",
"unaryExpression",
"unaryExpressionNotPlusMinus",
"primary",
"primaryPrefix",
"primarySuffix",
"fqnOrRefType",
"fqnOrRefTypePartRest",
"fqnOrRefTypePartCommon",
"fqnOrRefTypePartFirst",
"parenthesisExpression",
"castExpression",
"primitiveCastExpression",
"referenceTypeCastExpression",
"newExpression",
"unqualifiedClassInstanceCreationExpression",
"classOrInterfaceTypeToInstantiate",
"typeArgumentsOrDiamond",
"diamond",
"methodInvocationSuffix",
"argumentList",
"arrayCreationExpression",
"arrayCreationDefaultInitSuffix",
"arrayCreationExplicitInitSuffix",
"dimExprs",
"dimExpr",
"classLiteralSuffix",
"arrayAccessSuffix",
"methodReferenceSuffix",
"identifyNewExpressionType",
"isLambdaExpression",
"isCastExpression",
"isPrimitiveCastExpression",
"isReferenceTypeCastExpression",
"isRefTypeInMethodRef",
"interfaceDeclaration",
"normalInterfaceDeclaration",
"interfaceModifier",
"extendsInterfaces",
"interfaceBody",
"interfaceMemberDeclaration",
"constantDeclaration",
"constantModifier",
"interfaceMethodDeclaration",
"interfaceMethodModifier",
"annotationTypeDeclaration",
"annotationTypeBody",
"annotationTypeMemberDeclaration",
"annotationTypeElementDeclaration",
"annotationTypeElementModifier",
"defaultValue",
"annotation",
"elementValuePairList",
"elementValuePair",
"elementValue",
"elementValueArrayInitializer",
"elementValueList",
"identifyInterfaceBodyDeclarationType",
"identifyAnnotationBodyDeclarationType",
"isSimpleElementValueAnnotation",
"literal",
"integerLiteral",
"floatingPointLiteral",
"booleanLiteral",
"moduleName",
"packageName",
"typeName",
"expressionName",
"methodName",
"packageOrTypeName",
"ambiguousName",
"compilationUnit",
"ordinaryCompilationUnit",
"modularCompilationUnit",
"packageDeclaration",
"packageModifier",
"importDeclaration",
"typeDeclaration",
"moduleDeclaration",
"moduleDirective",
"requiresModuleDirective",
"exportsModuleDirective",
"opensModuleDirective",
"usesModuleDirective",
"providesModuleDirective",
"requiresModifier",
"isModuleCompilationUnit",
"primitiveType",
"numericType",
"integralType",
"floatingPointType",
"referenceType",
"classOrInterfaceType",
"classType",
"interfaceType",
"typeVariable",
"dims",
"typeParameter",
"typeParameterModifier",
"typeBound",
"additionalBound",
"typeArguments",
"typeArgumentList",
"typeArgument",
"wildcard",
"wildcardBounds"
];
Fix formatting of empty enums to not insert commas when trailing-comma is enabled #385)
// Input
public enum Enum {}
// Prettier v0.7.1
public enum Enum {
}
// Prettier v0.8.0
public enum Enum {}
Fix formatting of enum with comments #385)