@Nullable private String[] suggestVariableNameFromLiterals( PsiExpression expr, VariableKind variableKind, boolean correctKeywords) { final PsiElement[] literals = PsiTreeUtil.collectElements( expr, new PsiElementFilter() { @Override public boolean isAccepted(PsiElement element) { if (isStringPsiLiteral(element) && StringUtil.isJavaIdentifier(StringUtil.unquoteString(element.getText()))) { final PsiElement exprList = element.getParent(); if (exprList instanceof PsiExpressionList) { final PsiElement call = exprList.getParent(); if (call instanceof PsiNewExpression) { return true; } else if (call instanceof PsiMethodCallExpression) { // TODO: exclude or not getA().getB("name").getC(); or // getA(getB("name").getC()); It works fine for now in the most cases return true; } } } return false; } }); if (literals.length == 1) { final String text = StringUtil.unquoteString(literals[0].getText()); return getSuggestionsByName( text, variableKind, expr.getType() instanceof PsiArrayType, correctKeywords); } return null; }
public static PsiAnnotation[] getTestNGAnnotations(PsiElement element) { PsiElement[] annotations = PsiTreeUtil.collectElements( element, new PsiElementFilter() { public boolean isAccepted(PsiElement element) { if (!(element instanceof PsiAnnotation)) return false; String name = ((PsiAnnotation) element).getQualifiedName(); if (null == name) return false; if (name.startsWith("org.testng.annotations")) { return true; } return false; } }); PsiAnnotation[] array = new PsiAnnotation[annotations.length]; System.arraycopy(annotations, 0, array, 0, annotations.length); return array; }