コード例 #1
0
 public static PsiModifierList createNewModifierList(EncapsulateFieldsDescriptor descriptor) {
   PsiModifierList newModifierList = null;
   PsiElementFactory factory =
       JavaPsiFacade.getInstance(descriptor.getTargetClass().getProject()).getElementFactory();
   try {
     PsiField field = factory.createField("a", PsiType.INT);
     EncapsulateFieldsProcessor.setNewFieldVisibility(field, descriptor);
     newModifierList = field.getModifierList();
   } catch (IncorrectOperationException e) {
     LOG.error(e);
   }
   return newModifierList;
 }
コード例 #2
0
  public static void createFieldAndAddAssignment(
      final @NotNull Project project,
      final @NotNull PsiClass targetClass,
      final @NotNull PsiMethod method,
      final @NotNull PsiParameter parameter,
      final @NotNull PsiType fieldType,
      final @NotNull String fieldName,
      final boolean isStatic,
      final boolean isFinal)
      throws IncorrectOperationException {
    PsiManager psiManager = PsiManager.getInstance(project);
    PsiElementFactory factory =
        JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory();

    PsiField field = factory.createField(fieldName, fieldType);
    PsiModifierList modifierList = field.getModifierList();

    if (modifierList == null) return;
    modifierList.setModifierProperty(PsiModifier.STATIC, isStatic);
    modifierList.setModifierProperty(PsiModifier.FINAL, isFinal);

    NullableNotNullManager.getInstance(project).copyNullableOrNotNullAnnotation(parameter, field);

    PsiCodeBlock methodBody = method.getBody();
    if (methodBody == null) return;
    PsiStatement[] statements = methodBody.getStatements();

    Ref<Pair<PsiField, Boolean>> anchorRef = new Ref<Pair<PsiField, Boolean>>();
    int i = findFieldAssignmentAnchor(statements, anchorRef, targetClass, parameter);
    Pair<PsiField, Boolean> fieldAnchor = anchorRef.get();

    String stmtText = fieldName + " = " + parameter.getName() + ";";
    if (fieldName.equals(parameter.getName())) {
      @NonNls
      String prefix =
          isStatic ? targetClass.getName() == null ? "" : targetClass.getName() + "." : "this.";
      stmtText = prefix + stmtText;
    }

    PsiStatement assignmentStmt = factory.createStatementFromText(stmtText, methodBody);
    assignmentStmt = (PsiStatement) CodeStyleManager.getInstance(project).reformat(assignmentStmt);

    if (i == statements.length) {
      methodBody.add(assignmentStmt);
    } else {
      methodBody.addAfter(assignmentStmt, i > 0 ? statements[i - 1] : null);
    }

    if (fieldAnchor != null) {
      PsiVariable psiVariable = fieldAnchor.getFirst();
      psiVariable.normalizeDeclaration();
    }

    if (targetClass.findFieldByName(fieldName, false) == null) {
      if (fieldAnchor != null) {
        Boolean insertBefore = fieldAnchor.getSecond();
        PsiField inField = fieldAnchor.getFirst();
        if (insertBefore.booleanValue()) {
          targetClass.addBefore(field, inField);
        } else {
          targetClass.addAfter(field, inField);
        }
      } else {
        targetClass.add(field);
      }
    }
  }