private boolean isArrayLookup(PsiElement element, String indexName, PsiVariable arrayVariable) { if (element == null) { return false; } if (!(element instanceof PsiArrayAccessExpression)) { return false; } final PsiArrayAccessExpression arrayAccess = (PsiArrayAccessExpression) element; final PsiExpression indexExpression = arrayAccess.getIndexExpression(); if (indexExpression == null) { return false; } if (!indexName.equals(indexExpression.getText())) { return false; } final PsiExpression arrayExpression = arrayAccess.getArrayExpression(); if (!(arrayExpression instanceof PsiReferenceExpression)) { return false; } final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) arrayExpression; final PsiExpression qualifier = referenceExpression.getQualifierExpression(); if (qualifier != null && !(qualifier instanceof PsiThisExpression) && !(qualifier instanceof PsiSuperExpression)) { return false; } final PsiElement target = referenceExpression.resolve(); return arrayVariable.equals(target); }
public static boolean variableNameResolvesToTarget( @NotNull String variableName, @NotNull PsiVariable target, @NotNull PsiElement context) { final Project project = context.getProject(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiResolveHelper resolveHelper = psiFacade.getResolveHelper(); final PsiVariable variable = resolveHelper.resolveAccessibleReferencedVariable(variableName, context); return target.equals(variable); }
private boolean isListGetLookup(PsiElement element, String indexName, PsiVariable listVariable) { if (!(element instanceof PsiExpression)) { return false; } final PsiExpression expression = (PsiExpression)element; if (!expressionIsListGetLookup(expression)) { return false; } final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) ParenthesesUtils.stripParentheses(expression); if (methodCallExpression == null) { return false; } final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifierExpression = methodExpression.getQualifierExpression(); final PsiExpressionList argumentList = methodCallExpression.getArgumentList(); final PsiExpression[] expressions = argumentList.getExpressions(); if (expressions.length != 1) { return false; } if (!indexName.equals(expressions[0].getText())) { return false; } if (qualifierExpression == null || qualifierExpression instanceof PsiThisExpression || qualifierExpression instanceof PsiSuperExpression) { return listVariable == null; } if (!(qualifierExpression instanceof PsiReferenceExpression)) { return false; } final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)qualifierExpression; final PsiExpression qualifier = referenceExpression.getQualifierExpression(); if (qualifier != null && !(qualifier instanceof PsiThisExpression) && !(qualifier instanceof PsiSuperExpression)) { return false; } final PsiElement target = referenceExpression.resolve(); return listVariable.equals(target); }
@Override public void visitPostfixExpression(@NotNull PsiPostfixExpression postfixExpression) { if (read || written) { return; } super.visitPostfixExpression(postfixExpression); final IElementType tokenType = postfixExpression.getOperationTokenType(); if (!tokenType.equals(JavaTokenType.PLUSPLUS) && !tokenType.equals(JavaTokenType.MINUSMINUS)) { return; } final PsiExpression operand = postfixExpression.getOperand(); if (!(operand instanceof PsiReferenceExpression)) { return; } final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) operand; final PsiElement target = referenceExpression.resolve(); if (!variable.equals(target)) { return; } written = true; }
@Override public void visitAssignmentExpression(@NotNull PsiAssignmentExpression assignment) { if (read || written) { return; } super.visitAssignmentExpression(assignment); final PsiExpression lhs = assignment.getLExpression(); if (lhs instanceof PsiReferenceExpression) { PsiReferenceExpression referenceExpression = (PsiReferenceExpression) lhs; final PsiElement target = referenceExpression.resolve(); if (variable.equals(target)) { written = true; return; } } final PsiExpression rhs = assignment.getRExpression(); if (rhs == null) { return; } final VariableUsedVisitor visitor = new VariableUsedVisitor(variable); rhs.accept(visitor); read = visitor.isUsed(); reference = visitor.getReference(); }
public void run() { try { final boolean rebindNeeded2 = !myVariableName.equals(myFieldName) || myRebindNeeded; final PsiReference[] refs; if (rebindNeeded2) { refs = ReferencesSearch.search(myLocal, GlobalSearchScope.projectScope(myProject), false) .toArray(new PsiReference[0]); } else { refs = null; } final JavaPsiFacade facade = JavaPsiFacade.getInstance(myProject); PsiVariable psiVariable = facade.getResolveHelper().resolveAccessibleReferencedVariable(myFieldName, myLocal); if (psiVariable != null && (!psiVariable.equals(myLocal))) { CommonRefactoringUtil.showErrorMessage( GosuIntroduceFieldHandler.REFACTORING_NAME, LOCAL_VARIABLE_WITH_THIS_NAME_ALREADY_EXISTS, HelpID.INTRODUCE_FIELD, myProject); return; } if (refs != null) { for (PsiReference occur : refs) { psiVariable = facade .getResolveHelper() .resolveAccessibleReferencedVariable(myFieldName, (PsiElement) occur); if (psiVariable != null && (psiVariable.getName().equals(myFieldName))) { CommonRefactoringUtil.showErrorMessage( GosuIntroduceFieldHandler.REFACTORING_NAME, LOCAL_VARIABLE_WITH_THIS_NAME_ALREADY_EXISTS, HelpID.INTRODUCE_FIELD, myProject); return; } } } final PsiMethod enclosingConstructor = GosuBaseExpressionToFieldHandler.getEnclosingConstructor(myDestinationClass, myLocal); myField = mySettings.isIntroduceEnumConstant() ? EnumConstantsUtil.createEnumConstant(myDestinationClass, myLocal, myFieldName) : createField( myLocal, mySettings.getForcedType(), myFieldName, myInitializerPlace == IN_FIELD_DECLARATION); myField = (PsiField) myDestinationClass.add(myField); GosuBaseExpressionToFieldHandler.setModifiers(myField, mySettings); if (!mySettings.isIntroduceEnumConstant()) { VisibilityUtil.fixVisibility(myOccurences, myField, mySettings.getFieldVisibility()); } myLocal.normalizeDeclaration(); // PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement) // myLocal.getParent(); final GosuBaseExpressionToFieldHandler.InitializationPlace finalInitializerPlace; if (myLocal.getInitializer() == null) { finalInitializerPlace = IN_FIELD_DECLARATION; } else { finalInitializerPlace = myInitializerPlace; } final PsiElementFactory factory = JavaPsiFacade.getInstance(myProject).getElementFactory(); switch (finalInitializerPlace) { case IN_FIELD_DECLARATION: // declarationStatement.delete(); myLocal.delete(); break; case IN_CURRENT_METHOD: PsiStatement statement = createAssignment(myLocal, myFieldName, null); // myAssignmentStatement = (PsiStatement) // declarationStatement.replace(statement); myAssignmentStatement = (PsiStatement) myLocal.replace(statement); GeneratedMarkerVisitor.markGenerated(myAssignmentStatement); break; case IN_CONSTRUCTOR: myAssignmentStatement = addInitializationToConstructors(myLocal, myField, enclosingConstructor, factory); GeneratedMarkerVisitor.markGenerated(myAssignmentStatement); break; case IN_SETUP_METHOD: // myAssignmentStatement = addInitializationToSetUp(myLocal, myField, // factory); } if (enclosingConstructor != null && myInitializerPlace == IN_CONSTRUCTOR) { PsiStatement statement = createAssignment(myLocal, myFieldName, null); // myAssignmentStatement = (PsiStatement) // declarationStatement.replace(statement); myAssignmentStatement = (PsiStatement) myLocal.replace(statement); GeneratedMarkerVisitor.markGenerated(myAssignmentStatement); } if (rebindNeeded2) { for (final PsiReference reference : refs) { if (reference != null) { // expr = GosuRefactoringUtil.outermostParenthesizedExpression(expr); GosuRefactoringUtil.replaceOccurenceWithFieldRef( (PsiExpression) reference, myField, myDestinationClass); // replaceOccurenceWithFieldRef((PsiExpression)reference, field, aaClass); } } // GosuRefactoringUtil.renameVariableReferences(local, pPrefix + fieldName, // GlobalSearchScope.projectScope(myProject)); } ClassLord.doImportAndStick( mySettings.getForcedType().getCanonicalText(), myDestinationClass.getContainingFile()); } catch (IncorrectOperationException e) { LOG.error(e); } }