@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 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); }
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 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); }
private static String getUniqueParameterName( PsiParameter[] parameters, PsiVariable variable, HashMap<PsiField, String> usedNames) { final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(variable.getProject()); final SuggestedNameInfo nameInfo = styleManager.suggestVariableName( VariableKind.PARAMETER, styleManager.variableNameToPropertyName(variable.getName(), VariableKind.FIELD), null, variable.getType()); String newName = nameInfo.names[0]; int n = 1; while (true) { if (isUnique(parameters, newName, usedNames)) { break; } newName = nameInfo.names[0] + n++; } return newName; }
/** * @param sideEffects if null, delete usages, otherwise collect side effects * @return true if there are at least one unrecoverable side effect found, false if no side * effects, null if read usage found (may happen if interval between fix creation in invoke() * call was long enough) * @throws com.intellij.util.IncorrectOperationException */ static Boolean processUsage( PsiElement element, PsiVariable variable, List<PsiElement> sideEffects, int deleteMode) throws IncorrectOperationException { if (!element.isValid()) return null; PsiElementFactory factory = JavaPsiFacade.getInstance(variable.getProject()).getElementFactory(); while (element != null) { if (element instanceof PsiAssignmentExpression) { PsiAssignmentExpression expression = (PsiAssignmentExpression) element; PsiExpression lExpression = expression.getLExpression(); // there should not be read access to the variable, otherwise it is not unused if (!(lExpression instanceof PsiReferenceExpression) || variable != ((PsiReferenceExpression) lExpression).resolve()) { return null; } PsiExpression rExpression = expression.getRExpression(); rExpression = PsiUtil.deparenthesizeExpression(rExpression); if (rExpression == null) return true; // replace assignment with expression and resimplify boolean sideEffectFound = checkSideEffects(rExpression, variable, sideEffects); if (!(element.getParent() instanceof PsiExpressionStatement) || PsiUtil.isStatement(rExpression)) { if (deleteMode == MAKE_STATEMENT || deleteMode == DELETE_ALL && !(element.getParent() instanceof PsiExpressionStatement)) { element = replaceElementWithExpression(rExpression, factory, element); while (element.getParent() instanceof PsiParenthesizedExpression) { element = element.getParent().replace(element); } List<PsiElement> references = new ArrayList<PsiElement>(); collectReferences(element, variable, references); deleteReferences(variable, references, deleteMode); } else if (deleteMode == DELETE_ALL) { deleteWholeStatement(element, factory); } return true; } else { if (deleteMode != CANCEL) { deleteWholeStatement(element, factory); } return !sideEffectFound; } } else if (element instanceof PsiExpressionStatement && deleteMode != CANCEL) { final PsiElement parent = element.getParent(); if (parent instanceof PsiIfStatement || parent instanceof PsiLoopStatement && ((PsiLoopStatement) parent).getBody() == element) { element.replace( JavaPsiFacade.getElementFactory(element.getProject()) .createStatementFromText(";", element)); } else { element.delete(); } break; } else if (element instanceof PsiVariable && element == variable) { PsiExpression expression = variable.getInitializer(); if (expression != null) { expression = PsiUtil.deparenthesizeExpression(expression); } boolean sideEffectsFound = checkSideEffects(expression, variable, sideEffects); if (expression != null && PsiUtil.isStatement(expression) && variable instanceof PsiLocalVariable && !(variable.getParent() instanceof PsiDeclarationStatement && ((PsiDeclarationStatement) variable.getParent()).getDeclaredElements().length > 1)) { if (deleteMode == MAKE_STATEMENT) { element = element.replace(createStatementIfNeeded(expression, factory, element)); List<PsiElement> references = new ArrayList<PsiElement>(); collectReferences(element, variable, references); deleteReferences(variable, references, deleteMode); } else if (deleteMode == DELETE_ALL) { element.delete(); } return true; } else { if (deleteMode != CANCEL) { if (element instanceof PsiField) { ((PsiField) element).normalizeDeclaration(); } element.delete(); } return !sideEffectsFound; } } element = element.getParent(); } return true; }