private static PsiParameter createNewParameter(
     JavaChangeInfo changeInfo, JavaParameterInfo newParm, PsiSubstitutor substitutor)
     throws IncorrectOperationException {
   final PsiParameterList list = changeInfo.getMethod().getParameterList();
   final PsiElementFactory factory =
       JavaPsiFacade.getInstance(list.getProject()).getElementFactory();
   final PsiType type = substitutor.substitute(newParm.createType(list, list.getManager()));
   return factory.createParameter(newParm.getName(), type);
 }
示例#2
0
  public static PsiMethod generateSetterPrototype(
      PsiField field, final PsiClass containingClass, boolean returnSelf) {
    Project project = field.getProject();
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    PsiElementFactory factory = JavaPsiFacade.getInstance(field.getProject()).getElementFactory();

    String name = field.getName();
    boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
    VariableKind kind = codeStyleManager.getVariableKind(field);
    String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
    String setName = suggestSetterName(project, field);
    try {
      PsiMethod setMethod =
          factory.createMethod(
              setName, returnSelf ? factory.createType(containingClass) : PsiType.VOID);
      String parameterName =
          codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
      PsiParameter param = factory.createParameter(parameterName, field.getType());

      annotateWithNullableStuff(field, factory, param);

      setMethod.getParameterList().add(param);
      PsiUtil.setModifierProperty(setMethod, PsiModifier.PUBLIC, true);
      PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);

      @NonNls StringBuffer buffer = new StringBuffer();
      buffer.append("{\n");
      if (name.equals(parameterName)) {
        if (!isStatic) {
          buffer.append("this.");
        } else {
          String className = containingClass.getName();
          if (className != null) {
            buffer.append(className);
            buffer.append(".");
          }
        }
      }
      buffer.append(name);
      buffer.append("=");
      buffer.append(parameterName);
      buffer.append(";\n");
      if (returnSelf) {
        buffer.append("return this;\n");
      }
      buffer.append("}");
      PsiCodeBlock body = factory.createCodeBlockFromText(buffer.toString(), null);
      setMethod.getBody().replace(body);
      setMethod = (PsiMethod) CodeStyleManager.getInstance(project).reformat(setMethod);
      return setMethod;
    } catch (IncorrectOperationException e) {
      LOG.error(e);
      return null;
    }
  }
  private PsiMethod generateMethodFor(
      PsiField field, String methodName, String parameterName, PsiElementFactory elementFactory) {
    PsiMethod withMethod =
        elementFactory.createMethod(
            methodName, elementFactory.createType(field.getContainingClass()));
    PsiParameter parameter = elementFactory.createParameter(parameterName, field.getType());
    withMethod.getParameterList().add(parameter);
    PsiUtil.setModifierProperty(withMethod, PsiModifier.PUBLIC, true);

    return withMethod;
  }
 private void addParameters(
     final PsiElementFactory factory, final PsiMethod methodCopy, final boolean isInterface)
     throws IncorrectOperationException {
   final Set<Map.Entry<PsiClass, String>> entries = myOldClassParameterNames.entrySet();
   for (final Map.Entry<PsiClass, String> entry : entries) {
     final PsiClassType type = factory.createType(entry.getKey());
     final PsiParameter parameter = factory.createParameter(entry.getValue(), type);
     if (isInterface) {
       PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, false);
     }
     methodCopy.getParameterList().add(parameter);
   }
 }
    private void addMethodConflicts(MultiMap<PsiElement, String> conflicts) {
      String newMethodName = myChangeInfo.getNewName();
      if (!(myChangeInfo instanceof JavaChangeInfo)) {
        return;
      }
      try {
        PsiMethod prototype;
        final PsiMethod method = myChangeInfo.getMethod();
        if (!StdLanguages.JAVA.equals(method.getLanguage())) return;
        PsiManager manager = method.getManager();
        PsiElementFactory factory =
            JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
        final CanonicalTypes.Type returnType = myChangeInfo.getNewReturnType();
        if (returnType != null) {
          prototype = factory.createMethod(newMethodName, returnType.getType(method, manager));
        } else {
          prototype = factory.createConstructor();
          prototype.setName(newMethodName);
        }
        JavaParameterInfo[] parameters = myChangeInfo.getNewParameters();

        for (JavaParameterInfo info : parameters) {
          PsiType parameterType = info.createType(method, manager);
          if (parameterType == null) {
            parameterType =
                JavaPsiFacade.getElementFactory(method.getProject())
                    .createTypeFromText(CommonClassNames.JAVA_LANG_OBJECT, method);
          }
          PsiParameter param = factory.createParameter(info.getName(), parameterType);
          prototype.getParameterList().add(param);
        }

        ConflictsUtil.checkMethodConflicts(
            method.getContainingClass(), method, prototype, conflicts);
      } catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
  protected void changeSelf(PsiElementFactory factory, UsageInfo[] usages)
      throws IncorrectOperationException {
    final MethodJavaDocHelper javaDocHelper = new MethodJavaDocHelper(myMember);
    PsiParameterList paramList = myMember.getParameterList();
    PsiElement addParameterAfter = null;
    PsiDocTag anchor = null;
    List<PsiType> addedTypes = new ArrayList<PsiType>();

    final PsiClass containingClass = myMember.getContainingClass();
    LOG.assertTrue(containingClass != null);

    if (mySettings.isDelegate()) {
      List<ParameterInfoImpl> params = new ArrayList<ParameterInfoImpl>();
      PsiParameter[] parameters = myMember.getParameterList().getParameters();

      if (mySettings.isMakeClassParameter()) {
        params.add(
            new ParameterInfoImpl(
                -1,
                mySettings.getClassParameterName(),
                factory.createType(containingClass, PsiSubstitutor.EMPTY),
                "this"));
      }

      if (mySettings.isMakeFieldParameters()) {
        for (Settings.FieldParameter parameter : mySettings.getParameterOrderList()) {
          params.add(
              new ParameterInfoImpl(
                  -1,
                  mySettings.getClassParameterName(),
                  parameter.type,
                  parameter.field.getName()));
        }
      }

      for (int i = 0; i < parameters.length; i++) {
        params.add(new ParameterInfoImpl(i));
      }

      final PsiType returnType = myMember.getReturnType();
      LOG.assertTrue(returnType != null);
      JavaChangeSignatureUsageProcessor.generateDelegate(
          new JavaChangeInfoImpl(
              VisibilityUtil.getVisibilityModifier(myMember.getModifierList()),
              myMember,
              myMember.getName(),
              CanonicalTypes.createTypeWrapper(returnType),
              params.toArray(new ParameterInfoImpl[params.size()]),
              new ThrownExceptionInfo[0],
              false,
              Collections.<PsiMethod>emptySet(),
              Collections.<PsiMethod>emptySet()));
    }

    if (mySettings.isMakeClassParameter()) {
      // Add parameter for object
      PsiType parameterType = factory.createType(containingClass, PsiSubstitutor.EMPTY);
      addedTypes.add(parameterType);

      final String classParameterName = mySettings.getClassParameterName();
      PsiParameter parameter = factory.createParameter(classParameterName, parameterType);
      if (makeClassParameterFinal(usages)) {
        PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, true);
      }
      addParameterAfter = paramList.addAfter(parameter, null);
      anchor = javaDocHelper.addParameterAfter(classParameterName, anchor);
    }

    if (mySettings.isMakeFieldParameters()) {
      List<Settings.FieldParameter> parameters = mySettings.getParameterOrderList();

      for (Settings.FieldParameter fieldParameter : parameters) {
        final PsiType fieldParameterType = fieldParameter.field.getType();
        final PsiParameter parameter =
            factory.createParameter(fieldParameter.name, fieldParameterType);
        addedTypes.add(fieldParameterType);
        if (makeFieldParameterFinal(fieldParameter.field, usages)) {
          PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, true);
        }
        addParameterAfter = paramList.addAfter(parameter, addParameterAfter);
        anchor = javaDocHelper.addParameterAfter(fieldParameter.name, anchor);
      }
    }
    makeStatic(myMember);

    if (myAdditionalMethods != null) {
      for (PsiMethod method : myAdditionalMethods) {
        makeStatic(method);
      }
    }
  }