com.intellij.psi.PsiModifier Java Examples

The following examples show how to use com.intellij.psi.PsiModifier. 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: UtilityClassModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testUtilityClassModifiersMethod() {
  PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
  PsiMethod method = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiMethod.class);

  assertNotNull(method);
  assertNotNull(method.getModifierList());

  PsiElement parent = method.getParent();

  assertNotNull(parent);
  assertTrue(parent instanceof PsiClass);

  PsiClass parentClass = (PsiClass) parent;

  assertNotNull(parentClass.getModifierList());
  assertTrue("@UtilityClass should make parent class final", ((PsiClass) method.getParent()).getModifierList().hasModifierProperty(PsiModifier.FINAL));
  assertTrue("@UtilityClass should make method static", method.getModifierList().hasModifierProperty(PsiModifier.STATIC));
}
 
Example #2
Source File: UtilityClassModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testUtilityClassModifiersInnerClass() {
  PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
  PsiClass innerClass = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiClass.class);

  assertNotNull(innerClass);
  assertNotNull(innerClass.getModifierList());

  PsiElement parent = innerClass.getParent();

  assertNotNull(parent);
  assertTrue(parent instanceof PsiClass);

  PsiClass parentClass = (PsiClass) parent;

  assertNotNull(parentClass.getModifierList());
  assertTrue("@UtilityClass should make parent class final", ((PsiClass) innerClass.getParent()).getModifierList().hasModifierProperty(PsiModifier.FINAL));
  assertTrue("@UtilityClass should make inner class static", innerClass.getModifierList().hasModifierProperty(PsiModifier.STATIC));
}
 
Example #3
Source File: JavaTestContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
private static TestContext fromSelectedMethods(List<PsiMethod> selectedMethods) {
  // Sort so multiple configurations created with different selection orders are the same.
  selectedMethods.sort(Comparator.comparing(PsiMethod::getName));
  PsiMethod firstMethod = selectedMethods.get(0);
  PsiClass containingClass = firstMethod.getContainingClass();
  if (containingClass == null || containingClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
    return null;
  }
  for (PsiMethod method : selectedMethods) {
    if (!containingClass.equals(method.getContainingClass())) {
      return null;
    }
  }
  return fromClassAndMethods(containingClass, selectedMethods);
}
 
Example #4
Source File: SingularMapHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Collection<PsiField> renderBuilderFields(@NotNull BuilderInfo info) {
  final PsiType keyType = getKeyType(info.getManager(), info.getFieldType());
  final PsiType builderFieldKeyType = getBuilderFieldType(keyType, info.getProject());

  final PsiType valueType = getValueType(info.getManager(), info.getFieldType());
  final PsiType builderFieldValueType = getBuilderFieldType(valueType, info.getProject());

  return Arrays.asList(
    new LombokLightFieldBuilder(info.getManager(), info.getFieldName() + LOMBOK_KEY, builderFieldKeyType)
      .withContainingClass(info.getBuilderClass())
      .withModifier(PsiModifier.PRIVATE)
      .withNavigationElement(info.getVariable()),
    new LombokLightFieldBuilder(info.getManager(), info.getFieldName() + LOMBOK_VALUE, builderFieldValueType)
      .withContainingClass(info.getBuilderClass())
      .withModifier(PsiModifier.PRIVATE)
      .withNavigationElement(info.getVariable()));
}
 
Example #5
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 #6
Source File: UtilityClassModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testUtilityClassModifiersField() {
  PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
  PsiField field = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiField.class);

  assertNotNull(field);
  assertNotNull(field.getModifierList());

  PsiElement parent = field.getParent();

  assertNotNull(parent);
  assertTrue(parent instanceof PsiClass);

  PsiClass parentClass = (PsiClass) parent;

  assertNotNull(parentClass.getModifierList());
  assertTrue("@UtilityClass should make parent class final", parentClass.getModifierList().hasModifierProperty(PsiModifier.FINAL));
  assertTrue("@UtilityClass should make field static", field.getModifierList().hasModifierProperty(PsiModifier.STATIC));
}
 
