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.declaration;
  27 
  28 
  29 import java.lang.annotation.Annotation;
  30 import java.util.Collection;
  31 
  32 import com.sun.mirror.type.*;
  33 import com.sun.mirror.util.*;
  34 
  35 
  36 /**
  37  * Represents the declaration of a program element such as a package,
  38  * class, or method.  Each declaration represents a static, language-level
  39  * construct (and not, for example, a runtime construct of the virtual
  40  * machine), and typically corresponds one-to-one with a particular
  41  * fragment of source code.
  42  *
  43  * <p> Declarations should be compared using the {@link #equals(Object)}
  44  * method.  There is no guarantee that any particular declaration will
  45  * always be represented by the same object.
  46  *
  47  * @author Joseph D. Darcy
  48  * @author Scott Seligman
  49  *
  50  * @see Declarations
  51  * @see TypeMirror
  52  * @since 1.5
  53  */
  54 
  55 public interface Declaration {
  56 
  57     /**
  58      * Tests whether an object represents the same declaration as this.
  59      *
  60      * @param obj  the object to be compared with this declaration
  61      * @return <tt>true</tt> if the specified object represents the same
  62      *          declaration as this
  63      */
  64     boolean equals(Object obj);
  65 
  66     /**
  67      * Returns the text of the documentation ("javadoc") comment of
  68      * this declaration.
  69      *
  70      * @return the documentation comment of this declaration, or <tt>null</tt>
  71      *          if there is none
  72      */
  73     String getDocComment();
  74 
  75     /**
  76      * Returns the annotations that are directly present on this declaration.
  77      *
  78      * @return the annotations directly present on this declaration;
  79      *          an empty collection if there are none
  80      */
  81     Collection<AnnotationMirror> getAnnotationMirrors();
  82 
  83     /**
  84      * Returns the annotation of this declaration having the specified
  85      * type.  The annotation may be either inherited or directly
  86      * present on this declaration.
  87      *
  88      * <p> The annotation returned by this method could contain an element
  89      * whose value is of type <tt>Class</tt>.
  90      * This value cannot be returned directly:  information necessary to
  91      * locate and load a class (such as the class loader to use) is
  92      * not available, and the class might not be loadable at all.
  93      * Attempting to read a <tt>Class</tt> object by invoking the relevant
  94      * method on the returned annotation
  95      * will result in a {@link MirroredTypeException},
  96      * from which the corresponding {@link TypeMirror} may be extracted.
  97      * Similarly, attempting to read a <tt>Class[]</tt>-valued element
  98      * will result in a {@link MirroredTypesException}.
  99      *
 100      * <blockquote>
 101      * <i>Note:</i> This method is unlike
 102      * others in this and related interfaces.  It operates on run-time
 103      * reflective information -- representations of annotation types
 104      * currently loaded into the VM -- rather than on the mirrored
 105      * representations defined by and used throughout these
 106      * interfaces.  It is intended for callers that are written to
 107      * operate on a known, fixed set of annotation types.
 108      * </blockquote>
 109      *
 110      * @param <A>  the annotation type
 111      * @param annotationType  the <tt>Class</tt> object corresponding to
 112      *          the annotation type
 113      * @return the annotation of this declaration having the specified type
 114      *
 115      * @see #getAnnotationMirrors()
 116      */
 117     <A extends Annotation> A getAnnotation(Class<A> annotationType);
 118 
 119     /**
 120      * Returns the modifiers of this declaration, excluding annotations.
 121      * Implicit modifiers, such as the <tt>public</tt> and <tt>static</tt>
 122      * modifiers of interface members, are included.
 123      *
 124      * @return the modifiers of this declaration in undefined order;
 125      *          an empty collection if there are none
 126      */
 127     Collection<Modifier> getModifiers();
 128 
 129     /**
 130      * Returns the simple (unqualified) name of this declaration.
 131      * The name of a generic type does not include any reference
 132      * to its formal type parameters.
 133      * For example, the simple name of the interface declaration
 134      * {@code java.util.Set<E>} is <tt>"Set"</tt>.
 135      * If this declaration represents the empty package, an empty
 136      * string is returned.
 137      * If it represents a constructor, the simple name of its
 138      * declaring class is returned.
 139      *
 140      * @return the simple name of this declaration
 141      */
 142     String getSimpleName();
 143 
 144     /**
 145      * Returns the source position of the beginning of this declaration.
 146      * Returns <tt>null</tt> if the position is unknown or not applicable.
 147      *
 148      * <p> This source position is intended for use in providing
 149      * diagnostics, and indicates only approximately where a declaration
 150      * begins.
 151      *
 152      * @return the source position of the beginning of this declaration,
 153      *          or null if the position is unknown or not applicable
 154      */
 155     SourcePosition getPosition();
 156 
 157     /**
 158      * Applies a visitor to this declaration.
 159      *
 160      * @param v the visitor operating on this declaration
 161      */
 162     void accept(DeclarationVisitor v);
 163 }