Exemplo n.º 1
0
 /**
  * judge field exists
  *
  * @param part
  * @return
  */
 private boolean fieldExist(ViewPart part) {
   PsiField[] fields = mClass.getAllFields();
   for (PsiField field : fields) {
     if (field.getName().equals(part.getName())) {
       return true;
     }
   }
   return false;
 }
Exemplo n.º 2
0
  @Override
  protected void run() throws Throwable {
    int fieldCount = 0;
    PsiMethod initViewMethod = getInitView();
    StringBuilder methodBuild = new StringBuilder("private void initView() {");
    for (ViewPart viewPart : viewPartList) {
      if (!viewPart.isSelected() || fieldExist(viewPart)) {
        continue;
      }
      mClass.add(mFactory.createFieldFromText(viewPart.getDeclareString(false, false), mClass));
      if (initViewMethod != null) {
        initViewMethod
            .getBody()
            .add(mFactory.createStatementFromText(viewPart.getFindViewString(), mClass));
      } else {
        if (isViewHolder) {
          methodBuild.append(viewPart.getFindViewStringForViewHolder("convertView"));
        } else if (isAddRootView && !TextUtils.isEmpty(rootViewStr)) {
          methodBuild.append(viewPart.getFindViewStringWithRootView(rootViewStr));
        } else {
          methodBuild.append(viewPart.getFindViewString());
        }
        fieldCount++;
      }
    }
    methodBuild.append("}");
    if (fieldCount > 0) {
      mClass.add(mFactory.createMethodFromText(methodBuild.toString(), mClass));
    }
    addInitViewAfterOnCreate();

    // reformat class
    JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(mProject);
    styleManager.optimizeImports(psiFile);
    styleManager.shortenClassReferences(mClass);
    new ReformatCodeProcessor(mProject, mClass.getContainingFile(), null, false)
        .runWithoutProgress();
  }