001: /*
002: * @(#)AnnotationProcessors.java 1.2 04/06/21
003: *
004: * Copyright (c) 2004, Sun Microsystems, Inc.
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: * * Neither the name of the Sun Microsystems, Inc. nor the names of
016: * its contributors may be used to endorse or promote products derived from
017: * this software without specific prior written permission.
018: *
019: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
020: * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
021: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
022: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
023: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
024: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
025: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
026: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
027: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
028: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
029: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */
031:
032: package com.sun.mirror.apt;
033:
034: import java.util.*;
035:
036: /**
037: * Utilities to create specialized annotation processors.
038: *
039: * @since 1.5
040: * @author Joseph D. Darcy
041: * @author Scott Seligman
042: */
043: public class AnnotationProcessors {
044: static class NoOpAP implements AnnotationProcessor {
045: NoOpAP() {
046: }
047:
048: public void process() {
049: }
050: }
051:
052: /**
053: * Combines multiple annotation processors into a simple composite
054: * processor.
055: * The composite processor functions by invoking each of its component
056: * processors in sequence.
057: */
058: static class CompositeAnnotationProcessor implements
059: AnnotationProcessor {
060:
061: private List<AnnotationProcessor> aps = new LinkedList<AnnotationProcessor>();
062:
063: /**
064: * Constructs a new composite annotation processor.
065: * @param aps the component annotation processors
066: */
067: public CompositeAnnotationProcessor(
068: Collection<AnnotationProcessor> aps) {
069: this .aps.addAll(aps);
070: }
071:
072: /**
073: * Constructs a new composite annotation processor.
074: * @param aps the component annotation processors
075: */
076: public CompositeAnnotationProcessor(AnnotationProcessor... aps) {
077: for (AnnotationProcessor ap : aps)
078: this .aps.add(ap);
079: }
080:
081: /**
082: * Invokes the <tt>process</tt> method of each component processor,
083: * in the order in which the processors were passed to the constructor.
084: */
085: public void process() {
086: for (AnnotationProcessor ap : aps)
087: ap.process();
088: }
089: }
090:
091: /**
092: * An annotation processor that does nothing and has no state.
093: * May be used multiple times.
094: *
095: * @since 1.5
096: */
097: public final static AnnotationProcessor NO_OP = new NoOpAP();
098:
099: /**
100: * Constructs a new composite annotation processor. A composite
101: * annotation processor combines multiple annotation processors
102: * into one and functions by invoking each of its component
103: * processors' process methods in sequence.
104: *
105: * @param aps The processors to create a composite of
106: * @since 1.5
107: */
108: public static AnnotationProcessor getCompositeAnnotationProcessor(
109: AnnotationProcessor... aps) {
110: return new CompositeAnnotationProcessor(aps);
111: }
112:
113: /**
114: * Constructs a new composite annotation processor. A composite
115: * annotation processor combines multiple annotation processors
116: * into one and functions by invoking each of its component
117: * processors' process methods in the sequence the processors are
118: * returned by the collection's iterator.
119: *
120: * @param aps A collection of processors to create a composite of
121: * @since 1.5
122: */
123: public static AnnotationProcessor getCompositeAnnotationProcessor(
124: Collection<AnnotationProcessor> aps) {
125: return new CompositeAnnotationProcessor(aps);
126: }
127: }
|