001package ball.annotation.processing;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: ClassFileProcessor.java 7479 2021-02-14 02:19:05Z 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 - 2021 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: 7479 $
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  Exception       If the implementation throws a
054     *                          {@link Exception}.
055     */
056    public void process(Set<Class<?>> set, JavaFileManager fm) throws Exception;
057
058    /**
059     * Static method to get the list of {@link Class} file names that have
060     * been generated by the {@link JavaFileManager}.
061     *
062     * @param   fm              The configured {@link JavaFileManager}.
063     *
064     * @return  The {@link List} of class file names ({@link String}s).
065     *
066     * @throws  IOException     If the {@link JavaFileManager} throws an
067     *                          {@link IOException}.
068     */
069    public static List<String> list(JavaFileManager fm) throws IOException {
070        List<String> list = new LinkedList<>();
071
072        for (JavaFileObject file :
073                 fm.list(CLASS_OUTPUT, EMPTY,
074                         singleton(JavaFileObject.Kind.CLASS), true)) {
075            list.add(fm.inferBinaryName(CLASS_OUTPUT, file));
076        }
077
078        return list;
079    }
080}