Java Code Examples for com.intellij.psi.util.PsiUtil#setModifierProperty()

The following examples show how to use com.intellij.psi.util.PsiUtil#setModifierProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReplaceConditionalWithPolymorphism.java    From IntelliJDeodorant with MIT License 6 votes vote down vote up
private void setPublicModifierToSourceTypeDeclaration() {
    InheritanceTree tree = null;
    if (typeCheckElimination.getExistingInheritanceTree() != null) {
        tree = typeCheckElimination.getExistingInheritanceTree();
    } else if (typeCheckElimination.getInheritanceTreeMatchingWithStaticTypes() != null) {
        tree = typeCheckElimination.getInheritanceTreeMatchingWithStaticTypes();
    }

    String abstractClassName = null;
    if (tree != null) {
        DefaultMutableTreeNode root = tree.getRootNode();
        abstractClassName = (String) root.getUserObject();
    }
    String sourcePackageName = PsiUtil.getPackageName(sourceTypeDeclaration);
    if (sourcePackageName != null && abstractClassName != null && abstractClassName.contains(".")) {
        String targetPackageName = abstractClassName.substring(0, abstractClassName.lastIndexOf("."));
        if (!sourcePackageName.equals(targetPackageName)) {
            PsiUtil.setModifierProperty(sourceTypeDeclaration, PsiModifier.PUBLIC, true);
        }
    }
}
 
Example 2
Source File: InnerBuilderGenerator.java    From innerbuilder with Apache License 2.0 6 votes vote down vote up
private PsiMethod generateCopyConstructor(final PsiClass targetClass, final PsiType builderType,
                                          final Collection<PsiFieldMember> nonFinalFields,
                                          final Set<InnerBuilderOption> options) {

    final PsiMethod copyConstructor = psiElementFactory.createConstructor(builderType.getPresentableText());
    PsiUtil.setModifierProperty(copyConstructor, PsiModifier.PUBLIC, true);

    final PsiType targetClassType = psiElementFactory.createType(targetClass);
    final PsiParameter constructorParameter = psiElementFactory.createParameter("copy", targetClassType);
    final PsiModifierList parameterModifierList = constructorParameter.getModifierList();

    if (parameterModifierList != null) {
        if (options.contains(InnerBuilderOption.JSR305_ANNOTATIONS))
            parameterModifierList.addAnnotation(JSR305_NONNULL);
        if (options.contains(InnerBuilderOption.FINDBUGS_ANNOTATION))
            parameterModifierList.addAnnotation(FINDBUGS_NONNULL);
    }
    copyConstructor.getParameterList().add(constructorParameter);
    addCopyBody(nonFinalFields, copyConstructor, "this.");
    return copyConstructor;
}
 
Example 3
Source File: PushDownProcessor.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
private void removeFromTargetClass() throws IncorrectOperationException {
  for (MemberInfo memberInfo : myMemberInfos) {
    final PsiElement member = memberInfo.getMember();

    if (member instanceof PsiField) {
      member.delete();
    }
    else if (member instanceof PsiMethod) {
      if (memberInfo.isToAbstract()) {
        final PsiMethod method = (PsiMethod)member;
        if (method.hasModifierProperty(PsiModifier.PRIVATE)) {
          PsiUtil.setModifierProperty(method, PsiModifier.PROTECTED, true);
        }
        RefactoringUtil.makeMethodAbstract(myClass, method);
        myJavaDocPolicy.processOldJavaDoc(method.getDocComment());
      }
      else {
        member.delete();
      }
    }
    else if (member instanceof PsiClass) {
      //if (Boolean.FALSE.equals(memberInfo.getOverrides())) {
        //RefactoringUtil.removeFromReferenceList(myClass.getImplementsList(), (PsiClass)member);
      //}
      //else {
      //}

      member.delete();
    }
  }
}
 
