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

The following examples show how to use com.intellij.psi.PsiField#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: GetterFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@NotNull
public PsiMethod createGetterMethod(@NotNull PsiField psiField, @NotNull PsiClass psiClass, @NotNull String methodModifier) {
  final String methodName = LombokUtils.getGetterName(psiField);

  LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiField.getManager(), methodName)
    .withMethodReturnType(psiField.getType())
    .withContainingClass(psiClass)
    .withNavigationElement(psiField);
  if (StringUtil.isNotEmpty(methodModifier)) {
    methodBuilder.withModifier(methodModifier);
  }
  boolean isStatic = psiField.hasModifierProperty(PsiModifier.STATIC);
  if (isStatic) {
    methodBuilder.withModifier(PsiModifier.STATIC);
  }

  final String blockText = String.format("return %s.%s;", isStatic ? psiClass.getName() : "this", psiField.getName());
  methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(blockText, methodBuilder));

  PsiModifierList modifierList = methodBuilder.getModifierList();
  copyAnnotations(psiField, modifierList,
    LombokUtils.NON_NULL_PATTERN, LombokUtils.NULLABLE_PATTERN, LombokUtils.DEPRECATED_PATTERN);
  addOnXAnnotations(PsiAnnotationSearchUtil.findAnnotation(psiField, Getter.class), modifierList, "onMethod");
  return methodBuilder;
}
 
Example 2
Source File: SetterFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean validateFinalModifier(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiField psiField, @NotNull ProblemBuilder builder) {
  boolean result = true;
  if (psiField.hasModifierProperty(PsiModifier.FINAL) && null != LombokProcessorUtil.getMethodModifier(psiAnnotation)) {
    builder.addWarning("Not generating setter for this field: Setters cannot be generated for final fields.",
      PsiQuickFixFactory.createModifierListFix(psiField, PsiModifier.FINAL, false, false));
    result = false;
  }
  return result;
}
 
Example 3
Source File: SetterFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private PsiType getReturnType(@NotNull PsiField psiField) {
  PsiType result = PsiType.VOID;
  if (!psiField.hasModifierProperty(PsiModifier.STATIC) && AccessorsInfo.build(psiField).isChain()) {
    final PsiClass fieldClass = psiField.getContainingClass();
    if (null != fieldClass) {
      result = PsiClassUtil.getTypeWithGenerics(fieldClass);
    }
  }
  return result;
}
 
Example 4
Source File: GetterFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected boolean validate(@NotNull PsiAnnotation psiAnnotation, @NotNull PsiField psiField, @NotNull ProblemBuilder builder) {
  boolean result;

  final String methodVisibility = LombokProcessorUtil.getMethodModifier(psiAnnotation);
  result = null != methodVisibility;

  final boolean lazy = isLazyGetter(psiAnnotation);
  if (null == methodVisibility && lazy) {
    builder.addWarning("'lazy' does not work with AccessLevel.NONE.");
  }

  if (result && lazy) {
    if (!psiField.hasModifierProperty(PsiModifier.FINAL) || !psiField.hasModifierProperty(PsiModifier.PRIVATE)) {
      builder.addError("'lazy' requires the field to be private and final",
        PsiQuickFixFactory.createModifierListFix(psiField, PsiModifier.PRIVATE, true, false),
        PsiQuickFixFactory.createModifierListFix(psiField, PsiModifier.FINAL, true, false));
      result = false;
    }
    if (null == psiField.getInitializer()) {
      builder.addError("'lazy' requires field initialization.");
      result = false;
    }
  }

  if (result) {
    result = validateExistingMethods(psiField, builder);
  }

  if (result) {
    result = validateAccessorPrefix(psiField, builder);
  }

  return result;
}
 
