@Override public int compare(PsiVariable o1, PsiVariable o2) { if (o1 instanceof PsiParameter && ((PsiParameter) o1).isVarArgs()) return 1; if (o2 instanceof PsiParameter && ((PsiParameter) o2).isVarArgs()) return -1; if (o1 instanceof PsiField && o2 instanceof PsiField) { return o1.getTextOffset() - o2.getTextOffset(); } if (o1 instanceof PsiParameter && o2 instanceof PsiParameter) { return myParameterList.getParameterIndex((PsiParameter) o1) - myParameterList.getParameterIndex((PsiParameter) o2); } if (o1 instanceof PsiField && o2 instanceof PsiParameter) { final PsiField field = FieldFromParameterUtils.getParameterAssignedToField((PsiParameter) o2); if (field == null) return 1; return o1.getTextOffset() - field.getTextOffset(); } if (o1 instanceof PsiParameter && o2 instanceof PsiField) { final PsiField field = FieldFromParameterUtils.getParameterAssignedToField((PsiParameter) o1); if (field == null) return -1; return field.getTextOffset() - o2.getTextOffset(); } return 0; }
private static void checkSillyAssignment( PsiAssignmentExpression assignment, ProblemsHolder holder) { if (assignment.getOperationTokenType() != JavaTokenType.EQ) return; PsiExpression lExpression = assignment.getLExpression(); PsiExpression rExpression = assignment.getRExpression(); if (rExpression == null) return; lExpression = PsiUtil.deparenthesizeExpression(lExpression); rExpression = PsiUtil.deparenthesizeExpression(rExpression); if (!(lExpression instanceof PsiReferenceExpression)) return; PsiReferenceExpression rRef; if (!(rExpression instanceof PsiReferenceExpression)) { if (!(rExpression instanceof PsiAssignmentExpression)) return; final PsiAssignmentExpression rAssignmentExpression = (PsiAssignmentExpression) rExpression; final PsiExpression assignee = PsiUtil.deparenthesizeExpression(rAssignmentExpression.getLExpression()); if (!(assignee instanceof PsiReferenceExpression)) return; rRef = (PsiReferenceExpression) assignee; } else { rRef = (PsiReferenceExpression) rExpression; } PsiReferenceExpression lRef = (PsiReferenceExpression) lExpression; PsiManager manager = assignment.getManager(); if (!sameInstanceReferences(lRef, rRef, manager)) return; final PsiVariable variable = (PsiVariable) lRef.resolve(); if (variable == null) return; holder.registerProblem( assignment, InspectionsBundle.message("assignment.to.itself.problem.descriptor", variable.getName()), ProblemHighlightType.LIKE_UNUSED_SYMBOL); }
@NotNull @Override public PsiDeclarationStatement createVariableDeclarationStatement( @NotNull String name, @NotNull PsiType type, @Nullable PsiExpression initializer) throws IncorrectOperationException { if (!isIdentifier(name)) { throw new IncorrectOperationException("\"" + name + "\" is not an identifier."); } if (PsiType.NULL.equals(type)) { throw new IncorrectOperationException("Cannot create variable with type \"null\"."); } String text = "X " + name + (initializer != null ? " = x" : "") + ";"; PsiDeclarationStatement statement = (PsiDeclarationStatement) createStatementFromText(text, null); PsiVariable variable = (PsiVariable) statement.getDeclaredElements()[0]; replace(variable.getTypeElement(), createTypeElement(type), text); boolean generateFinalLocals = JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalLocals(); PsiUtil.setModifierProperty(variable, PsiModifier.FINAL, generateFinalLocals); if (initializer != null) { replace(variable.getInitializer(), initializer, text); } GeneratedMarkerVisitor.markGenerated(statement); return statement; }
private void makeFinal() { for (PsiVariable var : getVariablesToFix()) { if (var.isValid()) { PsiUtil.setModifierProperty(var, PsiModifier.FINAL, true); } } }
public static PsiExpression findDefinition( @NotNull PsiReferenceExpression referenceExpression, @Nullable PsiVariable variable) { if (variable == null) { final PsiElement target = referenceExpression.resolve(); if (!(target instanceof PsiVariable)) { return null; } variable = (PsiVariable) target; } final PsiCodeBlock block = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class); if (block == null) { return null; } final PsiElement[] defs = DefUseUtil.getDefs(block, variable, referenceExpression); if (defs.length != 1) { return null; } final PsiElement def = defs[0]; if (def instanceof PsiVariable) { final PsiVariable target = (PsiVariable) def; final PsiExpression initializer = target.getInitializer(); return ParenthesesUtils.stripParentheses(initializer); } else if (def instanceof PsiReferenceExpression) { final PsiElement parent = def.getParent(); if (!(parent instanceof PsiAssignmentExpression)) { return null; } final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) parent; if (assignmentExpression.getOperationTokenType() != JavaTokenType.EQ) { return null; } return ParenthesesUtils.stripParentheses(assignmentExpression.getRExpression()); } return null; }
private static void generateVariableInfo( PsiElement originalElement, StringBuilder buffer, PsiVariable variable) { if (variable instanceof PsiField) { final PsiClass parentClass = ((PsiField) variable).getContainingClass(); if (parentClass != null) { buffer.append(JavaDocUtil.getShortestClassName(parentClass, variable)); newLine(buffer); } generateModifiers(buffer, variable); } final PsiType type = variable instanceof GrVariable ? ((GrVariable) variable).getDeclaredType() : variable.getType(); PsiImplUtil.appendTypeString( buffer, calcSubstitutor(originalElement).substitute(type), originalElement); buffer.append(" "); buffer.append(variable.getName()); if (variable instanceof GrVariable) { newLine(buffer); while (originalElement != null) { PsiReference ref = originalElement.getReference(); if (ref != null && ref.resolve() != null) break; originalElement = originalElement.getParent(); } if (originalElement != null) { appendInferredType(originalElement, (GrVariable) variable, buffer); } } }
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; }
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); }
@Override protected LocalQuickFix createRemoveAssignmentFix(PsiReferenceExpression expression) { final PsiElement parent = expression.getParent(); if (parent instanceof PsiVariable) { final PsiVariable variable = (PsiVariable) parent; if (variable.hasModifierProperty(PsiModifier.FINAL)) { return null; } } return new RemoveSillyAssignmentFix(); }
@Override public void visitReferenceExpression(final PsiReferenceExpression reference) { if (myLineRange.intersects(reference.getTextRange())) { final PsiElement psiElement = reference.resolve(); if (psiElement instanceof PsiVariable) { final PsiVariable var = (PsiVariable) psiElement; if (var instanceof PsiField) { if (myCollectExpressions && !DebuggerUtils.hasSideEffectsOrReferencesMissingVars( reference, myVisibleLocals)) { /* if (var instanceof PsiEnumConstant && reference.getQualifier() == null) { final PsiClass enumClass = ((PsiEnumConstant)var).getContainingClass(); if (enumClass != null) { final PsiExpression expression = JavaPsiFacade.getInstance(var.getProject()).getParserFacade().createExpressionFromText(enumClass.getName() + "." + var.getName(), var); final PsiReference ref = expression.getReference(); if (ref != null) { ref.bindToElement(var); myExpressions.add(new TextWithImportsImpl(expression)); } } } else { myExpressions.add(new TextWithImportsImpl(reference)); } */ final PsiModifierList modifierList = var.getModifierList(); boolean isConstant = (var instanceof PsiEnumConstant) || (modifierList != null && modifierList.hasModifierProperty(PsiModifier.STATIC) && modifierList.hasModifierProperty(PsiModifier.FINAL)); if (!isConstant) { myExpressions.add(new TextWithImportsImpl(reference)); } } } else { if (myVisibleLocals.contains(var.getName())) { myVars.add(var.getName()); } else { // fix for variables used in inner classes if (!Comparing.equal( PsiTreeUtil.getParentOfType(reference, PsiClass.class), PsiTreeUtil.getParentOfType(var, PsiClass.class))) { myExpressions.add(new TextWithImportsImpl(reference)); } } } } } super.visitReferenceExpression(reference); }
@Override public boolean isAvailable( @NotNull Project project, @NotNull PsiFile file, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { final PsiModifierList myModifierList = (PsiModifierList) startElement; PsiVariable variable = myVariable == null ? null : myVariable.getElement(); return myModifierList.isValid() && myModifierList.getManager().isInProject(myModifierList) && myModifierList.hasExplicitModifier(myModifier) != myShouldHave && (variable == null || variable.isValid()); }
@Override protected void updateTitle(@Nullable PsiVariable variable, String value) { if (variable == null || !variable.hasInitializer()) { super.updateTitle(variable, value); } else { final PsiExpression initializer = variable.getInitializer(); assert initializer != null; String text = variable.getText().replace(variable.getName(), value); text = text.replace(initializer.getText(), GosuRenderFuction.render(initializer)); setPreviewText(text); revalidate(); } }
@Nullable static HighlightInfo checkVariableMustBeFinal( PsiVariable variable, PsiJavaCodeReferenceElement context, @NotNull LanguageLevel languageLevel) { if (variable.hasModifierProperty(PsiModifier.FINAL)) return null; final PsiClass innerClass = getInnerClassVariableReferencedFrom(variable, context); if (innerClass != null) { if (variable instanceof PsiParameter) { final PsiElement parent = variable.getParent(); if (parent instanceof PsiParameterList && parent.getParent() instanceof PsiLambdaExpression && notAccessedForWriting( variable, new LocalSearchScope(((PsiParameter) variable).getDeclarationScope()))) { return null; } } if (languageLevel.isAtLeast(LanguageLevel.JDK_1_8) && isEffectivelyFinal(variable, innerClass, context)) { return null; } final String description = JavaErrorMessages.message("variable.must.be.final", context.getText()); final HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(context) .descriptionAndTooltip(description) .create(); QuickFixAction.registerQuickFixAction( highlightInfo, QUICK_FIX_FACTORY.createVariableAccessFromInnerClassFix(variable, innerClass)); return highlightInfo; } final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(context, PsiLambdaExpression.class); if (lambdaExpression != null && !PsiTreeUtil.isAncestor(lambdaExpression, variable, true)) { final PsiElement parent = variable.getParent(); if (parent instanceof PsiParameterList && parent.getParent() == lambdaExpression) { return null; } if (!isEffectivelyFinal(variable, lambdaExpression, context)) { return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(context) .descriptionAndTooltip("Variable used in lambda expression should be effectively final") .create(); } } return null; }
@Nullable private static String getLookupObjectName(Object o) { if (o instanceof PsiVariable) { final PsiVariable variable = (PsiVariable) o; JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(variable.getProject()); VariableKind variableKind = codeStyleManager.getVariableKind(variable); return codeStyleManager.variableNameToPropertyName(variable.getName(), variableKind); } if (o instanceof PsiMethod) { return ((PsiMethod) o).getName(); } return null; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiElement element = descriptor.getStartElement(); if (!(element instanceof PsiLoopStatement)) return; PsiLoopStatement loop = (PsiLoopStatement) element; IteratorDeclaration declaration; declaration = IteratorDeclaration.fromLoop(loop); if (declaration == null) return; PsiStatement body = loop.getBody(); if (!(body instanceof PsiBlockStatement)) return; PsiStatement[] statements = ((PsiBlockStatement) body).getCodeBlock().getStatements(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); String replacement = null; CommentTracker ct = new CommentTracker(); if (statements.length == 2 && statements[1] instanceof PsiIfStatement) { PsiVariable variable = declaration.getNextElementVariable(statements[0]); if (variable == null) return; PsiExpression condition = ((PsiIfStatement) statements[1]).getCondition(); if (condition == null) return; replacement = generateRemoveIf(declaration, ct, condition, variable.getName()); } else if (statements.length == 1 && statements[0] instanceof PsiIfStatement) { PsiExpression condition = ((PsiIfStatement) statements[0]).getCondition(); if (condition == null) return; PsiElement ref = declaration.findOnlyIteratorRef(condition); if (ref != null) { PsiElement call = ref.getParent().getParent(); if (!declaration.isIteratorMethodCall(call, "next")) return; PsiType type = ((PsiExpression) call).getType(); JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project); SuggestedNameInfo info = javaCodeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, type); if (info.names.length == 0) { info = javaCodeStyleManager.suggestVariableName( VariableKind.PARAMETER, "value", null, type); } String paramName = javaCodeStyleManager.suggestUniqueVariableName(info, condition, true).names[0]; ct.replace(call, factory.createIdentifier(paramName)); replacement = generateRemoveIf(declaration, ct, condition, paramName); } } if (replacement == null) return; ct.delete(declaration.getIterator()); PsiElement result = ct.replaceAndRestoreComments(loop, replacement); LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result); CodeStyleManager.getInstance(project).reformat(result); }
public DfaVariableValue createVariableValue(PsiVariable myVariable, boolean isNegated) { PsiType varType = myVariable.getType(); if (varType instanceof PsiEllipsisType) { varType = new PsiArrayType(((PsiEllipsisType) varType).getComponentType()); } return createVariableValue(myVariable, varType, isNegated, null); }
@Override public void addBuilderField( @NotNull List<PsiField> fields, @NotNull PsiVariable psiVariable, @NotNull PsiClass innerClass, @NotNull AccessorsInfo accessorsInfo) { final String fieldName = psiVariable.getName(); final PsiType fieldType = getBuilderFieldType(psiVariable.getType(), psiVariable.getProject()); final LombokLightFieldBuilder fieldBuilder = new LombokLightFieldBuilder(psiVariable.getManager(), fieldName, fieldType) .withModifier(PsiModifier.PRIVATE) .withNavigationElement(psiVariable) .withContainingClass(innerClass); fields.add(fieldBuilder); }
@Override @NotNull public String getText() { return QuickFixBundle.message( myVariable instanceof PsiField ? "remove.unused.field" : "remove.unused.variable", myVariable.getName()); }
private static boolean canWriteToFinal( @NotNull PsiVariable variable, @NotNull PsiExpression expression, @NotNull PsiReferenceExpression reference, @NotNull PsiFile containingFile) { if (variable.hasInitializer()) return false; if (variable instanceof PsiParameter) return false; PsiElement innerClass = getInnerClassVariableReferencedFrom(variable, expression); if (variable instanceof PsiField) { // if inside some field initializer if (HighlightUtil.findEnclosingFieldInitializer(expression) != null) return true; // assignment from within inner class is illegal always PsiField field = (PsiField) variable; if (innerClass != null && !containingFile .getManager() .areElementsEquivalent(innerClass, field.getContainingClass())) return false; final PsiMember enclosingCtrOrInitializer = PsiUtil.findEnclosingConstructorOrInitializer(expression); return enclosingCtrOrInitializer != null && isSameField(enclosingCtrOrInitializer, field, reference, containingFile); } if (variable instanceof PsiLocalVariable) { boolean isAccessedFromOtherClass = innerClass != null; if (isAccessedFromOtherClass) { return false; } } return true; }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); final PsiModifierListOwner psiElement = PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class); if (psiElement != null) { RefJavaElement refElement = (RefJavaElement) (myManager != null ? myManager.getReference(psiElement) : null); try { if (psiElement instanceof PsiVariable) { ((PsiVariable) psiElement).normalizeDeclaration(); } final PsiModifierList modifierList = psiElement.getModifierList(); LOG.assertTrue(modifierList != null); modifierList.setModifierProperty(PsiModifier.FINAL, true); modifierList.setModifierProperty(PsiModifier.VOLATILE, false); } catch (IncorrectOperationException e) { LOG.error(e); } if (refElement != null) { RefJavaUtil.getInstance().setIsFinal(refElement, true); } } }
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); }
protected final void registerVariableError(@NotNull PsiVariable variable, Object... infos) { final PsiElement nameIdentifier = variable.getNameIdentifier(); if (nameIdentifier == null) { registerError(variable, infos); } else { registerError(nameIdentifier, infos); } }
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { final CaretModel caretModel = editor.getCaretModel(); final int position = caretModel.getOffset(); final PsiElement element = file.findElementAt(position); Pair<PsiVariable, PsiType> pair = findVariable(element); if (pair == null) return; PsiVariable variable = pair.getFirst(); PsiType type = pair.getSecond(); variable .getTypeElement() .replace( JavaPsiFacade.getInstance(variable.getProject()) .getElementFactory() .createTypeElement(type)); }
private void copyToFinal() throws IncorrectOperationException { PsiManager psiManager = myClass.getManager(); PsiElementFactory factory = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory(); PsiExpression initializer = factory.createExpressionFromText(myVariable.getName(), myClass); String newName = suggestNewName(psiManager.getProject(), myVariable); PsiType type = myVariable.getType(); PsiDeclarationStatement copyDecl = factory.createVariableDeclarationStatement(newName, type, initializer); PsiVariable newVariable = (PsiVariable) copyDecl.getDeclaredElements()[0]; PsiUtil.setModifierProperty(newVariable, PsiModifier.FINAL, true); PsiElement statement = getStatementToInsertBefore(); if (statement == null) return; statement.getParent().addBefore(copyDecl, statement); PsiExpression newExpression = factory.createExpressionFromText(newName, myVariable); replaceReferences(myClass, myVariable, newExpression); }
private Pair<PsiVariable, PsiType> findVariable(final PsiElement element) { PsiVariable variable = null; if (element instanceof PsiIdentifier) { if (element.getParent() instanceof PsiVariable) { variable = (PsiVariable) element.getParent(); } } else if (element instanceof PsiJavaToken) { final PsiJavaToken token = (PsiJavaToken) element; if (token.getTokenType() != JavaTokenType.EQ) return null; if (token.getParent() instanceof PsiVariable) { variable = (PsiVariable) token.getParent(); } } if (variable == null) { return null; } variableName = variable.getName(); final PsiExpression initializer = variable.getInitializer(); if (initializer == null) return null; final PsiType variableType = variable.getType(); final PsiType initializerType = initializer.getType(); if (!(variableType instanceof PsiClassType)) return null; final PsiClassType variableClassType = (PsiClassType) variableType; if (!variableClassType.isRaw()) return null; if (!(initializerType instanceof PsiClassType)) return null; final PsiClassType initializerClassType = (PsiClassType) initializerType; if (initializerClassType.isRaw()) return null; final PsiClassType.ClassResolveResult variableResolveResult = variableClassType.resolveGenerics(); final PsiClassType.ClassResolveResult initializerResolveResult = initializerClassType.resolveGenerics(); if (initializerResolveResult.getElement() == null) return null; final PsiSubstitutor targetSubstitutor = TypeConversionUtil.getClassSubstitutor( variableResolveResult.getElement(), initializerResolveResult.getElement(), initializerResolveResult.getSubstitutor()); if (targetSubstitutor == null) return null; PsiType type = JavaPsiFacade.getInstance(variable.getProject()) .getElementFactory() .createType(variableResolveResult.getElement(), targetSubstitutor); newTypeName = type.getCanonicalText(); return Pair.create(variable, type); }
@Override public void visitVariable(@NotNull PsiVariable variable) { super.visitVariable(variable); final String variableName = variable.getName(); if (!PsiKeyword.ASSERT.equals(variableName)) { return; } registerVariableError(variable); }
public MoveInstanceMethodProcessor( final Project project, final PsiMethod method, final PsiVariable targetVariable, final String newVisibility, final Map<PsiClass, String> oldClassParameterNames) { super(project); myMethod = method; myTargetVariable = targetVariable; myOldClassParameterNames = oldClassParameterNames; LOG.assertTrue( myTargetVariable instanceof PsiParameter || myTargetVariable instanceof PsiField); LOG.assertTrue(myTargetVariable.getType() instanceof PsiClassType); final PsiType type = myTargetVariable.getType(); LOG.assertTrue(type instanceof PsiClassType); myTargetClass = ((PsiClassType) type).resolve(); myNewVisibility = newVisibility; }
private boolean isArrayElementDeclaration( PsiStatement statement, PsiVariable arrayVariable, String indexName) { if (!(statement instanceof PsiDeclarationStatement)) { return false; } final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement) statement; final PsiElement[] declaredElements = declarationStatement.getDeclaredElements(); if (declaredElements.length != 1) { return false; } final PsiElement declaredElement = declaredElements[0]; if (!(declaredElement instanceof PsiVariable)) { return false; } final PsiVariable variable = (PsiVariable) declaredElement; final PsiExpression initializer = variable.getInitializer(); return isArrayLookup(initializer, indexName, arrayVariable); }
private boolean isIteratorNextDeclaration( PsiStatement statement, String iteratorName, PsiType contentType) { if (!(statement instanceof PsiDeclarationStatement)) { return false; } final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement) statement; final PsiElement[] declaredElements = declarationStatement.getDeclaredElements(); if (declaredElements.length != 1) { return false; } final PsiElement declaredElement = declaredElements[0]; if (!(declaredElement instanceof PsiVariable)) { return false; } final PsiVariable variable = (PsiVariable) declaredElement; final PsiExpression initializer = variable.getInitializer(); return isIteratorNext(initializer, iteratorName, contentType); }
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); }