Example #7
Source File: UtilityClassModifierProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void transformModifiers(@NotNull PsiModifierList modifierList, @NotNull final Set<String> modifiers) {
  final PsiElement parent = modifierList.getParent();

  // FINAL
  if (parent instanceof PsiClass) {
    PsiClass psiClass = (PsiClass) parent;
    if (PsiAnnotationSearchUtil.isAnnotatedWith(psiClass, UtilityClass.class)) {
      modifiers.add(PsiModifier.FINAL);
    }
  }

  // STATIC
  if (isElementFieldOrMethodOrInnerClass(parent)) {
    modifiers.add(PsiModifier.STATIC);
  }
}
 
Example #8
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 #9
Source File: ValModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void testValModifiersEditing() {
  PsiFile file = myFixture.configureByText("a.java", "import lombok.val;\nclass Foo { {val o = <caret>;} }");
  PsiLocalVariable var = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiLocalVariable.class);
  assertNotNull(var);

  PsiType type1 = var.getType();
  assertNotNull(type1);
  assertEquals("lombok.val", type1.getCanonicalText(false));

  myFixture.type('1');
  PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
  assertTrue(var.isValid());

  assertNotNull(var.getModifierList());
  assertTrue("val should make variable final", var.getModifierList().hasModifierProperty(PsiModifier.FINAL));
}
 
Example #10
Source File: InnerBuilderUtils.java    From innerbuilder with Apache License 2.0 6 votes vote down vote up
/**
 * @param file   psi file
 * @param editor editor
 * @return psiClass if class is static or top level. Otherwise returns {@code null}
 */
@Nullable
public static PsiClass getStaticOrTopLevelClass(PsiFile file, Editor editor) {
    final int offset = editor.getCaretModel().getOffset();
    final PsiElement element = file.findElementAt(offset);
    if (element == null) {
        return null;
    }

    PsiClass topLevelClass = PsiUtil.getTopLevelClass(element);
    PsiClass psiClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
    if (psiClass != null && (psiClass.hasModifierProperty(PsiModifier.STATIC) ||
            psiClass.getManager().areElementsEquivalent(psiClass, topLevelClass)))
        return psiClass;
    else
        return null;
}
 
Example #11
Source File: ComponentsMethodDeclarationHandler.java    From litho with Apache License 2.0 6 votes vote down vote up
private static PsiMethod[] findSpecMethods(PsiMethod componentMethod, Project project) {
  if (!componentMethod.getModifierList().hasModifierProperty(PsiModifier.STATIC)) {
    return PsiMethod.EMPTY_ARRAY;
  }

  final PsiClass containingCls =
      (PsiClass) PsiTreeUtil.findFirstParent(componentMethod, PsiClass.class::isInstance);
  if (containingCls == null) return PsiMethod.EMPTY_ARRAY;

  if (!LithoPluginUtils.isGeneratedClass(containingCls)) return PsiMethod.EMPTY_ARRAY;

  // For Unit testing we don't care about package
  final String containingClsName =
      ApplicationManager.getApplication().isUnitTestMode()
          ? containingCls.getName()
          : containingCls.getQualifiedName();
  final PsiClass specCls =
      PsiSearchUtils.findOriginalClass(
          project, LithoPluginUtils.getLithoComponentSpecNameFromComponent(containingClsName));
  if (specCls == null) return PsiMethod.EMPTY_ARRAY;

  return specCls.findMethodsByName(componentMethod.getName(), true);
}
 
Example #12
Source File: AbstractSingularHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Collection<PsiField> renderBuilderFields(@NotNull BuilderInfo info) {
  final PsiType builderFieldType = getBuilderFieldType(info.getFieldType(), info.getProject());
  return Collections.singleton(
    new LombokLightFieldBuilder(info.getManager(), info.getFieldName(), builderFieldType)
      .withContainingClass(info.getBuilderClass())
      .withModifier(PsiModifier.PRIVATE)
      .withNavigationElement(info.getVariable()));
}
 
