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

Add visitModifier() to base JavaVisitor #4562

Merged
merged 3 commits into from
Oct 11, 2024
Merged
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 @@ -229,6 +229,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, P p)
return (J.MethodInvocation) super.visitMethodInvocation(method, p);
}

@Override
public J.Modifier visitModifier(J.Modifier modifier, P p) {
return (J.Modifier) super.visitModifier(modifier, p);
}

@Override
public J.MultiCatch visitMultiCatch(J.MultiCatch multiCatch, P p) {
return (J.MultiCatch) super.visitMultiCatch(multiCatch, p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ protected void visitRightPadded(@Nullable JRightPadded<? extends J> rightPadded,
}
}

protected void visitModifier(Modifier mod, PrintOutputCapture<P> p) {
@Override
public J visitModifier(Modifier mod, PrintOutputCapture<P> p) {
visit(mod.getAnnotations(), p);
String keyword = "";
switch (mod.getType()) {
Expand Down Expand Up @@ -154,6 +155,7 @@ protected void visitModifier(Modifier mod, PrintOutputCapture<P> p) {
beforeSyntax(mod, Space.Location.MODIFIER_PREFIX, p);
p.append(keyword);
afterSyntax(mod, p);
return mod;
}

@Override
Expand Down
14 changes: 8 additions & 6 deletions rewrite-java/src/main/java/org/openrewrite/java/JavaVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,6 @@ public J visitClassDeclaration(J.ClassDeclaration classDecl, P p) {
c = (J.ClassDeclaration) temp;
}
c = c.withLeadingAnnotations(ListUtils.map(c.getLeadingAnnotations(), a -> visitAndCast(a, p)));
c = c.withModifiers(ListUtils.map(c.getModifiers(),
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.getPadding().withKind(
Expand Down Expand Up @@ -846,8 +844,6 @@ public J visitMethodDeclaration(J.MethodDeclaration method, P p) {
}
m = m.withLeadingAnnotations(ListUtils.map(m.getLeadingAnnotations(), a -> visitAndCast(a, p)));
m = m.withModifiers(ListUtils.map(m.getModifiers(), e -> visitAndCast(e, p)));
m = m.withModifiers(ListUtils.map(m.getModifiers(),
mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p))));
J.TypeParameters typeParameters = m.getAnnotations().getTypeParameters();
if (typeParameters != null) {
m = m.getAnnotations().withTypeParameters(typeParameters.withAnnotations(
Expand Down Expand Up @@ -920,6 +916,14 @@ public J visitMethodInvocation(J.MethodInvocation method, P p) {
return m;
}

public J visitModifier(J.Modifier modifer, P p) {
macsux marked this conversation as resolved.
Show resolved Hide resolved
J.Modifier m = modifer;
m = m.withPrefix(visitSpace(m.getPrefix(), Space.Location.MODIFIER_PREFIX, p));
m = m.withMarkers(visitMarkers(m.getMarkers(), p));
m = m.withAnnotations(ListUtils.map(m.getAnnotations(), a -> visitAndCast(a, p)));
return m;
}

public J visitMultiCatch(J.MultiCatch multiCatch, P p) {
J.MultiCatch m = multiCatch;
m = m.withPrefix(visitSpace(m.getPrefix(), Space.Location.MULTI_CATCH_PREFIX, p));
Expand All @@ -941,8 +945,6 @@ public J visitVariableDeclarations(J.VariableDeclarations multiVariable, P p) {
}
m = m.withLeadingAnnotations(ListUtils.map(m.getLeadingAnnotations(), a -> visitAndCast(a, p)));
m = m.withModifiers(Objects.requireNonNull(ListUtils.map(m.getModifiers(), e -> visitAndCast(e, p))));
m = m.withModifiers(ListUtils.map(m.getModifiers(),
mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p))));
m = m.withTypeExpression(visitAndCast(m.getTypeExpression(), p));
m = m.withTypeExpression(m.getTypeExpression() == null ?
null :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

return t;
}

@Override
protected void visitModifier(J.Modifier modifier, PrintOutputCapture<ExecutionContext> p) {
PositionPrintOutputCapture prefix = new PositionPrintOutputCapture(ppoc.pos, ppoc.line, ppoc.column);
spacePrinter.visitSpace(modifier.getPrefix(), Space.Location.ANY, prefix);

Range.Position startPosition = new Range.Position(prefix.pos, prefix.line, prefix.column);
super.visitModifier(modifier, p);
Range.Position endPosition = new Range.Position(ppoc.pos, ppoc.line, ppoc.column);
positionMap.put(modifier, new Range(randomId(), startPosition, endPosition));
}

};

return new JavaVisitor<ExecutionContext>() {
Expand Down
5 changes: 5 additions & 0 deletions rewrite-java/src/main/java/org/openrewrite/java/tree/J.java
Original file line number Diff line number Diff line change
Expand Up @@ -4015,6 +4015,11 @@ public static boolean hasModifier(Collection<Modifier> modifiers, Modifier.Type
@Getter
List<Annotation> annotations;

@Override
public <P> J acceptJava(JavaVisitor<P> v, P p) {
return v.visitModifier(this, p);
}

/**
* @deprecated Use {@link #Modifier(UUID, Space, Markers, String, Type, List)} instead.
*/
Expand Down