Java Code Examples for com.intellij.psi.PsiField#getModifierList()

The following examples show how to use com.intellij.psi.PsiField#getModifierList() . 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: RefactorSetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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();
      }
    }
  }
}
 
Example 2
Source File: RefactorGetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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();
        }
      }
    }
  }
 
Example 3
Source File: ThinrDetector.java    From thinr with Apache License 2.0 5 votes vote down vote up
private void markLeakSuspects(PsiElement element, PsiElement lambdaBody, @NonNull final JavaContext context) {
    if (element instanceof PsiReferenceExpression) {
        PsiReferenceExpression ref = (PsiReferenceExpression) element;

        if (ref.getQualifierExpression() == null) {

            PsiElement res = ref.resolve();
            if (!(res instanceof PsiParameter)) {
                if (!(res instanceof PsiClass)) {

                    boolean error = false;
                    if (res instanceof PsiLocalVariable) {
                        PsiLocalVariable lVar = (PsiLocalVariable) res;
                        if (!isParent(lambdaBody, lVar.getParent())) {
                            error = true;
                        }
                    }

                    if (res instanceof PsiField) {
                        PsiField field = (PsiField) res;
                        final PsiModifierList modifierList = field.getModifierList();
                        if (modifierList == null) {
                            error = true;
                        } else if (!modifierList.hasExplicitModifier(PsiModifier.STATIC)) {
                            error = true;
                        }
                    }

                    if (error) {
                        context.report(ISSUE, element, context.getNameLocation(element), "Possible leak");
                    }
                }
            }
        }
    }

    for (PsiElement psiElement : element.getChildren()) {
        markLeakSuspects(psiElement, lambdaBody, context);
    }
}
 
Example 4
Source File: AbstractInjectionAnnotationDeclarationOnFieldInspection.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public final ProblemDescriptor[] checkField( @NotNull PsiField field,
                                             @NotNull InspectionManager manager,
                                             boolean isOnTheFly )
{
    PsiAnnotation annotationToCheck = getAnnotationToCheck( field );
    if( annotationToCheck == null )
    {
        return null;
    }

    PsiModifierList modifierList = field.getModifierList();
    if( modifierList != null )
    {
        if( modifierList.hasModifierProperty( com.intellij.psi.PsiModifier.STATIC ) )
        {
            String message = getInjectionAnnotationValidDeclarationMessage();
            AbstractFix removeAnnotationFix = createRemoveAnnotationFix( annotationToCheck );
            ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(
                annotationToCheck, message, removeAnnotationFix, com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR_OR_WARNING
            );

            return new ProblemDescriptor[]{ problemDescriptor };
        }
    }

    return verifyAnnotationDeclaredCorrectly( field, annotationToCheck, manager );
}
 
Example 5
Source File: AbstractFieldNameConstantsProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
Collection<PsiField> filterFields(@NotNull PsiClass psiClass, PsiAnnotation psiAnnotation) {
  final Collection<PsiField> psiFields = new ArrayList<>();

  final boolean onlyExplicitlyIncluded = PsiAnnotationUtil.getBooleanAnnotationValue(psiAnnotation, "onlyExplicitlyIncluded", false);

  for (PsiField psiField : PsiClassUtil.collectClassFieldsIntern(psiClass)) {
    boolean useField = true;
    PsiModifierList modifierList = psiField.getModifierList();
    if (null != modifierList) {

      //Skip static fields.
      useField = !modifierList.hasModifierProperty(PsiModifier.STATIC);
      //Skip transient fields
      useField &= !modifierList.hasModifierProperty(PsiModifier.TRANSIENT);
    }
    //Skip fields that start with $
    useField &= !psiField.getName().startsWith(LombokUtils.LOMBOK_INTERN_FIELD_MARKER);
    //Skip fields annotated with @FieldNameConstants.Exclude
    useField &= !PsiAnnotationSearchUtil.isAnnotatedWith(psiField, FIELD_NAME_CONSTANTS_EXCLUDE);

    if (onlyExplicitlyIncluded) {
      //Only use fields annotated with @FieldNameConstants.Include, Include annotation overrides other rules
      useField = PsiAnnotationSearchUtil.isAnnotatedWith(psiField, FIELD_NAME_CONSTANTS_INCLUDE);
    }

    if (useField) {
      psiFields.add(psiField);
    }
  }
  return psiFields;
}
 
Example 6
Source File: FieldDefaultsModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
private PsiModifierList getFieldModifierListAtCaret() {
  PsiFile file = loadToPsiFile(getTestName(false) + ".java");
  PsiField field = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiField.class);

  assertNotNull(field);

  PsiModifierList modifierList = field.getModifierList();
  assertNotNull(modifierList);

  return modifierList;
}
 
Example 7
Source File: PsiModifierExtractor.java    From litho with Apache License 2.0 4 votes vote down vote up
static Modifier[] extractModifiers(PsiField psiField) {
  PsiModifierList modifierList = psiField.getModifierList();
  return modifierList == null ? EMPTY : extractModifiersInternal(modifierList).toArray(EMPTY);
}
 
Example 8
Source File: QuarkusConfigRootProvider.java    From intellij-quarkus with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Returns true if the given field can generate a Quarkus property and false
 * otherwise.
 *
 * @param field
 * @return true if the given field can generate a Quarkus property and false
 *         otherwise.
 */
private static boolean canProcess(PsiField field) {
	PsiModifierList flags = field.getModifierList();
	return !flags.hasModifierProperty(PsiModifier.STATIC) && !flags.hasModifierProperty(PsiModifier.PRIVATE);
}