@Override public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiElement forElement = descriptor.getPsiElement(); final PsiElement parent = forElement.getParent(); if (!(parent instanceof PsiForStatement)) { return; } final PsiForStatement forStatement = (PsiForStatement)parent; final String newExpression; if (isArrayLoopStatement(forStatement)) { newExpression = createArrayIterationText(forStatement); } else if (isCollectionLoopStatement(forStatement, ignoreUntypedCollections)) { newExpression = createCollectionIterationText(forStatement); } else if (isIndexedListLoopStatement(forStatement, ignoreUntypedCollections)) { newExpression = createListIterationText(forStatement); } else { return; } if (newExpression == null) { return; } replaceStatementAndShortenClassNames(forStatement, newExpression); }
@Override public void visitElement(PsiElement element) { if (element.getLanguage() != JavaLanguage.INSTANCE) { return; } if (!PsiUtil.isLanguageLevel5OrHigher(element)) { return; } super.visitElement(element); }
private void replaceCollectionGetAccess( PsiElement element, String contentVariableName, PsiVariable listVariable, String indexName, PsiElement childToSkip, StringBuilder out) { if (isListGetLookup(element, indexName, listVariable)) { out.append(contentVariableName); } else { final PsiElement[] children = element.getChildren(); if (children.length == 0) { final String text = element.getText(); if (PsiKeyword.INSTANCEOF.equals(text) && out.charAt(out.length() - 1) != ' ') { out.append(' '); } out.append(text); } else { boolean skippingWhiteSpace = false; for (final PsiElement child : children) { if (child.equals(childToSkip)) { skippingWhiteSpace = true; } else if (child instanceof PsiWhiteSpace && skippingWhiteSpace) { //don't do anything } else { skippingWhiteSpace = false; replaceCollectionGetAccess(child, contentVariableName, listVariable, indexName, childToSkip, out); } } } } }
private void replaceIteratorNext( PsiElement element, String contentVariableName, String iteratorName, PsiElement childToSkip, StringBuilder out, PsiType contentType) { if (isIteratorNext(element, iteratorName, contentType)) { out.append(contentVariableName); } else { final PsiElement[] children = element.getChildren(); if (children.length == 0) { final String text = element.getText(); if (PsiKeyword.INSTANCEOF.equals(text) && out.charAt(out.length() - 1) != ' ') { out.append(' '); } out.append(text); } else { boolean skippingWhiteSpace = false; for (final PsiElement child : children) { if (child.equals(childToSkip)) { skippingWhiteSpace = true; } else if (child instanceof PsiWhiteSpace && skippingWhiteSpace) { //don't do anything } else { skippingWhiteSpace = false; replaceIteratorNext(child, contentVariableName, iteratorName, childToSkip, out, contentType); } } } } }