Skip to content
Draft
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ class B { class B {
returnType,
new J.MethodDeclaration.IdentifierWithAnnotations(name, emptyList()),
JContainer.build(beforeParen, params, Markers.EMPTY),
emptyList(),
throws_,
body,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,28 @@ public J visitMethod(MethodTree node, Space fmt) {
}

List<J.Annotation> returnTypeAnnotations = collectAnnotations(annotationPosTable);
TypeTree returnType = convert(node.getReturnType());
boolean cStyleArrayReturn = false;
TypeTree returnType;
if (node.getReturnType() instanceof JCArrayTypeTree) {
JCExpression elementType = (JCExpression) node.getReturnType();
while (elementType instanceof JCArrayTypeTree || elementType instanceof JCAnnotatedType) {
if (elementType instanceof JCAnnotatedType) {
elementType = ((JCAnnotatedType) elementType).underlyingType;
}
if (elementType instanceof JCArrayTypeTree) {
elementType = ((JCArrayTypeTree) elementType).elemtype;
}
}
int idx = indexOfNextNonWhitespace(elementType.getEndPosition(endPosTable), source);
if (idx != -1 && (source.charAt(idx) == '[' || source.charAt(idx) == '@')) {
returnType = convert(node.getReturnType());
} else {
returnType = convert(elementType);
cStyleArrayReturn = true;
}
} else {
returnType = convert(node.getReturnType());
}
if (returnType != null && !returnTypeAnnotations.isEmpty()) {
returnType = new J.AnnotatedType(randomId(), EMPTY, Markers.EMPTY,
returnTypeAnnotations, returnType);
Expand Down Expand Up @@ -979,6 +1000,8 @@ public J visitMethod(MethodTree node, Space fmt) {
JContainer.build(paramFmt, singletonList(padRight(new J.Empty(randomId(), sourceBefore(")"),
Markers.EMPTY), EMPTY)), Markers.EMPTY);

List<JLeftPadded<Space>> cStyleDimensions = cStyleArrayReturn ? arrayDimensions() : emptyList();

JContainer<NameTree> throws_ = node.getThrows().isEmpty() ? null :
JContainer.build(sourceBefore("throws"), convertAll(node.getThrows(), commaDelim, noDelim),
Markers.EMPTY);
Expand All @@ -991,7 +1014,7 @@ public J visitMethod(MethodTree node, Space fmt) {
return new J.MethodDeclaration(randomId(), fmt, Markers.EMPTY,
modifierResults.getLeadingAnnotations(),
modifierResults.getModifiers(), typeParams,
returnType, name, params, throws_, body, defaultValue,
returnType, name, params, cStyleDimensions, throws_, body, defaultValue,
typeMapping.methodDeclarationType(jcMethod.sym, null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,28 @@ public J visitMethod(MethodTree node, Space fmt) {
}

List<J.Annotation> returnTypeAnnotations = collectAnnotations(annotationPosTable);
TypeTree returnType = convert(node.getReturnType());
boolean cStyleArrayReturn = false;
TypeTree returnType;
if (node.getReturnType() instanceof JCArrayTypeTree) {
JCExpression elementType = (JCExpression) node.getReturnType();
while (elementType instanceof JCArrayTypeTree || elementType instanceof JCAnnotatedType) {
if (elementType instanceof JCAnnotatedType) {
elementType = ((JCAnnotatedType) elementType).underlyingType;
}
if (elementType instanceof JCArrayTypeTree) {
elementType = ((JCArrayTypeTree) elementType).elemtype;
}
}
int idx = indexOfNextNonWhitespace(elementType.getEndPosition(endPosTable), source);
if (idx != -1 && (source.charAt(idx) == '[' || source.charAt(idx) == '@')) {
returnType = convert(node.getReturnType());
} else {
returnType = convert(elementType);
cStyleArrayReturn = true;
}
} else {
returnType = convert(node.getReturnType());
}
if (returnType != null && !returnTypeAnnotations.isEmpty()) {
returnType = new J.AnnotatedType(randomId(), EMPTY, Markers.EMPTY,
returnTypeAnnotations, returnType);
Expand Down Expand Up @@ -1116,6 +1137,8 @@ public J visitMethod(MethodTree node, Space fmt) {
Markers.EMPTY), EMPTY)), Markers.EMPTY);
}

List<JLeftPadded<Space>> cStyleDimensions = cStyleArrayReturn ? arrayDimensions() : emptyList();

JContainer<NameTree> throws_ = node.getThrows().isEmpty() ? null :
JContainer.build(sourceBefore("throws"), convertAll(node.getThrows(), commaDelim, noDelim),
Markers.EMPTY);
Expand All @@ -1128,7 +1151,7 @@ public J visitMethod(MethodTree node, Space fmt) {
J.MethodDeclaration md = new J.MethodDeclaration(randomId(), fmt, Markers.EMPTY,
modifierResults.getLeadingAnnotations(),
modifierResults.getModifiers(), typeParams,
returnType, name, params, throws_, body, defaultValue,
returnType, name, params, cStyleDimensions, throws_, body, defaultValue,
typeMapping.methodDeclarationType(jcMethod.sym, null));
return isCompactConstructor ? md.withMarkers(md.getMarkers().addIfAbsent(new CompactConstructor(randomId()))) : md;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,28 @@ public J visitMethod(MethodTree node, Space fmt) {
}

List<J.Annotation> returnTypeAnnotations = collectAnnotations(annotationPosTable);
TypeTree returnType = convert(node.getReturnType());
boolean cStyleArrayReturn = false;
TypeTree returnType;
if (node.getReturnType() instanceof JCArrayTypeTree) {
JCExpression elementType = (JCExpression) node.getReturnType();
while (elementType instanceof JCArrayTypeTree || elementType instanceof JCAnnotatedType) {
if (elementType instanceof JCAnnotatedType) {
elementType = ((JCAnnotatedType) elementType).underlyingType;
}
if (elementType instanceof JCArrayTypeTree) {
elementType = ((JCArrayTypeTree) elementType).elemtype;
}
}
int idx = indexOfNextNonWhitespace(elementType.getEndPosition(endPosTable), source);
if (idx != -1 && (source.charAt(idx) == '[' || source.charAt(idx) == '@')) {
returnType = convert(node.getReturnType());
} else {
returnType = convert(elementType);
cStyleArrayReturn = true;
}
} else {
returnType = convert(node.getReturnType());
}
if (returnType != null && !returnTypeAnnotations.isEmpty()) {
returnType = new J.AnnotatedType(randomId(), EMPTY, Markers.EMPTY,
returnTypeAnnotations, returnType);
Expand Down Expand Up @@ -1147,6 +1168,8 @@ public J visitMethod(MethodTree node, Space fmt) {
Markers.EMPTY), EMPTY)), Markers.EMPTY);
}

List<JLeftPadded<Space>> cStyleDimensions = cStyleArrayReturn ? arrayDimensions() : emptyList();

JContainer<NameTree> throws_ = node.getThrows().isEmpty() ? null :
JContainer.build(sourceBefore("throws"), convertAll(node.getThrows(), commaDelim, noDelim),
Markers.EMPTY);
Expand All @@ -1159,7 +1182,7 @@ public J visitMethod(MethodTree node, Space fmt) {
J.MethodDeclaration md = new J.MethodDeclaration(randomId(), fmt, Markers.EMPTY,
modifierResults.getLeadingAnnotations(),
modifierResults.getModifiers(), typeParams,
returnType, name, params, throws_, body, defaultValue,
returnType, name, params, cStyleDimensions, throws_, body, defaultValue,
typeMapping.methodDeclarationType(jcMethod.sym, null));
return isCompactConstructor ? md.withMarkers(md.getMarkers().addIfAbsent(new CompactConstructor(randomId()))) : md;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,28 @@ public J visitMethod(MethodTree node, Space fmt) {
}

List<J.Annotation> returnTypeAnnotations = collectAnnotations(annotationPosTable);
TypeTree returnType = convert(node.getReturnType());
boolean cStyleArrayReturn = false;
TypeTree returnType;
if (node.getReturnType() instanceof JCArrayTypeTree) {
JCExpression elementType = (JCExpression) node.getReturnType();
while (elementType instanceof JCArrayTypeTree || elementType instanceof JCAnnotatedType) {
if (elementType instanceof JCAnnotatedType) {
elementType = ((JCAnnotatedType) elementType).underlyingType;
}
if (elementType instanceof JCArrayTypeTree) {
elementType = ((JCArrayTypeTree) elementType).elemtype;
}
}
int idx = indexOfNextNonWhitespace(elementType.getEndPosition(endPosTable), source);
if (idx != -1 && (source.charAt(idx) == '[' || source.charAt(idx) == '@')) {
returnType = convert(node.getReturnType());
} else {
returnType = convert(elementType);
cStyleArrayReturn = true;
}
} else {
returnType = convert(node.getReturnType());
}
if (returnType != null && !returnTypeAnnotations.isEmpty()) {
returnType = new J.AnnotatedType(randomId(), EMPTY, Markers.EMPTY,
returnTypeAnnotations, returnType);
Expand Down Expand Up @@ -1179,6 +1200,8 @@ public J visitMethod(MethodTree node, Space fmt) {
Markers.EMPTY), EMPTY)), Markers.EMPTY);
}

