1 /*
   2  * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package com.sun.mirror.apt;
  27 
  28 import com.sun.mirror.apt.*;
  29 import java.util.*;
  30 
  31 /**
  32  * Utilities to create specialized annotation processors.
  33  *
  34  * @since 1.5
  35  * @author Joseph D. Darcy
  36  * @author Scott Seligman
  37  */
  38 public class AnnotationProcessors {
  39     static class NoOpAP implements AnnotationProcessor {
  40         NoOpAP() {}
  41         public void process(){}
  42     }
  43 
  44     /**
  45      * Combines multiple annotation processors into a simple composite
  46      * processor.
  47      * The composite processor functions by invoking each of its component
  48      * processors in sequence.
  49      */
  50     static class CompositeAnnotationProcessor implements AnnotationProcessor {
  51 
  52         private List<AnnotationProcessor> aps =
  53             new LinkedList<AnnotationProcessor>();
  54 
  55         /**
  56          * Constructs a new composite annotation processor.
  57          * @param aps  the component annotation processors
  58          */
  59         public CompositeAnnotationProcessor(Collection<AnnotationProcessor> aps) {
  60             this.aps.addAll(aps);
  61         }
  62 
  63         /**
  64          * Constructs a new composite annotation processor.
  65          * @param aps  the component annotation processors
  66          */
  67         public CompositeAnnotationProcessor(AnnotationProcessor... aps) {
  68             for(AnnotationProcessor ap: aps)
  69                 this.aps.add(ap);
  70         }
  71 
  72         /**
  73          * Invokes the <tt>process</tt> method of each component processor,
  74          * in the order in which the processors were passed to the constructor.
  75          */
  76         public void process() {
  77             for(AnnotationProcessor ap: aps)
  78                 ap.process();
  79         }
  80     }
  81 
  82 
  83     /**
  84      *  An annotation processor that does nothing and has no state.
  85      *  May be used multiple times.
  86      *
  87      * @since 1.5
  88      */
  89     public final static AnnotationProcessor NO_OP = new NoOpAP();
  90 
  91     /**
  92      * Constructs a new composite annotation processor.  A composite
  93      * annotation processor combines multiple annotation processors
  94      * into one and functions by invoking each of its component
  95      * processors' process methods in sequence.
  96      *
  97      * @param aps The processors to create a composite of
  98      * @since 1.5
  99      */
 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 }