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

The following examples show how to use com.intellij.psi.PsiField#getContainingClass() . 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: SetterFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean validateExistingMethods(@NotNull PsiField psiField, @NotNull ProblemBuilder builder) {
  boolean result = true;
  final PsiClass psiClass = psiField.getContainingClass();
  if (null != psiClass) {
    final Collection<PsiMethod> classMethods = PsiClassUtil.collectClassMethodsIntern(psiClass);
    filterToleratedElements(classMethods);

    final boolean isBoolean = PsiType.BOOLEAN.equals(psiField.getType());
    final Collection<String> methodNames = getAllSetterNames(psiField, isBoolean);

    for (String methodName : methodNames) {
      if (PsiMethodUtil.hasSimilarMethod(classMethods, methodName, 1)) {
        final String setterMethodName = getSetterName(psiField, isBoolean);

        builder.addWarning("Not generated '%s'(): A method with similar name '%s' already exists", setterMethodName, methodName);
        result = false;
      }
    }
  }
  return result;
}
 
Example 2
Source File: GetterFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private boolean validateExistingMethods(@NotNull PsiField psiField, @NotNull ProblemBuilder builder) {
  boolean result = true;
  final PsiClass psiClass = psiField.getContainingClass();
  if (null != psiClass) {
    final boolean isBoolean = PsiType.BOOLEAN.equals(psiField.getType());
    final AccessorsInfo accessorsInfo = AccessorsInfo.build(psiField);
    final Collection<String> methodNames = LombokUtils.toAllGetterNames(accessorsInfo, psiField.getName(), isBoolean);
    final Collection<PsiMethod> classMethods = PsiClassUtil.collectClassMethodsIntern(psiClass);
    filterToleratedElements(classMethods);

    for (String methodName : methodNames) {
      if (PsiMethodUtil.hasSimilarMethod(classMethods, methodName, 0)) {
        final String setterMethodName = LombokUtils.getGetterName(psiField);

        builder.addWarning("Not generated '%s'(): A method with similar name '%s' already exists", setterMethodName, methodName);
        result = false;
      }
    }
  }
  return result;
}
 
Example 3
Source File: LombokFieldFindUsagesHandlerFactory.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean canFindUsages(@NotNull PsiElement element) {
  if (element instanceof PsiField && !DumbService.isDumb(element.getProject())) {
    final PsiField psiField = (PsiField) element;
    final PsiClass containingClass = psiField.getContainingClass();
    if (containingClass != null) {
      return Arrays.stream(containingClass.getMethods()).anyMatch(LombokLightMethodBuilder.class::isInstance) ||
        Arrays.stream(containingClass.getInnerClasses()).anyMatch(LombokLightClassBuilder.class::isInstance);
    }
  }
  return false;
}
 
Example 4
Source File: LombokReferenceSearcher.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void processPsiField(final PsiField refPsiField, final SearchRequestCollector collector) {
  final PsiClass containingClass = refPsiField.getContainingClass();
  if (null != containingClass) {
    processClassMethods(refPsiField, collector, containingClass);

    final PsiClass[] innerClasses = containingClass.getInnerClasses();
    Arrays.stream(innerClasses)
      .forEach(psiClass -> processClassMethods(refPsiField, collector, psiClass));

    Arrays.stream(innerClasses)
      .forEach(psiClass -> processClassFields(refPsiField, collector, psiClass));
  }
}
 
Example 5
Source File: AbstractFieldNameConstantsProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public LombokPsiElementUsage checkFieldUsage(@NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation) {
  final PsiClass containingClass = psiField.getContainingClass();
  if (null != containingClass) {
    if (PsiClassUtil.getNames(filterFields(containingClass, psiAnnotation)).contains(psiField.getName())) {
      return LombokPsiElementUsage.USAGE;
    }
  }
  return LombokPsiElementUsage.NONE;
}
 
Example 6
Source File: RequiredArgsConstructorProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public LombokPsiElementUsage checkFieldUsage(@NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation) {
  final PsiClass containingClass = psiField.getContainingClass();
  if (null != containingClass) {
    if (PsiClassUtil.getNames(getRequiredFields(containingClass)).contains(psiField.getName())) {
      return LombokPsiElementUsage.WRITE;
    }
  }
  return LombokPsiElementUsage.NONE;
}
 
Example 7
Source File: NoArgsConstructorProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public LombokPsiElementUsage checkFieldUsage(@NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation) {
  final PsiClass containingClass = psiField.getContainingClass();
  if (null != containingClass) {

    final boolean forceConstructorWithJavaDefaults = isForceConstructor(psiAnnotation);
    final Collection<PsiField> params = getConstructorFields(containingClass, forceConstructorWithJavaDefaults);

    if (PsiClassUtil.getNames(params).contains(psiField.getName())) {
      return LombokPsiElementUsage.WRITE;
    }
  }
  return LombokPsiElementUsage.NONE;
}
 
Example 8
Source File: AllArgsConstructorProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public LombokPsiElementUsage checkFieldUsage(@NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation) {
  final PsiClass containingClass = psiField.getContainingClass();
  if (null != containingClass) {
    if (PsiClassUtil.getNames(getAllNotInitializedAndNotStaticFields(containingClass)).contains(psiField.getName())) {
      return LombokPsiElementUsage.WRITE;
    }
  }
  return LombokPsiElementUsage.NONE;
}
 
Example 9
Source File: SetterFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void generatePsiElements(@NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation, @NotNull List<? super PsiElement> target) {
  final String methodVisibility = LombokProcessorUtil.getMethodModifier(psiAnnotation);
  final PsiClass psiClass = psiField.getContainingClass();
  if (methodVisibility != null && psiClass != null) {
    target.add(createSetterMethod(psiField, psiClass, methodVisibility));
  }
}
 
Example 10
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 11
Source File: GetterFieldProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void generatePsiElements(@NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation, @NotNull List<? super PsiElement> target) {
  final String methodVisibility = LombokProcessorUtil.getMethodModifier(psiAnnotation);
  final PsiClass psiClass = psiField.getContainingClass();
  if (null != methodVisibility && null != psiClass) {
    target.add(createGetterMethod(psiField, psiClass, methodVisibility));
  }
}
 
Example 12
Source File: ElementUtils.java    From aircon with MIT License 4 votes vote down vote up
public static boolean isEnumConst(PsiField psiField) {
	final String fieldType = psiField.getType()
	                                 .getCanonicalText();
	final PsiClass containingClass = psiField.getContainingClass();
	return containingClass.isEnum() && fieldType.equals(containingClass.getQualifiedName());
}
 
Example 13
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;
}