From e7658abc8c5e05c1ef50bbc7fcf6e69e7987ab34 Mon Sep 17 00:00:00 2001 From: Andrey K Date: Thu, 14 Nov 2019 15:35:02 +0100 Subject: [PATCH] Parameters resolving improvement --- build.gradle | 2 +- .../legionivo/plugin/TestRailApiWrapper.java | 41 ++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index d6325d5..67cc690 100644 --- a/build.gradle +++ b/build.gradle @@ -12,7 +12,7 @@ plugins { } group 'com.zoomint' -version '1.4.6' +version '1.4.7' sourceCompatibility = 1.8 repositories { diff --git a/src/main/java/io/github/legionivo/plugin/TestRailApiWrapper.java b/src/main/java/io/github/legionivo/plugin/TestRailApiWrapper.java index 0fcba48..84c3e7a 100644 --- a/src/main/java/io/github/legionivo/plugin/TestRailApiWrapper.java +++ b/src/main/java/io/github/legionivo/plugin/TestRailApiWrapper.java @@ -6,6 +6,7 @@ import com.intellij.psi.*; import com.intellij.psi.impl.source.tree.java.PsiMethodCallExpressionImpl; import com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl; +import com.intellij.psi.impl.source.tree.java.PsiReturnStatementImpl; import io.github.legionivo.plugin.api.TestRailClient; import io.github.legionivo.plugin.api.TestRailClientBuilder; import io.github.legionivo.plugin.enums.State; @@ -243,13 +244,29 @@ private static String getValueFromExpression(PsiExpression expression) { if (expression instanceof PsiMethodCallExpression) { PsiMethod method = ((PsiMethodCallExpressionImpl) expression).resolveMethod(); if (Objects.requireNonNull(method).hasAnnotation(Annotations.OWNER_KEY_ANNOTATION) || method.getAnnotations().length > 0) { - PsiModifierList modifierList = method.getModifierList(); - Project project = expression.getProject(); - PsiAnnotation[] list = modifierList.getAnnotations(); - PsiLiteralExpression expression1 = (PsiLiteralExpression) list[0].findAttributeValue("value"); - String parameterValue = Objects.requireNonNull(expression1).getText().replace("\"", ""); - text = PropertiesImplUtil.findPropertiesByKey(project, parameterValue).get(0).getValue(); + text = getValueFromAnnotatedMethod(expression, method); + } else if (method.getBody() != null) { + PsiStatement[] statements = method.getBody().getStatements(); + if (((PsiReturnStatementImpl) statements[0]).getReturnValue() instanceof PsiReferenceExpression) { + PsiExpression psiReferenceExpression = (((PsiReturnStatementImpl) statements[0]).getReturnValue()); + assert psiReferenceExpression != null; + PsiVariable variable = (PsiVariable) ((PsiReferenceExpressionImpl) psiReferenceExpression).resolve(); + if ((((PsiReturnStatementImpl) statements[0]).getReturnValue() instanceof PsiLiteralExpression)) { + text = ((Objects.requireNonNull(((PsiReturnStatementImpl) statements[0]).getReturnValue()).getText())).replaceAll("^\"|\"$", ""); + } else if (Objects.requireNonNull(variable).hasInitializer()) { + PsiExpression initializer = Objects.requireNonNull(variable).getInitializer(); + if (initializer == null) { + text = Objects.requireNonNull(variable.getNameIdentifier()).getText(); + } else if (initializer instanceof PsiMethodCallExpressionImpl) { + PsiMethod psiMethod = ((PsiMethodCallExpressionImpl) initializer).resolveMethod(); + if (Objects.requireNonNull(psiMethod).hasAnnotation(Annotations.OWNER_KEY_ANNOTATION) || psiMethod.getAnnotations().length > 0) { + text = getValueFromAnnotatedMethod(initializer, psiMethod); + } + } else text = Objects.requireNonNull(initializer).getText().replace("\"", ""); + } else text = expression.getText(); + } } else text = expression.getText(); + } else if (expression instanceof PsiReferenceExpression) { PsiVariable variable = (PsiVariable) ((PsiReferenceExpressionImpl) expression).resolve(); if (Objects.requireNonNull(variable).hasInitializer()) { @@ -268,6 +285,18 @@ private static String getValueFromExpression(PsiExpression expression) { } + + private static String getValueFromAnnotatedMethod(PsiExpression expression, PsiMethod method) { + String text; + PsiModifierList modifierList = method.getModifierList(); + Project project = expression.getProject(); + PsiAnnotation[] list = modifierList.getAnnotations(); + PsiLiteralExpression expression1 = (PsiLiteralExpression) list[0].findAttributeValue("value"); + String parameterValue = Objects.requireNonNull(expression1).getText().replace("\"", ""); + text = PropertiesImplUtil.findPropertiesByKey(project, parameterValue).get(0).getValue(); + return text; + } + private static String getValueFromPsiPolyadicExpression(PsiPolyadicExpression polyadicExpression) { PsiExpression[] operands = polyadicExpression.getOperands(); StringBuilder fullExpression = new StringBuilder();