PsiField (PsiLocalVariable local, PsiType forcedType, String fieldName, boolean includeInitializer) { @NonNls StringBuilder pattern = new StringBuilder(); pattern.append("private int "); pattern.append(fieldName); if (local.getInitializer() == null) { includeInitializer = false; } if (includeInitializer) { pattern.append("=0"); } pattern.append(";"); final Project project = local.getProject(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); try { PsiField field = factory.createFieldFromText(pattern.toString(), null); field.getTypeElement().replace(factory.createTypeElement(forcedType)); if (includeInitializer) { PsiExpression initializer = CommonJavaRefactoringUtil.convertInitializerToNormalExpression(local.getInitializer(), forcedType); field.getInitializer().replace(initializer); } PsiModifierList sourceModifierList = local.getModifierList(); LOG.assertTrue(sourceModifierList != null); PsiModifierList fieldModifierList = field.getModifierList(); LOG.assertTrue(fieldModifierList != null); GenerateMembersUtil.copyAnnotations(sourceModifierList, fieldModifierList); return field; } catch (IncorrectOperationException e) { LOG.error(e); return null; } }