<commit_before>package io.t28.shade.compiler;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Names;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.inject.Inject;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

import io.t28.shade.annotation.Preferences;
import io.t28.shade.compiler.factories.TypeFactory;
import io.t28.shade.compiler.inject.EditorModule;
import io.t28.shade.compiler.inject.EntityModule;
import io.t28.shade.compiler.inject.PreferencesModule;
import io.t28.shade.compiler.inject.ShadeModule;
import io.t28.shade.compiler.utils.Writer;

@SuppressWarnings({"unused", "WeakerAccess"})
@AutoService(Processor.class)
public class ShadeProcessor extends AbstractProcessor {
    private Injector injector;

    @Inject
    private Messager messager;

    @Inject
    private Writer writer;

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        return ImmutableSet.of(Preferences.class.getCanonicalName());
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }

    @Override
    public synchronized void init(ProcessingEnvironment environment) {
        super.init(environment);
        injector = Guice.createInjector(new ShadeModule(environment));
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment environment) {
        injector.injectMembers(this);
        environment.getElementsAnnotatedWith(Preferences.class)
                .stream()
                .map(TypeElement.class::cast)
                .filter(element -> {
                    final ElementKind kind = element.getKind();
                    if (kind != ElementKind.CLASS && kind != ElementKind.INTERFACE) {
                        messager.printMessage(Diagnostic.Kind.ERROR, "@Preferences is not allowed to use for " + kind);
                        return false;
                    }
                    return true;
                })
                .forEach(element -> {
                    try {
                        final Injector childInjector = injector.createChildInjector(new PreferencesModule(element), new EntityModule(), new EditorModule());
                        final String packageName = childInjector.getInstance(Key.get(String.class, Names.named("PackageName")));
                        final TypeFactory factory = childInjector.getInstance(Key.get(TypeFactory.class, Names.named("Preferences")));
                        writer.write(packageName, factory.create());
                    } catch (Exception e) {
                        messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage());
                    }<commit_msg>Made ShadeProcessor readable (#79)<commit_after>package io.t28.shade.compiler;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Names;

import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.inject.Inject;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

import io.t28.shade.annotation.Preferences;
import io.t28.shade.compiler.factories.TypeFactory;
import io.t28.shade.compiler.inject.EditorModule;
import io.t28.shade.compiler.inject.EntityModule;
import io.t28.shade.compiler.inject.PreferencesModule;
import io.t28.shade.compiler.inject.ShadeModule;
import io.t28.shade.compiler.utils.Writer;

@SuppressWarnings({"unused", "WeakerAccess"})
@AutoService(Processor.class)
public class ShadeProcessor extends AbstractProcessor {
    private static final Key<String> PACKAGE_NAME_KEY = Key.get(String.class, Names.named("PackageName"));
    private static final Key<TypeFactory> TYPE_FACTORY_KEY = Key.get(TypeFactory.class, Names.named("Preferences"));

    private Injector injector;

    @Inject
    private Messager messager;

    @Inject
    private Writer writer;

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        return ImmutableSet.of(Preferences.class.getCanonicalName());
    }

    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }

    @Override
    public synchronized void init(@Nonnull ProcessingEnvironment environment) {
        super.init(environment);
        injector = Guice.createInjector(new ShadeModule(environment));
    }

    @Override
    public boolean process(@Nonnull Set<? extends TypeElement> annotations, @Nonnull RoundEnvironment environment) {
        injector.injectMembers(this);
        environment.getElementsAnnotatedWith(Preferences.class)
                .stream()
                .map(TypeElement.class::cast)
                .filter(element -> {
                    final ElementKind kind = element.getKind();
                    if (kind == ElementKind.INTERFACE || kind == ElementKind.CLASS) {
                        return true;
                    }
                    messager.printMessage(Diagnostic.Kind.ERROR, "@Preferences is not allowed to use for " + kind);
                    return false;
                })
                .forEach(element -> {
                    try {
                        final Injector childInjector = injector.createChildInjector(
                                new PreferencesModule(element),
                                new EntityModule(),
                                new EditorModule()
                        );
                        final String packageName = childInjector.getInstance(PACKAGE_NAME_KEY);
                        final TypeFactory factory = childInjector.getInstance(TYPE_FACTORY_KEY);
                        writer.write(packageName, factory.create());
                    } catch (Exception e) {
                        messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage());
                    }
{'fraction_non_alphanumeric': 0.09430631160806238, 'fraction_numerical': 0.005002206855965867, 'mean_word_length': 2.785077951002227, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '17665', 'n_tokens_mistral': 1758, 'n_tokens_neox': 1701, 'n_words': 331}