Example #13
Source File: FieldDefaultsModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testFieldDefaultsPublic() {

    PsiModifierList modifierList = getFieldModifierListAtCaret();

    assertTrue("@FieldDefaults(level = AccessLevel.PUBLIC) should make non-@PackagePrivate fields public", modifierList.hasModifierProperty(PsiModifier.PUBLIC));
    assertFalse("@FieldDefaults(level = AccessLevel.PUBLIC) should not make fields package-private", modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL));
    assertFalse("@FieldDefaults(level = AccessLevel.PUBLIC) should not make fields protected", modifierList.hasModifierProperty(PsiModifier.PROTECTED));
    assertFalse("@FieldDefaults(level = AccessLevel.PUBLIC) should not make fields private", modifierList.hasModifierProperty(PsiModifier.PRIVATE));
  }
 
Example #14
Source File: PerformanceTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testFieldDefaults() {
  final String testName = getTestName(true);
  loadToPsiFile("/performance/" + testName + "/lombok.config");
  final PsiFile psiFile = loadToPsiFile("/performance/" + testName + "/HugeClass.java");
  PlatformTestUtil.startPerformanceTest(getTestName(false), 500, () -> {
    type(' ');
    PsiDocumentManager.getInstance(getProject()).commitDocument(getEditor().getDocument());
    ((PsiJavaFileImpl) psiFile).getClasses()[0].getFields()[0].hasModifierProperty(PsiModifier.FINAL);

    backspace();
    PsiDocumentManager.getInstance(getProject()).commitDocument(getEditor().getDocument());
    ((PsiJavaFileImpl) psiFile).getClasses()[0].getFields()[0].hasModifierProperty(PsiModifier.FINAL);
  }).assertTiming();
}
 
Example #15
Source File: FieldDefaultsModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testFieldDefaultsPublicWithPackagePrivate() {
  //TODO disable assertions for the moment
  RecursionManager.disableMissedCacheAssertions(myFixture.getProjectDisposable());
  PsiModifierList modifierList = getFieldModifierListAtCaret();

  assertFalse("@FieldDefaults(level = AccessLevel.PUBLIC) should not make @PackagePrivate fields public", modifierList.hasModifierProperty(PsiModifier.PUBLIC));
  assertTrue("@FieldDefaults should keep @PackagePrivate fields package-private", modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL));
}
 
Example #16
Source File: ValueModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testValueModifiersStatic() {
  PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");

  PsiField field = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiField.class);
  assertNotNull(field);
  assertNotNull(field.getModifierList());

  assertFalse("@Value should not make static variable final", field.getModifierList().hasModifierProperty(PsiModifier.FINAL));
  assertFalse("@Value should not make static variable private", field.getModifierList().hasModifierProperty(PsiModifier.PRIVATE));
}
 
Example #17
Source File: FieldDefaultsModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testFieldDefaultsProtected() {

    PsiModifierList modifierList = getFieldModifierListAtCaret();

    assertFalse("@FieldDefaults(level = AccessLevel.PROTECTED) should not make fields public", modifierList.hasModifierProperty(PsiModifier.PUBLIC));
    assertFalse("@FieldDefaults(level = AccessLevel.PROTECTED) should not make fields package-private", modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL));
    assertTrue("@FieldDefaults(level = AccessLevel.PROTECTED) should non-@PackagePrivate make fields protected", modifierList.hasModifierProperty(PsiModifier.PROTECTED));
    assertFalse("@FieldDefaults(level = AccessLevel.PROTECTED) should not make fields private", modifierList.hasModifierProperty(PsiModifier.PRIVATE));
  }
 
Example #18
Source File: VarModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testVarModifiers() {
  PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
  PsiLocalVariable var = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiLocalVariable.class);

  assertNotNull(var);
  assertNotNull(var.getModifierList());
  boolean isFinal = var.getModifierList().hasModifierProperty(PsiModifier.FINAL);
  assertFalse("var doesn't make variable final", isFinal);
}
 
