Skip to content
Draft
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
5 changes: 5 additions & 0 deletions rewrite-csharp/csharp/OpenRewrite/CSharp/CSharpParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,7 @@ [new JRightPadded<Statement>(returnStmt, Space.Empty, Markers.Empty)],
returnType,
name,
paramsContainer,
[], // DimensionsAfterName
null, // Throws
body,
null, // DefaultValue
Expand Down Expand Up @@ -1495,6 +1496,7 @@ [new JRightPadded<Statement>(returnStmt, Space.Empty, Markers.Empty)],
null, // ReturnTypeExpression (constructors have none)
name,
paramsContainer,
[], // DimensionsAfterName
null, // Throws
body,
defaultValue,
Expand Down Expand Up @@ -1579,6 +1581,7 @@ [new JRightPadded<Statement>(returnStmt, Space.Empty, Markers.Empty)],
null, // TypeParameters
null, // ReturnTypeExpression (destructors have none)
name, paramsContainer,
[], // DimensionsAfterName
null, // Throws
body,
null, // DefaultValue
Expand Down Expand Up @@ -7442,6 +7445,7 @@ private MethodDeclaration ParsePrimaryConstructor(ParameterListSyntax paramList)
null, // ReturnType (constructors don't have return types)
methodName,
paramsContainer,
[], // DimensionsAfterName
null, // Throws
null, // Body
null, // DefaultValue
Expand Down Expand Up @@ -8129,6 +8133,7 @@ [new JRightPadded<Statement>(returnStmt, Space.Empty, Markers.Empty)],
returnType,
name,
paramsContainer,
[], // DimensionsAfterName
null, // Throws
body,
null, // DefaultValue
Expand Down
30 changes: 17 additions & 13 deletions rewrite-csharp/csharp/OpenRewrite/Java/J.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,7 @@ public sealed class MethodDeclaration(
TypeTree? returnTypeExpression,
Identifier name,
JContainer<Statement> parameters,
IList<JLeftPadded<Space>> dimensionsAfterName,
JContainer<NameTree>? throws,
Block? body,
JLeftPadded<Expression>? defaultValue,
Expand All @@ -1097,37 +1098,40 @@ public sealed class MethodDeclaration(
public TypeTree? ReturnTypeExpression { get; } = returnTypeExpression;
public Identifier Name { get; } = name;
public JContainer<Statement> Parameters { get; } = parameters;
public IList<JLeftPadded<Space>> DimensionsAfterName { get; } = dimensionsAfterName;
public JContainer<NameTree>? Throws { get; } = throws;
public Block? Body { get; } = body;
public JLeftPadded<Expression>? DefaultValue { get; } = defaultValue;
public JavaType.Method? MethodType { get; } = methodType;

public MethodDeclaration WithId(Guid id) =>
id == Id ? this : new(id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, Throws, Body, DefaultValue, MethodType);
id == Id ? this : new(id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, DimensionsAfterName, Throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithPrefix(Space prefix) =>
ReferenceEquals(prefix, Prefix) ? this : new(Id, prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, Throws, Body, DefaultValue, MethodType);
ReferenceEquals(prefix, Prefix) ? this : new(Id, prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, DimensionsAfterName, Throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithMarkers(Markers markers) =>
ReferenceEquals(markers, Markers) ? this : new(Id, Prefix, markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, Throws, Body, DefaultValue, MethodType);
ReferenceEquals(markers, Markers) ? this : new(Id, Prefix, markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, DimensionsAfterName, Throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithLeadingAnnotations(IList<Annotation> leadingAnnotations) =>
ReferenceEquals(leadingAnnotations, LeadingAnnotations) ? this : new(Id, Prefix, Markers, leadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, Throws, Body, DefaultValue, MethodType);
ReferenceEquals(leadingAnnotations, LeadingAnnotations) ? this : new(Id, Prefix, Markers, leadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, DimensionsAfterName, Throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithModifiers(IList<Modifier> modifiers) =>
ReferenceEquals(modifiers, Modifiers) ? this : new(Id, Prefix, Markers, LeadingAnnotations, modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, Throws, Body, DefaultValue, MethodType);
ReferenceEquals(modifiers, Modifiers) ? this : new(Id, Prefix, Markers, LeadingAnnotations, modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, DimensionsAfterName, Throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithTypeParameters(JContainer<TypeParameter>? typeParameters) =>
ReferenceEquals(typeParameters, TypeParameters) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, typeParameters, ReturnTypeExpression, Name, Parameters, Throws, Body, DefaultValue, MethodType);
ReferenceEquals(typeParameters, TypeParameters) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, typeParameters, ReturnTypeExpression, Name, Parameters, DimensionsAfterName, Throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithReturnTypeExpression(TypeTree? returnTypeExpression) =>
ReferenceEquals(returnTypeExpression, ReturnTypeExpression) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, returnTypeExpression, Name, Parameters, Throws, Body, DefaultValue, MethodType);
ReferenceEquals(returnTypeExpression, ReturnTypeExpression) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, returnTypeExpression, Name, Parameters, DimensionsAfterName, Throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithName(Identifier name) =>
ReferenceEquals(name, Name) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, name, Parameters, Throws, Body, DefaultValue, MethodType);
ReferenceEquals(name, Name) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, name, Parameters, DimensionsAfterName, Throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithParameters(JContainer<Statement> parameters) =>
ReferenceEquals(parameters, Parameters) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, parameters, Throws, Body, DefaultValue, MethodType);
ReferenceEquals(parameters, Parameters) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, parameters, DimensionsAfterName, Throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithDimensionsAfterName(IList<JLeftPadded<Space>> dimensionsAfterName) =>
ReferenceEquals(dimensionsAfterName, DimensionsAfterName) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, dimensionsAfterName, Throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithThrows(JContainer<NameTree>? throws) =>
ReferenceEquals(throws, Throws) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, throws, Body, DefaultValue, MethodType);
ReferenceEquals(throws, Throws) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, DimensionsAfterName, throws, Body, DefaultValue, MethodType);
public MethodDeclaration WithBody(Block? body) =>
ReferenceEquals(body, Body) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, Throws, body, DefaultValue, MethodType);
ReferenceEquals(body, Body) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, DimensionsAfterName, Throws, body, DefaultValue, MethodType);
public MethodDeclaration WithDefaultValue(JLeftPadded<Expression>? defaultValue) =>
ReferenceEquals(defaultValue, DefaultValue) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, Throws, Body, defaultValue, MethodType);
ReferenceEquals(defaultValue, DefaultValue) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, DimensionsAfterName, Throws, Body, defaultValue, MethodType);
public MethodDeclaration WithMethodType(JavaType.Method? methodType) =>
ReferenceEquals(methodType, MethodType) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, Throws, Body, DefaultValue, methodType);
ReferenceEquals(methodType, MethodType) ? this : new(Id, Prefix, Markers, LeadingAnnotations, Modifiers, TypeParameters, ReturnTypeExpression, Name, Parameters, DimensionsAfterName, Throws, Body, DefaultValue, methodType);