List<JLeftPadded<Space>> cStyleDimensions = cStyleArrayReturn ? arrayDimensions() : emptyList();

JContainer<NameTree> throws_ = node.getThrows().isEmpty() ? null :
JContainer.build(sourceBefore("throws"), convertAll(node.getThrows(), commaDelim, noDelim),
Markers.EMPTY);
Expand All @@ -1191,7 +1214,7 @@ public J visitMethod(MethodTree node, Space fmt) {
J.MethodDeclaration md = new J.MethodDeclaration(randomId(), fmt, Markers.EMPTY,
modifierResults.getLeadingAnnotations(),
modifierResults.getModifiers(), typeParams,
returnType, name, params, throws_, body, defaultValue,
returnType, name, params, cStyleDimensions, throws_, body, defaultValue,
typeMapping.methodDeclarationType(jcMethod.sym, null));
return isCompactConstructor ? md.withMarkers(md.getMarkers().addIfAbsent(new CompactConstructor(randomId()))) : md;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,28 @@ public J visitMethod(MethodTree node, Space fmt) {
}

List<J.Annotation> returnTypeAnnotations = collectAnnotations(annotationPosTable);
TypeTree returnType = convert(node.getReturnType());
boolean cStyleArrayReturn = false;
TypeTree returnType;
if (node.getReturnType() instanceof JCArrayTypeTree) {
JCExpression elementType = (JCExpression) node.getReturnType();
while (elementType instanceof JCArrayTypeTree || elementType instanceof JCAnnotatedType) {
if (elementType instanceof JCAnnotatedType) {
elementType = ((JCAnnotatedType) elementType).underlyingType;
}
if (elementType instanceof JCArrayTypeTree) {
elementType = ((JCArrayTypeTree) elementType).elemtype;
}
}
int idx = indexOfNextNonWhitespace(elementType.getEndPosition(endPosTable), source);
if (idx != -1 && (source.charAt(idx) == '[' || source.charAt(idx) == '@')) {
returnType = convert(node.getReturnType());
} else {
returnType = convert(elementType);
cStyleArrayReturn = true;
}
} else {
returnType = convert(node.getReturnType());
}
if (returnType != null && !returnTypeAnnotations.isEmpty()) {
returnType = new J.AnnotatedType(randomId(), EMPTY, Markers.EMPTY,
returnTypeAnnotations, returnType);
Expand Down Expand Up @@ -978,6 +999,8 @@ public J visitMethod(MethodTree node, Space fmt) {
JContainer.build(paramFmt, singletonList(padRight(new J.Empty(randomId(), sourceBefore(")"),
Markers.EMPTY), EMPTY)), Markers.EMPTY);

List<JLeftPadded<Space>> cStyleDimensions = cStyleArrayReturn ? arrayDimensions() : emptyList();

JContainer<NameTree> throws_ = node.getThrows().isEmpty() ? null :
JContainer.build(sourceBefore("throws"), convertAll(node.getThrows(), commaDelim, noDelim),
Markers.EMPTY);
Expand All @@ -990,7 +1013,7 @@ public J visitMethod(MethodTree node, Space fmt) {
return new J.MethodDeclaration(randomId(), fmt, Markers.EMPTY,
modifierResults.getLeadingAnnotations(),
modifierResults.getModifiers(), typeParams,
returnType, name, params, throws_, body, defaultValue,
returnType, name, params, cStyleDimensions, throws_, body, defaultValue,
typeMapping.methodDeclarationType(jcMethod.sym, null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,57 @@ public void load(String str) {
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/6398")
@Test
void cstyleArrayMethod() {
rewriteRun(
// language=java
java(
"""
class A {
public byte toByteArray()[] {
return new byte[]{};
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/6398")
@Test
void cstyleArrayMethodWithParams() {
rewriteRun(
// language=java
java(
"""
class A {
public byte getData(int offset, int length)[] {
return new byte[]{};
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/6398")
@Test
void cstyleArrayMethodMultiDimension() {
rewriteRun(
// language=java
java(
"""
class A {
public int get()[][] {
return new int[][]{};
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/5445")
@Test
void parseSwitchBlock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,12 @@ public J visitMethodDeclaration(MethodDeclaration method, PrintOutputCapture<P>
if (!method.getMarkers().findFirst(CompactConstructor.class).isPresent()) {
visitContainer("(", method.getPadding().getParameters(), JContainer.Location.METHOD_DECLARATION_PARAMETERS, ",", ")", p);
}
for (JLeftPadded<Space> dimension : method.getDimensionsAfterName()) {
visitSpace(dimension.getBefore(), Space.Location.DIMENSION_PREFIX, p);
p.append('[');
visitSpace(dimension.getElement(), Space.Location.DIMENSION, p);
p.append(']');
}
visitContainer("throws", method.getPadding().getThrows(), JContainer.Location.THROWS, ",", null, p);
visit(method.getBody(), p);
visitLeftPadded("default", method.getPadding().getDefaultValue(), JLeftPadded.Location.METHOD_DECLARATION_DEFAULT_VALUE, p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ public J visitMethodDeclaration(J.MethodDeclaration method, RpcReceiveQueue q) {
.getAnnotations().withName(method.getAnnotations().getName().withAnnotations(q.receiveList(method.getAnnotations().getName().getAnnotations(), a -> (J.Annotation) visitNonNull(a, q))))
.withName(q.receive(method.getName(), n -> (J.Identifier) visitNonNull(n, q)))
.getPadding().withParameters(q.receive(method.getPadding().getParameters(), p -> visitContainer(p, q)))
.withDimensionsAfterName(q.receiveList(method.getDimensionsAfterName(), d -> visitLeftPadded(d, q)))
.getPadding().withThrows(q.receive(method.getPadding().getThrows(), t -> visitContainer(t, q)))
.withBody(q.receive(method.getBody(), b -> (J.Block) visitNonNull(b, q)))
.getPadding().withDefaultValue(q.receive(method.getPadding().getDefaultValue(), d -> visitLeftPadded(d, q)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ public J visitMethodDeclaration(J.MethodDeclaration method, RpcSendQueue q) {
q.getAndSendList(method, m -> m.getAnnotations().getName().getAnnotations(), J.Annotation::getId, name -> visit(name, q));
q.getAndSend(method, J.MethodDeclaration::getName, name -> visit(name, q));
q.getAndSend(method, m -> m.getPadding().getParameters(), params -> visitContainer(params, q));
q.getAndSendList(method, J.MethodDeclaration::getDimensionsAfterName, l -> l.getElement().toString(), dim -> visitLeftPadded(dim, q));
q.getAndSend(method, m -> m.getPadding().getThrows(), thr -> visitContainer(thr, q));
q.getAndSend(method, J.MethodDeclaration::getBody, body -> visit(body, q));
q.getAndSend(method, m -> m.getPadding().getDefaultValue(), def -> visitLeftPadded(def, q));
Expand Down
Loading
Loading