001    /*
002     * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.mirror.apt;
027    
028    import com.sun.mirror.apt.*;
029    import java.util.*;
030    
031    /**
032     * Utilities to create specialized annotation processors.
033     *
034     * @since 1.5
035     * @author Joseph D. Darcy
036     * @author Scott Seligman
037     */
038    public class AnnotationProcessors {
039        static class NoOpAP implements AnnotationProcessor {
040            NoOpAP() {}
041            public void process(){}
042        }
043    
044        /**
045         * Combines multiple annotation processors into a simple composite
046         * processor.
047         * The composite processor functions by invoking each of its component
048         * processors in sequence.
049         */
050        static class CompositeAnnotationProcessor implements AnnotationProcessor {
051    
052            private List<AnnotationProcessor> aps =
053                new LinkedList<AnnotationProcessor>();
054    
055            /**
056             * Constructs a new composite annotation processor.
057             * @param aps  the component annotation processors
058             */
059            public CompositeAnnotationProcessor(Collection<AnnotationProcessor> aps) {
060                this.aps.addAll(aps);
061            }
062    
063            /**
064             * Constructs a new composite annotation processor.
065             * @param aps  the component annotation processors
066             */
067            public CompositeAnnotationProcessor(AnnotationProcessor... aps) {
068                for(AnnotationProcessor ap: aps)
069                    this.aps.add(ap);
070            }
071    
072            /**
073             * Invokes the <tt>process</tt> method of each component processor,
074             * in the order in which the processors were passed to the constructor.
075             */
076            public void process() {
077                for(AnnotationProcessor ap: aps)
078                    ap.process();
079            }
080        }
081    
082    
083        /**
084         *  An annotation processor that does nothing and has no state.
085         *  May be used multiple times.
086         *
087         * @since 1.5
088         */
089        public final static AnnotationProcessor NO_OP = new NoOpAP();
090    
091        /**
092         * Constructs a new composite annotation processor.  A composite
093         * annotation processor combines multiple annotation processors
094         * into one and functions by invoking each of its component
095         * processors' process methods in sequence.
096         *
097         * @param aps The processors to create a composite of
098         * @since 1.5
099         */
100        public static AnnotationProcessor getCompositeAnnotationProcessor(AnnotationProcessor... aps) {
101            return new CompositeAnnotationProcessor(aps);
102        }
103    
104        /**
105         * Constructs a new composite annotation processor.  A composite
106         * annotation processor combines multiple annotation processors
107         * into one and functions by invoking each of its component
108         * processors' process methods in the sequence the processors are
109         * returned by the collection's iterator.
110         *
111         * @param aps A collection of processors to create a composite of
112         * @since 1.5
113         */
114        public static AnnotationProcessor getCompositeAnnotationProcessor(Collection<AnnotationProcessor> aps) {
115            return new CompositeAnnotationProcessor(aps);
116        }
117    }