001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.model.annotation;
038:
039: import java.lang.annotation.Annotation;
040: import java.lang.reflect.Field;
041: import java.lang.reflect.Method;
042:
043: import javax.xml.bind.annotation.XmlTransient;
044:
045: import com.sun.istack.Nullable;
046: import com.sun.xml.bind.v2.model.core.ErrorHandler;
047:
048: /**
049: * Reads annotations for the given property.
050: *
051: * <p>
052: * This is the lowest abstraction that encapsulates the difference
053: * between reading inline annotations and external binding files.
054: *
055: * <p>
056: * Because the former operates on a {@link Field} and {@link Method}
057: * while the latter operates on a "property", the methods defined
058: * on this interface takes both, and the callee gets to choose which
059: * to use.
060: *
061: * <p>
062: * Most of the get method takes {@link Locatable}, which points to
063: * the place/context in which the annotation is read. The returned
064: * annotation also implements {@link Locatable} (so that it can
065: * point to the place where the annotation is placed), and its
066: * {@link Locatable#getUpstream()} will return the given
067: * {@link Locatable}.
068: *
069: *
070: * <p>
071: * Errors found during reading annotations are reported through the error handler.
072: * A valid {@link ErrorHandler} must be registered before the {@link AnnotationReader}
073: * is used.
074: *
075: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
076: */
077: public interface AnnotationReader<T, C, F, M> {
078:
079: /**
080: * Sets the error handler that receives errors found
081: * during reading annotations.
082: *
083: * @param errorHandler
084: * must not be null.
085: */
086: void setErrorHandler(ErrorHandler errorHandler);
087:
088: /**
089: * Reads an annotation on a property that consists of a field.
090: */
091: <A extends Annotation> A getFieldAnnotation(Class<A> annotation,
092: F field, Locatable srcpos);
093:
094: /**
095: * Checks if the given field has an annotation.
096: */
097: boolean hasFieldAnnotation(
098: Class<? extends Annotation> annotationType, F field);
099:
100: /**
101: * Checks if a class has the annotation.
102: */
103: boolean hasClassAnnotation(C clazz,
104: Class<? extends Annotation> annotationType);
105:
106: /**
107: * Gets all the annotations on a field.
108: */
109: Annotation[] getAllFieldAnnotations(F field, Locatable srcPos);
110:
111: /**
112: * Reads an annotation on a property that consists of a getter and a setter.
113: *
114: */
115: <A extends Annotation> A getMethodAnnotation(Class<A> annotation,
116: M getter, M setter, Locatable srcpos);
117:
118: /**
119: * Checks if the given method has an annotation.
120: */
121: boolean hasMethodAnnotation(Class<? extends Annotation> annotation,
122: String propertyName, M getter, M setter, Locatable srcPos);
123:
124: /**
125: * Gets all the annotations on a method.
126: *
127: * @param srcPos
128: * the location from which this annotation is read.
129: */
130: Annotation[] getAllMethodAnnotations(M method, Locatable srcPos);
131:
132: // TODO: we do need this to read certain annotations,
133: // but that shows inconsistency wrt the spec. consult the spec team about the abstraction.
134: <A extends Annotation> A getMethodAnnotation(Class<A> annotation,
135: M method, Locatable srcpos);
136:
137: boolean hasMethodAnnotation(Class<? extends Annotation> annotation,
138: M method);
139:
140: /**
141: * Reads an annotation on a parameter of the method.
142: *
143: * @return null
144: * if the annotation was not found.
145: */
146: @Nullable
147: <A extends Annotation> A getMethodParameterAnnotation(
148: Class<A> annotation, M method, int paramIndex,
149: Locatable srcPos);
150:
151: /**
152: * Reads an annotation on a class.
153: */
154: @Nullable
155: <A extends Annotation> A getClassAnnotation(Class<A> annotation,
156: C clazz, Locatable srcpos);
157:
158: /**
159: * Reads an annotation on the package that the given class belongs to.
160: */
161: @Nullable
162: <A extends Annotation> A getPackageAnnotation(Class<A> annotation,
163: C clazz, Locatable srcpos);
164:
165: /**
166: * Reads a value of an annotation that returns a Class object.
167: *
168: * <p>
169: * Depending on the underlying reflection library, you can't always
170: * obtain the {@link Class} object directly (see the APT MirrorTypeException
171: * for example), so use this method to avoid that.
172: *
173: * @param name
174: * The name of the annotation parameter to be read.
175: */
176: T getClassValue(Annotation a, String name);
177:
178: /**
179: * Similar to {@link #getClassValue(Annotation, String)} method but
180: * obtains an array parameter.
181: */
182: T[] getClassArrayValue(Annotation a, String name);
183: }
|