<commit_before>package com.sambosley.javatraits.processor;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;

import com.sambosley.javatraits.annotations.HasTraits;
import com.sambosley.javatraits.annotations.Trait;
import com.sambosley.javatraits.processor.data.ClassWithTraits;
import com.sambosley.javatraits.processor.data.TraitElement;
import com.sambosley.javatraits.processor.writers.ClassWithTraitsSuperclassWriter;
import com.sambosley.javatraits.processor.writers.TraitDelegateWriter;
import com.sambosley.javatraits.processor.writers.TraitInterfaceWriter;
import com.sambosley.javatraits.utils.FullyQualifiedName;

@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class TraitProcessor extends AbstractProcessor {

    private Messager messager;
    private Filer filer;

    @Override
    public synchronized void init(ProcessingEnvironment env) {
        super.init(env);

        this.messager = env.getMessager();
        this.filer = env.getFiler();
    }

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        Set<String> supportedTypes = new LinkedHashSet<String>();
        supportedTypes.add(Trait.class.getCanonicalName());
        supportedTypes.add(HasTraits.class.getCanonicalName());
        return supportedTypes;
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
        Map<FullyQualifiedName, TraitElement> traitMap = getTraitElements(env);
        Set<ClassWithTraits> classesWithTraits = getClassesWithTraits(env, traitMap);

        generateTraitInterfaces(traitMap);
        generateTraitDelegates(classesWithTraits, traitMap);
        generateTraitImplementingSuperclasses(classesWithTraits, traitMap);
        return true;
    }

    private Map<FullyQualifiedName, TraitElement> getTraitElements(RoundEnvironment env) {
        Map<FullyQualifiedName, TraitElement> result = new HashMap<FullyQualifiedName, TraitElement>();
        Set<? extends Element> traitElements = env.getElementsAnnotatedWith(Trait.class);
        for (Element e : traitElements) {
            if (e.getKind() != ElementKind.CLASS)
                messager.printMessage(Kind.ERROR, "Only a class can be annotated with @Trait", e);
            else {
                TypeElement typeElem = (TypeElement) e;
                TraitElement traitElement = new TraitElement(typeElem, messager);
                result.put(traitElement.getFullyQualifiedName(), traitElement);
            }
        }
        return result;
    }

    private Set<ClassWithTraits> getClassesWithTraits(RoundEnvironment env, Map<FullyQualifiedName, TraitElement> traitMap) {
        Set<? extends Element> elementsWithTraits = env.getElementsAnnotatedWith(HasTraits.class);
        Set<ClassWithTraits> result = new HashSet<ClassWithTraits>();
        for (Element e : elementsWithTraits) {
            if (e.getKind() != ElementKind.CLASS)
                messager.printMessage(Kind.ERROR, "Only a class can be annotated with @Trait", e);
            else {
                TypeElement typeElem = (TypeElement) e;
                result.add(new ClassWithTraits(typeElem, messager, traitMap));
            }
        }
        return result;
    }

    private void generateTraitInterfaces(Map<FullyQualifiedName, TraitElement> traitElements) {
        for (TraitElement te : traitElements.values()) {
            new TraitInterfaceWriter(te, messager).writeInterface(filer);
        }
    }
<commit_msg>Use @SupportedAnnotationTypes instead of overriding method<commit_after>package com.sambosley.javatraits.processor;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;

import com.sambosley.javatraits.annotations.HasTraits;
import com.sambosley.javatraits.annotations.Trait;
import com.sambosley.javatraits.processor.data.ClassWithTraits;
import com.sambosley.javatraits.processor.data.TraitElement;
import com.sambosley.javatraits.processor.writers.ClassWithTraitsSuperclassWriter;
import com.sambosley.javatraits.processor.writers.TraitDelegateWriter;
import com.sambosley.javatraits.processor.writers.TraitInterfaceWriter;
import com.sambosley.javatraits.utils.FullyQualifiedName;

@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes(value="com.sambosley.javatraits.annotations.*")
public class TraitProcessor extends AbstractProcessor {

    private Messager messager;
    private Filer filer;
    
    private int invocations = 0;
    
    private Set<? extends Element> traitElements = null;
    private Set<? extends Element> elementsWithTraits = null;

    @Override
    public synchronized void init(ProcessingEnvironment env) {
        super.init(env);

        this.messager = env.getMessager();
        this.filer = env.getFiler();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
        invocations++;
        for (TypeElement e : annotations) {
            messager.printMessage(Kind.NOTE, "This round has: " + e.toString() + ", invocations: " + invocations);
        }
        
        tryToInitTraitElements(env);
        tryToInitClassesWithTraits(env);
        
        if (traitElements != null && elementsWithTraits != null) {
            Map<FullyQualifiedName, TraitElement> traitMap = getTraitElements();
            Set<ClassWithTraits> classesWithTraits = getClassesWithTraits(traitMap);
            generateTraitInterfaces(traitMap);
            generateTraitDelegates(classesWithTraits, traitMap);
            generateTraitImplementingSuperclasses(classesWithTraits, traitMap);
        }

        
        return true;
    }
    
    private void tryToInitTraitElements(RoundEnvironment env) {
        Set<? extends Element> traitElements = env.getElementsAnnotatedWith(Trait.class);
        if (traitElements.size() > 0)
            this.traitElements = traitElements;
    }
    
    private void tryToInitClassesWithTraits(RoundEnvironment env) {
        Set<? extends Element> elementsWithTraits = env.getElementsAnnotatedWith(HasTraits.class);
        if (elementsWithTraits.size() > 0)
            this.elementsWithTraits = elementsWithTraits;
    }

    private Map<FullyQualifiedName, TraitElement> getTraitElements() {
        Map<FullyQualifiedName, TraitElement> result = new HashMap<FullyQualifiedName, TraitElement>();
        
        for (Element e : traitElements) {
            if (e.getKind() != ElementKind.CLASS)
                messager.printMessage(Kind.ERROR, "Only a class can be annotated with @Trait", e);
            else {
                TypeElement typeElem = (TypeElement) e;
                TraitElement traitElement = new TraitElement(typeElem, messager);
                result.put(traitElement.getFullyQualifiedName(), traitElement);
            }
        }
        return result;
    }

    private Set<ClassWithTraits> getClassesWithTraits(Map<FullyQualifiedName, TraitElement> traitMap) {
        Set<ClassWithTraits> result = new HashSet<ClassWithTraits>();
        for (Element e : elementsWithTraits) {
            if (e.getKind() != ElementKind.CLASS)
                messager.printMessage(Kind.ERROR, "Only a class can be annotated with @Trait", e);
            else {
                TypeElement typeElem = (TypeElement) e;
                result.add(new ClassWithTraits(typeElem, messager, traitMap));
            }
        }
        return result;
    }

    private void generateTraitInterfaces(Map<FullyQualifiedName, TraitElement> traitElements) {
        for (TraitElement te : traitElements.values()) {
            new TraitInterfaceWriter(te, messager).writeInterface(filer);
        }
    }

{'fraction_non_alphanumeric': 0.08942815413998427, 'fraction_numerical': 0.0005617346365576902, 'mean_word_length': 3.6005167958656332, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 36, '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': '5044935', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2440, 'n_words': 471}