Example 5
Source File: SynchronizedProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
@Override
public Collection<LombokProblem> verifyAnnotation(@NotNull PsiAnnotation psiAnnotation) {
  final ProblemNewBuilder problemNewBuilder = new ProblemNewBuilder(2);

  PsiMethod psiMethod = PsiTreeUtil.getParentOfType(psiAnnotation, PsiMethod.class);
  if (null != psiMethod) {
    if (psiMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
      problemNewBuilder.addError("'@Synchronized' is legal only on concrete methods.",
        PsiQuickFixFactory.createModifierListFix(psiMethod, PsiModifier.ABSTRACT, false, false)
      );
    }

    final String lockFieldName = PsiAnnotationUtil.getStringAnnotationValue(psiAnnotation, "value");
    if (StringUtil.isNotEmpty(lockFieldName)) {
      final PsiClass containingClass = psiMethod.getContainingClass();

      if (null != containingClass) {
        final PsiField lockField = containingClass.findFieldByName(lockFieldName, true);
        if (null != lockField) {
          if (!lockField.hasModifierProperty(PsiModifier.FINAL)) {
            problemNewBuilder.addWarning(String.format("Synchronization on a non-final field %s.", lockFieldName),
              PsiQuickFixFactory.createModifierListFix(lockField, PsiModifier.FINAL, true, false));
          }
        } else {
          final PsiClassType javaLangObjectType = PsiType.getJavaLangObject(containingClass.getManager(), containingClass.getResolveScope());

          problemNewBuilder.addError(String.format("The field %s does not exist.", lockFieldName),
            PsiQuickFixFactory.createNewFieldFix(containingClass, lockFieldName, javaLangObjectType, "new Object()", PsiModifier.PRIVATE, PsiModifier.FINAL));
        }
      }
    }
  } else {
    problemNewBuilder.addError("'@Synchronized' is legal only on methods.");
  }

  return problemNewBuilder.getProblems();
}
 
Example 6
Source File: LombokLoggerHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private boolean isValidLoggerField(@NotNull PsiField psiField, @NotNull String lombokLoggerName, boolean lombokLoggerIsStatic) {
  boolean isPrivate = psiField.hasModifierProperty(PsiModifier.PRIVATE);
  boolean isStatic = lombokLoggerIsStatic == psiField.hasModifierProperty(PsiModifier.STATIC);
  boolean isFinal = psiField.hasModifierProperty(PsiModifier.FINAL);
  boolean isProperlyNamed = lombokLoggerName.equals(psiField.getName());

  return isPrivate && isStatic && isFinal && isProperlyNamed;
}
 
Example 7
Source File: GenerateDialog.java    From android-parcelable-intellij-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Exclude static fields.
 */
private List<PsiField> getClassFields(PsiField[] allFields) {
    final List<PsiField> fields = new ArrayList<PsiField>();
    for (PsiField field : allFields) {
        if (!field.hasModifierProperty(PsiModifier.STATIC) && !field.hasModifierProperty(PsiModifier.TRANSIENT)) {
            fields.add(field);
        }
    }
    return fields;
}
 
Example 8
Source File: LCOMConverter.java    From intellij-reference-diagram with Apache License 2.0 4 votes vote down vote up
public LCOMNode.Type resolveType(DiagramNode<PsiElement> referenceNode) {
    PsiElementDispatcher<LCOMNode.Type> elementDispatcher = new PsiElementDispatcher<LCOMNode.Type>() {

        @Override
        public LCOMNode.Type processClass(PsiClass psiClass) {
            return LCOMNode.Type.Class;
        }

        @Override
        public LCOMNode.Type processMethod(PsiMethod psiMethod) {
            if (psiMethod.isConstructor()) {
                return LCOMNode.Type.Constructur;
            } else {
                return LCOMNode.Type.Method;
            }
        }

        @Override
        public LCOMNode.Type processField(PsiField psiField) {
            if (psiField.hasModifierProperty("static")) {
                return LCOMNode.Type.Constant;
            } else {
                return LCOMNode.Type.Field;
            }
        }

        @Override
        public LCOMNode.Type processClassInitializer(PsiClassInitializer psiClassInitializer) {
            return LCOMNode.Type.ClassInitializer;
        }

        @Override
        public LCOMNode.Type processInnerClass(PsiClass innerClass) {
            return LCOMNode.Type.InnerClass;
        }

        @Override
        public LCOMNode.Type processStaticInnerClass(PsiClass staticInnerClass) {
            return LCOMNode.Type.StaticInnerClass;
        }

        @Override
        public LCOMNode.Type processEnum(PsiClass anEnum) {
            return LCOMNode.Type.Enum;
        }

        @Override
        public LCOMNode.Type processPackage(PsiJavaDirectoryImpl aPackage) {
            return LCOMNode.Type.Package;
        }

        @Override
        public LCOMNode.Type processFile(PsiJavaFile psiElement) {
            return LCOMNode.Type.File;
        }
    };
    return elementDispatcher.dispatch(referenceNode.getIdentifyingElement());
}
 
