001package ball.annotation.processing;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: ClassFileProcessor.java 6087 2020-06-02 04:36:27Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/annotation/processing/ClassFileProcessor.java $
007 * %%
008 * Copyright (C) 2008 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.io.IOException;
024import java.util.LinkedList;
025import java.util.List;
026import java.util.Set;
027import javax.annotation.processing.Processor;
028import javax.tools.JavaFileManager;
029import javax.tools.JavaFileObject;
030
031import static java.util.Collections.singleton;
032import static javax.tools.StandardLocation.CLASS_OUTPUT;
033import static org.apache.commons.lang3.StringUtils.EMPTY;
034
035/**
036 * Interface to provide an alternate entry point for annotation processing
037 * on compiled class files.  Implementors must extend
038 * {@link AnnotatedProcessor}.
039 *
040 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
041 * @version $Revision: 6087 $
042 */
043public interface ClassFileProcessor extends Processor {
044
045    /**
046     * {@link Processor} alternate entry point.
047     *
048     * @param   set             The {@link Set} of {@link Class}es to
049     *                          analyze.
050     * @param   fm              The configured {@link JavaFileManager} (for
051     *                          writing output files).
052     *
053     * @throws  Throwable       If the implementation throws a
054     *                          {@link Throwable}.
055     */
056    public void process(Set<Class<?>> set,
057                        JavaFileManager fm) throws Throwable;
058
059    /**
060     * Static method to get the list of {@link Class} file names that have
061     * been generated by the {@link JavaFileManager}.
062     *
063     * @param   fm              The configured {@link JavaFileManager}.
064     *
065     * @return  The {@link List} of class file names ({@link String}s).
066     *
067     * @throws  IOException     If the {@link JavaFileManager} throws an
068     *                          {@link IOException}.
069     */
070    public static List<String> list(JavaFileManager fm) throws IOException {
071        List<String> list = new LinkedList<>();
072
073        for (JavaFileObject file :
074                 fm.list(CLASS_OUTPUT, EMPTY,
075                         singleton(JavaFileObject.Kind.CLASS), true)) {
076            list.add(fm.inferBinaryName(CLASS_OUTPUT, file));
077        }
078
079        return list;
080    }
081}