Java Code Examples for com.intellij.psi.PsiClass#getFields()

The following examples show how to use com.intellij.psi.PsiClass#getFields() . 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: InvalidEnumClassDetector.java    From aircon with MIT License 6 votes vote down vote up
@Override
protected void visitEnumConfigAnnotation(final UAnnotation node, final PsiClass enumClass) {
	final PsiField[] fields = enumClass.getFields();

	if (fields.length == 0) {
		report(node);
		return;
	}

	for (PsiField psiField : fields) {
		if (ElementUtils.isEnumConst(psiField) && !ConfigElementsUtils.hasRemoteValueAnnotation(psiField)) {
			report(node);
			return;
		}
	}
}
 
Example 2
Source File: InvalidEnumDefaultValueDetector.java    From aircon with MIT License 6 votes vote down vote up
@Override
protected void visitEnumConfigAnnotation(final UAnnotation node, final PsiClass enumClass) {
	final List<Object> remoteValues = new ArrayList<>();

	for (PsiField psiField : enumClass.getFields()) {
		final Object remoteValue = ConfigElementsUtils.getRemoteValue(psiField);
		if (remoteValue == null) {
			return;
		}
		remoteValues.add(remoteValue);
	}

	final Object defaultValue = ConfigElementsUtils.getDefaultValueAttribute(node.getJavaPsi());

	if (!remoteValues.contains(defaultValue)) {
		report(node, getIssueExplanation() + " " + Arrays.toString(remoteValues.toArray()));
	}
}
 
Example 3
Source File: PsiEventDeclarationsExtractor.java    From litho with Apache License 2.0 6 votes vote down vote up
static ImmutableList<FieldModel> getFields(@Nullable PsiClass psiClass) {
  if (psiClass == null) {
    return ImmutableList.of();
  }
  final List<FieldModel> fieldModels = new ArrayList<>();
  for (PsiField psiField : psiClass.getFields()) {
    fieldModels.add(
        new FieldModel(
            FieldSpec.builder(
                    PsiTypeUtils.getTypeName(psiField.getType()),
                    psiField.getName(),
                    PsiModifierExtractor.extractModifiers(psiField))
                .build(),
            psiField));
  }

  return ImmutableList.copyOf(fieldModels);
}
 
Example 4
Source File: SqliteMagicStructureViewExtension.java    From sqlitemagic with Apache License 2.0 6 votes vote down vote up
@Override
public StructureViewTreeElement[] getChildren(PsiElement parent) {
  Collection<StructureViewTreeElement> result = new ArrayList<StructureViewTreeElement>();
  final PsiClass psiClass = (PsiClass) parent;

  for (PsiField psiField : psiClass.getFields()) {
    if (psiField instanceof SqliteMagicLightFieldBuilder) {
      result.add(new PsiFieldTreeElement(psiField, false));
    }
  }

  for (PsiMethod psiMethod : psiClass.getMethods()) {
    if (psiMethod instanceof SqliteMagicLightMethodBuilder) {
      result.add(new PsiMethodTreeElement(psiMethod, false));
    }
  }

  if (!result.isEmpty()) {
    return result.toArray(new StructureViewTreeElement[result.size()]);
  } else {
    return StructureViewTreeElement.EMPTY_ARRAY;
  }
}
 
Example 5
Source File: MethodReferenceDiagramDataModel.java    From intellij-reference-diagram with Apache License 2.0 6 votes vote down vote up
private void collectNodes(PsiClass psiClass) {
    for (PsiMethod psiMethod : psiClass.getMethods()) {
        addUserElement(psiMethod);
    }

    for (PsiField psiField : psiClass.getFields()) {
        addUserElement(psiField);
    }

    for (PsiClassInitializer psiClassInitializer : psiClass.getInitializers()) {
        addUserElement(psiClassInitializer);
    }

    for (PsiClass innerClass : psiClass.getInnerClasses()) {
        addUserElement(innerClass);
    }
}
 
