コード例 #1
0
 private static boolean isIndexedListLoopStatement(PsiForStatement forStatement, boolean ignoreUntypedCollections) {
   final PsiStatement initialization = forStatement.getInitialization();
   if (!(initialization instanceof PsiDeclarationStatement)) {
     return false;
   }
   final PsiDeclarationStatement declaration = (PsiDeclarationStatement)initialization;
   final PsiElement[] declaredElements = declaration.getDeclaredElements();
   final PsiElement secondDeclaredElement;
   if (declaredElements.length == 1) {
     secondDeclaredElement = null;
   }
   else if (declaredElements.length == 2) {
     secondDeclaredElement = declaredElements[1];
   }
   else {
     return false;
   }
   final PsiElement declaredElement = declaredElements[0];
   if (!(declaredElement instanceof PsiVariable)) {
     return false;
   }
   final PsiVariable indexVariable = (PsiVariable)declaredElement;
   final PsiExpression initialValue = indexVariable.getInitializer();
   if (initialValue == null) {
     return false;
   }
   final Object constant = ExpressionUtils.computeConstantExpression(initialValue);
   if (!(constant instanceof Number)) {
     return false;
   }
   final Number number = (Number)constant;
   if (number.intValue() != 0) {
     return false;
   }
   final PsiExpression condition = forStatement.getCondition();
   final Holder collectionHolder = getCollectionFromSizeComparison(condition, indexVariable, secondDeclaredElement);
   if (collectionHolder == null) {
     return false;
   }
   final PsiStatement update = forStatement.getUpdate();
   if (!VariableAccessUtils.variableIsIncremented(indexVariable, update)) {
     return false;
   }
   final PsiStatement body = forStatement.getBody();
   if (!isIndexVariableOnlyUsedAsListIndex(collectionHolder, indexVariable, body)) {
     return false;
   }
   if (collectionHolder != Holder.DUMMY) {
     final PsiVariable collection = collectionHolder.getVariable();
     final PsiClassType collectionType = (PsiClassType)collection.getType();
     final PsiType[] parameters = collectionType.getParameters();
     if (ignoreUntypedCollections && parameters.length == 0) {
       return false;
     }
     return !VariableAccessUtils.variableIsAssigned(collection, body);
   }
   return true;
 }
コード例 #2
0
 static boolean isArrayLoopStatement(PsiForStatement forStatement) {
   final PsiStatement initialization = forStatement.getInitialization();
   if (!(initialization instanceof PsiDeclarationStatement)) {
     return false;
   }
   final PsiDeclarationStatement declaration = (PsiDeclarationStatement)initialization;
   final PsiElement[] declaredElements = declaration.getDeclaredElements();
   final PsiElement secondDeclaredElement;
   if (declaredElements.length == 1) {
     secondDeclaredElement = null;
   }
   else if (declaredElements.length == 2) {
     secondDeclaredElement = declaredElements[1];
   }
   else {
     return false;
   }
   final PsiElement declaredElement = declaredElements[0];
   if (!(declaredElement instanceof PsiVariable)) {
     return false;
   }
   final PsiVariable indexVariable = (PsiVariable)declaredElement;
   final PsiExpression initialValue = indexVariable.getInitializer();
   if (initialValue == null) {
     return false;
   }
   final Object constant =
     ExpressionUtils.computeConstantExpression(initialValue);
   if (!(constant instanceof Integer)) {
     return false;
   }
   final Integer integer = (Integer)constant;
   if (integer.intValue() != 0) {
     return false;
   }
   final PsiStatement update = forStatement.getUpdate();
   if (!VariableAccessUtils.variableIsIncremented(indexVariable, update)) {
     return false;
   }
   final PsiExpression condition = forStatement.getCondition();
   final PsiReferenceExpression arrayReference = getVariableReferenceFromCondition(condition, indexVariable, secondDeclaredElement);
   if (arrayReference == null) {
     return false;
   }
   final PsiElement element = arrayReference.resolve();
   if (!(element instanceof PsiVariable)) {
     return false;
   }
   final PsiVariable arrayVariable = (PsiVariable)element;
   final PsiStatement body = forStatement.getBody();
   return body == null ||
          isIndexVariableOnlyUsedAsIndex(arrayVariable, indexVariable, body) &&
          !VariableAccessUtils.variableIsAssigned(arrayVariable, body) &&
          !VariableAccessUtils.arrayContentsAreAssigned(arrayVariable, body);
 }