Example #19
Source File: FieldDefaultsModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testFieldDefaultsPrivate() {

    PsiModifierList modifierList = getFieldModifierListAtCaret();

    assertFalse("@FieldDefaults(level = AccessLevel.PRIVATE) should not make fields public", modifierList.hasModifierProperty(PsiModifier.PUBLIC));
    assertFalse("@FieldDefaults(level = AccessLevel.PRIVATE) should not make fields protected", modifierList.hasModifierProperty(PsiModifier.PROTECTED));
    assertFalse("@FieldDefaults(level = AccessLevel.PRIVATE) should not make fields package-private", modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL));
    assertTrue("@FieldDefaults(level = AccessLevel.PRIVATE) should make non-@PackagePrivate fields private", modifierList.hasModifierProperty(PsiModifier.PRIVATE));
  }
 
Example #20
Source File: VarModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testVarModifiersEditing() {
  PsiFile file = myFixture.configureByFile(getTestName(false) + ".java");
  PsiLocalVariable var = PsiTreeUtil.getParentOfType(file.findElementAt(myFixture.getCaretOffset()), PsiLocalVariable.class);
  assertNotNull(var);
  assertNotNull(var.getType());

  myFixture.type('1');
  PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
  assertTrue(var.isValid());
  assertEquals(PsiPrimitiveType.INT, var.getType());

  assertNotNull(var.getModifierList());
  boolean isFinal = var.getModifierList().hasModifierProperty(PsiModifier.FINAL);
  assertFalse("var doesn't make variable final", isFinal);
}
 
Example #21
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 #22
Source File: FieldDefaultsModifierTest.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void testFieldDefaultsNone() {

    PsiModifierList modifierList = getFieldModifierListAtCaret();

    assertFalse("@FieldDefaults should not make fields public", modifierList.hasModifierProperty(PsiModifier.PUBLIC));
    assertFalse("@FieldDefaults should not make fields protected", modifierList.hasModifierProperty(PsiModifier.PROTECTED));
    assertFalse("@FieldDefaults should not make fields private", modifierList.hasModifierProperty(PsiModifier.PRIVATE));
    assertTrue("@FieldDefaults should keep fields as package-private", modifierList.hasModifierProperty(PsiModifier.PACKAGE_LOCAL));
  }
 
Example #23
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 #24
Source File: LombokLightModifierList.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void setModifierProperty(@PsiModifier.ModifierConstant @NotNull @NonNls String name, boolean value) throws IncorrectOperationException {
  if (value) {
    addModifier(name);
  } else {
    if (hasModifierProperty(name)) {
      removeModifier(name);
    }
  }
}
 
Example #25
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 #26
Source File: SingularGuavaTableHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Collection<PsiField> renderBuilderFields(@NotNull BuilderInfo info) {
  final PsiType builderFieldKeyType = getBuilderFieldType(info.getFieldType(), info.getProject());
  return Collections.singleton(
    new LombokLightFieldBuilder(info.getManager(), info.getFieldName(), builderFieldKeyType)
      .withContainingClass(info.getBuilderClass())
      .withModifier(PsiModifier.PRIVATE)
      .withNavigationElement(info.getVariable()));
}
 
Example #27
Source File: SingularGuavaMapHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Collection<PsiField> renderBuilderFields(@NotNull BuilderInfo info) {
  final PsiType builderFieldKeyType = getBuilderFieldType(info.getFieldType(), info.getProject());
  return Collections.singleton(
    new LombokLightFieldBuilder(info.getManager(), info.getFieldName(), builderFieldKeyType)
      .withContainingClass(info.getBuilderClass())
      .withModifier(PsiModifier.PRIVATE)
      .withNavigationElement(info.getVariable()));
}
 
Example #28
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 #29
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 #30
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;
}