Skip to content

Commit

Permalink
Merge pull request #42897 from Shadow-Devil/use-pattern-matching-vari…
Browse files Browse the repository at this point in the history
…ables

[Refactoring] Use pattern matching variable instead of cast
  • Loading branch information
gimantha authored Sep 10, 2024
2 parents bc7e5e9 + 4d2abe5 commit a0bceed
Show file tree
Hide file tree
Showing 207 changed files with 1,054 additions and 1,289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ protected Object invokeScheduledMethod(ClassLoader classLoader, String className
// Unexpected runtime error
throw new InvokerPanicException(panic);
}
if (result instanceof Throwable) {
if (result instanceof Throwable throwable) {
// Function returned error (panic)
throw new InvokerPanicException((Throwable) result);
throw new InvokerPanicException(throwable);
}
return result;
} catch (ClassNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,25 +227,23 @@ public PackageCompilation getCompilation(Collection<Snippet> newSnippets) throws
// Others are processed later.
for (Snippet newSnippet : newSnippets) {

if (newSnippet instanceof ImportDeclarationSnippet) {
processImport((ImportDeclarationSnippet) newSnippet);
if (newSnippet instanceof ImportDeclarationSnippet importSnippet) {
processImport(importSnippet);

} else if (newSnippet instanceof VariableDeclarationSnippet) {
VariableDeclarationSnippet varDclnSnippet = (VariableDeclarationSnippet) newSnippet;
} else if (newSnippet instanceof VariableDeclarationSnippet varDclnSnippet) {
variableNames.addAll(varDclnSnippet.names());
variableDeclarations.put(varDclnSnippet, varDclnSnippet.names());

} else if (newSnippet instanceof ModuleMemberDeclarationSnippet) {
ModuleMemberDeclarationSnippet moduleDclnSnippet = (ModuleMemberDeclarationSnippet) newSnippet;
} else if (newSnippet instanceof ModuleMemberDeclarationSnippet moduleDclnSnippet) {
Identifier moduleDeclarationName = moduleDclnSnippet.name();
moduleDeclarations.put(moduleDeclarationName, moduleDclnSnippet);
availableModuleDeclarations.put(moduleDeclarationName, moduleDclnSnippet);
Set<Identifier> usedPrefixes = newSnippet.usedImports().stream()
.map(Identifier::new).collect(Collectors.toSet());
newImports.put(moduleDeclarationName, usedPrefixes);

} else if (newSnippet instanceof ExecutableSnippet) {
executableSnippets.add((ExecutableSnippet) newSnippet);
} else if (newSnippet instanceof ExecutableSnippet executableSnippet) {
executableSnippets.add(executableSnippet);

} else {
throw new UnsupportedOperationException("Unimplemented snippet category.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public static GlobalVariableSymbol fromSymbol(Symbol symbol) {
if (symbol.getName().isEmpty()) {
throw new UnsupportedOperationException("Cannot create a global symbol without name");
}
if (symbol instanceof VariableSymbol) {
return new GlobalVariableSymbol(symbol.getName().get(), ((VariableSymbol) symbol).typeDescriptor());
} else if (symbol instanceof FunctionSymbol) {
return new GlobalVariableSymbol(symbol.getName().get(), ((FunctionSymbol) symbol).typeDescriptor());
if (symbol instanceof VariableSymbol variableSymbol) {
return new GlobalVariableSymbol(symbol.getName().get(), variableSymbol.typeDescriptor());
} else if (symbol instanceof FunctionSymbol functionSymbol) {
return new GlobalVariableSymbol(symbol.getName().get(), functionSymbol.typeDescriptor());
}
throw new UnsupportedOperationException("Symbol type not supported for creating global variable.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public Collection<Node> parseDeclarations(String source) throws TreeParserExcept
* Whether the declaration is allowed to be parsed.
*/
private boolean isModuleDeclarationAllowed(ModuleMemberDeclarationNode declarationNode) {
if (declarationNode instanceof FunctionDefinitionNode) {
String functionName = ((FunctionDefinitionNode) declarationNode).functionName().text();
if (declarationNode instanceof FunctionDefinitionNode functionDefinitionNode) {
String functionName = functionDefinitionNode.functionName().text();
if (ParserConstants.isFunctionNameRestricted(functionName)) {
addWarnDiagnostic("Found '" + functionName + "' function in the declarations.\n" +
"Discarded '" + functionName + "' function without loading.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public Collection<Node> parse(String source) throws ParserTrialFailedException {
}

private void validateModuleDeclaration(ModuleMemberDeclarationNode declarationNode) {
if (declarationNode instanceof FunctionDefinitionNode) {
String functionName = ((FunctionDefinitionNode) declarationNode).functionName().text();
if (declarationNode instanceof FunctionDefinitionNode functionDefinitionNode) {
String functionName = functionDefinitionNode.functionName().text();
if (ParserConstants.isFunctionNameRestricted(functionName)) {
String message = "Function name " + "'" + functionName + "'" + " not allowed in Ballerina Shell.\n";
throw new InvalidMethodException(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ public class BasicSnippetFactory extends SnippetFactory {

@Override
public ImportDeclarationSnippet createImportSnippet(Node node) {
if (node instanceof ImportDeclarationNode) {
ImportDeclarationNode importDeclarationNode = (ImportDeclarationNode) node;
if (node instanceof ImportDeclarationNode importDeclarationNode) {
return new ImportDeclarationSnippet(importDeclarationNode);
}
return null;
Expand All @@ -143,10 +142,9 @@ public List<VariableDeclarationSnippet> createVariableDeclarationSnippets(Node n
return null;
}

if (node instanceof ModuleVariableDeclarationNode) {
dclnNode = (ModuleVariableDeclarationNode) node;
} else if (node instanceof VariableDeclarationNode) {
VariableDeclarationNode varNode = (VariableDeclarationNode) node;
if (node instanceof ModuleVariableDeclarationNode moduleVariableDeclarationNode) {
dclnNode = moduleVariableDeclarationNode;
} else if (node instanceof VariableDeclarationNode varNode) {
VariableDeclarationNode newNode = null;
NodeList<Token> qualifiers = NodeFactory.createEmptyNodeList();
// Only final qualifier is transferred.
Expand Down Expand Up @@ -230,14 +228,14 @@ public ModuleMemberDeclarationSnippet createModuleMemberDeclarationSnippet(Node
return null;
}

if (node instanceof ModuleMemberDeclarationNode) {
if (node instanceof ModuleMemberDeclarationNode moduleMemberDeclarationNode) {
assert MODULE_MEM_DCLNS.containsKey(node.getClass());
SnippetSubKind subKind = MODULE_MEM_DCLNS.get(node.getClass());
if (subKind.hasError()) {
addErrorDiagnostic(subKind.getError());
throw new SnippetException();
} else if (subKind.isValid()) {
return new ModuleMemberDeclarationSnippet(subKind, (ModuleMemberDeclarationNode) node);
return new ModuleMemberDeclarationSnippet(subKind, moduleMemberDeclarationNode);
}
}
return null;
Expand All @@ -264,8 +262,8 @@ public StatementSnippet createStatementSnippet(Node node) throws SnippetExceptio

@Override
public ExpressionSnippet createExpressionSnippet(Node node) {
if (node instanceof ExpressionNode) {
return new ExpressionSnippet((ExpressionNode) node);
if (node instanceof ExpressionNode expressionNode) {
return new ExpressionSnippet(expressionNode);
}
return null;
}
Expand All @@ -277,11 +275,11 @@ public ExpressionSnippet createExpressionSnippet(Node node) {
* @return node contains isolated keyword or not.
*/
private boolean containsIsolated(Node node) {
if (node instanceof ModuleVariableDeclarationNode) {
NodeList<Token> nodeList = ((ModuleVariableDeclarationNode) node).qualifiers();
if (node instanceof ModuleVariableDeclarationNode moduleVariableDeclarationNode) {
NodeList<Token> nodeList = moduleVariableDeclarationNode.qualifiers();
return nodeList.stream().anyMatch(token -> token.kind() == SyntaxKind.ISOLATED_KEYWORD);
} else if (node instanceof FunctionDefinitionNode) {
NodeList<Token> nodeList = ((FunctionDefinitionNode) node).qualifierList();
} else if (node instanceof FunctionDefinitionNode functionDefinitionNode) {
NodeList<Token> nodeList = functionDefinitionNode.qualifierList();
return nodeList.stream().anyMatch(token -> token.kind() == SyntaxKind.ISOLATED_KEYWORD);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,26 @@ public ModuleMemberDeclarationSnippet(SnippetSubKind subKind, ModuleMemberDeclar
* If the module declaration has no name, this will return null.
*/
public Identifier name() {
if (rootNode instanceof ClassDefinitionNode) {
String className = ((ClassDefinitionNode) rootNode).className().text();
if (rootNode instanceof ClassDefinitionNode classDefinitionNode) {
String className = classDefinitionNode.className().text();
return new Identifier(className);
} else if (rootNode instanceof ConstantDeclarationNode) {
String constName = ((ConstantDeclarationNode) rootNode).variableName().text();
} else if (rootNode instanceof ConstantDeclarationNode constantDeclarationNode) {
String constName = constantDeclarationNode.variableName().text();
return new Identifier(constName);
} else if (rootNode instanceof EnumDeclarationNode) {
String enumName = ((EnumDeclarationNode) rootNode).identifier().text();
} else if (rootNode instanceof EnumDeclarationNode enumDeclarationNode) {
String enumName = enumDeclarationNode.identifier().text();
return new Identifier(enumName);
} else if (rootNode instanceof FunctionDefinitionNode) {
String funcName = ((FunctionDefinitionNode) rootNode).functionName().text();
} else if (rootNode instanceof FunctionDefinitionNode functionDefinitionNode) {
String funcName = functionDefinitionNode.functionName().text();
return new Identifier(funcName);
} else if (rootNode instanceof ListenerDeclarationNode) {
String listenerName = ((ListenerDeclarationNode) rootNode).variableName().text();
} else if (rootNode instanceof ListenerDeclarationNode listenerDeclarationNode) {
String listenerName = listenerDeclarationNode.variableName().text();
return new Identifier(listenerName);
} else if (rootNode instanceof ModuleXMLNamespaceDeclarationNode) {
ModuleXMLNamespaceDeclarationNode namespaceNode = (ModuleXMLNamespaceDeclarationNode) rootNode;
} else if (rootNode instanceof ModuleXMLNamespaceDeclarationNode namespaceNode) {
return namespaceNode.namespacePrefix().map(Token::text).map(Identifier::new)
.orElseGet(this::createAnonModuleName);
} else if (rootNode instanceof TypeDefinitionNode) {
String typeName = ((TypeDefinitionNode) rootNode).typeName().text();
} else if (rootNode instanceof TypeDefinitionNode typeDefinitionNode) {
String typeName = typeDefinitionNode.typeName().text();
return new Identifier(typeName);
} else {
return createAnonModuleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ public static String getExpressionStringValue(Object object) {
* @return Converted string.
*/
public static String getErrorStringValue(Throwable error) {
if (error instanceof BError) {
return ((BError) error).getErrorMessage() + " " + ((BError) error).getDetails();
if (error instanceof BError bError) {
return bError.getErrorMessage() + " " + bError.getDetails();
}
return error.getMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public static BError createError(Type type, BString message, BString details) {
* @return new error
*/
public static BError createError(Throwable error) {
if (error instanceof BError) {
return (BError) error;
if (error instanceof BError bError) {
return bError;
}
BError bError = createError(StringUtils.fromString(error.toString()));
bError.setStackTrace(error.getStackTrace());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof Parameter)) {
if (!(o instanceof Parameter that)) {
return false;
}

Parameter that = (Parameter) o;
return this.name.equals(that.name) && this.type.equals(that.type) && this.isDefault == that.isDefault;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ public class AnnotationUtils {
* @param bType The type for which annotations need to be set
*/
public static void processAnnotations(MapValue globalAnnotMap, Type bType) {
if (!(bType instanceof BAnnotatableType)) {
if (!(bType instanceof BAnnotatableType type)) {
return;
}

BAnnotatableType type = (BAnnotatableType) bType;

BString annotationKey = StringUtils.fromString(type.getAnnotationKey());
if (globalAnnotMap.containsKey(annotationKey)) {
type.setAnnotations((MapValue<BString, Object>) globalAnnotMap.get(annotationKey));
Expand Down Expand Up @@ -105,8 +103,7 @@ public static void processObjectCtorAnnotations(BObjectType bType, MapValue glob
for (MethodType attachedFunction : bType.getMethods()) {
processObjectMethodLambdaAnnotation(globalAnnotMap, strand, attachedFunction);
}
if (bType instanceof BServiceType) {
var serviceType = (BServiceType) bType;
if (bType instanceof BServiceType serviceType) {
for (var resourceFunction : serviceType.getResourceMethods()) {
processObjectMethodLambdaAnnotation(globalAnnotMap, strand, resourceFunction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ public static Object cloneValue(Object value) {
return null;
}

if (!(value instanceof BRefValue)) {
if (!(value instanceof BRefValue refValue)) {
return value;
}

BRefValue refValue = (BRefValue) value;
return refValue.copy(new HashMap<>());
}

Expand All @@ -74,11 +73,10 @@ public static Object cloneReadOnly(Object value) {
return null;
}

if (!(value instanceof BRefValue)) {
if (!(value instanceof BRefValue refValue)) {
return value;
}

BRefValue refValue = (BRefValue) value;
return refValue.frozenCopy(new HashMap<>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public static ErrorValue createInteropError(Throwable e) {
}

public static Object handleResourceError(Object returnValue) {
if (returnValue instanceof BError) {
throw (BError) returnValue;
if (returnValue instanceof BError error) {
throw error;
}
return returnValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ public void serialize(Object json) throws IOException {

switch (TypeUtils.getImpliedType(TypeChecker.getType(json)).getTag()) {
case TypeTags.ARRAY_TAG:
if (json instanceof StreamingJsonValue) {
((StreamingJsonValue) json).serialize(this);
if (json instanceof StreamingJsonValue streamingJsonValue) {
streamingJsonValue.serialize(this);
break;
}
this.writeStartArray();
Expand Down
Loading

0 comments on commit a0bceed

Please sign in to comment.