Skip to content

Commit

Permalink
Remove J.ClassDeclaration.Annotations wrapper (#4172)
Browse files Browse the repository at this point in the history
* Remove `J.ClassDeclaration.Annotations` wrapper

Instead, the already existing `Padding` wrapper can be used to access the raw `kind` property value.

* Fix references
  • Loading branch information
knutwannheden committed May 13, 2024
1 parent 249c0a5 commit 9f634a3
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ public J visitClassDeclaration(ClassDeclaration classDecl, PrintOutputCapture<P>
for (Modifier m : classDecl.getModifiers()) {
visitModifier(m, p);
}
visit(classDecl.getAnnotations().getKind().getAnnotations(), p);
visitSpace(classDecl.getAnnotations().getKind().getPrefix(), Space.Location.CLASS_KIND, p);
visit(classDecl.getPadding().getKind().getAnnotations(), p);
visitSpace(classDecl.getPadding().getKind().getPrefix(), Space.Location.CLASS_KIND, p);
p.append(kind);
visit(classDecl.getName(), p);
visitContainer("<", classDecl.getPadding().getTypeParameters(), JContainer.Location.TYPE_PARAMETERS, ",", ">", p);
Expand Down
25 changes: 11 additions & 14 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,14 +457,14 @@ public J visitClassDeclaration(J.ClassDeclaration classDecl, P p) {
mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p))));
c = c.withModifiers(ListUtils.map(c.getModifiers(), m -> visitAndCast(m, p)));
//Kind can have annotations associated with it, need to visit those.
c = c.getAnnotations().withKind(
classDecl.getAnnotations().getKind().withAnnotations(
ListUtils.map(classDecl.getAnnotations().getKind().getAnnotations(), a -> visitAndCast(a, p))
c = c.getPadding().withKind(
classDecl.getPadding().getKind().withAnnotations(
ListUtils.map(classDecl.getPadding().getKind().getAnnotations(), a -> visitAndCast(a, p))
)
);
c = c.getAnnotations().withKind(
c.getAnnotations().getKind().withPrefix(
visitSpace(c.getAnnotations().getKind().getPrefix(), Space.Location.CLASS_KIND, p)
c = c.getPadding().withKind(
c.getPadding().getKind().withPrefix(
visitSpace(c.getPadding().getKind().getPrefix(), Space.Location.CLASS_KIND, p)
)
);
c = c.withName(visitAndCast(c.getName(), p));
Expand Down Expand Up @@ -1404,16 +1404,13 @@ public <T> JLeftPadded<T> visitLeftPadded(@Nullable JLeftPadded<T> left, JLeftPa
}

setCursor(getCursor().getParent());
if (t == null) {
// If nothing changed leave AST node the same
if (left.getElement() == null && before == left.getBefore()) {
return left;
}
//noinspection ConstantConditions
return null;
// If nothing changed leave AST node the same
if (left.getElement() == t && before == left.getBefore()) {
return left;
}

return (before == left.getBefore() && t == left.getElement()) ? left : new JLeftPadded<>(before, t, left.getMarkers());
//noinspection ConstantConditions
return t == null ? null : new JLeftPadded<>(before, t, left.getMarkers());
}

public <J2 extends J> JContainer<J2> visitContainer(@Nullable JContainer<J2> container,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
} else if (c.getPadding().getTypeParameters() != null) {
c = c.getPadding().withTypeParameters(c.getPadding().getTypeParameters().withBefore(c.getPadding().getTypeParameters().getBefore().withWhitespace("")));
} else {
c = c.getAnnotations().withKind(c.getAnnotations().getKind().withPrefix(c.getAnnotations().getKind().getPrefix().withWhitespace("")));
c = c.getPadding().withKind(c.getPadding().getKind().withPrefix(c.getPadding().getKind().getPrefix().withWhitespace("")));
}
} else {
List<J.Annotation> newLeadingAnnotations = removeAnnotationOrEmpty(leadingAnnotations, annotationRemoved);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
return c;
}

if (!c.getAnnotations().getKind().getPrefix().isEmpty()) {
c = concatenatePrefix(c, c.getAnnotations().getKind().getPrefix());
c = c.getAnnotations().withKind(c.getAnnotations().getKind().withPrefix(Space.EMPTY));
if (!c.getPadding().getKind().getPrefix().isEmpty()) {
c = concatenatePrefix(c, c.getPadding().getKind().getPrefix());
c = c.getPadding().withKind(c.getPadding().getKind().withPrefix(Space.EMPTY));
return c;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, P
if (!j.getModifiers().isEmpty()) {
j = j.withModifiers(withNewline(j.getModifiers()));
} else {
J.ClassDeclaration.Kind kind = j.getAnnotations().getKind();
J.ClassDeclaration.Kind kind = j.getPadding().getKind();
if (!kind.getPrefix().getWhitespace().contains("\n")) {
j = j.getAnnotations().withKind(kind.withPrefix(
j = j.getPadding().withKind(kind.withPrefix(
kind.getPrefix().withWhitespace("\n" + kind.getPrefix().getWhitespace())
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public J visitClassDeclaration(J.ClassDeclaration classDecl, Integer p) {
c = c.withTypeParameters(ListUtils.map(c.getTypeParameters(), tp -> tp.withAnnotations(emptyList())));
}
c = c.withModifiers(ListUtils.map(c.getModifiers(), m -> m.withAnnotations(emptyList())));
c = c.getAnnotations().withKind(c.getAnnotations().getKind().withAnnotations(emptyList()));
c = c.getPadding().withKind(c.getPadding().getKind().withAnnotations(emptyList()));
} else {
for (J.Annotation a : gen) {
c = c.withLeadingAnnotations(ListUtils.insertInOrder(c.getLeadingAnnotations(), a,
Expand Down
47 changes: 8 additions & 39 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -1159,10 +1159,6 @@ final class ClassDeclaration implements J, Statement, TypedTree {
@NonFinal
transient WeakReference<Padding> padding;

@Nullable
@NonFinal
transient WeakReference<Annotations> annotations;

@With
@Getter
@EqualsAndHashCode.Include
Expand Down Expand Up @@ -1191,11 +1187,11 @@ public Kind.Type getKind() {
}

public ClassDeclaration withKind(Kind.Type type) {
Kind k = getAnnotations().getKind();
Kind k = getPadding().getKind();
if (k.type == type) {
return this;
} else {
return getAnnotations().withKind(k.withType(type));
return getPadding().withKind(k.withType(type));
}
}

Expand Down Expand Up @@ -1420,34 +1416,6 @@ public ClassDeclaration withTypeParameters(@Nullable JContainer<TypeParameter> t
return t.typeParameters == typeParameters ? t : new ClassDeclaration(t.id, t.prefix, t.markers, t.leadingAnnotations, t.modifiers, t.kind, t.name, typeParameters, t.primaryConstructor, t.extendings, t.implementings, t.permitting, t.body, t.type);
}
}

public Annotations getAnnotations() {
Annotations a;
if (this.annotations == null) {
a = new Annotations(this);
this.annotations = new WeakReference<>(a);
} else {
a = this.annotations.get();
if (a == null || a.t != this) {
a = new Annotations(this);
this.annotations = new WeakReference<>(a);
}
}
return a;
}

@RequiredArgsConstructor
public static class Annotations {
private final ClassDeclaration t;

public Kind getKind() {
return t.kind;
}

public ClassDeclaration withKind(Kind kind) {
return t.kind == kind ? t : new ClassDeclaration(t.id, t.prefix, t.markers, t.leadingAnnotations, t.modifiers, kind, t.name, t.typeParameters, t.primaryConstructor, t.extendings, t.implementings, t.permitting, t.body, t.type);
}
}
}

@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -3586,19 +3554,20 @@ public List<TypeParameter> getTypeParameters() {
}

public MethodDeclaration withTypeParameters(@Nullable List<TypeParameter> typeParameters) {
Annotations annotations = getAnnotations();
if (typeParameters == null) {
if (this.getAnnotations().getTypeParameters() == null) {
if (annotations.getTypeParameters() == null) {
return this;
} else {
return this.getAnnotations().withTypeParameters(null);
return annotations.withTypeParameters(null);
}
} else {
TypeParameters currentTypeParameters = this.getAnnotations().getTypeParameters();
TypeParameters currentTypeParameters = annotations.getTypeParameters();
if (currentTypeParameters == null) {
return getAnnotations().withTypeParameters(new TypeParameters(Tree.randomId(), Space.EMPTY, Markers.EMPTY,
return annotations.withTypeParameters(new TypeParameters(Tree.randomId(), Space.EMPTY, Markers.EMPTY,
null, typeParameters.stream().map(JRightPadded::build).collect(toList())));
} else {
return getAnnotations().withTypeParameters(currentTypeParameters.withTypeParameters(typeParameters));
return annotations.withTypeParameters(currentTypeParameters.withTypeParameters(typeParameters));
}
}
}
Expand Down

0 comments on commit 9f634a3

Please sign in to comment.