public static String getAttributeValue(PsiAnnotation annotation, String attributeName, DefaultPolicy policy) { final PsiAnnotationMemberValue value = getAnnotationMemberValue(annotation, attributeName); if (value != null) { if (value instanceof PsiClassObjectAccessExpression) { final PsiType type = ((PsiClassObjectAccessExpression) value).getType(); if (type == null) { return null; } return type.getCanonicalText(); } else { final String text = value.getText(); return text.substring(1, text.length() - 1); } } if (policy == DefaultPolicy.OWNER_IDENTIFIER_NAME) { return PsiUtil.getName(getImmediateOwnerElement(annotation)); } else { return null; } }
public static Location getParameterizedLocation(PsiClass psiClass, String paramSetName, String parameterizedClassName) { final PsiAnnotation annotation = AnnotationUtil.findAnnotationInHierarchy(psiClass, Collections.singleton(JUnitUtil.RUN_WITH)); if(annotation != null) { final PsiAnnotationMemberValue attributeValue = annotation.findAttributeValue("value"); if(attributeValue instanceof PsiClassObjectAccessExpression) { final PsiTypeElement operand = ((PsiClassObjectAccessExpression) attributeValue).getOperand(); if(InheritanceUtil.isInheritor(operand.getType(), parameterizedClassName)) { return new PsiMemberParameterizedLocation(psiClass.getProject(), psiClass, null, paramSetName); } } } return null; }
@Nullable @Override public String getTemplateString(PsiElement psiElement) { String clazz; if (psiElement instanceof PsiReference) { // foo.mock PsiReference psiReference = (PsiReference) psiElement; PsiLocalVariable psiLocalVariable = (PsiLocalVariable) psiReference.resolve(); if (psiLocalVariable == null) { return psiElement.getText(); } PsiType psiType = psiLocalVariable.getType(); clazz = psiType.getPresentableText() + ".class"; } else if (psiElement instanceof PsiClassObjectAccessExpression) { // String.class.mock PsiClassObjectAccessExpression psiClassObjectAccessExpression = (PsiClassObjectAccessExpression) psiElement; String classText = psiClassObjectAccessExpression.getText(); if (classText == null) { return psiElement.getText(); } clazz = classText; } else { return psiElement.getText(); } return "org.mockito.Mockito.mock(" + clazz + ")$END$"; }
@Override public void fillActions(LookupElement element, Lookup lookup, Consumer<LookupElementAction> consumer) { Object o = element.getObject(); if(o instanceof PsiClassObjectAccessExpression) { o = PsiUtil.resolveClassInType(((PsiClassObjectAccessExpression) o).getOperand().getType()); } if(o instanceof PsiClass) { PsiClass clazz = (PsiClass) o; addExcludes(consumer, clazz, clazz.getQualifiedName()); } else if(o instanceof PsiMethod) { final PsiMethod method = (PsiMethod) o; if(method.hasModifierProperty(PsiModifier.STATIC)) { addExcludes(consumer, method, PsiUtil.getMemberQualifiedName(method)); } } else if(o instanceof PsiField) { final PsiField field = (PsiField) o; if(field.hasModifierProperty(PsiModifier.STATIC)) { addExcludes(consumer, field, PsiUtil.getMemberQualifiedName(field)); } } }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiPolyadicExpression)) { return false; } if (element instanceof PsiLiteralExpression || element instanceof PsiClassObjectAccessExpression) { return false; } final PsiPolyadicExpression expression = (PsiPolyadicExpression)element; final PsiType expressionType = expression.getType(); if (expressionType == null || expressionType.equalsToText("java.lang.String")) { // intention disabled for string concatenations because of performance issues on // relatively common large string expressions. return false; } final PsiExpression[] operands = expression.getOperands(); for (PsiExpression operand : operands) { if (operand == null) { return false; } final PsiType type = operand.getType(); if (type == null || type.equalsToText("java.lang.String")) { return false; } } if (!PsiUtil.isConstantExpression(expression)) { return false; } try { final Object value = ExpressionUtils.computeConstantExpression(expression, true); if (value == null) { return false; } } catch (ConstantEvaluationOverflowException ignore) { return false; } final PsiElement parent = element.getParent(); return !(parent instanceof PsiExpression) || !PsiUtil.isConstantExpression((PsiExpression)parent); }
@Override public void visitClassObjectAccessExpression(PsiClassObjectAccessExpression expression) { mVisitor.report("PsiClassObjectAccessExpression", expression.getText(), expression); super.visitExpression(expression); }