private static void addSuggestionsInspiredByFieldNames(Set<LookupElement> set, PrefixMatcher matcher, PsiVariable var, Project project, JavaCodeStyleManager codeStyleManager) { PsiClass psiClass = PsiTreeUtil.getParentOfType(var, PsiClass.class); if (psiClass == null) { return; } for (PsiField field : psiClass.getFields()) { if (field.getType().isAssignableFrom(var.getType())) { String prop = codeStyleManager.variableNameToPropertyName(field.getName(), VariableKind.FIELD); addLookupItems(set, null, matcher, project, codeStyleManager.propertyNameToVariableName(prop, VariableKind.PARAMETER)); } } }
private static void registerPriorityActions(@NotNull QuickFixActionRegistrar registrar, @NotNull TextRange fixRange, @NotNull PsiReferenceExpression refExpr) { final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(refExpr.getProject()); final Map<VariableKind, IntentionAction> map = new EnumMap<VariableKind, IntentionAction>(VariableKind.class); map.put(VariableKind.FIELD, new CreateFieldFromUsageFix(refExpr)); map.put(VariableKind.STATIC_FINAL_FIELD, new CreateConstantFieldFromUsageFix(refExpr)); if (!refExpr.isQualified()) { map.put(VariableKind.LOCAL_VARIABLE, new CreateLocalFromUsageFix(refExpr)); map.put(VariableKind.PARAMETER, new CreateParameterFromUsageFix(refExpr)); } final VariableKind kind = getKind(styleManager, refExpr); if (map.containsKey(kind)) { map.put(kind, PriorityIntentionActionWrapper.highPriority(map.get(kind))); } for (IntentionAction action : map.values()) { registrar.register(fixRange, action, null); } }
private ParameterInfoImpl[] getNewParametersInfo(PsiMethod method) throws IncorrectOperationException { List<ParameterInfoImpl> result = new ArrayList<ParameterInfoImpl>(); PsiParameter[] parameters = method.getParameterList().getParameters(); PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory(); JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(method.getProject()); SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, myParameterType); PsiParameter newParameter = factory.createParameter(nameInfo.names[0], myParameterType); if (method.getContainingClass().isInterface()) { PsiUtil.setModifierProperty(newParameter, PsiModifier.FINAL, false); } for (int i = 0; i < parameters.length; i++) { PsiParameter parameter = parameters[i]; if (i == myIndex) { newParameter.setName(parameter.getName()); parameter = newParameter; } result.add(new ParameterInfoImpl(i, parameter.getName(), parameter.getType())); } if (parameters.length == myIndex) { result.add(new ParameterInfoImpl(-1, newParameter.getName(), newParameter.getType())); } return result.toArray(new ParameterInfoImpl[result.size()]); }
@Nullable private static PsiField findFieldToAssign(@NotNull Project project, @NotNull PsiParameter myParameter) { final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project); final String parameterName = myParameter.getName(); final String propertyName = styleManager.variableNameToPropertyName(parameterName, VariableKind.PARAMETER); final PsiMethod method = (PsiMethod)myParameter.getDeclarationScope(); final boolean isMethodStatic = method.hasModifierProperty(PsiModifier.STATIC); final VariableKind kind = isMethodStatic ? VariableKind.STATIC_FIELD : VariableKind.FIELD; final SuggestedNameInfo suggestedNameInfo = styleManager.suggestVariableName(kind, propertyName, null, FieldFromParameterUtils.getSubstitutedType(myParameter)); final String fieldName = suggestedNameInfo.names[0]; PsiClass aClass = method.getContainingClass(); if (aClass == null) return null; PsiField field = aClass.findFieldByName(fieldName, false); if (field == null) return null; if (!field.hasModifierProperty(PsiModifier.STATIC) && isMethodStatic) return null; return field; }
private void outputConstructor(StringBuilder out) { final String typeText = myValueType.getCanonicalText(true); final String name = "value"; final String parameterName = JavaCodeStyleManager.getInstance(myProject).propertyNameToVariableName(name, VariableKind.PARAMETER); final String fieldName = getFieldName(name); out.append("\tpublic ").append(myClassName).append('('); out.append(CodeStyleSettingsManager.getSettings(myProject).GENERATE_FINAL_PARAMETERS ? "final " : ""); out.append(typeText).append(' ').append(parameterName); out.append(") {\n"); if (fieldName.equals(parameterName)) { out.append("\t\tthis.").append(fieldName).append(" = ").append(parameterName).append(";\n"); } else { out.append("\t\t").append(fieldName).append(" = ").append(parameterName).append(";\n"); } out.append("\t}"); }
private PsiMethod createMethodSignature(String createMethodName) { JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(myProject); final StringBuffer buf = new StringBuffer(); final PsiMethod constructor = getWorkingConstructor(); for (PsiParameter parameter : constructor.getParameterList().getParameters()) { final String pureParamName = styleManager.variableNameToPropertyName(parameter.getName(), VariableKind.PARAMETER); if (buf.length() > 0) buf.append(", "); buf.append(myParametersMap.get(pureParamName).getFieldName()); } return myElementFactory.createMethodFromText("public " + constructor.getName() + " " + createMethodName + "(){\n return new " + constructor.getName() + "(" + buf.toString() + ")" + ";\n}", constructor); }
private void applyNewSetterPrefix() { final String setterPrefix = Messages.showInputDialog(myTable, "New setter prefix:", "Rename Setters Prefix", null, mySetterPrefix, new MySetterPrefixInputValidator()); if (setterPrefix != null) { mySetterPrefix = setterPrefix; PropertiesComponent.getInstance(myProject).setValue(SETTER_PREFIX_KEY, setterPrefix); final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(myProject); for (String paramName : myParametersMap.keySet()) { final ParameterData data = myParametersMap.get(paramName); paramName = data.getParamName(); final String propertyName = javaCodeStyleManager.variableNameToPropertyName(paramName, VariableKind.PARAMETER); data.setSetterName(PropertyUtil.suggestSetterName(propertyName, setterPrefix)); } myTable.revalidate(); myTable.repaint(); } }
@Nullable private static SuggestedNameInfo suggestNamesForElement(final PsiElement element, PsiElement nameSuggestionContext) { PsiVariable var = null; if (element instanceof PsiVariable) { var = (PsiVariable)element; } else if (element instanceof PsiIdentifier) { PsiIdentifier identifier = (PsiIdentifier)element; if (identifier.getParent() instanceof PsiVariable) { var = (PsiVariable)identifier.getParent(); } } if (var == null) return null; JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(element.getProject()); VariableKind variableKind = codeStyleManager.getVariableKind(var); final SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(variableKind, null, var.getInitializer(), var.getType()); final PsiExpression expression = PsiTreeUtil.getParentOfType(nameSuggestionContext, PsiExpression.class, false); if (expression != null) { return new SuggestedNameInfo.Delegate(codeStyleManager.suggestVariableName(variableKind, null, expression, var.getType()).names, nameInfo); } return nameInfo; }
public void setup(final PsiClass innerClass, final String className, final boolean passOuterClass, final String parameterName, boolean searchInComments, boolean searchInNonJava, @NotNull final PsiElement targetContainer) { myNewClassName = className; myInnerClass = innerClass; myDescriptiveName = DescriptiveNameUtil.getDescriptiveName(myInnerClass); myOuterClass = myInnerClass.getContainingClass(); myTargetContainer = targetContainer; JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myProject); myParameterNameOuterClass = passOuterClass ? parameterName : null; if (myParameterNameOuterClass != null) { myFieldNameOuterClass = codeStyleManager.variableNameToPropertyName(myParameterNameOuterClass, VariableKind.PARAMETER); myFieldNameOuterClass = codeStyleManager.propertyNameToVariableName(myFieldNameOuterClass, VariableKind.FIELD); } mySearchInComments = searchInComments; mySearchInNonJavaFiles = searchInNonJava; }
public VariableInfo[] getVariableInfos() { JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(myProject); VariableInfo[] infos = new VariableInfo[myVariableData.length]; for (int idx = 0; idx < myVariableData.length; idx++) { VariableData data = myVariableData[idx]; VariableInfo info = myVariableToInfoMap.get(data.variable); info.passAsParameter = data.passAsParameter; info.parameterName = data.name; info.parameterName = data.name; String propertyName = codeStyleManager.variableNameToPropertyName(data.name, VariableKind.PARAMETER); info.fieldName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.FIELD); infos[idx] = info; } return infos; }
@Override protected boolean checkOutputVariablesCount() { myMultipleExitPoints = super.checkOutputVariablesCount(); myOutputFields = new PsiField[myOutputVariables.length]; for (int i = 0; i < myOutputVariables.length; i++) { PsiVariable variable = myOutputVariables[i]; if (!myInputVariables.contains(variable)) { //one field creation final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(myProject); final String fieldName = styleManager.suggestVariableName(VariableKind.FIELD, getPureName(variable), null, variable.getType()).names[0]; try { myOutputFields[i] = myElementFactory.createField(fieldName, variable.getType()); } catch (IncorrectOperationException e) { LOG.error(e); } } } return !myCreateInnerClass && myMultipleExitPoints; }
private void calculateBackpointerName() { final String baseName; if (originalClassName.indexOf((int)'.') == 0) { baseName = StringUtil.decapitalize(originalClassName); } else { final String simpleClassName = originalClassName.substring(originalClassName.lastIndexOf('.') + 1); baseName = StringUtil.decapitalize(simpleClassName); } String name = myJavaCodeStyleManager.propertyNameToVariableName(baseName, VariableKind.FIELD); if (!existsFieldWithName(name)) { backPointerName = name; return; } int counter = 1; while (true) { name = name + counter; if (!existsFieldWithName(name)) { backPointerName = name; return; } counter++; } }
@Override protected String[] suggestNames(boolean replaceAll, PsiVariable variable) { final PsiType defaultType = getType(); final String propertyName = variable != null ? JavaCodeStyleManager.getInstance(myProject).variableNameToPropertyName(variable.getName(), VariableKind.LOCAL_VARIABLE) : null; final String[] names = suggestNames(defaultType, propertyName); if (propertyName != null && names.length > 1) { final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(myProject); final String paramName = javaCodeStyleManager.propertyNameToVariableName(propertyName, getVariableKind()); final int idx = ArrayUtil.find(names, paramName); if (idx > -1) { ArrayUtil.swap(names, 0, idx); } } return names; }
protected static NameSuggestionsGenerator createNameSuggestionGenerator(final PsiExpression expr, final String propName, final Project project, final String enteredName) { return new NameSuggestionsGenerator() { public SuggestedNameInfo getSuggestedNameInfo(PsiType type) { final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); SuggestedNameInfo info = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, propName, expr != null && expr.isValid() ? expr : null, type); if (expr != null && expr.isValid()) { info = codeStyleManager.suggestUniqueVariableName(info, expr, true); } final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil .completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, info), expr); return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings): strings, info); } }; }
protected static NameSuggestionsGenerator createNameSuggestionGenerator(final String propertyName, final PsiExpression psiExpression, final JavaCodeStyleManager codeStyleManager, final String enteredName, final PsiClass parentClass) { return new NameSuggestionsGenerator() { public SuggestedNameInfo getSuggestedNameInfo(PsiType type) { SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.STATIC_FINAL_FIELD, propertyName, psiExpression, type); if (psiExpression != null) { String[] names = nameInfo.names; for (int i = 0, namesLength = names.length; i < namesLength; i++) { String name = names[i]; if (parentClass.findFieldByName(name, false) != null) { names[i] = codeStyleManager.suggestUniqueVariableName(name, psiExpression, true); } } } final String[] strings = AbstractJavaInplaceIntroducer.appendUnresolvedExprName(JavaCompletionUtil .completeVariableNameForRefactoring(codeStyleManager, type, VariableKind.LOCAL_VARIABLE, nameInfo), psiExpression); return new SuggestedNameInfo.Delegate(enteredName != null ? ArrayUtil.mergeArrays(new String[]{enteredName}, strings): strings, nameInfo); } }; }
public static String createTempVar(PsiExpression expr, PsiElement context, boolean declareFinal) throws IncorrectOperationException { PsiElement anchorStatement = getParentStatement(context, true); LOG.assertTrue(anchorStatement != null && anchorStatement.getParent() != null); Project project = expr.getProject(); String[] suggestedNames = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.LOCAL_VARIABLE, null, expr, null).names; final String prefix = suggestedNames.length > 0 ? suggestedNames[0] : "var"; final String id = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(prefix, context, true); PsiElementFactory factory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory(); if (expr instanceof PsiParenthesizedExpression) { PsiExpression expr1 = ((PsiParenthesizedExpression)expr).getExpression(); if (expr1 != null) { expr = expr1; } } PsiDeclarationStatement decl = factory.createVariableDeclarationStatement(id, expr.getType(), expr); if (declareFinal) { PsiUtil.setModifierProperty(((PsiLocalVariable)decl.getDeclaredElements()[0]), PsiModifier.FINAL, true); } anchorStatement.getParent().addBefore(decl, anchorStatement); return id; }
private void updateTargetClass() { final PsiClass targetClass = getSelectedTargetClass(); PsiManager psiManager = myClass.getManager(); PsiType superType = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createType(targetClass); SuggestedNameInfo suggestedNameInfo = JavaCodeStyleManager.getInstance(psiManager.getProject()).suggestVariableName(VariableKind.FIELD, null, null, superType); myFieldNameField.setSuggestions(suggestedNameInfo.names); myInnerClassNameField.getComponent().setEnabled(InheritanceToDelegationUtil.isInnerClassNeeded(myClass, targetClass)); @NonNls final String suggestion = "My" + targetClass.getName(); myInnerClassNameField.setSuggestions(new String[]{suggestion}); myDataChangedListener = new NameSuggestionsField.DataChanged() { public void dataChanged() { validateButtons(); } }; myInnerClassNameField.addDataChangedListener(myDataChangedListener); myFieldNameField.addDataChangedListener(myDataChangedListener); myMemberSelectionPanel.getTable().setMemberInfos(myBasesToMemberInfos.get(targetClass)); myMemberSelectionPanel.getTable().fireExternalDataChange(); }
private static String createNewVariableName(@NotNull PsiWhileStatement scope, PsiType type, String containerName) { final Project project = scope.getProject(); final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); @NonNls String baseName; if (containerName != null) { baseName = StringUtils.createSingularFromName(containerName); } else { final SuggestedNameInfo suggestions = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, type); final String[] names = suggestions.names; if (names != null && names.length > 0) { baseName = names[0]; } else { baseName = "value"; } } if (baseName == null || baseName.isEmpty()) { baseName = "value"; } return codeStyleManager.suggestUniqueVariableName(baseName, scope, true); }
private static String createNewVariableName(@NotNull PsiElement scope, @NotNull PsiType type) { final Project project = scope.getProject(); final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); @NonNls String baseName; final SuggestedNameInfo suggestions = codeStyleManager.suggestVariableName(VariableKind.LOCAL_VARIABLE, null, null, type); final String[] names = suggestions.names; if (names != null && names.length > 0) { baseName = names[0]; } else { baseName = "entry"; } if (baseName == null || baseName.isEmpty()) { baseName = "entry"; } return codeStyleManager.suggestUniqueVariableName(baseName, scope, true); }
@Override public PsiMethod[] generateGetters(PsiField field) { final Project project = field.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiMethod getter = GenerateMembersUtil.generateSimpleGetterPrototype(field); final PsiType wrappedType = JavaFxPsiUtil.getWrappedPropertyType(field, project, JavaFxCommonClassNames.ourReadOnlyMap); final PsiTypeElement returnTypeElement = getter.getReturnTypeElement(); LOG.assertTrue(returnTypeElement != null); returnTypeElement.replace(factory.createTypeElement(wrappedType)); final PsiCodeBlock getterBody = getter.getBody(); LOG.assertTrue(getterBody != null); getterBody.getStatements()[0].replace(factory.createStatementFromText("return " + field.getName() + ".get();", field)); final PsiMethod propertyGetter = PropertyUtil.generateGetterPrototype(field); propertyGetter.setName(JavaCodeStyleManager.getInstance(project).variableNameToPropertyName(field.getName(), VariableKind.FIELD) + "Property"); return new PsiMethod[] {getter, GenerateMembersUtil.annotateOnOverrideImplement(field.getContainingClass(), propertyGetter)}; }
private static void registerPriorityActions(@NotNull QuickFixActionRegistrar registrar, @NotNull TextRange fixRange, @NotNull PsiReferenceExpression refExpr) { final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(refExpr.getProject()); final Map<VariableKind, IntentionAction> map = new HashMap<VariableKind, IntentionAction>(); map.put(VariableKind.FIELD, new CreateFieldFromUsageFix(refExpr)); map.put(VariableKind.STATIC_FINAL_FIELD, new CreateConstantFieldFromUsageFix(refExpr)); if (!refExpr.isQualified()) { map.put(VariableKind.LOCAL_VARIABLE, new CreateLocalFromUsageFix(refExpr)); map.put(VariableKind.PARAMETER, new CreateParameterFromUsageFix(refExpr)); } final VariableKind kind = getKind(styleManager, refExpr); if (map.containsKey(kind)) { map.put(kind, PriorityIntentionActionWrapper.highPriority(map.get(kind))); } for (IntentionAction action : map.values()) { registrar.register(fixRange, action, null); } }
@NotNull private static VariableKind getKind(@NotNull JavaCodeStyleManager styleManager, @NotNull PsiReferenceExpression refExpr) { final String reference = refExpr.getText(); if (reference.toUpperCase().equals(reference)){ return VariableKind.STATIC_FINAL_FIELD; } for (VariableKind kind : VariableKind.values()) { final String prefix = styleManager.getPrefixByVariableKind(kind); final String suffix = styleManager.getSuffixByVariableKind(kind); if (prefix.isEmpty() && suffix.isEmpty()) { continue; } if (reference.startsWith(prefix) && reference.endsWith(suffix)) { return kind; } } return VariableKind.LOCAL_VARIABLE; }
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; }
private void outputConstructor(@NonNls StringBuffer out) { out.append("\tpublic " + className + '('); final String typeText = valueType.getCanonicalText(); @NonNls final String name = "value"; final String parameterName = JavaCodeStyleManager.getInstance(myProject).propertyNameToVariableName(name, VariableKind.PARAMETER); out.append(CodeStyleSettingsManager.getSettings(myProject).GENERATE_FINAL_PARAMETERS ? "final " : ""); out.append(typeText + ' ' + parameterName); out.append(")\n"); out.append("\t{\n"); final String fieldName = getFieldName(name); if (fieldName.equals(parameterName)) { out.append("\t\tthis." + fieldName + " = " + parameterName + ";\n"); } else { out.append("\t\t" + fieldName + " = " + parameterName + ";\n"); } out.append("\t}\n"); out.append('\n'); }
private void outputSetter(ParameterSpec field, @NonNls StringBuffer out) { if (!field.isSetterRequired()) { return; } final PsiParameter parameter = field.getParameter(); final PsiType type = field.getType(); final String typeText; if (parameter.isVarArgs()) { typeText = ((PsiArrayType) type).getComponentType().getCanonicalText() + "..."; } else { typeText = type.getCanonicalText(); } final String name = calculateStrippedName(field.getName()); final String capitalizedName = StringUtil.capitalize(name); final String parameterName = myJavaCodeStyleManager.propertyNameToVariableName(name, VariableKind.PARAMETER); out.append("\tpublic void set" + capitalizedName + '('); outputAnnotationString(parameter, out); out.append(CodeStyleSettingsManager.getSettings(myProject).GENERATE_FINAL_PARAMETERS?"final " : ""); out.append(' ' +typeText + ' ' + parameterName + ")\n"); out.append("\t{\n"); final String fieldName = myJavaCodeStyleManager.propertyNameToVariableName(name, VariableKind.FIELD); generateFieldAssignment(out, parameterName, fieldName); out.append("\t}\n"); }
private void outputGetter(ParameterSpec field, @NonNls StringBuffer out) { final PsiParameter parameter = field.getParameter(); final PsiType type = field.getType(); final String typeText; if (parameter.isVarArgs()) { typeText = ((PsiArrayType) type).getComponentType().getCanonicalText() + "[]"; } else { typeText = type.getCanonicalText(); } final String name = calculateStrippedName(field.getName()); final String capitalizedName = StringUtil.capitalize(name); if (PsiType.BOOLEAN.equals(type)) { out.append('\t'); outputAnnotationString(parameter, out); out.append(" public "+ typeText + " is" + capitalizedName + "()\n"); } else { out.append('\t'); outputAnnotationString(parameter, out); out.append(" public " +typeText + " get" + capitalizedName + "()\n"); } out.append("\t{\n"); final String fieldName = myJavaCodeStyleManager.propertyNameToVariableName(name, VariableKind.FIELD); out.append("\t\treturn " + fieldName + ";\n"); out.append("\t}\n"); }
private void outputField(ParameterSpec field, StringBuffer out) { final PsiParameter parameter = field.getParameter(); final PsiDocComment docComment = getJavadocForVariable(parameter); if (docComment != null) { out.append(docComment.getText()); out.append('\n'); } final PsiType type = field.getType(); final String typeText; if (parameter.isVarArgs()) { final PsiType componentType = ((PsiArrayType) type).getComponentType(); typeText = componentType.getCanonicalText() + "[]"; } else { typeText = type.getCanonicalText(); } final String name = calculateStrippedName(field.getName()); @NonNls String modifierString = "private "; if (!field.isSetterRequired()) { modifierString += "final "; } outputAnnotationString(parameter, out); out.append('\t' + modifierString + typeText + ' ' + myJavaCodeStyleManager.propertyNameToVariableName(name, VariableKind.FIELD) + ";\n"); }
@Override protected String[] suggestNames(boolean replaceAll, PsiVariable variable) { myTypeSelectorManager.setAllOccurrences(replaceAll); final PsiType defaultType = myTypeSelectorManager.getTypeSelector().getSelectedType(); final String propertyName = variable != null ? JavaCodeStyleManager.getInstance(myProject).variableNameToPropertyName(variable.getName(), VariableKind.LOCAL_VARIABLE) : null; final String[] names = suggestNames(defaultType, propertyName); if (propertyName != null && names.length > 1) { final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(myProject); final String paramName = javaCodeStyleManager.propertyNameToVariableName(propertyName, getVariableKind()); final int idx = ArrayUtil.find(names, paramName); if (idx > -1) { ArrayUtil.swap(names, 0, idx); } } return names; }
public static String createTempVar(PsiExpression expr, PsiElement context, boolean declareFinal) throws IncorrectOperationException { PsiElement anchorStatement = getParentStatement(context, true); LOG.assertTrue(anchorStatement != null && anchorStatement.getParent() != null); Project project = expr.getProject(); String[] suggestedNames = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.LOCAL_VARIABLE, null, expr, null).names; final String prefix = suggestedNames[0]; final String id = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(prefix, context, true); PsiElementFactory factory = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory(); if (expr instanceof PsiParenthesizedExpression) { PsiExpression expr1 = ((PsiParenthesizedExpression)expr).getExpression(); if (expr1 != null) { expr = expr1; } } PsiDeclarationStatement decl = factory.createVariableDeclarationStatement(id, expr.getType(), expr); if (declareFinal) { PsiUtil.setModifierProperty(((PsiLocalVariable)decl.getDeclaredElements()[0]), PsiModifier.FINAL, true); } anchorStatement.getParent().addBefore(decl, anchorStatement); return id; }