private static boolean isNullLiteralExpression(PsiElement expr) { if (expr instanceof PsiLiteralExpression) { final PsiLiteralExpression literalExpression = (PsiLiteralExpression) expr; return PsiType.NULL.equals(literalExpression.getType()); } return false; }
public void updateReturnValueTemplate(PsiExpression expression) { if (myReturnValueTemplate == null) return; if (!getSuperMethods().isEmpty()) { for (final RefMethod refMethod : getSuperMethods()) { RefMethodImpl refSuper = (RefMethodImpl) refMethod; refSuper.updateReturnValueTemplate(expression); } } else { String newTemplate = null; final RefJavaUtil refUtil = RefJavaUtil.getInstance(); if (expression instanceof PsiLiteralExpression) { PsiLiteralExpression psiLiteralExpression = (PsiLiteralExpression) expression; newTemplate = psiLiteralExpression.getText(); } else if (expression instanceof PsiReferenceExpression) { PsiReferenceExpression referenceExpression = (PsiReferenceExpression) expression; PsiElement resolved = referenceExpression.resolve(); if (resolved instanceof PsiField) { PsiField psiField = (PsiField) resolved; if (psiField.hasModifierProperty(PsiModifier.STATIC) && psiField.hasModifierProperty(PsiModifier.FINAL) && refUtil.compareAccess(refUtil.getAccessModifier(psiField), getAccessModifier()) >= 0) { newTemplate = PsiFormatUtil.formatVariable( psiField, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_CONTAINING_CLASS | PsiFormatUtilBase.SHOW_FQ_NAME, PsiSubstitutor.EMPTY); } } } else if (refUtil.isCallToSuperMethod(expression, (PsiMethod) getElement())) return; //noinspection StringEquality if (myReturnValueTemplate == RETURN_VALUE_UNDEFINED) { myReturnValueTemplate = newTemplate; } else if (!Comparing.equal(myReturnValueTemplate, newTemplate)) { myReturnValueTemplate = null; } } }
public static boolean isPropertyRef( final PsiLiteralExpression expression, final String key, final String resourceBundleName) { if (resourceBundleName == null) { return !PropertiesImplUtil.findPropertiesByKey(expression.getProject(), key).isEmpty(); } else { final List<PropertiesFile> propertiesFiles = propertiesFilesByBundleName(resourceBundleName, expression); boolean containedInPropertiesFile = false; for (PropertiesFile propertiesFile : propertiesFiles) { containedInPropertiesFile |= propertiesFile.findPropertyByKey(key) != null; } return containedInPropertiesFile; } }
public static boolean mustBePropertyKey( @NotNull Project project, @NotNull PsiLiteralExpression expression, @NotNull Map<String, Object> annotationAttributeValues) { final PsiElement parent = expression.getParent(); if (parent instanceof PsiVariable) { final PsiAnnotation annotation = AnnotationUtil.findAnnotation((PsiVariable) parent, AnnotationUtil.PROPERTY_KEY); if (annotation != null) { return processAnnotationAttributes(annotationAttributeValues, annotation); } } return isPassedToAnnotatedParam( project, expression, AnnotationUtil.PROPERTY_KEY, annotationAttributeValues, null); }
/** * Returns number of different parameters in i18n message. For example, for string <i>Class {0} * info: Class {0} extends class {1} and implements interface {2}</i> number of parameters is 3. * * @param expression i18n literal * @return number of parameters */ public static int getPropertyValueParamsMaxCount(final PsiLiteralExpression expression) { int maxCount = -1; for (PsiReference reference : expression.getReferences()) { if (reference instanceof PsiPolyVariantReference) { for (ResolveResult result : ((PsiPolyVariantReference) reference).multiResolve(false)) { if (result.isValidResult() && result.getElement() instanceof IProperty) { String value = ((IProperty) result.getElement()).getValue(); MessageFormat format; try { format = new MessageFormat(value); } catch (Exception e) { continue; // ignore syntax error } try { int count = format.getFormatsByArgumentIndex().length; maxCount = Math.max(maxCount, count); } catch (IllegalArgumentException ignored) { } } } } } return maxCount; }
private NamesByExprInfo suggestVariableNameByExpressionOnly( PsiExpression expr, final VariableKind variableKind, boolean correctKeywords, boolean useAllMethodNames) { if (expr instanceof PsiMethodCallExpression) { PsiReferenceExpression methodExpr = ((PsiMethodCallExpression) expr).getMethodExpression(); String methodName = methodExpr.getReferenceName(); if (methodName != null) { if ("of".equals(methodName) || "ofNullable".equals(methodName)) { if (isJavaUtilMethodCall((PsiMethodCallExpression) expr)) { PsiExpression[] expressions = ((PsiMethodCallExpression) expr).getArgumentList().getExpressions(); if (expressions.length > 0) { return suggestVariableNameByExpressionOnly( expressions[0], variableKind, correctKeywords, useAllMethodNames); } } } if ("map".equals(methodName) || "flatMap".equals(methodName) || "filter".equals(methodName)) { if (isJavaUtilMethodCall((PsiMethodCallExpression) expr)) { return new NamesByExprInfo(null); } } String[] words = NameUtil.nameToWords(methodName); if (words.length > 0) { final String firstWord = words[0]; if (GET_PREFIX.equals(firstWord) || IS_PREFIX.equals(firstWord) || FIND_PREFIX.equals(firstWord) || CREATE_PREFIX.equals(firstWord)) { if (words.length > 1) { final String propertyName = methodName.substring(firstWord.length()); String[] names = getSuggestionsByName(propertyName, variableKind, false, correctKeywords); final PsiExpression qualifierExpression = methodExpr.getQualifierExpression(); if (qualifierExpression instanceof PsiReferenceExpression && ((PsiReferenceExpression) qualifierExpression).resolve() instanceof PsiVariable) { names = ArrayUtil.append( names, StringUtil.sanitizeJavaIdentifier( changeIfNotIdentifier( qualifierExpression.getText() + StringUtil.capitalize(propertyName)))); } return new NamesByExprInfo(propertyName, names); } } else if (words.length == 1 || useAllMethodNames) { return new NamesByExprInfo( methodName, getSuggestionsByName(methodName, variableKind, false, correctKeywords)); } } } } else if (expr instanceof PsiReferenceExpression) { String propertyName = ((PsiReferenceExpression) expr).getReferenceName(); PsiElement refElement = ((PsiReferenceExpression) expr).resolve(); if (refElement instanceof PsiVariable) { VariableKind refVariableKind = getVariableKind((PsiVariable) refElement); propertyName = variableNameToPropertyName(propertyName, refVariableKind); } if (refElement != null && propertyName != null) { String[] names = getSuggestionsByName(propertyName, variableKind, false, correctKeywords); return new NamesByExprInfo(propertyName, names); } } else if (expr instanceof PsiArrayAccessExpression) { PsiExpression arrayExpr = ((PsiArrayAccessExpression) expr).getArrayExpression(); if (arrayExpr instanceof PsiReferenceExpression) { String arrayName = ((PsiReferenceExpression) arrayExpr).getReferenceName(); PsiElement refElement = ((PsiReferenceExpression) arrayExpr).resolve(); if (refElement instanceof PsiVariable) { VariableKind refVariableKind = getVariableKind((PsiVariable) refElement); arrayName = variableNameToPropertyName(arrayName, refVariableKind); } if (arrayName != null) { String name = StringUtil.unpluralize(arrayName); if (name != null) { String[] names = getSuggestionsByName(name, variableKind, false, correctKeywords); return new NamesByExprInfo(name, names); } } } } else if (expr instanceof PsiLiteralExpression && variableKind == VariableKind.STATIC_FINAL_FIELD) { final PsiLiteralExpression literalExpression = (PsiLiteralExpression) expr; final Object value = literalExpression.getValue(); if (value instanceof String) { final String stringValue = (String) value; String[] names = getSuggestionsByValue(stringValue); if (names.length > 0) { return new NamesByExprInfo(null, constantValueToConstantName(names)); } } } else if (expr instanceof PsiParenthesizedExpression) { return suggestVariableNameByExpressionOnly( ((PsiParenthesizedExpression) expr).getExpression(), variableKind, correctKeywords, useAllMethodNames); } else if (expr instanceof PsiTypeCastExpression) { return suggestVariableNameByExpressionOnly( ((PsiTypeCastExpression) expr).getOperand(), variableKind, correctKeywords, useAllMethodNames); } else if (expr instanceof PsiLiteralExpression) { final String text = StringUtil.stripQuotesAroundValue(expr.getText()); if (isIdentifier(text)) { return new NamesByExprInfo( text, getSuggestionsByName(text, variableKind, false, correctKeywords)); } } return new NamesByExprInfo(null, ArrayUtil.EMPTY_STRING_ARRAY); }