<commit_before>package de.plushnikov.intellij.plugin.provider;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.augment.PsiAugmentProvider;
import com.intellij.psi.impl.source.Constants;
import com.intellij.psi.impl.source.PsiClassImpl;
import de.plushnikov.intellij.lombok.UserMapKeys;
import de.plushnikov.intellij.lombok.processor.clazz.LombokClassProcessor;
import de.plushnikov.intellij.lombok.processor.field.LombokFieldProcessor;
import de.plushnikov.intellij.plugin.core.GenericServiceLocator;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
 * Provides support for lombok generated elements
 *
 * @author <NAME>
 */
public class LombokAugmentProvider extends PsiAugmentProvider {
  private static final Logger LOG = Logger.getInstance(LombokAugmentProvider.class.getName());

  private final Collection<LombokFieldProcessor> allFieldHandlers;
  private final Collection<LombokClassProcessor> allClassHandlers;

  public LombokAugmentProvider() {
    List<LombokClassProcessor> lombokClassProcessors = GenericServiceLocator.locateAll(LombokClassProcessor.class);
    List<LombokFieldProcessor> lombokFieldProcessors = GenericServiceLocator.locateAll(LombokFieldProcessor.class);

    allClassHandlers = new HashSet<LombokClassProcessor>(lombokClassProcessors);
    allFieldHandlers = new HashSet<LombokFieldProcessor>(lombokFieldProcessors);
  }

  /**
   * To add a getters based on field annotations the provider should be implemented somewhat like this:
   * only respond to requests where parameter "element" is of type PsiClass and "type" equals to PsiMethod.class;
   * find all fields in a given  "element" for which a getter should be added;
   * and return a PsiMethod implementation for each such field
   * (for Lombok I think LightMethod instances should be used, see PsiClassImpl code for example)
   */
  @NotNull
  @Override
  public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element, @NotNull Class<Psi> type) {
    // Expecting that we are only augmenting an PsiClass
    if (!(element instanceof PsiClass) || !element.isValid() || !element.isPhysical()) {
      //noinspection unchecked
      return Collections.emptyList();
    }
    final List<Psi> result = new ArrayList<Psi>();
    final PsiClass psiClass = (PsiClass) element;
    LOG.info("Called for class: " + psiClass.getQualifiedName() + " type: " + type.getName());

    final PsiMethod[] classMethods = collectClassMethodsIntern((PsiClassImpl) psiClass);
    if (type.isAssignableFrom(PsiField.class)) {
      LOG.info("collect field of class: " + psiClass.getQualifiedName());
      processPsiClassAnnotations(result, psiClass, classMethods, type);

    } else if (type.isAssignableFrom(PsiMethod.class)) {
      LOG.info("collect methods of class: " + psiClass.getQualifiedName());

      cleanAttributeUsage(psiClass);
      processPsiClassAnnotations(result, psiClass, classMethods, type);
      processPsiClassFieldAnnotation(result, psiClass, classMethods, type);
    }
    return result;
  }

  /**
   * Workaround to get all of original Methods of the psiClass.
   * Normal call to psiClass.getMethods() is impossible because of incorrect cache implementation of IntelliJ Idea
   *
   * @param psiClass psiClass to collect all of methods from
   * @return all intern methods of the class
   */
  private PsiMethod[] collectClassMethodsIntern(@NotNull PsiClassImpl psiClass) {
    return psiClass.getStubOrPsiChildren(Constants.METHOD_BIT_SET, PsiMethod.ARRAY_FACTORY);
  }

  protected void cleanAttributeUsage(PsiClass psiClass) {
    for (PsiField psiField : psiClass.getFields()) {
      UserMapKeys.removeAllUsagesFrom(psiField);
    }<commit_msg>deprecate some methods<commit_after>package de.plushnikov.intellij.plugin.provider;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.augment.PsiAugmentProvider;
import com.intellij.psi.impl.source.Constants;
import com.intellij.psi.impl.source.PsiClassImpl;
import de.plushnikov.intellij.lombok.UserMapKeys;
import de.plushnikov.intellij.lombok.processor.clazz.LombokClassProcessor;
import de.plushnikov.intellij.lombok.processor.field.LombokFieldProcessor;
import de.plushnikov.intellij.plugin.core.GenericServiceLocator;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

/**
 * Provides support for lombok generated elements
 *
 * @author <NAME>
 */
public class LombokAugmentProvider extends PsiAugmentProvider {
  private static final Logger LOG = Logger.getInstance(LombokAugmentProvider.class.getName());

  private final Collection<LombokFieldProcessor> allFieldHandlers;
  private final Collection<LombokClassProcessor> allClassHandlers;

  public LombokAugmentProvider() {
    List<LombokClassProcessor> lombokClassProcessors = GenericServiceLocator.locateAll(LombokClassProcessor.class);
    List<LombokFieldProcessor> lombokFieldProcessors = GenericServiceLocator.locateAll(LombokFieldProcessor.class);

    allClassHandlers = new HashSet<LombokClassProcessor>(lombokClassProcessors);
    allFieldHandlers = new HashSet<LombokFieldProcessor>(lombokFieldProcessors);
  }

  /**
   * To add a getters based on field annotations the provider should be implemented somewhat like this:
   * only respond to requests where parameter "element" is of type PsiClass and "type" equals to PsiMethod.class;
   * find all fields in a given  "element" for which a getter should be added;
   * and return a PsiMethod implementation for each such field
   * (for Lombok I think LightMethod instances should be used, see PsiClassImpl code for example)
   */
  @NotNull
  @Override
  public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element, @NotNull Class<Psi> type) {
    // Expecting that we are only augmenting an PsiClass
    if (!(element instanceof PsiClass) || !element.isValid() || !element.isPhysical()) {
      //noinspection unchecked
      return Collections.emptyList();
    }
    final List<Psi> result = new ArrayList<Psi>();
    final PsiClass psiClass = (PsiClass) element;
    LOG.info("Called for class: " + psiClass.getQualifiedName() + " type: " + type.getName());

    final PsiMethod[] classMethods = collectClassMethodsIntern((PsiClassImpl) psiClass);
    if (type.isAssignableFrom(PsiField.class)) {
      LOG.info("collect field of class: " + psiClass.getQualifiedName());
      processPsiClassAnnotations(result, psiClass, classMethods, type);

    } else if (type.isAssignableFrom(PsiMethod.class)) {
      LOG.info("collect methods of class: " + psiClass.getQualifiedName());

      cleanAttributeUsage(psiClass);
      processPsiClassAnnotations(result, psiClass, classMethods, type);
      processPsiClassFieldAnnotation(result, psiClass, classMethods, type);
    }
    return result;
  }

  /**
   * Workaround to get all of original Methods of the psiClass.
   * Normal call to psiClass.getMethods() is impossible because of incorrect cache implementation of IntelliJ Idea
   *
   * @param psiClass psiClass to collect all of methods from
   * @return all intern methods of the class
   * @deprecated
   */
  @Deprecated
  private PsiMethod[] collectClassMethodsIntern(@NotNull PsiClassImpl psiClass) {
    return psiClass.getStubOrPsiChildren(Constants.METHOD_BIT_SET, PsiMethod.ARRAY_FACTORY);
  }

  protected void cleanAttributeUsage(PsiClass psiClass) {
    for (PsiField psiField : psiClass.getFields()) {
      UserMapKeys.removeAllUsagesFrom(psiField);
    }
{'fraction_non_alphanumeric': 0.09029484029484029, 'fraction_numerical': 0.0, 'mean_word_length': 5.0527881040892195, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4829050', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2399, 'n_words': 586}