Example 4
Source File: HaxePullUpHelper.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
public void setCorrectVisibility(MemberInfo info) {
  PsiModifierListOwner modifierListOwner = info.getMember();
  if (myIsTargetInterface) {
    PsiUtil.setModifierProperty(modifierListOwner, PsiModifier.PUBLIC, true);
  }
  else if (modifierListOwner.hasModifierProperty(PsiModifier.PRIVATE)) {
    if (info.isToAbstract() || willBeUsedInSubclass(modifierListOwner, myTargetSuperClass, mySourceClass)) {
      PsiUtil.setModifierProperty(modifierListOwner, PsiModifier.PROTECTED, true);
    }
    if (modifierListOwner instanceof PsiClass) {
      modifierListOwner.accept(new JavaRecursiveElementWalkingVisitor() {
        @Override
        public void visitMethod(PsiMethod method) {
          check(method);
        }

        @Override
        public void visitField(PsiField field) {
          check(field);
        }

        @Override
        public void visitClass(PsiClass aClass) {
          check(aClass);
          super.visitClass(aClass);
        }

        private void check(PsiMember member) {
          if (member.hasModifierProperty(PsiModifier.PRIVATE)) {
            if (willBeUsedInSubclass(member, myTargetSuperClass, mySourceClass)) {
              PsiUtil.setModifierProperty(member, PsiModifier.PROTECTED, true);
            }
          }
        }
      });
    }
  }
}
 