Example 6
Source File: LombokLoggerHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected void processClass(@NotNull PsiClass psiClass) {
  final Collection<AbstractLogProcessor> logProcessors = Arrays.asList(
    ServiceManager.getService(CommonsLogProcessor.class), ServiceManager.getService(JBossLogProcessor.class),
    ServiceManager.getService(Log4jProcessor.class), ServiceManager.getService(Log4j2Processor.class), ServiceManager.getService(LogProcessor.class),
    ServiceManager.getService(Slf4jProcessor.class), ServiceManager.getService(XSlf4jProcessor.class), ServiceManager.getService(FloggerProcessor.class),
    ServiceManager.getService(CustomLogProcessor.class));

  final String lombokLoggerName = AbstractLogProcessor.getLoggerName(psiClass);
  final boolean lombokLoggerIsStatic = AbstractLogProcessor.isLoggerStatic(psiClass);

  for (AbstractLogProcessor logProcessor : logProcessors) {
    for (PsiField psiField : psiClass.getFields()) {
      String loggerType = logProcessor.getLoggerType(psiClass); // null when the custom log's declaration is invalid
      if (loggerType != null && psiField.getType().equalsToText(loggerType)
        && checkLoggerField(psiField, lombokLoggerName, lombokLoggerIsStatic)) {
        processLoggerField(psiField, psiClass, logProcessor, lombokLoggerName);
      }
    }
  }
}
 
Example 7
Source File: GenerateDialog.java    From android-parcelable-intellij-plugin with Apache License 2.0 6 votes vote down vote up
protected GenerateDialog(final PsiClass psiClass) {
    super(psiClass.getProject());
    setTitle("Select Fields for Parcelable Generation");

    fieldsCollection = new CollectionListModel<PsiField>();
    final JBList fieldList = new JBList(fieldsCollection);
    fieldList.setCellRenderer(new DefaultPsiElementCellRenderer());
    final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(fieldList).disableAddAction();
    final JPanel panel = decorator.createPanel();

    fieldsComponent = LabeledComponent.create(panel, "Fields to include in Parcelable");

    includeSubclasses = new JBCheckBox("Include fields from base classes");
    setupCheckboxClickAction(psiClass);
    showCheckbox = psiClass.getFields().length != psiClass.getAllFields().length;

    updateFieldsDisplay(psiClass);
    init();
}
 
Example 8
Source File: PsiPropDefaultsExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
/** Get the prop defaults from the given {@link PsiClass}. */
public static ImmutableList<PropDefaultModel> getPropDefaults(PsiClass psiClass) {
  final List<PropDefaultModel> propDefaults = new ArrayList<>();

  for (PsiField psiField : psiClass.getFields()) {
    propDefaults.addAll(extractFromField(psiField));
  }

  return ImmutableList.copyOf(propDefaults);
}
 
Example 9
Source File: PsiUtils.java    From intellij-reference-diagram with Apache License 2.0 5 votes vote down vote up
private static boolean classHasField(PsiClass psiClass, PsiField other) {
    for (PsiField psiField : psiClass.getFields()) {
        if (psiField.equals(other)) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: RefactorSetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected List<EncapsulatableClassMember> getEncapsulatableClassMembers(PsiClass psiClass) {
  final List<EncapsulatableClassMember> result = new ArrayList<>();
  for (PsiField field : psiClass.getFields()) {
    if (null != PropertyUtil.findPropertySetter(psiClass, field.getName(), false, false)) {
      result.add(new PsiFieldMember(field));
    }
  }
  return result;
}
 
Example 11
Source File: LombokGetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void processClass(@NotNull PsiClass psiClass) {
  final Map<PsiField, PsiMethod> fieldMethodMap = new HashMap<>();
  for (PsiField psiField : psiClass.getFields()) {
    PsiMethod propertyGetter = PropertyUtil.findPropertyGetter(psiClass, psiField.getName(), psiField.hasModifierProperty(PsiModifier.STATIC), false);

    if (null != propertyGetter) {
      fieldMethodMap.put(psiField, propertyGetter);
    }
  }

  processIntern(fieldMethodMap, psiClass, Getter.class);
}
 
Example 12
Source File: LombokSetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void processClass(@NotNull PsiClass psiClass) {
  final Map<PsiField, PsiMethod> fieldMethodMap = new HashMap<>();
  for (PsiField psiField : psiClass.getFields()) {
    PsiMethod propertySetter = PropertyUtil.findPropertySetter(psiClass, psiField.getName(), psiField.hasModifierProperty(PsiModifier.STATIC), false);

    if (null != propertySetter) {
      fieldMethodMap.put(psiField, propertySetter);
    }
  }

  processIntern(fieldMethodMap, psiClass, Setter.class);
}
 
Example 13
Source File: RefactorGetterHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected List<EncapsulatableClassMember> getEncapsulatableClassMembers(PsiClass psiClass) {
  final List<EncapsulatableClassMember> result = new ArrayList<>();
  for (PsiField field : psiClass.getFields()) {
    if (null != PropertyUtil.findPropertyGetter(psiClass, field.getName(), false, false)) {
      result.add(new PsiFieldMember(field));
    }
  }
  return result;
}
 
Example 14
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;
}