Example 9
Source File: SetterFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@NotNull
public PsiMethod createSetterMethod(@NotNull PsiField psiField, @NotNull PsiClass psiClass, @NotNull String methodModifier) {
  final String fieldName = psiField.getName();
  final PsiType psiFieldType = psiField.getType();
  final PsiAnnotation setterAnnotation = PsiAnnotationSearchUtil.findAnnotation(psiField, Setter.class);

  final String methodName = getSetterName(psiField, PsiType.BOOLEAN.equals(psiFieldType));

  PsiType returnType = getReturnType(psiField);
  LombokLightMethodBuilder methodBuilder = new LombokLightMethodBuilder(psiField.getManager(), methodName)
    .withMethodReturnType(returnType)
    .withContainingClass(psiClass)
    .withParameter(fieldName, psiFieldType)
    .withNavigationElement(psiField);
  if (StringUtil.isNotEmpty(methodModifier)) {
    methodBuilder.withModifier(methodModifier);
  }
  boolean isStatic = psiField.hasModifierProperty(PsiModifier.STATIC);
  if (isStatic) {
    methodBuilder.withModifier(PsiModifier.STATIC);
  }

  PsiParameter methodParameter = methodBuilder.getParameterList().getParameters()[0];
  PsiModifierList methodParameterModifierList = methodParameter.getModifierList();
  if (null != methodParameterModifierList) {
    final Collection<String> annotationsToCopy = PsiAnnotationUtil.collectAnnotationsToCopy(psiField,
      LombokUtils.NON_NULL_PATTERN, LombokUtils.NULLABLE_PATTERN);
    for (String annotationFQN : annotationsToCopy) {
      methodParameterModifierList.addAnnotation(annotationFQN);
    }
    addOnXAnnotations(setterAnnotation, methodParameterModifierList, "onParam");
  }

  final String codeBlockText = createCodeBlockText(psiField, psiClass, returnType, isStatic, methodParameter);
  methodBuilder.withBody(PsiMethodUtil.createCodeBlockFromText(codeBlockText, methodBuilder));

  PsiModifierList methodModifierList = methodBuilder.getModifierList();
  copyAnnotations(psiField, methodModifierList, LombokUtils.DEPRECATED_PATTERN);
  addOnXAnnotations(setterAnnotation, methodModifierList, "onMethod");

  return methodBuilder;
}
 
Example 10
Source File: InnerBuilderCollector.java    From innerbuilder with Apache License 2.0 4 votes vote down vote up
private static List<PsiFieldMember> collectFieldsInClass(final PsiElement element, final PsiClass accessObjectClass,
        final PsiClass clazz) {
    final List<PsiFieldMember> classFieldMembers = new ArrayList<PsiFieldMember>();
    final PsiResolveHelper helper = JavaPsiFacade.getInstance(clazz.getProject()).getResolveHelper();

    for (final PsiField field : clazz.getFields()) {

        // check access to the field from the builder container class (eg. private superclass fields)
        if ((helper.isAccessible(field, clazz, accessObjectClass) || hasSetter(clazz, field.getName()))
                && !PsiTreeUtil.isAncestor(field, element, false)) {

            // skip static fields
            if (field.hasModifierProperty(PsiModifier.STATIC)) {
                continue;
            }

            // skip any uppercase fields
            if (!hasLowerCaseChar(field.getName())) {
                continue;
            }

            // skip eventual logging fields
            final String fieldType = field.getType().getCanonicalText();
            if ("org.apache.log4j.Logger".equals(fieldType) || "org.apache.logging.log4j.Logger".equals(fieldType)
                    || "java.util.logging.Logger".equals(fieldType) || "org.slf4j.Logger".equals(fieldType)
                    || "ch.qos.logback.classic.Logger".equals(fieldType)
                    || "net.sf.microlog.core.Logger".equals(fieldType)
                    || "org.apache.commons.logging.Log".equals(fieldType)
                    || "org.pmw.tinylog.Logger".equals(fieldType) || "org.jboss.logging.Logger".equals(fieldType)
                    || "jodd.log.Logger".equals(fieldType)) {
                continue;
            }

            if (field.hasModifierProperty(PsiModifier.FINAL)) {
                if (field.getInitializer() != null) {
                    continue; // skip final fields that are assigned in the declaration
                }

                if (!accessObjectClass.isEquivalentTo(clazz)) {
                    continue; // skip final superclass fields
                }
            }

            final PsiClass containingClass = field.getContainingClass();
            if (containingClass != null) {
                classFieldMembers.add(buildFieldMember(field, containingClass, clazz));
            }
        }
    }

    return classFieldMembers;
}