PsiStatement (PsiMethodCallExpression methodCallExpression, String variableName, PsiType parameterType) { @NonNls final StringBuilder newStatementText = new StringBuilder(); final Project project = methodCallExpression.getProject(); if (JavaCodeStyleSettings.getInstance(methodCallExpression.getContainingFile()).GENERATE_FINAL_LOCALS) { newStatementText.append("final "); } newStatementText.append(CommonClassNames.JAVA_UTIL_ITERATOR); if (parameterType != null) { final String typeText = parameterType.getCanonicalText(); newStatementText.append('<'); newStatementText.append(typeText); newStatementText.append('>'); } newStatementText.append(' '); newStatementText.append(variableName); newStatementText.append('='); final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifier = methodExpression.getQualifierExpression(); final String qualifierText; if (qualifier == null) { qualifierText = ""; } else { qualifierText = qualifier.getText() + '.'; } newStatementText.append(qualifierText); @NonNls final String methodName = methodExpression.getReferenceName(); if ("elements".equals(methodName)) { if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.util.Vector")) { newStatementText.append(ITERATOR_TEXT); } else if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.util.Hashtable")) { newStatementText.append(VALUES_ITERATOR_TEXT); } else { return null; } } else if ("keys".equals(methodName)) { if (TypeUtils.expressionHasTypeOrSubtype(qualifier, "java.util.Hashtable")) { newStatementText.append(KEY_SET_ITERATOR_TEXT); } else { return null; } } else { return null; } newStatementText.append(';'); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiStatement statement = factory.createStatementFromText(newStatementText.toString(), methodExpression); final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project); styleManager.shortenClassReferences(statement); return statement; }