001package ball.annotation.processing;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: JAXBIndexProcessor.java 6105 2020-06-03 19:38:25Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/annotation/processing/JAXBIndexProcessor.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 */
023
024import ball.annotation.ServiceProviderFor;
025import java.io.PrintWriter;
026import java.lang.annotation.Annotation;
027import java.util.Map;
028import java.util.Set;
029import java.util.TreeMap;
030import java.util.TreeSet;
031import javax.annotation.processing.Processor;
032import javax.tools.FileObject;
033import javax.tools.JavaFileManager;
034import javax.xml.bind.annotation.XmlRootElement;
035import javax.xml.bind.annotation.XmlType;
036import lombok.NoArgsConstructor;
037import lombok.ToString;
038
039import static java.lang.reflect.Modifier.isAbstract;
040import static javax.tools.StandardLocation.CLASS_OUTPUT;
041
042/**
043 * {@link Processor} implementation to generate {@code jaxb.index} files
044 * from {@link Class}es annotated with JAXB annotations.
045 *
046 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
047 * @version $Revision: 6105 $
048 */
049@ServiceProviderFor({ Processor.class })
050@For({ XmlRootElement.class, XmlType.class })
051@NoArgsConstructor @ToString
052public class JAXBIndexProcessor extends AnnotatedProcessor
053                                implements ClassFileProcessor {
054    private static final String JAXB_INDEX = "jaxb.index";
055    private static final String DOT = ".";
056
057    private final Set<String> set = new TreeSet<>();
058
059    @Override
060    public void process(Set<Class<?>> set,
061                        JavaFileManager fm) throws Throwable {
062        Map<String,Set<String>> map = new TreeMap<>();
063
064        for (Class<?> type : set) {
065            if (! isAbstract(type.getModifiers())) {
066                for (Class<? extends Annotation> annotation :
067                         getSupportedAnnotationTypeList()) {
068                    if (type.isAnnotationPresent(annotation)) {
069                        String key = type.getPackage().getName();
070                        String value =
071                            type.getCanonicalName().substring(key.length());
072
073                        if (value.startsWith(DOT)) {
074                            value = value.substring(DOT.length());
075                        }
076
077                        map.computeIfAbsent(key, k -> new TreeSet<>())
078                            .add(value);
079                    }
080                }
081            }
082        }
083
084        for (Map.Entry<String,Set<String>> entry : map.entrySet()) {
085            String path = JAXB_INDEX;
086            FileObject file =
087                fm.getFileForOutput(CLASS_OUTPUT,
088                                    entry.getKey(), JAXB_INDEX, null);
089
090            try (PrintWriter writer = new PrintWriter(file.openWriter())) {
091                writer.println("# " + JAXB_INDEX);
092
093                entry.getValue().stream().forEach(t -> writer.println(t));
094            }
095        }
096    }
097}