prettier-plugin-java
Advanced tools
Changelog
v2.1.0
// Input
/**
* @prettier
*/
public class Example {
private int test = -1;
}
// Output with require-pragma option activated
/**
* @prettier
*/
public class Example {
private int test = -1;
}
// Input
public class Example {
private int test = -1;
}
// Output with require-pragma option activated
public class Example {
private int test = -1;
}
// Input
class Example {
void example() {
Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
new Object().someMethod();
Object[] aParticularlyLongAndObnoxiousNameForIllustrativePurposes2 =
new Object[] { new Object(), new Object() };
Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes3 =
SomeClass.someStaticMethod();
Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
someMethod().anotherMethod();
Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
anotherValue != null ? anotherValue : new Object();
}
}
// Output
class Example {
void example() {
Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
new Object().someMethod();
Object[] aParticularlyLongAndObnoxiousNameForIllustrativePurposes2 =
new Object[] { new Object(), new Object() };
Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes3 =
SomeClass.someStaticMethod();
Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
someMethod().anotherMethod();
Object aParticularlyLongAndObnoxiousNameForIllustrativePurposes =
anotherValue != null ? anotherValue : new Object();
}
}
Changelog
v2.0.0
// Input
class T {
void test(Buyer other) {
return switch (other) {
case null -> true;
case Buyer b && this.bestPrice > b.bestPrice -> true;
case Buyer b && this.bestPrice > b.bestPrice -> {
return true;
}
case (Buyer b && this.bestPrice > b.bestPrice) -> true;
case Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice -> true;
case Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice -> {
return true;
}
case (
Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice
) -> true;
case (
Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice
) -> {
return true;
}
default -> false;
};
}
}
// Output
class T {
void test(Buyer other) {
return switch (other) {
case null -> true;
case Buyer b && this.bestPrice > b.bestPrice -> true;
case Buyer b && this.bestPrice > b.bestPrice -> {
return true;
}
case (Buyer b && this.bestPrice > b.bestPrice) -> true;
case Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice -> true;
case Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice -> {
return true;
}
case (
Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice
) -> true;
case (
Buyer b &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice &&
this.bestPrice > b.bestPrice
) -> {
return true;
}
default -> false;
};
}
}
public class Test {
public static final String REGEX = "^\s$";
}
// Input
open module org.myorg.module {
requires some.required.module;
exports org.myorg.module.exportpackage1;
exports org.myorg.module.exportpackage2;
}
// Prettier 1.6.2
open module org.myorg.module {
requires some.required.module;
exports org.myorg.module.exportpackage1;
exports org.myorg.module.exportpackage2;
}
// Prettier 1.6.3
open module org.myorg.module {
requires some.required.module;
exports org.myorg.module.exportpackage1;
exports org.myorg.module.exportpackage2;
}
Changelog
v1.6.2
public interface Test {
sealed interface Inner {}
public abstract static sealed class SealedParent {}
non-sealed interface Inner {}
public abstract static non-sealed class SealedParent {}
static final class SealedChild extends SealedParent {}
}
// Input
public class Foo<T> {
public <U extends @NotNull T> void example(U u) {}
public <U extends com.java.Any.@NotNull T> void example(U u) {}
}
// Prettier 1.6.1
public class Foo<T> {
public <U extends @NotNullT> void example(U u) {}
public <U extends com.java.Any.@NotNullT> void example(U u) {}
}
// Prettier 1.6.2
public class Foo<T> {
public <U extends @NotNull T> void example(U u) {}
public <U extends com.java.Any.@NotNull T> void example(U u) {}
}
Changelog
v1.6.1
// Input
public class SealedClasses {
public abstract static sealed class SealedParent permits SealedChild {}
static final class SealedChild extends SealedParent {}
}
// Output
public class NestedSealedClasses {
public abstract static sealed class SealedParent permits SealedChild {}
static final class SealedChild extends SealedParent {}
}
Changelog
v1.6.0
// Input
public record Record(
@JsonSerialize(
using = StatusSerializer.class,
nullsUsing = NullSerializer.class
)
@Schema(type = "integer", description = "Some fancy description")
Status status,
@NotNull Integer number
) {}
public record Record(
@JsonSerialize(
using = StatusSerializer.class,
nullsUsing = NullSerializer.class
)
@Schema(type = "integer", description = "Some fancy description")
// comment
Status status,
// another comment
@NotNull Integer number
) {}
// Prettier v1.5.0
public record Record(
@JsonSerialize(
using = StatusSerializer.class,
nullsUsing = NullSerializer.class
)
@Schema(type = "integer", description = "Some fancy description")
Status status,
@NotNull Integer number
) {}
public record Record(
@JsonSerialize(
using = StatusSerializer.class,
nullsUsing = NullSerializer.class
)
@Schema(type = "integer", description = "Some fancy description") // comment
Status status,
// another comment
@NotNull Integer number
) {}
// Prettier v1.6.0
public record Record(
@JsonSerialize(
using = StatusSerializer.class,
nullsUsing = NullSerializer.class
)
@Schema(type = "integer", description = "Some fancy description")
Status status,
@NotNull Integer number
) {}
public record Record(
@JsonSerialize(
using = StatusSerializer.class,
nullsUsing = NullSerializer.class
)
@Schema(type = "integer", description = "Some fancy description")
// comment
Status status,
// another comment
@NotNull Integer number
) {}
// Input
class Example {
void should_cast_with_single_element() {
var myElem = (int) othrElement;
var myElem = (A) othrElement;
var myElem = (A) (othrElement, value) -> othrElement + value;
var myElem = (Aaeaozeaonzeoazneaozenazonelkadndpndpazdpazdpazdpazdpazeazpeaazdpazdpazpdazdpa) othrElement;
}
void should_cast_with_additional_bounds() {
foo((A & B) obj);
foo((A & B & C) obj);
foo((Aaeaozeaonzeoazneaozenazone & Bazoieoainzeonaozenoazne & Cjneazeanezoanezoanzeoaneonazeono) obj);
foo((Aaeaozeaonzeoazneaozenazone & Bazoieoainzeonaozenoazne & Cjneazeanezoanezoanzeoaneonazeono) (othrElement, value) -> othrElement + value);
}
}
// Prettier v1.5.0
class Example {
void should_cast_with_single_element() {
var myElem = (int) othrElement;
var myElem = (A) othrElement;
var myElem = (A) (othrElement, value) -> othrElement + value;
var myElem = (Aaeaozeaonzeoazneaozenazonelkadndpndpazdpazdpazdpazdpazeazpeaazdpazdpazpdazdpa) othrElement;
}
void should_cast_with_additional_bounds() {
foo((A) & B obj);
foo((A) & B& C obj);
foo(
(Aaeaozeaonzeoazneaozenazone) & Bazoieoainzeonaozenoazne& Cjneazeanezoanezoanzeoaneonazeono obj
);
foo(
(Aaeaozeaonzeoazneaozenazone) & Bazoieoainzeonaozenoazne& Cjneazeanezoanezoanzeoaneonazeono (
othrElement,
value
) ->
othrElement + value
);
}
}
// Prettier v1.6.0
class Example {
void should_cast_with_single_element() {
var myElem = (int) othrElement;
var myElem = (A) othrElement;
var myElem = (A) (othrElement, value) -> othrElement + value;
var myElem = (Aaeaozeaonzeoazneaozenazonelkadndpndpazdpazdpazdpazdpazeazpeaazdpazdpazpdazdpa) othrElement;
}
void should_cast_with_additional_bounds() {
foo((A & B) obj);
foo((A & B & C) obj);
foo(
(
Aaeaozeaonzeoazneaozenazone
& Bazoieoainzeonaozenoazne
& Cjneazeanezoanezoanzeoaneonazeono
) obj
);
foo(
(
Aaeaozeaonzeoazneaozenazone
& Bazoieoainzeonaozenoazne
& Cjneazeanezoanezoanzeoaneonazeono
) (othrElement, value) -> othrElement + value
);
}
}
Changelog
v1.5.0
Split record parameters on several lines if they do not fit on a single line (#509)
// Input
public record Person(
String firstName,
String lastName,
String email,
String phoneNumber,
String streetAddress,
String city,
String state,
String zipCode
) {}
// Prettier 1.4.0
public record Person(
String firstName,
String lastName,
String email,
String phoneNumber,
String streetAddress,
String city,
String state,
String zipCode
) {}
// Prettier 1.5.0
public record Person(
String firstName,
String lastName,
String email,
String phoneNumber,
String streetAddress,
String city,
String state,
String zipCode
) {}
Support pattern matching in switch statements preview feature (#511)
// Input
class T {
static String formatterPatternSwitch(Object o) {
return switch (o) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s", s);
case TOTO -> String.format("TOTO %s", o);
case null -> String.format("Null !");
case default -> String.format("Default !");
default -> o.toString();
};
}
}
// Output
class T {
static String formatterPatternSwitch(Object o) {
return switch (o) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s", s);
case TOTO -> String.format("TOTO %s", o);
case null -> String.format("Null !");
case default -> String.format("Default !");
default -> o.toString();
};
}
}
Improve printing of class with long typeParameterList (#512)
// Input
public class ComplexGenericClass<
BEAN extends AbstractBean & BeanItemSelect<BEANTYPE>,
BEANTYPE,
CONFIG extends BeanConfig<BEAN, BEANTYPE, CONFIG>
> {}
// Prettier 1.4.0
public class ComplexGenericClass<
BEAN extends AbstractBean & BeanItemSelect<BEANTYPE>,
BEANTYPE,
CONFIG extends BeanConfig<BEAN, BEANTYPE, CONFIG>
> {}
// Prettier 1.5.0
public class ComplexGenericClass<
BEAN extends AbstractBean & BeanItemSelect<BEANTYPE>,
BEANTYPE,
CONFIG extends BeanConfig<BEAN, BEANTYPE, CONFIG>
> {}