Example 5
Source File: HaxePullUpHelper.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
private void doMoveField(PsiSubstitutor substitutor, MemberInfo info) {
  PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(myProject);
  PsiField field = (PsiField)info.getMember();

  field.normalizeDeclaration();
  RefactoringUtil.replaceMovedMemberTypeParameters(field, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
  fixReferencesToStatic(field);
  if (myIsTargetInterface) {
    PsiUtil.setModifierProperty(field, PsiModifier.PUBLIC, true);
  }
  final PsiMember movedElement = (PsiMember)myTargetSuperClass.addBefore(convertFieldToLanguage(field, myTargetSuperClass.getLanguage()), myTargetSuperClass.getRBrace());
  myMembersAfterMove.add(movedElement);
  field.delete();
}
 
Example 6
Source File: InnerBuilderGenerator.java    From innerbuilder with Apache License 2.0 5 votes vote down vote up
private PsiMethod generateBuilderConstructor(final PsiClass builderClass,
                                             final Collection<PsiFieldMember> finalFields,
                                             final Set<InnerBuilderOption> options) {

    final PsiMethod builderConstructor = psiElementFactory.createConstructor(builderClass.getName());
    if (options.contains(InnerBuilderOption.NEW_BUILDER_METHOD)) {
        PsiUtil.setModifierProperty(builderConstructor, PsiModifier.PRIVATE, true);
    } else {
        PsiUtil.setModifierProperty(builderConstructor, PsiModifier.PUBLIC, true);
    }
    final PsiCodeBlock builderConstructorBody = builderConstructor.getBody();
    if (builderConstructorBody != null) {
        for (final PsiFieldMember member : finalFields) {
            final PsiField field = member.getElement();
            final PsiType fieldType = field.getType();
            final String fieldName = field.getName();

            final PsiParameter parameter = psiElementFactory.createParameter(fieldName, fieldType);
            final PsiModifierList parameterModifierList = parameter.getModifierList();
            final boolean useJsr305 = options.contains(InnerBuilderOption.JSR305_ANNOTATIONS);
            final boolean useFindbugs = options.contains(InnerBuilderOption.FINDBUGS_ANNOTATION);

            if (!InnerBuilderUtils.isPrimitive(field) && parameterModifierList != null) {
                if (useJsr305) parameterModifierList.addAnnotation(JSR305_NONNULL);
                if (useFindbugs) parameterModifierList.addAnnotation(FINDBUGS_NONNULL);
            }

            builderConstructor.getParameterList().add(parameter);
            final PsiStatement assignStatement = psiElementFactory.createStatementFromText(String.format(
                    "this.%1$s = %1$s;", fieldName), builderConstructor);
            builderConstructorBody.add(assignStatement);
        }
    }

    return builderConstructor;
}
 
Example 7
Source File: InnerBuilderGenerator.java    From innerbuilder with Apache License 2.0 5 votes vote down vote up
private PsiMethod generateNewBuilderMethod(final PsiType builderType, final Collection<PsiFieldMember> finalFields,
                                           final Set<InnerBuilderOption> options) {
    final PsiMethod newBuilderMethod = psiElementFactory.createMethod("newBuilder", builderType);
    PsiUtil.setModifierProperty(newBuilderMethod, PsiModifier.STATIC, true);
    PsiUtil.setModifierProperty(newBuilderMethod, PsiModifier.PUBLIC, true);

    final StringBuilder fieldList = new StringBuilder();
    if (!finalFields.isEmpty()) {
        for (final PsiFieldMember member : finalFields) {
            final PsiField field = member.getElement();
            final PsiType fieldType = field.getType();
            final String fieldName = field.getName();

            final PsiParameter parameter = psiElementFactory.createParameter(fieldName, fieldType);
            final PsiModifierList parameterModifierList = parameter.getModifierList();
            if (parameterModifierList != null) {

                if (!InnerBuilderUtils.isPrimitive(field)) {
                    if (options.contains(InnerBuilderOption.JSR305_ANNOTATIONS))
                        parameterModifierList.addAnnotation(JSR305_NONNULL);
                    if (options.contains(InnerBuilderOption.FINDBUGS_ANNOTATION))
                        parameterModifierList.addAnnotation(FINDBUGS_NONNULL);
                }
            }
            newBuilderMethod.getParameterList().add(parameter);
            if (fieldList.length() > 0) {
                fieldList.append(", ");
            }
            fieldList.append(fieldName);
        }
    }
    final PsiCodeBlock newBuilderMethodBody = newBuilderMethod.getBody();
    if (newBuilderMethodBody != null) {
        final PsiStatement newStatement = psiElementFactory.createStatementFromText(String.format(
                        "return new %s(%s);", builderType.getPresentableText(), fieldList.toString()),
                newBuilderMethod);
        newBuilderMethodBody.add(newStatement);
    }
    return newBuilderMethod;
}
 
Example 8
Source File: InnerBuilderGenerator.java    From innerbuilder with Apache License 2.0 5 votes vote down vote up
@NotNull
private PsiClass createBuilderClass(final PsiClass targetClass) {
    final PsiClass builderClass = (PsiClass) targetClass.add(psiElementFactory.createClass(BUILDER_CLASS_NAME));
    PsiUtil.setModifierProperty(builderClass, PsiModifier.STATIC, true);
    PsiUtil.setModifierProperty(builderClass, PsiModifier.FINAL, true);
    setBuilderComment(builderClass, targetClass);
    setBuilderAnnotation(builderClass);
    return builderClass;
}
 
Example 9
Source File: PolymorphismRefactoring.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
private void setPublicModifierToSourceField(PsiField variableBinding) {
    PsiUtil.setModifierProperty(variableBinding, PsiModifier.PUBLIC, true);
}
 
Example 10
Source File: PolymorphismRefactoring.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
protected void setPublicModifierToAccessedMethods() {
    for (PsiMethod methodDeclaration : typeCheckElimination.getAccessedMethods()) {
        PsiUtil.setModifierProperty(methodDeclaration, PsiModifier.PUBLIC, true);
    }
}
 
Example 11
Source File: ReplaceConditionalWithPolymorphism.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
private PsiMethod createPolymorphicMethodHeader() {
    String methodName = typeCheckElimination.getAbstractMethodName();
    PsiType returnType = PsiType.VOID;

    if (returnedVariable != null) {
        returnType = returnedVariable.getType();
    } else if (typeCheckElimination.typeCheckCodeFragmentContainsReturnStatement()) { // TODO: looks really suspicious
        returnType = typeCheckElimination.getTypeCheckMethodReturnType();
    }

    PsiMethod createdMethod = elementFactory.createMethod(methodName, returnType);
    PsiUtil.setModifierProperty(createdMethod, PsiModifier.PUBLIC, true);

    PsiParameterList abstractMethodParameters = createdMethod.getParameterList();
    if (returnedVariable != null && !typeCheckElimination.returnedVariableDeclaredAndReturnedInBranches()) {
        abstractMethodParameters.add(elementFactory.createParameter(returnedVariable.getName(), returnedVariable.getType()));
    }
    for (PsiParameter accessedParameter : typeCheckElimination.getAccessedParameters()) {
        if (!accessedParameter.equals(returnedVariable) && !accessedParameter.equals(typeVariable)) {
            abstractMethodParameters.add(elementFactory.createParameter(accessedParameter.getName(), accessedParameter.getType()));
        }
    }
    for (PsiVariable fragment : typeCheckElimination.getAccessedLocalVariables()) {
        if (!fragment.equals(returnedVariable) && !fragment.equals(typeVariable)) {
            abstractMethodParameters.add(elementFactory.createParameter(fragment.getName(), fragment.getType()));
        }
    }

    if (sourceTypeRequiredForExtraction()) {
        String parameterName = sourceTypeDeclaration.getName();
        parameterName = parameterName.substring(0, 1).toLowerCase() + parameterName.substring(1);
        PsiType parameterType = PsiTypesUtil.getClassType(sourceTypeDeclaration);
        abstractMethodParameters.add(elementFactory.createParameter(parameterName, parameterType));
    }

    PsiReferenceList abstractMethodThrownExceptionsRewrite = createdMethod.getThrowsList();
    for (PsiClassType typeBinding : thrownExceptions) {
        abstractMethodThrownExceptionsRewrite.add(elementFactory.createReferenceElementByType(typeBinding));
    }
    return createdMethod;
}
 
Example 12
Source File: InnerBuilderGenerator.java    From innerbuilder with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    final PsiClass targetClass = InnerBuilderUtils.getStaticOrTopLevelClass(file, editor);
    if (targetClass == null) {
        return;
    }
    final Set<InnerBuilderOption> options = currentOptions();
    final PsiClass builderClass = findOrCreateBuilderClass(targetClass);
    final PsiType builderType = psiElementFactory.createTypeFromText(BUILDER_CLASS_NAME, null);
    final PsiMethod constructor = generateConstructor(targetClass, builderType);

    addMethod(targetClass, null, constructor, true);
    final Collection<PsiFieldMember> finalFields = new ArrayList<PsiFieldMember>();
    final Collection<PsiFieldMember> nonFinalFields = new ArrayList<PsiFieldMember>();

    PsiElement lastAddedField = null;
    for (final PsiFieldMember fieldMember : selectedFields) {
        lastAddedField = findOrCreateField(builderClass, fieldMember, lastAddedField);
        if (fieldMember.getElement().hasModifierProperty(PsiModifier.FINAL)
                && !options.contains(InnerBuilderOption.FINAL_SETTERS)) {
            finalFields.add(fieldMember);
            PsiUtil.setModifierProperty((PsiField) lastAddedField, PsiModifier.FINAL, true);
        } else {
            nonFinalFields.add(fieldMember);
        }
    }
    if (options.contains(InnerBuilderOption.NEW_BUILDER_METHOD)) {
        final PsiMethod newBuilderMethod = generateNewBuilderMethod(builderType, finalFields, options);
        addMethod(targetClass, null, newBuilderMethod, false);
    }

    // builder constructor, accepting the final fields
    final PsiMethod builderConstructorMethod = generateBuilderConstructor(builderClass, finalFields, options);
    addMethod(builderClass, null, builderConstructorMethod, false);

    // builder copy constructor or static copy method
    if (options.contains(InnerBuilderOption.COPY_CONSTRUCTOR)) {
        if (options.contains(InnerBuilderOption.NEW_BUILDER_METHOD)) {
            final PsiMethod copyBuilderMethod = generateCopyBuilderMethod(targetClass, builderType,
                    nonFinalFields, options);
            addMethod(targetClass, null, copyBuilderMethod, true);
        } else {
            final PsiMethod copyConstructorBuilderMethod = generateCopyConstructor(targetClass, builderType,
                    selectedFields, options);
            addMethod(builderClass, null, copyConstructorBuilderMethod, true);
        }
    }

    // builder methods
    PsiElement lastAddedElement = null;
    for (final PsiFieldMember member : nonFinalFields) {
        final PsiMethod setterMethod = generateBuilderSetter(builderType, member, options);
        lastAddedElement = addMethod(builderClass, lastAddedElement, setterMethod, false);
    }

    // builder.build() method
    final PsiMethod buildMethod = generateBuildMethod(targetClass, options);
    addMethod(builderClass, lastAddedElement, buildMethod, false);

    JavaCodeStyleManager.getInstance(project).shortenClassReferences(file);
    CodeStyleManager.getInstance(project).reformat(builderClass);
}