<commit_before>/*
 * Copyright 2014 Yahoo Inc.

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 *   http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yahoo.aptutils.utils;

import com.yahoo.aptutils.model.*;
import com.yahoo.aptutils.model.TypeName.TypeNameVisitor;
import com.yahoo.aptutils.visitors.ImportGatheringTypeMirrorVisitor;
import com.yahoo.aptutils.visitors.ImportGatheringTypeNameVisitor;
import com.yahoo.aptutils.writer.JavaFileWriter;
import com.yahoo.aptutils.writer.parameters.MethodDeclarationParameters;

import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import javax.lang.model.util.Types;
import javax.lang.model.util.Elements;
import java.util.*;
import java.util.Map.Entry;

/**
 * Utilities class containing functions that facilitate working with the apt-utils library.
 * Contains several helper methods for converting the classes used by the Java APT to more basic
 * objects used by apt-utils.
 */
public class AptUtils {

    /**
     * "java.lang.Object"
     */
    public static final String OBJECT_CLASS_NAME = CoreTypes.JAVA_OBJECT.toString();

    private final Messager messager;
    private final Types types;
    private final Elements elements;

    /**
     * @param messager {@link Messager} object. Should be obtained from {@link ProcessingEnvironment#getMessager()}
     * @param types {@link Types} object. Should be obtained from {@link ProcessingEnvironment#getTypeUtils()}
     * @param elements {@link Elements} object. Should be obtained from {@link ProcessingEnvironment#getElementUtils()} ()}
     */
    public AptUtils(Messager messager, Types types, Elements elements) {
        this.messager = messager;
        this.types = types;
        this.elements = elements;
    }

    /**
     * @return the {@link Messager} this AptUtils was constructed with
     */
    public Messager getMessager() {
        return messager;
    }

    /**
     * @return the {@link Types} this AptUtils was constructed with
     */
    public Types getTypes() {
        return types;
    }

    /**
     * @return the {@link Elements} this AptUtils was constructed with
     */
    public Elements getElements() {
        return elements;
    }

    // --- Imports helpers

    /**
     * @param accumulate a {@link Set} in which to add accumulated imports required by the given elements
     * @param elems {@link Element}s to accumulate imports from
     */
    public void accumulateImportsFromElements(Set<DeclaredTypeName> accumulate, Collection<? extends Element> elems) {
        if (!isEmpty(elems)) {
            for (Element elem : elems) {
                elem.asType().accept(new ImportGatheringTypeMirrorVisitor(elem, this), accumulate);
            }
        }
    }

    /**
     * @param accumulate a {@link Set} in which to add accumulated imports required by the given TypeNames
     * @param typeNames {@link TypeName}s to accumulate imports from
     */
    public void accumulateImportsFromTypeNames(Set<DeclaredTypeName> accumulate, Collection<? extends TypeName> typeNames) {
        if (!isEmpty(typeNames)) {
            ImportGatheringTypeNameVisitor visitor = new ImportGatheringTypeNameVisitor();
            for (TypeName typeName : typeNames) {
                typeName.accept(visitor, accumulate);
            }
        }
    }
<commit_msg>AptUtils should be constructed from a ProcessingEnvironment so it can easily provide access to everything<commit_after>/*
 * Copyright 2014 Yahoo Inc.

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 *   http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yahoo.aptutils.utils;

import com.yahoo.aptutils.model.*;
import com.yahoo.aptutils.model.TypeName.TypeNameVisitor;
import com.yahoo.aptutils.visitors.ImportGatheringTypeMirrorVisitor;
import com.yahoo.aptutils.visitors.ImportGatheringTypeNameVisitor;
import com.yahoo.aptutils.writer.JavaFileWriter;
import com.yahoo.aptutils.writer.parameters.MethodDeclarationParameters;

import javax.annotation.processing.Filer;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import javax.lang.model.util.Types;
import javax.lang.model.util.Elements;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;

/**
 * Utilities class containing functions that facilitate working with the apt-utils library.
 * Contains several helper methods for converting the classes used by the Java APT to more basic
 * objects used by apt-utils.
 */
public class AptUtils {

    /**
     * "java.lang.Object"
     */
    public static final String OBJECT_CLASS_NAME = CoreTypes.JAVA_OBJECT.toString();

    private final ProcessingEnvironment env;
    private final Messager messager;
    private final Types types;
    private final Elements elements;
    private final Filer filer;

    /**
     * @param env a {@link ProcessingEnvironment}
     */
    public AptUtils(ProcessingEnvironment env) {
        this.env = env;
        this.messager = env.getMessager();
        this.types = env.getTypeUtils();
        this.elements = env.getElementUtils();
        this.filer = env.getFiler();
    }

    /**
     * @return the {@link ProcessingEnvironment} this AptUtils was constructed from
     */
    public ProcessingEnvironment getProcessingEnvironment() {
        return env;
    }

    /**
     * @return an instance of {@link Messager} for the ProcessingEnvironment this AptUtils was constructed with
     */
    public Messager getMessager() {
        return messager;
    }

    /**
     * @return an instance of {@link Types} for the ProcessingEnvironment this AptUtils was constructed with
     */
    public Types getTypes() {
        return types;
    }

    /**
     * @return an instance of {@link Elements} for the ProcessingEnvironment this AptUtils was constructed with
     */
    public Elements getElements() {
        return elements;
    }

    /**
     * @return an instance of {@link Filer} for the ProcessingEnvironment this AptUtils was constructed with
     */
    public Filer getFiler() {
        return filer;
    }

    /**
     *
     * @param generatedTypeName the fully qualified type name for the file to be generated
     * @param sourceElement the {@link Element} that caused this file to be generated
     * @return a new instance of {@link JavaFileWriter} for the given type
     * @throws IOException
     */
    public JavaFileWriter newJavaFileWriter(DeclaredTypeName generatedTypeName, Element sourceElement) throws IOException {
        JavaFileObject jfo = filer.createSourceFile(generatedTypeName.toString(), sourceElement);
        return new JavaFileWriter(jfo.openWriter());
    }

    // --- Imports helpers

    /**
     * @param accumulate a {@link Set} in which to add accumulated imports required by the given elements
     * @param elems {@link Element}s to accumulate imports from
     */
    public void accumulateImportsFromElements(Set<DeclaredTypeName> accumulate, Collection<? extends Element> elems) {
        if (!isEmpty(elems)) {
            for (Element elem : elems) {
                elem.asType().accept(new ImportGatheringTypeMirrorVisitor(elem, this), accumulate);
            }
        }
    }

    /**
     * @param accumulate a {@link Set} in which to add accumulated imports required by the given TypeNames
     * @param typeNames {@link TypeName}s to accumulate imports from
     */
    public void accumulateImportsFromTypeNames(Set<DeclaredTypeName> accumulate, Collection<? extends TypeName> typeNames) {
        if (!isEmpty(typeNames)) {
            ImportGatheringTypeNameVisitor visitor = new ImportGatheringTypeNameVisitor();
            for (TypeName typeName : typeNames) {
                typeName.accept(visitor, accumulate);
            }
        }
    }

{'fraction_non_alphanumeric': 0.08627274769714671, 'fraction_numerical': 0.001797348910357223, 'mean_word_length': 3.237505949547834, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3102809', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2236, 'n_words': 785}