com.intellij.psi.PsiMethod#delete ( )源码实例Demo

下面列出了com.intellij.psi.PsiMethod#delete ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: ParcelablePlease   文件: CodeGenerator.java
/**
 * Finds and removes a given method
 * @param methodName
 * @param arguments
 */
private void findAndRemoveMethod(String methodName, String... arguments) {
  // Maybe there's an easier way to do this with mClass.findMethodBySignature(), but I'm not an expert on Psi*
  PsiMethod[] methods = psiClass.findMethodsByName(methodName, false);

  for (PsiMethod method : methods) {
    PsiParameterList parameterList = method.getParameterList();

    if (parameterList.getParametersCount() == arguments.length) {
      boolean shouldDelete = true;

      PsiParameter[] parameters = parameterList.getParameters();

      for (int i = 0; i < arguments.length; i++) {
        if (!parameters[i].getType().getCanonicalText().equals(arguments[i])) {
          shouldDelete = false;
        }
      }

      if (shouldDelete) {
        method.delete();
      }
    }
  }
}
 
@Override
protected void process(List<ClassMember> classMembers) {
  for (ClassMember classMember : classMembers) {
    final PsiElementClassMember elementClassMember = (PsiElementClassMember) classMember;

    PsiField psiField = (PsiField) elementClassMember.getPsiElement();
    PsiMethod psiMethod = PropertyUtil.findPropertySetter(psiField.getContainingClass(), psiField.getName(), false, false);
    if (null != psiMethod) {
      PsiModifierList modifierList = psiField.getModifierList();
      if (null != modifierList) {
        PsiAnnotation psiAnnotation = modifierList.addAnnotation(Setter.class.getName());

        psiMethod.delete();
      }
    }
  }
}
 
@Override
  protected void process(List<ClassMember> classMembers) {
    for (ClassMember classMember : classMembers) {
      final PsiElementClassMember elementClassMember = (PsiElementClassMember) classMember;

      PsiField psiField = (PsiField) elementClassMember.getPsiElement();
      PsiMethod psiMethod = PropertyUtil.findPropertyGetter(psiField.getContainingClass(), psiField.getName(), false, false);

      if (null != psiMethod) {
        PsiModifierList modifierList = psiField.getModifierList();
        if (null != modifierList) {
          PsiAnnotation psiAnnotation = modifierList.addAnnotation(Getter.class.getName());
//          psiAnnotation.setDeclaredAttributeValue("value", )

          psiMethod.delete();
        }
      }
    }
  }
 
protected void processClass(@NotNull PsiClass psiClass) {
  final PsiElementFactory factory = JavaPsiFacade.getElementFactory(psiClass.getProject());
  final PsiClassType stringClassType = factory.createTypeByFQClassName(CommonClassNames.JAVA_LANG_STRING, psiClass.getResolveScope());

  final PsiMethod toStringMethod = findPublicNonStaticMethod(psiClass, "toString", stringClassType);
  if (null != toStringMethod) {
    toStringMethod.delete();
  }
  addAnnotation(psiClass, ToString.class);
}
 
protected void processClass(@NotNull PsiClass psiClass) {
  final PsiMethod equalsMethod = findPublicNonStaticMethod(psiClass, "equals", PsiType.BOOLEAN,
    PsiType.getJavaLangObject(psiClass.getManager(), psiClass.getResolveScope()));
  if (null != equalsMethod) {
    equalsMethod.delete();
  }

  final PsiMethod hashCodeMethod = findPublicNonStaticMethod(psiClass, "hashCode", PsiType.INT);
  if (null != hashCodeMethod) {
    hashCodeMethod.delete();
  }

  addAnnotation(psiClass, EqualsAndHashCode.class);
}