Skip to content

Commit

Permalink
Merge pull request #28513 from pubudu91/deprecate-param-apis
Browse files Browse the repository at this point in the history
Deprecate name() and kind() APIs in ParameterSymbol
  • Loading branch information
pubudu91 authored Feb 9, 2021
2 parents 84c2700 + c3e25fc commit e002057
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ protected void visitParameter(ParameterSymbol parameterSymbol) {
StringJoiner joiner = new StringJoiner(" ");
parameterSymbol.qualifiers().forEach(accessModifier -> joiner.add(accessModifier.getValue()));
String signature;
if (parameterSymbol.kind() == ParameterKind.REST) {
if (parameterSymbol.paramKind() == ParameterKind.REST) {
signature = transformType(parameterSymbol.typeDescriptor());
signature = signature.substring(0, signature.length() - 2) + "...";
} else {
signature = transformType(parameterSymbol.typeDescriptor());
}

joiner.add(signature);
if (parameterSymbol.name().isPresent()) {
joiner.add(parameterSymbol.name().get());
if (parameterSymbol.getName().isPresent()) {
joiner.add(parameterSymbol.getName().get());
}

this.setState(joiner.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public Optional<String> name() {
return Optional.ofNullable(parameterName);
}

@Override
public Optional<String> getName() {
return Optional.ofNullable(this.parameterName);
}

/**
* Get the type descriptor of the field.
*
Expand Down Expand Up @@ -96,15 +101,15 @@ public String signature() {
StringJoiner joiner = new StringJoiner(" ");
this.qualifiers().forEach(accessModifier -> joiner.add(accessModifier.getValue()));
String signature;
if (this.kind() == ParameterKind.REST) {
if (this.paramKind() == ParameterKind.REST) {
signature = this.typeDescriptor().signature();
signature = signature.substring(0, signature.length() - 2) + "...";
} else {
signature = this.typeDescriptor().signature();
}
joiner.add(signature);
if (this.name().isPresent()) {
joiner.add(this.name().get());
if (this.getName().isPresent()) {
joiner.add(this.getName().get());
}

return joiner.toString();
Expand All @@ -114,4 +119,9 @@ public String signature() {
public ParameterKind kind() {
return this.kind;
}

@Override
public ParameterKind paramKind() {
return this.kind;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -16,36 +16,37 @@
*/
package io.ballerina.compiler.api.symbols;

import java.util.List;
import java.util.Optional;

/**
* Represents a parameter with a name and type.
*
* @since 2.0.0
*/
public interface ParameterSymbol extends Annotatable {
public interface ParameterSymbol extends Annotatable, Qualifiable {

/**
* Get the parameter name.
*
* @return {@link Optional} name of the field
* @deprecated This method will be removed in a later version. Use getName() instead.
*/
@Deprecated
Optional<String> name();

/**
* Get the type descriptor of the field.
* Get the parameter name.
*
* @return {@link TypeSymbol} of the field
* @return {@link Optional} name of the field
*/
TypeSymbol typeDescriptor();
Optional<String> getName();

/**
* Get the access modifiers.
* Get the type descriptor of the field.
*
* @return {@link List} of access modifiers
* @return {@link TypeSymbol} of the field
*/
List<Qualifier> qualifiers();
TypeSymbol typeDescriptor();

/**
* Get a string representation of the signature of the parameter.
Expand All @@ -59,5 +60,13 @@ public interface ParameterSymbol extends Annotatable {
*
* @return {@link ParameterKind} of the param
*/
@Deprecated
ParameterKind kind();

/**
* Get the kind of the parameter.
*
* @return {@link ParameterKind} of the param
*/
ParameterKind paramKind();
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private static Either<String, MarkupContent> getDocumentation(FunctionSymbol fun
docAttachment.ifPresent(documentation -> documentation.parameterMap().forEach(docParamsMap::put));

List<ParameterSymbol> defaultParams = functionTypeDesc.parameters().stream()
.filter(parameter -> parameter.kind() == ParameterKind.DEFAULTABLE)
.filter(parameter -> parameter.paramKind() == ParameterKind.DEFAULTABLE)
.collect(Collectors.toList());

MarkupContent docMarkupContent = new MarkupContent();
Expand All @@ -183,11 +183,11 @@ private static Either<String, MarkupContent> getDocumentation(FunctionSymbol fun
}

Optional<ParameterSymbol> defaultVal = defaultParams.stream()
.filter(parameter -> parameter.name().get().equals(param.name().get()))
.filter(parameter -> parameter.getName().get().equals(param.getName().get()))
.findFirst();
String paramDescription = "- " + "`" + paramType + "` " + param.name().get();
if (param.name().isPresent() && docParamsMap.containsKey(param.name().get())) {
paramDescription += ": " + docParamsMap.get(param.name().get());
String paramDescription = "- " + "`" + paramType + "` " + param.getName().get();
if (param.getName().isPresent() && docParamsMap.containsKey(param.getName().get())) {
paramDescription += ": " + docParamsMap.get(param.getName().get());
}
if (defaultVal.isPresent()) {
joiner.add(paramDescription + "(Defaultable)");
Expand Down Expand Up @@ -269,15 +269,15 @@ private static List<String> getFuncArguments(FunctionSymbol symbol, BallerinaCom
continue;
}
ParameterSymbol param = parameterDefs.get(i);
args.add(CommonUtil.getModifiedTypeName(ctx, param.typeDescriptor()) + (param.name().isEmpty() ? ""
: " " + param.name().get()));
args.add(CommonUtil.getModifiedTypeName(ctx, param.typeDescriptor()) + (param.getName().isEmpty() ? ""
: " " + param.getName().get()));
}
restParam.ifPresent(param -> {
// Rest param is represented as an array type symbol
ArrayTypeSymbol typeSymbol = (ArrayTypeSymbol) param.typeDescriptor();
args.add(CommonUtil.getModifiedTypeName(ctx, typeSymbol.memberTypeDescriptor())
+ (param.name().isEmpty() ? "" : "... "
+ param.name().get()));
+ (param.getName().isEmpty() ? "" : "... "
+ param.getName().get()));
});
return (!args.isEmpty()) ? args : new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ private static Hover getFunctionHoverMarkupContent(FunctionSymbol symbol, HoverC

params.addAll(symbol.typeDescriptor().parameters().stream()
.map(param -> {
if (param.name().isEmpty()) {
if (param.getName().isEmpty()) {
return quotedString(CommonUtil.getModifiedTypeName(ctx, param.typeDescriptor()));
}
String paramName = param.name().get();
String paramName = param.getName().get();
String desc = paramsMap.get(paramName);
return quotedString(CommonUtil.getModifiedTypeName(ctx, param.typeDescriptor())) + " "
+ italicString(boldString(paramName)) + " : " + desc;
Expand All @@ -311,9 +311,9 @@ private static Hover getFunctionHoverMarkupContent(FunctionSymbol symbol, HoverC
if (restParam.isPresent()) {
String modifiedTypeName = CommonUtil.getModifiedTypeName(ctx, restParam.get().typeDescriptor());
StringBuilder restParamBuilder = new StringBuilder(quotedString(modifiedTypeName + "..."));
if (restParam.get().name().isPresent()) {
restParamBuilder.append(" ").append(italicString(boldString(restParam.get().name().get())))
.append(" : ").append(paramsMap.get(restParam.get().name().get()));
if (restParam.get().getName().isPresent()) {
restParamBuilder.append(" ").append(italicString(boldString(restParam.get().getName().get())))
.append(" : ").append(paramsMap.get(restParam.get().getName().get()));
}
params.add(restParamBuilder.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ public Parameter(ParameterSymbol parameterSymbol,
}

public Optional<String> getName() {
return (parameterSymbol.name().isPresent() && this.isOptional)
? Optional.of(parameterSymbol.name().get() + "?") : parameterSymbol.name();
return (parameterSymbol.getName().isPresent() && this.isOptional)
? Optional.of(parameterSymbol.getName().get() + "?") : parameterSymbol.getName();
}

public String getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ static Map<String, Value> generateNamedArgs(SuspendedContext context, String fun

for (ParameterSymbol parameterSymbol : definition.parameters()) {
params.add(parameterSymbol);
remainingParams.put(parameterSymbol.name().get(), parameterSymbol);
remainingParams.put(parameterSymbol.getName().get(), parameterSymbol);
}
if (definition.restParam().isPresent()) {
params.add(definition.restParam().get());
remainingParams.put(definition.restParam().get().name().get(), definition.restParam().get());
remainingParams.put(definition.restParam().get().getName().get(), definition.restParam().get());
}

Map<String, Value> argValues = new HashMap<>();
Expand All @@ -76,7 +76,7 @@ static Map<String, Value> generateNamedArgs(SuspendedContext context, String fun
"too many arguments in call to '" + functionName + "'."));
}

String parameterName = params.get(i).name().get();
String parameterName = params.get(i).getName().get();
argValues.put(parameterName, arg.getValue().evaluate().getJdiValue());
remainingParams.remove(parameterName);
} else if (argType == ArgType.NAMED) {
Expand All @@ -101,7 +101,7 @@ static Map<String, Value> generateNamedArgs(SuspendedContext context, String fun

String restParamName = null;
for (Map.Entry<String, ParameterSymbol> entry : remainingParams.entrySet()) {
ParameterKind parameterType = entry.getValue().kind();
ParameterKind parameterType = entry.getValue().paramKind();
if (parameterType == ParameterKind.REST) {
restParamName = entry.getKey();
break;
Expand All @@ -119,7 +119,7 @@ static Map<String, Value> generateNamedArgs(SuspendedContext context, String fun

for (Map.Entry<String, ParameterSymbol> entry : remainingParams.entrySet()) {
String paramName = entry.getKey();
ParameterKind parameterType = entry.getValue().kind();
ParameterKind parameterType = entry.getValue().paramKind();
if (parameterType == ParameterKind.REQUIRED) {
throw new EvaluationException(String.format(EvaluationExceptionKind.CUSTOM_ERROR.getString(),
"missing required parameter '" + paramName + "'."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,21 +476,21 @@ private static List<Function> getInclusionFunctions(TypeSymbol typeSymbol, Type
.anyMatch(annotationSymbol -> annotationSymbol.name().equals("deprecated"));
Type type = new Type(parameterSymbol.typeDescriptor().signature());
Type.resolveSymbol(type, parameterSymbol.typeDescriptor());
parameters.add(new DefaultableVariable(parameterSymbol.name().isPresent() ?
parameterSymbol.name().get() : "", "", parameterDeprecated, type, ""));
parameters.add(new DefaultableVariable(parameterSymbol.getName().isPresent() ?
parameterSymbol.getName().get() : "", "", parameterDeprecated, type, ""));
});

if (methodSymbol.typeDescriptor().restParam().isPresent()) {
ParameterSymbol restParam = methodSymbol.typeDescriptor().restParam().get();
boolean parameterDeprecated = restParam.annotations().stream()
.anyMatch(annotationSymbol -> annotationSymbol.name().equals("deprecated"));
Type type = new Type(restParam.name().isPresent() ? restParam.name().get() : "");
Type type = new Type(restParam.getName().isPresent() ? restParam.getName().get() : "");
type.isRestParam = true;
Type elemType = new Type(restParam.typeDescriptor().signature());
Type.resolveSymbol(elemType, restParam.typeDescriptor());
type.elementType = elemType;
parameters.add(new DefaultableVariable(restParam.name().isPresent() ?
restParam.name().get() : "", "", parameterDeprecated, type, ""));
parameters.add(new DefaultableVariable(restParam.getName().isPresent() ?
restParam.getName().get() : "", "", parameterDeprecated, type, ""));
}

if (methodSymbol.typeDescriptor().returnTypeDescriptor().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public void testSymbolAtCursor() {

MethodSymbol initMethod = symbol.initMethod().get();
assertEquals(initMethod.name(), "init");
assertEquals(
initMethod.typeDescriptor().parameters().stream().map(p -> p.name().get()).collect(Collectors.toList()),
fieldNames);
assertEquals(initMethod.typeDescriptor().parameters().stream()
.map(p -> p.getName().get())
.collect(Collectors.toList()), fieldNames);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,8 +607,8 @@ private Symbol getSymbol(int line, int column) {
}

private void validateParam(ParameterSymbol param, String name, ParameterKind kind, TypeDescKind typeKind) {
assertEquals(param.name().get(), name);
assertEquals(param.kind(), kind);
assertEquals(param.getName().get(), name);
assertEquals(param.paramKind(), kind);
assertEquals(param.typeDescriptor().typeKind(), typeKind);
}
}

0 comments on commit e002057

Please sign in to comment.