Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use padding for J.Assert#detail model #4699

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public J visitAssert(Assert assert_, PrintOutputCapture<P> p) {
beforeSyntax(assert_, Space.Location.ASSERT_PREFIX, p);
p.append("assert");
visit(assert_.getCondition(), p);
visitLeftPadded(":", assert_.getDetail(), JLeftPadded.Location.ASSERT_DETAIL, p);
visitLeftPadded(":", assert_.getPadding().getDetail(), JLeftPadded.Location.ASSERT_DETAIL, p);
afterSyntax(assert_, p);
return assert_;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public J visitAssert(J.Assert assert_, P p) {
}
a = a.withCondition(visitAndCast(a.getCondition(), p));
if (a.getDetail() != null) {
a = a.withDetail(visitLeftPadded(a.getDetail(), JLeftPadded.Location.ASSERT_DETAIL, p));
a = a.getPadding().withDetail(visitLeftPadded(a.getPadding().getDetail(), JLeftPadded.Location.ASSERT_DETAIL, p));
}
return a;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public J.Assert visitAssert(J.Assert _assert, J j) {

visit(_assert.getCondition(), compareTo.getCondition());
if (_assert.getDetail() != null && compareTo.getDetail() != null) {
visit(_assert.getDetail().getElement(), compareTo.getDetail().getElement());
visit(_assert.getDetail(), compareTo.getDetail());
}
}
return _assert;
Expand Down
48 changes: 46 additions & 2 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,25 +411,41 @@ public CoordinateBuilder.Expression getCoordinates() {

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true)
@Data
@RequiredArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
final class Assert implements J, Statement {
@Nullable
@NonFinal
transient WeakReference<Padding> padding;

@With
@Getter
@EqualsAndHashCode.Include
UUID id;

@With
@Getter
Space prefix;

@With
@Getter
Markers markers;

@With
@Getter
Expression condition;

@Nullable
@With
JLeftPadded<Expression> detail;

public @Nullable Expression getDetail() {
return detail != null ? detail.getElement() : null;
}

public Assert withDetail(@Nullable Expression detail) {
return getPadding().withDetail(JLeftPadded.withElement(this.detail, detail));
}

@Override
public <P> J acceptJava(JavaVisitor<P> v, P p) {
return v.visitAssert(this, p);
Expand All @@ -441,10 +457,38 @@ public CoordinateBuilder.Statement getCoordinates() {
return new CoordinateBuilder.Statement(this);
}

public Padding getPadding() {
Padding p;
if (this.padding == null) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
} else {
p = this.padding.get();
if (p == null || p.t != this) {
p = new Padding(this);
this.padding = new WeakReference<>(p);
}
}
return p;
}

@Override
public String toString() {
return withPrefix(Space.EMPTY).printTrimmed(new JavaPrinter<>());
}

@RequiredArgsConstructor
public static class Padding {
private final Assert t;

public @Nullable JLeftPadded<Expression> getDetail() {
return t.detail;
}

public Assert withDetail(@Nullable JLeftPadded<Expression> detail) {
return t.detail == detail ? t : new Assert(t.id, t.prefix, t.markers, t.condition, detail);
}
}
}

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
Expand Down