Tree Tree.WithId(Guid id) => WithId(id);

Expand Down
3 changes: 2 additions & 1 deletion rewrite-csharp/csharp/OpenRewrite/Java/Rpc/JavaReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,12 @@ public override J VisitMethodDeclaration(MethodDeclaration method, RpcReceiveQue
var nameAnnotations = q.ReceiveList(method.Name?.Annotations ?? [], el => (Annotation)VisitNonNull(el, q));
var name = q.Receive((J?)method.Name ?? new Identifier(Guid.NewGuid(), Space.Empty, Markers.Empty, [], "", null, null), el => (J)VisitNonNull(el, q));
var parameters = q.Receive(method.Parameters, c => VisitContainer(c, q));
var dimensionsAfterName = q.ReceiveList(method.DimensionsAfterName, lp => VisitLeftPadded(lp, q));
var throws_ = q.Receive(method.Throws, c => VisitContainer(c, q));
var body = q.Receive((J?)method.Body, el => (J)VisitNonNull(el!, q));
var defaultValue = q.Receive(method.DefaultValue, lp => VisitLeftPadded(lp, q));
var methodType = q.Receive(method.MethodType, t => (JavaType.Method)VisitType(t, q)!);
return method.WithId(_pvId).WithPrefix(_pvPrefix).WithMarkers(_pvMarkers).WithLeadingAnnotations(leadingAnnotations!).WithModifiers(modifiers!).WithTypeParameters(typeParameters).WithReturnTypeExpression((TypeTree?)returnTypeExpression).WithName(((Identifier)name!).WithAnnotations(nameAnnotations!)).WithParameters(parameters!).WithThrows(throws_).WithBody((Block?)body).WithDefaultValue(defaultValue).WithMethodType(methodType);
return method.WithId(_pvId).WithPrefix(_pvPrefix).WithMarkers(_pvMarkers).WithLeadingAnnotations(leadingAnnotations!).WithModifiers(modifiers!).WithTypeParameters(typeParameters).WithReturnTypeExpression((TypeTree?)returnTypeExpression).WithName(((Identifier)name!).WithAnnotations(nameAnnotations!)).WithParameters(parameters!).WithDimensionsAfterName(dimensionsAfterName!).WithThrows(throws_).WithBody((Block?)body).WithDefaultValue(defaultValue).WithMethodType(methodType);
}

public override J VisitMethodInvocation(MethodInvocation methodInvocation, RpcReceiveQueue q)
Expand Down
2 changes: 2 additions & 0 deletions rewrite-csharp/csharp/OpenRewrite/Java/Rpc/JavaSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ public override J VisitMethodDeclaration(MethodDeclaration method, RpcSendQueue
q.GetAndSendList(method, m => m.Name.Annotations, a => a.Id, a => Visit(a, q));
q.GetAndSend(method, m => (J)m.Name, name => Visit(name, q));
q.GetAndSend(method, m => m.Parameters, @params => VisitContainer(@params, q));
q.GetAndSendList(method, m => m.DimensionsAfterName,
lp => lp.Element.ToString()!, lp => VisitLeftPadded(lp, q));
q.GetAndSend(method, m => m.Throws, thr => VisitContainer(thr, q));
q.GetAndSend(method, m => m.Body, body => Visit(body, q));
q.GetAndSend(method, m => m.DefaultValue, def => VisitLeftPadded(def, q));
Expand Down
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
Loading