From 49c38eb82e562f3d6d70d640e3fb5730ef19c7f2 Mon Sep 17 00:00:00 2001 From: santanu-ghosh1 Date: Wed, 24 Apr 2024 20:40:14 +0530 Subject: [PATCH] sonarfix for instanceof check and cast --- .../parser/helper/CamelJavaParserHelper.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java index a7c9414b419b1..e48ba41ce9e19 100644 --- a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java +++ b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaParserHelper.java @@ -338,8 +338,8 @@ private static void extractEndpointUriFromArgument( return; } } - if (fields && arg instanceof SimpleName) { - FieldSource field = ParserCommon.getField(clazz, block, (SimpleName) arg); + if (fields && arg instanceof SimpleName simpleName) { + FieldSource field = ParserCommon.getField(clazz, block, simpleName); if (field != null) { // find the endpoint uri from the annotation AnnotationSource annotation = field.getAnnotation("org.apache.camel.cdi.Uri"); @@ -350,20 +350,20 @@ private static void extractEndpointUriFromArgument( Expression exp = extractExpression(annotation.getInternal()); String uri = CamelJavaParserHelper.getLiteralValue(clazz, block, exp); if (!Strings.isNullOrEmpty(uri)) { - int position = ((SimpleName) arg).getStartPosition(); - int len = ((SimpleName) arg).getLength(); + int position = (simpleName).getStartPosition(); + int len = (simpleName).getLength(); uris.add(new ParserResult(node, position, len, uri)); } } else { // the field may be initialized using variables, so we need to evaluate those expressions Object fi = field.getInternal(); - if (fi instanceof VariableDeclaration) { - Expression exp = ((VariableDeclaration) fi).getInitializer(); + if (fi instanceof VariableDeclaration variableDeclaration) { + Expression exp = (variableDeclaration).getInitializer(); String uri = CamelJavaParserHelper.getLiteralValue(clazz, block, exp); if (!Strings.isNullOrEmpty(uri)) { // we want the position of the field, and not in the route - int position = ((VariableDeclaration) fi).getStartPosition(); - int len = ((VariableDeclaration) fi).getLength(); + int position = (variableDeclaration).getStartPosition(); + int len = (variableDeclaration).getLength(); uris.add(new ParserResult(node, position, len, uri)); } } @@ -441,8 +441,8 @@ private static void doParseCamelLanguage( if (list != null && list.size() == 1) { ASTNode o = (ASTNode) list.get(0); ASTNode p = o.getParent(); - if (p instanceof MethodInvocation) { - String pName = ((MethodInvocation) p).getName().getIdentifier(); + if (p instanceof MethodInvocation methodInvocation) { + String pName = (methodInvocation).getName().getIdentifier(); if (language.equals(pName)) { // okay find the parent of the language which is the method that uses the language parent = (Expression) p.getParent(); @@ -487,8 +487,8 @@ private static boolean isLanguagePredicate(String name) { public static String getLiteralValue(JavaClassSource clazz, Block block, Expression expression) { // unwrap parenthesis - if (expression instanceof ParenthesizedExpression) { - expression = ((ParenthesizedExpression) expression).getExpression(); + if (expression instanceof ParenthesizedExpression parenthesizedExpression) { + expression = (parenthesizedExpression).getExpression(); } if (expression instanceof StringLiteral stringLiteral) { @@ -515,8 +515,8 @@ public static String getLiteralValue(JavaClassSource clazz, Block block, Express return "{{" + name + "}}"; } - if (expression instanceof SimpleName) { - FieldSource field = ParserCommon.getField(clazz, block, (SimpleName) expression); + if (expression instanceof SimpleName expSimpleName) { + FieldSource field = ParserCommon.getField(clazz, block, expSimpleName); if (field != null) { // is the field annotated with a Camel endpoint if (field.getAnnotations() != null) { @@ -535,7 +535,7 @@ public static String getLiteralValue(JavaClassSource clazz, Block block, Express return endpointTypeCheck(clazz, block, field); } else { // we could not find the field in this class/method, so its maybe from some other super class, so insert a dummy value - final String fieldName = ((SimpleName) expression).getIdentifier(); + final String fieldName = (expSimpleName).getIdentifier(); return "{{" + fieldName + "}}"; } } else if (expression instanceof InfixExpression ie) {