@Nullable private static PsiType guessElementTypeFromReference( MethodPatternMap methodPatternMap, PsiElement ref, TextRange rangeToIgnore) { PsiElement refParent = ref.getParent(); if (refParent instanceof PsiReferenceExpression) { PsiReferenceExpression parentExpr = (PsiReferenceExpression) refParent; if (ref.equals(parentExpr.getQualifierExpression()) && parentExpr.getParent() instanceof PsiMethodCallExpression) { String methodName = parentExpr.getReferenceName(); PsiMethodCallExpression methodCall = (PsiMethodCallExpression) parentExpr.getParent(); PsiExpression[] args = methodCall.getArgumentList().getExpressions(); MethodPattern pattern = methodPatternMap.findPattern(methodName, args.length); if (pattern != null) { if (pattern.parameterIndex < 0) { // return value if (methodCall.getParent() instanceof PsiTypeCastExpression && (rangeToIgnore == null || !rangeToIgnore.contains(methodCall.getTextRange()))) { return ((PsiTypeCastExpression) methodCall.getParent()).getType(); } } else { return args[pattern.parameterIndex].getType(); } } } } return null; }
private void addExprTypesWhenContainerElement(LinkedHashSet<PsiType> set, PsiExpression expr) { if (expr instanceof PsiMethodCallExpression) { PsiMethodCallExpression callExpr = (PsiMethodCallExpression) expr; PsiReferenceExpression methodExpr = callExpr.getMethodExpression(); String methodName = methodExpr.getReferenceName(); MethodPattern pattern = myMethodPatternMap.findPattern( methodName, callExpr.getArgumentList().getExpressions().length); if (pattern != null && pattern.parameterIndex < 0 /* return value */) { PsiExpression qualifier = methodExpr.getQualifierExpression(); if (qualifier != null) { PsiType[] types = guessContainerElementType(qualifier, null); for (PsiType type : types) { if (type instanceof PsiClassType) { if (((PsiClassType) type).resolve() instanceof PsiAnonymousClass) continue; } set.add(type); } } } } }
@SuppressWarnings({"HardCodedStringLiteral"}) private void initMethodPatterns() { // Collection myMethodPatternMap.addPattern(new MethodPattern("add", 1, 0)); myMethodPatternMap.addPattern(new MethodPattern("contains", 1, 0)); myMethodPatternMap.addPattern(new MethodPattern("remove", 1, 0)); // Vector myMethodPatternMap.addPattern(new MethodPattern("add", 2, 1)); myMethodPatternMap.addPattern(new MethodPattern("addElement", 1, 0)); myMethodPatternMap.addPattern(new MethodPattern("elementAt", 1, -1)); myMethodPatternMap.addPattern(new MethodPattern("firstElement", 0, -1)); myMethodPatternMap.addPattern(new MethodPattern("lastElement", 0, -1)); myMethodPatternMap.addPattern(new MethodPattern("get", 1, -1)); myMethodPatternMap.addPattern(new MethodPattern("indexOf", 1, 0)); myMethodPatternMap.addPattern(new MethodPattern("indexOf", 2, 0)); myMethodPatternMap.addPattern(new MethodPattern("lastIndexOf", 1, 0)); myMethodPatternMap.addPattern(new MethodPattern("lastIndexOf", 2, 0)); myMethodPatternMap.addPattern(new MethodPattern("insertElementAt", 2, 0)); myMethodPatternMap.addPattern(new MethodPattern("removeElement", 1, 0)); myMethodPatternMap.addPattern(new MethodPattern("set", 2, 1)); myMethodPatternMap.addPattern(new MethodPattern("setElementAt", 2, 0)); }