Java Code Examples for com.intellij.psi.PsiModifierList#hasModifierProperty()

The following examples show how to use com.intellij.psi.PsiModifierList#hasModifierProperty() . 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: InjectedFieldInJobNotTransientDetector.java    From cathode with Apache License 2.0 6 votes vote down vote up
@Override public boolean visitField(UField field) {
  PsiModifierList modifierList = field.getModifierList();
  if (modifierList == null || modifierList.hasModifierProperty(PsiModifier.TRANSIENT)) {
    return false;
  }

  if (!isInJob(field)) {
    return false;
  }

  if (hasAnnotation(modifierList, JAVAX_INJECT)) {
    context.report(ISSUE, context.getLocation(field), LINT_ERROR_MESSAGE);
  }

  return super.visitField(field);
}
 
Example 2
Source File: DialogExtendLintDetector.java    From SimpleDialogFragments with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClass(JavaContext context, UClass declaration) {
    PsiModifierList classModifiers = declaration.getModifierList();
    if (classModifiers == null || !classModifiers.hasModifierProperty("abstract")) {
        // check for static build method
        boolean hasBuildMethod = false;
        for (PsiMethod method : declaration.getMethods()) {
            if ("build".equals(method.getName()) && method.getModifierList()
                    .hasModifierProperty("static")) {
                hasBuildMethod = true;
                break;
            }
        }
        if (!hasBuildMethod){
            context.report(BUILD_OVERWRITE, context.getLocation(declaration.getExtendsList()),
                    BUILD_OVERWRITE_MESSAGE);
        }

        // check for public static String TAG
        boolean hasTag = false;
        for (UField field : declaration.getFields()) {
            PsiModifierList modifiers = field.getModifierList();
            if ("TAG".equals(field.getName()) && LintUtils.isString(field.getType()) &&
                    modifiers != null && modifiers.hasModifierProperty("public") &&
                    modifiers.hasModifierProperty("static")) {
                hasTag = true;
                break;
            }
        }
        if (!hasTag) {
            context.report(TAG, context.getLocation(declaration.getExtendsList()), TAG_MESSAGE);
        }

    }
}
 
Example 3
Source File: DialogExtendLintDetector.java    From SimpleDialogFragments with Apache License 2.0 5 votes vote down vote up
@Override
public void visitClass(JavaContext context, UClass declaration) {
    PsiModifierList classModifiers = declaration.getModifierList();
    if (classModifiers == null || !classModifiers.hasModifierProperty("abstract")) {
        // check for static build method
        boolean hasBuildMethod = false;
        for (PsiMethod method : declaration.getMethods()) {
            if ("build".equals(method.getName()) && method.getModifierList()
                    .hasModifierProperty("static")) {
                hasBuildMethod = true;
                break;
            }
        }
        if (!hasBuildMethod){
            context.report(BUILD_OVERWRITE, context.getLocation(declaration.getExtendsList()),
                    BUILD_OVERWRITE_MESSAGE);
        }

        // check for public static String TAG
        boolean hasTag = false;
        for (UField field : declaration.getFields()) {
            PsiModifierList modifiers = field.getModifierList();
            if ("TAG".equals(field.getName()) && LintUtils.isString(field.getType()) &&
                    modifiers != null && modifiers.hasModifierProperty("public") &&
                    modifiers.hasModifierProperty("static")) {
                hasTag = true;
                break;
            }
        }
        if (!hasTag) {
            context.report(TAG, context.getLocation(declaration.getExtendsList()), TAG_MESSAGE);
        }

    }
}
 
Example 4
Source File: PolygeneServiceAnnotationUtil.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
/**
 * Validates whether the variable has {@code @Service} annotation declared correctly.
 *
 * @param variable variable to check.
 * @return Look at {@link ServiceAnnotationDeclarationValidationResult}.
 * @since 0.1
 */
@NotNull
public static ServiceAnnotationDeclarationValidationResult isValidServiceAnnotationDeclaration(
    @NotNull PsiVariable variable )
{
    PsiAnnotation serviceAnnotation = getServiceAnnotation( variable );
    if( serviceAnnotation == null )
    {
        return invalidServiceAnnotationNotDeclared;
    }

    PsiModifierList modifierList = variable.getModifierList();
    if( modifierList != null )
    {
        if( modifierList.hasModifierProperty( STATIC ) )
        {
            return invalidDeclaredOnStaticVariable;
        }
    }

    // Can't be type that is injected by @Structure
    if( isInjecteableByStructureAnnotation( variable ) )
    {
        return invalidTypeIsInjectedViaStructureAnnotation;
    }

    return valid;
}
 
Example 5
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 6
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 7
Source File: UtilityClassProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static boolean isStatic(PsiModifierList modifierList) {
  return modifierList != null && modifierList.hasModifierProperty(PsiModifier.STATIC);
}
 
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);
}