src/share/sample/language/model/CoreReflectionFactory.java

Wed, 22 Jun 2016 08:52:25 -0700

author
asaha
date
Wed, 22 Jun 2016 08:52:25 -0700
changeset 3235
bc44f1f002c9
parent 1781
0f8e9a0e5d9a
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u102-b14 for changeset 0549bf2f507d

     1 /*
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     3  *
     4  * Redistribution and use in source and binary forms, with or without
     5  * modification, are permitted provided that the following conditions
     6  * are met:
     7  *
     8  *   - Redistributions of source code must retain the above copyright
     9  *     notice, this list of conditions and the following disclaimer.
    10  *
    11  *   - Redistributions in binary form must reproduce the above copyright
    12  *     notice, this list of conditions and the following disclaimer in the
    13  *     documentation and/or other materials provided with the distribution.
    14  *
    15  *   - Neither the name of Oracle nor the names of its
    16  *     contributors may be used to endorse or promote products derived
    17  *     from this software without specific prior written permission.
    18  *
    19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30  */
    32 import java.lang.annotation.Annotation;
    33 import javax.annotation.processing.SupportedSourceVersion;
    34 import javax.lang.model.element.*;
    35 import javax.lang.model.element.Modifier;
    36 import javax.lang.model.type.*;
    37 import javax.lang.model.util.*;
    38 import java.lang.reflect.*;
    39 import java.io.Writer;
    40 import java.util.*;
    42 import static javax.lang.model.SourceVersion.RELEASE_8;
    43 import static java.util.Objects.*;
    45 /**
    46  * This class provides a proof-of-concept implementation of the {@code
    47  * javax.lang.model.*} API backed by core reflection. That is, rather
    48  * than having a source file or compile-time class file as the
    49  * originator of the information about an element or type, as done
    50  * during standard annotation processing, runtime core reflection
    51  * objects serve that purpose instead.
    52  *
    53  * With this kind of implementation, the same logic can be used for
    54  * both compile-time and runtime processing of annotations.
    55  *
    56  * The nested types in this class define a specialization of {@code
    57  * javax.lang.model.*} to provide some additional functionality and
    58  * type information. The original {@code javax.lang.model.*} API was
    59  * designed to accommodate such a specialization by using wildcards in
    60  * the return types of methods.
    61  *
    62  * It would be technically possible for further specializations of the
    63  * API implemented in this class to define alternative semantics of
    64  * annotation look-up. For example to allow one annotation to have the
    65  * effect of macro-expanding into a set of other annotations.
    66  *
    67  * Some aspects of the implementation are left as "exercises for the
    68  * reader" to complete if interested.
    69  *
    70  * When passed null pointers, the methods defined in this type will
    71  * generally throw null pointer exceptions.
    72  *
    73  * To get started, first compile this file with a command line like:
    74  *
    75  * <pre>
    76  * $JDK/bin/javac -parameters -Xdoclint:all/public -Xlint:all -d $OUTPUT_DIR CoreReflectionFactory.java
    77  * </pre>
    78  *
    79  * and then run the main method of {@code CoreReflectionFactory},
    80  * which will print out a representation of {@code
    81  * CoreReflectionFactory}. To use the printing logic defined in {@code
    82  * javac}, put {@code tools.jar} on the classpath as in:
    83  *
    84  * <pre>
    85  * $JDK/bin/java -cp $OUTPUT_DIR:$JDK_ROOT/lib/tools.jar CoreReflectionFactory
    86  * </pre>
    87  *
    88  * @author Joseph D. Darcy (darcy)
    89  * @author Joel Borggren-Franck (jfranck)
    90  */
    91 public class CoreReflectionFactory {
    92     private CoreReflectionFactory() {
    93         throw new AssertionError("No instances of CoreReflectionFactory for you!");
    94     }
    96     /**
    97      * Returns a reflection type element mirroring a {@code Class} object.
    98      * @return a reflection type element mirroring a {@code Class} object
    99      * @param clazz the {@code Class} to mirror
   100      */
   101     public static ReflectionTypeElement createMirror(Class<?> clazz) {
   102         return new CoreReflTypeElement(Objects.requireNonNull(clazz));
   103     }
   105     /**
   106      * Returns a reflection package element mirroring a {@code Package} object.
   107      * @return a reflection package element mirroring a {@code Package} object
   108      * @param pkg the {@code Package} to mirror
   109      */
   110     public static ReflectionPackageElement createMirror(Package pkg) {
   111         // Treat a null pkg to mean an unnamed package.
   112         return new CoreReflPackageElement(pkg);
   113     }
   115     /**
   116      * Returns a reflection variable element mirroring a {@code Field} object.
   117      * @return a reflection variable element mirroring a {@code Field} object
   118      * @param field the {@code Field} to mirror
   119      */
   120     public static ReflectionVariableElement createMirror(Field field) {
   121         return new CoreReflFieldVariableElement(Objects.requireNonNull(field));
   122     }
   124     /**
   125      * Returns a reflection executable element mirroring a {@code Method} object.
   126      * @return a reflection executable element mirroring a {@code Method} object
   127      * @param method the {@code Method} to mirror
   128      */
   129     public static ReflectionExecutableElement createMirror(Method method)  {
   130         return new CoreReflMethodExecutableElement(Objects.requireNonNull(method));
   131     }
   133     /**
   134      * Returns a reflection executable element mirroring a {@code Constructor} object.
   135      * @return a reflection executable element mirroring a {@code Constructor} object
   136      * @param constructor the {@code Constructor} to mirror
   137      */
   138     public static ReflectionExecutableElement createMirror(Constructor<?> constructor)  {
   139         return new CoreReflConstructorExecutableElement(Objects.requireNonNull(constructor));
   140     }
   142     /**
   143      * Returns a type parameter element mirroring a {@code TypeVariable} object.
   144      * @return a type parameter element mirroring a {@code TypeVariable} object
   145      * @param tv the {@code TypeVariable} to mirror
   146      */
   147     public static TypeParameterElement createMirror(java.lang.reflect.TypeVariable<?> tv) {
   148         return new CoreReflTypeParameterElement(Objects.requireNonNull(tv));
   149     }
   151     /**
   152      * Returns a variable element mirroring a {@code Parameter} object.
   153      * @return a variable element mirroring a {@code Parameter} object
   154      * @param p the {Parameter} to mirror
   155      */
   156     public static VariableElement createMirror(java.lang.reflect.Parameter p) {
   157         return new CoreReflParameterVariableElement(Objects.requireNonNull(p));
   158     }
   160     /**
   161      * Returns an annotation mirror mirroring an annotation object.
   162      * @return an annotation mirror mirroring an annotation object
   163      * @param annotation the annotation to mirror
   164      */
   165     public static AnnotationMirror createMirror(Annotation annotation)  {
   166         return new CoreReflAnnotationMirror(Objects.requireNonNull(annotation));
   167     }
   169     /**
   170      * Returns a {@code Types} utility object for type objects backed by core reflection.
   171      * @return a {@code Types} utility object for type objects backed by core reflection
   172      */
   173     public static Types getTypes() {
   174         return CoreReflTypes.instance();
   175     }
   177     /**
   178      * Returns an {@code Elements} utility object for type objects backed by core reflection.
   179      * @return an {@code Elements} utility object for type objects backed by core reflection
   180      */
   181     public static Elements getElements() {
   182         return CoreReflElements.instance();
   183     }
   185     // Helper
   186     private static TypeMirror createTypeMirror(Class<?> c) {
   187         return TypeFactory.instance(Objects.requireNonNull(c));
   188     }
   190     /**
   191      * Main method; prints out a representation of this class.
   192      * @param args command-line arguments, currently ignored
   193      */
   194     public static void main(String... args) {
   195         getElements().printElements(new java.io.PrintWriter(System.out),
   196                                     createMirror(CoreReflectionFactory.class));
   197     }
   199     /**
   200      * A specialization of {@code javax.lang.model.element.Element} that is
   201      * backed by core reflection.
   202      */
   203     public static interface ReflectionElement
   204         extends Element, AnnotatedElement {
   206         /**
   207          * {@inheritDoc}
   208          */
   209         @Override
   210         ReflectionElement getEnclosingElement();
   212         /**
   213          * {@inheritDoc}
   214          */
   215         @Override
   216         List<ReflectionElement> getEnclosedElements();
   218         /**
   219          * Applies a visitor to this element.
   220          *
   221          * @param v the visitor operating on this element
   222          * @param p additional parameter to the visitor
   223          * @param <R> the return type of the visitor's methods
   224          * @param <P> the type of the additional parameter to the visitor's methods
   225          * @return a visitor-specified result
   226          */
   227         <R,P> R accept(ReflectionElementVisitor<R,P> v, P p);
   229         // Functionality specific to the specialization
   230         /**
   231          * Returns the underlying core reflection source object, if applicable.
   232          * @return the underlying core reflection source object, if applicable
   233          */
   234         AnnotatedElement getSource();
   236         // Functionality from javax.lang.model.util.Elements
   237         /**
   238          * Returns the package of an element. The package of a package
   239          * is itself.
   240          * @return the package of an element
   241          */
   242         ReflectionPackageElement getPackage();
   244     }
   246     /**
   247      * A logical specialization of {@code
   248      * javax.lang.model.element.ElementVisitor} being backed by core
   249      * reflection.
   250      *
   251      * @param <R> the return type of this visitor's methods.
   252      * @param <P> the type of the additional parameter to this visitor's
   253      *            methods.
   254      */
   255     public static interface ReflectionElementVisitor<R, P> {
   256         /**
   257          * Visits an element.
   258          * @param e  the element to visit
   259          * @param p  a visitor-specified parameter
   260          * @return a visitor-specified result
   261          */
   262         R visit(ReflectionElement e, P p);
   264         /**
   265          * A convenience method equivalent to {@code v.visit(e, null)}.
   266          * @param e  the element to visit
   267          * @return a visitor-specified result
   268          */
   269         R visit(ReflectionElement e);
   271         /**
   272          * Visits a package element.
   273          * @param e  the element to visit
   274          * @param p  a visitor-specified parameter
   275          * @return a visitor-specified result
   276          */
   277         R visitPackage(ReflectionPackageElement e, P p);
   279         /**
   280          * Visits a type element.
   281          * @param e  the element to visit
   282          * @param p  a visitor-specified parameter
   283          * @return a visitor-specified result
   284          */
   285         R visitType(ReflectionTypeElement e, P p);
   287         /**
   288          * Visits a variable element.
   289          * @param e  the element to visit
   290          * @param p  a visitor-specified parameter
   291          * @return a visitor-specified result
   292          */
   293         R visitVariable(ReflectionVariableElement e, P p);
   295         /**
   296          * Visits an executable element.
   297          * @param e  the element to visit
   298          * @param p  a visitor-specified parameter
   299          * @return a visitor-specified result
   300          */
   301         R visitExecutable(ReflectionExecutableElement e, P p);
   303         /**
   304          * Visits a type parameter element.
   305          * @param e  the element to visit
   306          * @param p  a visitor-specified parameter
   307          * @return a visitor-specified result
   308          */
   309         R visitTypeParameter(ReflectionTypeParameterElement e, P p);
   311         /**
   312          * Visits an unknown kind of element.
   313          * This can occur if the language evolves and new kinds
   314          * of elements are added to the {@code Element} hierarchy.
   315          *
   316          * @param e  the element to visit
   317          * @param p  a visitor-specified parameter
   318          * @return a visitor-specified result
   319          * @throws UnknownElementException
   320          * a visitor implementation may optionally throw this exception
   321          */
   322         R visitUnknown(ReflectionElement e, P p);
   323     }
   325     /**
   326      * A specialization of {@code javax.lang.model.element.ExecutableElement} that is
   327      * backed by core reflection.
   328      */
   329     public static interface ReflectionExecutableElement
   330         extends ReflectionElement, ExecutableElement, ReflectionParameterizable {
   332         /**
   333          * {@inheritDoc}
   334          */
   335         @Override
   336         List<ReflectionTypeParameterElement> getTypeParameters();
   338         /**
   339          * {@inheritDoc}
   340          */
   341         @Override
   342         List<ReflectionVariableElement> getParameters();
   344         // Functionality specific to the specialization
   345         /**
   346          * Returns all parameters, including synthetic ones.
   347          * @return all parameters, including synthetic ones
   348          */
   349         List<ReflectionVariableElement> getAllParameters();
   351         /**
   352          * {@inheritDoc}
   353          */
   354         @Override
   355         Executable getSource();
   357         /**
   358          * Returns true if this executable is a synthetic construct; returns false otherwise.
   359          * @return true if this executable is a synthetic construct; returns false otherwise
   360          */
   361         boolean isSynthetic();
   363         /**
   364          * Returns true if this executable is a bridge method; returns false otherwise.
   365          * @return true if this executable is a bridge method; returns false otherwise
   366          */
   367         boolean isBridge();
   368     }
   370     /**
   371      * A specialization of {@code javax.lang.model.element.PackageElement} being
   372      * backed by core reflection.
   373      */
   374     public static interface ReflectionPackageElement
   375         extends ReflectionElement, PackageElement {
   377         // Functionality specific to the specialization
   378         /**
   379          * {@inheritDoc}
   380          */
   381         @Override
   382         Package getSource();
   383     }
   385     /**
   386      * A specialization of {@code javax.lang.model.element.TypeElement} that is
   387      * backed by core reflection.
   388      */
   389     public static interface ReflectionTypeElement
   390         extends ReflectionElement, TypeElement, ReflectionParameterizable {
   392         /**
   393          * {@inheritDoc}
   394          */
   395         @Override
   396         List<ReflectionTypeParameterElement> getTypeParameters();
   398         /**
   399          * {@inheritDoc}
   400          */
   401         @Override
   402         List<ReflectionElement> getEnclosedElements();
   404         // Methods specific to the specialization, but functionality
   405         // also present in javax.lang.model.util.Elements.
   406         /**
   407          * Returns all members of a type element, whether inherited or
   408          * declared directly. For a class the result also includes its
   409          * constructors, but not local or anonymous classes.
   410          * @return all members of the type
   411          */
   412         List<ReflectionElement> getAllMembers();
   414         /**
   415          * Returns the binary name of a type element.
   416          * @return the binary name of a type element
   417          */
   418         Name getBinaryName();
   420         // Functionality specific to the specialization
   421         /**
   422          * {@inheritDoc}
   423          */
   424         @Override
   425         Class<?> getSource();
   426     }
   428     /**
   429      * A specialization of {@code javax.lang.model.element.TypeParameterElement} being
   430      * backed by core reflection.
   431      */
   432     public static interface ReflectionTypeParameterElement
   433         extends ReflectionElement, TypeParameterElement {
   435         /**
   436          * {@inheritDoc}
   437          */
   438         @Override
   439         ReflectionElement getGenericElement();
   441         // Functionality specific to the specialization
   442         /**
   443          * {@inheritDoc}
   444          */
   445         @Override
   446         java.lang.reflect.TypeVariable<?> getSource();
   447     }
   449     /**
   450      * A specialization of {@code javax.lang.model.element.VariableElement} that is
   451      * backed by core reflection.
   452      */
   453     public static interface ReflectionVariableElement
   454         extends ReflectionElement, VariableElement {
   456         // Functionality specific to the specialization
   457         /**
   458          * Returns true if this variable is a synthetic construct; returns false otherwise.
   459          * @return true if this variable is a synthetic construct; returns false otherwise
   460          */
   461         boolean isSynthetic();
   463         /**
   464          * Returns true if this variable is implicitly declared in source code; returns false otherwise.
   465          * @return true if this variable is implicitly declared in source code; returns false otherwise
   466          */
   467         boolean isImplicit();
   469         // The VariableElement concept covers fields, variables, and
   470         // method and constructor parameters. Therefore, this
   471         // interface cannot define a more precise override of
   472         // getSource since those three concept have different core
   473         // reflection types with no supertype more precise than
   474         // AnnotatedElement.
   475     }
   477     /**
   478      * A specialization of {@code javax.lang.model.element.Parameterizable} being
   479      * backed by core reflection.
   480      */
   481     public static interface ReflectionParameterizable
   482         extends ReflectionElement, Parameterizable {
   483         @Override
   484         List<ReflectionTypeParameterElement> getTypeParameters();
   485     }
   487     /**
   488      * Base class for concrete visitors of elements backed by core reflection.
   489      */
   490     public static abstract class AbstractReflectionElementVisitor8<R, P>
   491         extends AbstractElementVisitor8<R, P>
   492         implements ReflectionElementVisitor<R, P> {
   493         protected AbstractReflectionElementVisitor8() {
   494             super();
   495         }
   496     }
   498     /**
   499      * Base class for simple visitors of elements that are backed by core reflection.
   500      */
   501     @SupportedSourceVersion(value=RELEASE_8)
   502     public static abstract class SimpleReflectionElementVisitor8<R, P>
   503         extends SimpleElementVisitor8<R, P>
   504         implements ReflectionElementVisitor<R, P> {
   506         protected SimpleReflectionElementVisitor8(){
   507             super();
   508         }
   510         protected SimpleReflectionElementVisitor8(R defaultValue) {
   511             super(defaultValue);
   512         }
   514         // Create manual "bridge methods" for now.
   516         @Override
   517         public final R visitPackage(PackageElement e, P p) {
   518             return visitPackage((ReflectionPackageElement) e , p);
   519         }
   521         @Override
   522         public final R visitType(TypeElement e, P p) {
   523             return visitType((ReflectionTypeElement) e , p);
   524         }
   526         @Override
   527         public final R visitVariable(VariableElement e, P p) {
   528             return visitVariable((ReflectionVariableElement) e , p);
   529         }
   531         @Override
   532         public final R visitExecutable(ExecutableElement e, P p) {
   533             return visitExecutable((ReflectionExecutableElement) e , p);
   534         }
   536         @Override
   537         public final R visitTypeParameter(TypeParameterElement e, P p) {
   538             return visitTypeParameter((ReflectionTypeParameterElement) e , p);
   539         }
   540     }
   542     /**
   543      * {@inheritDoc}
   544      */
   545     public static interface ReflectionElements  extends Elements {
   546         /**
   547          * Returns the innermost enclosing {@link ReflectionTypeElement}
   548          * of the {@link ReflectionElement} or {@code null} if the
   549          * supplied ReflectionElement is toplevel or represents a
   550          * Package.
   551          *
   552          * @param e the {@link ReflectionElement} whose innermost
   553          * enclosing {@link ReflectionTypeElement} is sought
   554          * @return the innermost enclosing {@link
   555          * ReflectionTypeElement} or @{code null} if the parameter
   556          * {@code e} is a toplevel element or a package
   557          */
   558         ReflectionTypeElement getEnclosingTypeElement(ReflectionElement e);
   560         /**
   561          * {@inheritDoc}
   562          */
   563         @Override
   564         List<? extends ReflectionElement> getAllMembers(TypeElement type);
   566         /**
   567          * {@inheritDoc}
   568          */
   569         @Override
   570         ReflectionPackageElement getPackageElement(CharSequence name);
   572         /**
   573          * {@inheritDoc}
   574          */
   575         @Override
   576         ReflectionPackageElement getPackageOf(Element type);
   578         /**
   579          * {@inheritDoc}
   580          */
   581         @Override
   582         ReflectionTypeElement getTypeElement(CharSequence name);
   583     }
   585     // ------------------------- Implementation classes ------------------------
   587     // Exercise for the reader: review the CoreReflElement class
   588     // hierarchy below with an eye toward exposing it as an extensible
   589     // API that could be subclassed to provide customized behavior,
   590     // such as alternate annotation lookup semantics.
   592     private static abstract class CoreReflElement
   593         implements ReflectionElement, AnnotatedElement {
   594         public abstract AnnotatedElement getSource();
   596         protected CoreReflElement() {
   597             super();
   598         }
   600         // ReflectionElement methods
   601         @Override
   602         public ReflectionPackageElement getPackage() {
   603             throw new UnsupportedOperationException();
   604         }
   606         @Override
   607         public TypeMirror asType() {
   608             throw new UnsupportedOperationException(getClass().toString());
   609         }
   611         @Override
   612         public List<? extends AnnotationMirror> getAnnotationMirrors() {
   613             Annotation[] annotations = getSource().getDeclaredAnnotations();
   614             int len = annotations.length;
   616             if (len > 0) {
   617                 List<AnnotationMirror> res = new ArrayList<>(len);
   618                 for (Annotation a : annotations) {
   619                     res.add(createMirror(a));
   620                 }
   621                 return Collections.unmodifiableList(res);
   622             } else {
   623                 return Collections.emptyList();
   624             }
   625         }
   627         @Override
   628         public Set<Modifier> getModifiers() {
   629             return ModifierUtil.instance(0, false);
   630         }
   632         @Override
   633         public abstract Name getSimpleName();
   635         @Override
   636         public abstract ReflectionElement getEnclosingElement();
   638         @Override
   639         public abstract List<ReflectionElement> getEnclosedElements();
   641         //AnnotatedElement methods
   642         @Override
   643         public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
   644             return getSource().getAnnotation(annotationClass);
   645         }
   647         @Override
   648         public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
   649             return getSource().getAnnotationsByType(annotationClass);
   650         }
   652         @Override
   653         public Annotation[] getAnnotations() {
   654             return getSource().getAnnotations();
   655         }
   657         @Override
   658         public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
   659             return getSource().getDeclaredAnnotation(annotationClass);
   660         }
   662         @Override
   663         public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
   664             return getSource().getDeclaredAnnotationsByType(annotationClass);
   665         }
   667         @Override
   668         public Annotation[] getDeclaredAnnotations() {
   669             return getSource().getDeclaredAnnotations();
   670         }
   672         // java.lang.Object methods
   673         @Override
   674         public boolean equals(Object obj) {
   675             if (obj instanceof CoreReflElement) {
   676                 CoreReflElement other = (CoreReflElement)obj;
   677                 return Objects.equals(other.getSource(), this.getSource());
   678             }
   679             return false;
   680         }
   682         @Override
   683         public int hashCode() {
   684             return Objects.hashCode(getSource());
   685         }
   687         @Override
   688         public String toString() {
   689             return getKind().toString() + " " + getSimpleName().toString();
   690         }
   691     }
   693     // Type
   694     private static class CoreReflTypeElement extends CoreReflElement
   695         implements ReflectionTypeElement {
   696         private final Class<?> source;
   698         protected CoreReflTypeElement(Class<?> source) {
   699             Objects.requireNonNull(source);
   700             if (source.isPrimitive() ||
   701                 source.isArray()) {
   702                 throw new IllegalArgumentException("Cannot create a ReflectionTypeElement based on class: " + source);
   703             }
   705             this.source = source;
   706         }
   708         @Override
   709         public TypeMirror asType() {
   710             return createTypeMirror(source);
   711         }
   713         @Override
   714         public Class<?> getSource() {
   715             return source;
   716         }
   718         @Override
   719         public boolean equals(Object o) {
   720             if (o instanceof CoreReflTypeElement) {
   721                 return source.equals(((CoreReflTypeElement)o).getSource());
   722             } else {
   723                 return false;
   724             }
   725         }
   727         @Override
   728         public <R,P> R accept(ElementVisitor<R,P> v, P p) {
   729             return v.visitType(this, p);
   730         }
   732         @Override
   733         public <R,P> R accept(ReflectionElementVisitor<R,P> v, P p) {
   734             return v.visitType(this, p);
   735         }
   737         @Override
   738         public Set<Modifier> getModifiers() {
   739             return ModifierUtil.instance(source.getModifiers() &
   740                                          (source.isInterface() ?
   741                                           java.lang.reflect.Modifier.interfaceModifiers() :
   742                                           java.lang.reflect.Modifier.classModifiers()),
   743                                          false);
   744         }
   746         @Override
   747         public List<ReflectionElement> getEnclosedElements() {
   748             List<ReflectionElement> enclosedElements = new ArrayList<>();
   750             for (Class<?> declaredClass : source.getDeclaredClasses()) {
   751                 enclosedElements.add(createMirror(declaredClass));
   752             }
   754             // Add elements in the conventional ordering: fields, then
   755             // constructors, then methods.
   756             for (Field f : source.getDeclaredFields()) {
   757                 enclosedElements.add(createMirror(f));
   758             }
   760             for (Constructor<?> c : source.getDeclaredConstructors()) {
   761                 enclosedElements.add(createMirror(c));
   762             }
   764             for (Method m : source.getDeclaredMethods()) {
   765                 enclosedElements.add(createMirror(m));
   766             }
   768             return (enclosedElements.isEmpty() ?
   769                     Collections.emptyList():
   770                     Collections.unmodifiableList(enclosedElements));
   771         }
   773         // Review for default method handling.
   774         @Override
   775         public List<ReflectionElement> getAllMembers() {
   776             List<ReflectionElement> allMembers = new ArrayList<>();
   778             // If I only had a MultiMap ...
   779             List<ReflectionElement> fields = new ArrayList<>();
   780             List<ReflectionExecutableElement> methods = new ArrayList<>();
   781             List<ReflectionElement> classes = new ArrayList<>();
   783             // Add all fields for this class
   784             for (Field f : source.getDeclaredFields()) {
   785                 fields.add(createMirror(f));
   786             }
   788             // Add all methods for this class
   789             for (Method m : source.getDeclaredMethods()) {
   790                 methods.add(createMirror(m));
   791             }
   793             // Add all classes for this class, except anonymous/local as per Elements.getAllMembers doc
   794             for (Class<?> c : source.getDeclaredClasses()) {
   795                 if (c.isLocalClass() || c.isAnonymousClass())
   796                     continue;
   797                 classes.add(createMirror(c));
   798             }
   800             Class<?> cls = source;
   801             if (cls.isInterface()) {
   802                 cls = null;
   803             }
   804             do {
   805                 // Walk up superclasses adding non-private elements.
   806                 // If source is an interface, just add Object's
   807                 // elements.
   809                 if (cls == null) {
   810                     cls = java.lang.Object.class;
   811                 } else {
   812                     cls = cls.getSuperclass();
   813                 }
   815                 addMembers(cls, fields, methods, classes);
   817             } while (cls != java.lang.Object.class);
   819             // add members on (super)interface(s)
   820             Set<Class<?>> seenInterfaces = new HashSet<>();
   821             Queue<Class<?>> interfaces = new LinkedList<>();
   822             if (source.isInterface()) {
   823                 seenInterfaces.add(source);
   824                 interfaces.add(source);
   825             } else {
   826                 Class<?>[] ifaces = source.getInterfaces();
   827                 for (Class<?> iface : ifaces) {
   828                     seenInterfaces.add(iface);
   829                     interfaces.add(iface);
   830                 }
   831             }
   833             while (interfaces.peek() != null) {
   834                 Class<?> head = interfaces.remove();
   835                 addMembers(head, fields, methods, classes);
   837                 Class<?>[] ifaces = head.getInterfaces();
   838                 for (Class<?> iface : ifaces) {
   839                     if (!seenInterfaces.contains(iface)) {
   840                         seenInterfaces.add(iface);
   841                         interfaces.add(iface);
   842                     }
   843                 }
   844             }
   846             // Add constructors
   847             for (Constructor<?> c : source.getDeclaredConstructors()) {
   848                 allMembers.add(createMirror(c));
   849             }
   851             // Add all unique methods
   852             allMembers.addAll(methods);
   854             // Add all unique fields
   855             allMembers.addAll(fields);
   857             // Add all unique classes
   858             allMembers.addAll(classes);
   860             return Collections.unmodifiableList(allMembers);
   861         }
   863         private void addMembers(Class<?> cls,
   864                                 List<ReflectionElement> fields,
   865                                 List<ReflectionExecutableElement> methods,
   866                                 List<ReflectionElement> classes) {
   867             Elements elements = getElements();
   869             for (Field f : cls.getDeclaredFields()) {
   870                 if (java.lang.reflect.Modifier.isPrivate(f.getModifiers())) { continue; }
   871                 ReflectionElement tmp = createMirror(f);
   872                 boolean add = true;
   873                 for (ReflectionElement e : fields) {
   874                     if (elements.hides(e, tmp)) {
   875                         add = false;
   876                         break;
   877                     }
   878                 }
   879                 if (add) {
   880                     fields.add(tmp);
   881                 }
   882             }
   884             for (Method m : cls.getDeclaredMethods()) {
   885                 if (java.lang.reflect.Modifier.isPrivate(m.getModifiers()))
   886                     continue;
   888                 ReflectionExecutableElement tmp = createMirror(m);
   889                 boolean add = true;
   890                 for (ReflectionExecutableElement e : methods) {
   891                     if (elements.hides(e, tmp)) {
   892                         add = false;
   893                         break;
   894                     } else if (elements.overrides(e, tmp, this)) {
   895                         add = false;
   896                         break;
   897                     }
   898                 }
   899                 if (add) {
   900                     methods.add(tmp);
   901                 }
   902             }
   904             for (Class<?> c : cls.getDeclaredClasses()) {
   905                 if (java.lang.reflect.Modifier.isPrivate(c.getModifiers()) ||
   906                     c.isLocalClass() ||
   907                     c.isAnonymousClass())
   908                     continue;
   910                 ReflectionElement tmp = createMirror(c);
   911                 boolean add = true;
   912                 for (ReflectionElement e : classes) {
   913                     if (elements.hides(e, tmp)) {
   914                         add = false;
   915                         break;
   916                     }
   917                 }
   918                 if (add) {
   919                     classes.add(tmp);
   920                 }
   921             }
   922         }
   924         @Override
   925         public ElementKind getKind() {
   926             if (source.isInterface()) {
   927                 if (source.isAnnotation())
   928                     return ElementKind.ANNOTATION_TYPE;
   929                 else
   930                     return ElementKind.INTERFACE;
   931             } else if (source.isEnum()) {
   932                 return ElementKind.ENUM;
   933             } else
   934                 return ElementKind.CLASS;
   935         }
   937         @Override
   938         public NestingKind getNestingKind() {
   939             if (source.isAnonymousClass())
   940                 return NestingKind.ANONYMOUS;
   941             else if (source.isLocalClass())
   942                 return NestingKind.LOCAL;
   943             else if (source.isMemberClass())
   944                 return NestingKind.MEMBER;
   945             else return
   946                 NestingKind.TOP_LEVEL;
   947         }
   949         @Override
   950         public Name getQualifiedName() {
   951             String name = source.getCanonicalName(); // TODO, this should be a FQN for
   952                                                      // the current element
   953             if (name == null)
   954                 name = "";
   955             return StringName.instance(name);
   956         }
   958         @Override
   959         public Name getSimpleName() {
   960             return StringName.instance(source.getSimpleName());
   961         }
   963         @Override
   964         public TypeMirror getSuperclass() {
   965             if (source.equals(java.lang.Object.class)) {
   966                 return NoType.getNoneInstance();
   967             } else {
   968                 return createTypeMirror(source.getSuperclass());
   969             }
   970         }
   972         @Override
   973         public List<? extends TypeMirror> getInterfaces() {
   974             Class[] interfaces = source.getInterfaces();
   975             int len = interfaces.length;
   976             List<TypeMirror> res = new ArrayList<>(len);
   978             if (len > 0) {
   979                 for (Class<?> c : interfaces) {
   980                     res.add(createTypeMirror(c));
   981                 }
   982             } else {
   983                 return Collections.emptyList();
   984             }
   985             return Collections.unmodifiableList(res);
   986         }
   988         @Override
   989         public List<ReflectionTypeParameterElement> getTypeParameters() {
   990             return createTypeParameterList(source);
   991         }
   993         @Override
   994         public ReflectionElement getEnclosingElement() {
   995             // Returns the package of a top-level type and returns the
   996             // immediately lexically enclosing element for a nested type.
   998             switch(getNestingKind()) {
   999             case TOP_LEVEL:
  1000                 return createMirror(source.getPackage());
  1001             case MEMBER:
  1002                 return createMirror(source.getEnclosingClass());
  1003             default:
  1004                 if (source.getEnclosingConstructor() != null) {
  1005                     return createMirror(source.getEnclosingConstructor());
  1006                 } else if (source.getEnclosingMethod() != null) {
  1007                     return createMirror(source.getEnclosingMethod());
  1008                 } else {
  1009                     return createMirror(source.getEnclosingClass());
  1014         @Override
  1015         public Name getBinaryName() {
  1016             return StringName.instance(getSource().getName());
  1020     private static abstract class CoreReflExecutableElement extends CoreReflElement
  1021         implements ReflectionExecutableElement {
  1023         protected Executable source = null;
  1024         protected final List<CoreReflParameterVariableElement> parameters;
  1026         protected CoreReflExecutableElement(Executable source,
  1027                                             List<CoreReflParameterVariableElement> parameters) {
  1028             this.source = Objects.requireNonNull(source);
  1029             this.parameters = Objects.requireNonNull(parameters);
  1032         @Override
  1033         public <R,P> R accept(ElementVisitor<R,P> v, P p) {
  1034             return v.visitExecutable(this, p);
  1037         @Override
  1038         public <R,P> R accept(ReflectionElementVisitor<R,P> v, P p) {
  1039             return v.visitExecutable(this, p);
  1042         @Override
  1043         public abstract ExecutableType asType();
  1045         // Only Types and Packages enclose elements; see Element.getEnclosedElements()
  1046         @Override
  1047         public List<ReflectionElement> getEnclosedElements() {
  1048             return Collections.emptyList();
  1051         @Override
  1052         public List<ReflectionVariableElement> getParameters() {
  1053             List<ReflectionVariableElement> tmp = new ArrayList<>();
  1054             for (ReflectionVariableElement parameter : parameters) {
  1055                 if (!parameter.isSynthetic())
  1056                     tmp.add(parameter);
  1058             return tmp;
  1061         @Override
  1062         public List<ReflectionVariableElement> getAllParameters() {
  1063             // Could "fix" this if the return type included wildcards
  1064             @SuppressWarnings("unchecked")
  1065             List<ReflectionVariableElement> tmp = (List<ReflectionVariableElement>)(List)parameters;
  1066             return tmp;
  1069         @Override
  1070         public List<? extends TypeMirror> getThrownTypes() {
  1071             Class<?>[] thrown = source.getExceptionTypes();
  1072             int len = thrown.length;
  1073             List<TypeMirror> res = new ArrayList<>(len);
  1075             if (len > 0) {
  1076                 for (Class<?> c : thrown) {
  1077                     res.add(createTypeMirror(c));
  1079             } else {
  1080                 return Collections.emptyList();
  1082             return Collections.unmodifiableList(res);
  1085         @Override
  1086         public boolean isVarArgs() {
  1087             return source.isVarArgs();
  1090         @Override
  1091         public boolean isSynthetic() {
  1092             return source.isSynthetic();
  1095         @Override
  1096         public boolean isBridge() {
  1097             return false;
  1100         @Override
  1101         public List<ReflectionTypeParameterElement> getTypeParameters() {
  1102             return createTypeParameterList(source);
  1105         public abstract AnnotationValue getDefaultValue();
  1107         @Override
  1108         public TypeMirror getReceiverType() {
  1109             // New in JDK 8
  1110             throw new UnsupportedOperationException(this.toString());
  1114     private static class CoreReflConstructorExecutableElement
  1115         extends CoreReflExecutableElement {
  1117         protected CoreReflConstructorExecutableElement(Constructor<?> source) {
  1118             super(Objects.requireNonNull(source),
  1119                   createParameterList(source));
  1122         @Override
  1123         public  Constructor<?> getSource() {
  1124             return (Constructor<?>)source;
  1127         @Override
  1128         public TypeMirror getReturnType() {
  1129             return NoType.getVoidInstance();
  1132         @Override
  1133         public ExecutableType asType() {
  1134             throw new UnsupportedOperationException(getClass().toString());
  1137         @Override
  1138         public boolean equals(Object o) {
  1139             if (o instanceof CoreReflConstructorExecutableElement) {
  1140                 return source.equals(((CoreReflConstructorExecutableElement)o).getSource());
  1141             } else {
  1142                 return false;
  1146         @Override
  1147         public ElementKind getKind() {
  1148             return ElementKind.CONSTRUCTOR;
  1151         @Override
  1152         public Set<Modifier> getModifiers() {
  1153             return ModifierUtil.instance(source.getModifiers() &
  1154                                          java.lang.reflect.Modifier.constructorModifiers(), false);
  1157         @Override
  1158         public ReflectionElement getEnclosingElement() {
  1159             return createMirror(source.getDeclaringClass());
  1162         @Override
  1163         public Name getSimpleName() {
  1164             return StringName.instance("<init>");
  1167         @Override
  1168         public AnnotationValue getDefaultValue() {
  1169             // a constructor is never an annotation element
  1170             return null;
  1173         @Override
  1174         public boolean isDefault() {
  1175             return false; // A constructor cannot be a default method
  1179     private static class CoreReflMethodExecutableElement
  1180         extends CoreReflExecutableElement {
  1182         protected CoreReflMethodExecutableElement(Method source) {
  1183             super(Objects.requireNonNull(source),
  1184                   createParameterList(source));
  1185             this.source = source;
  1188         @Override
  1189         public Method getSource() {
  1190             return (Method)source;
  1193         @Override
  1194         public TypeMirror getReturnType() {
  1195             return TypeFactory.instance(getSource().getReturnType());
  1198         @Override
  1199         public boolean equals(Object o) {
  1200             if (o instanceof CoreReflMethodExecutableElement) {
  1201                 return source.equals( ((CoreReflMethodExecutableElement)o).getSource());
  1202             } else {
  1203                 return false;
  1207         @Override
  1208         public ElementKind getKind() {
  1209             return ElementKind.METHOD;
  1212         @Override
  1213         public Set<Modifier> getModifiers() {
  1214             return ModifierUtil.instance(source.getModifiers() &
  1215                                          java.lang.reflect.Modifier.methodModifiers(),
  1216                                          isDefault());
  1219         @Override
  1220         public ReflectionElement getEnclosingElement() {
  1221             return createMirror(source.getDeclaringClass());
  1224         @Override
  1225         public Name getSimpleName() {
  1226             return StringName.instance(source.getName());
  1229         @Override
  1230         public AnnotationValue getDefaultValue() {
  1231             Object value = getSource().getDefaultValue();
  1232             if (null == value) {
  1233                 return null;
  1234             } else {
  1235                 return new CoreReflAnnotationValue(value);
  1239         @Override
  1240         public boolean isDefault() {
  1241             return getSource().isDefault();
  1244         @Override
  1245         public boolean isBridge() {
  1246             return getSource().isBridge();
  1249         @Override
  1250         public ExecutableType asType() {
  1251             return TypeFactory.instance(getSource());
  1255     private static List<CoreReflParameterVariableElement> createParameterList(Executable source) {
  1256         Parameter[] parameters = source.getParameters();
  1257         int length = parameters.length;
  1258         if (length == 0)
  1259             return Collections.emptyList();
  1260         else {
  1261             List<CoreReflParameterVariableElement> tmp = new ArrayList<>(length);
  1262             for (Parameter parameter : parameters) {
  1263                 tmp.add(new CoreReflParameterVariableElement(parameter));
  1265             return Collections.unmodifiableList(tmp);
  1269     private static List<ReflectionTypeParameterElement> createTypeParameterList(GenericDeclaration source) {
  1270         java.lang.reflect.TypeVariable<?>[] typeParams = source.getTypeParameters();
  1271         int length = typeParams.length;
  1272         if (length == 0)
  1273             return Collections.emptyList();
  1274         else {
  1275             List<ReflectionTypeParameterElement> tmp = new ArrayList<>(length);
  1276             for (java.lang.reflect.TypeVariable<?> typeVar : typeParams)
  1277                 tmp.add(new CoreReflTypeParameterElement(typeVar));
  1278             return Collections.unmodifiableList(tmp);
  1282     private static class CoreReflTypeParameterElement
  1283         extends CoreReflElement
  1284         implements ReflectionTypeParameterElement {
  1286         private final GenericDeclaration source;
  1287         private final java.lang.reflect.TypeVariable<?> sourceTypeVar;
  1289         protected CoreReflTypeParameterElement(java.lang.reflect.TypeVariable<?> sourceTypeVar) {
  1290             this.sourceTypeVar = Objects.requireNonNull(sourceTypeVar);
  1291             this.source = Objects.requireNonNull(sourceTypeVar.getGenericDeclaration());
  1294         @Override
  1295         public java.lang.reflect.TypeVariable<?> getSource() {
  1296             return sourceTypeVar;
  1299         protected java.lang.reflect.TypeVariable<?> getSourceTypeVar() {
  1300             return sourceTypeVar;
  1303         @Override
  1304         public boolean equals(Object o) {
  1305             if (o instanceof CoreReflTypeParameterElement) {
  1306                 return sourceTypeVar.equals(((CoreReflTypeParameterElement)o).sourceTypeVar);
  1307             } else {
  1308                 return false;
  1312         @Override
  1313         public <R,P> R accept(ElementVisitor<R,P> v, P p) {
  1314             return v.visitTypeParameter(this, p);
  1317         @Override
  1318         public <R,P> R accept(ReflectionElementVisitor<R,P> v, P p) {
  1319             return v.visitTypeParameter(this, p);
  1322         @Override
  1323         public List<ReflectionElement> getEnclosedElements() {
  1324             return Collections.emptyList();
  1327         @Override
  1328         public ReflectionElement getEnclosingElement() {
  1329             if (source instanceof Class)
  1330                 return createMirror((Class<?>)source);
  1331             else if (source instanceof Method)
  1332                 return createMirror((Method)source);
  1333             else if (source instanceof Constructor)
  1334                 return createMirror((Constructor<?>)source);
  1335             else
  1336                 throw new AssertionError("Unexpected enclosing element: " + source);
  1339         @Override
  1340         public ElementKind getKind() {
  1341             return ElementKind.TYPE_PARAMETER;
  1344         @Override
  1345         public Name getSimpleName() {
  1346             return StringName.instance(sourceTypeVar.getName());
  1349         // TypeParameterElement methods
  1350         @Override
  1351         public ReflectionElement getGenericElement() {
  1352             return getEnclosingElement(); // As per the doc,
  1353                                           // getEnclosingElement and
  1354                                           // getGenericElement return
  1355                                           // the same information.
  1358         @Override
  1359         public List<? extends TypeMirror> getBounds() {
  1360             Type[] types = getSourceTypeVar().getBounds();
  1361             int len = types.length;
  1363             if (len > 0) {
  1364                 List<TypeMirror> res = new ArrayList<>(len);
  1365                 for (Type t : types) {
  1366                     res.add(TypeFactory.instance(t));
  1368                 return Collections.unmodifiableList(res);
  1369             } else {
  1370                 return Collections.emptyList();
  1375     private abstract static class CoreReflVariableElement extends CoreReflElement
  1376         implements ReflectionVariableElement {
  1378         protected CoreReflVariableElement() {}
  1380         // Element visitor
  1381         @Override
  1382         public <R,P> R accept(ElementVisitor<R,P>v, P p) {
  1383             return v.visitVariable(this, p);
  1386         // ReflectElement visitor
  1387         @Override
  1388         public <R,P> R accept(ReflectionElementVisitor<R,P> v, P p) {
  1389             return v.visitVariable(this, p);
  1392         @Override
  1393         public List<ReflectionElement> getEnclosedElements() {
  1394             return Collections.emptyList();
  1397         @Override
  1398         public ReflectionElement getEnclosingElement() {
  1399             return null;
  1402         @Override
  1403         public boolean isSynthetic() {
  1404             return false;
  1407         @Override
  1408         public boolean isImplicit() {
  1409             return false;
  1413     private static class CoreReflFieldVariableElement extends CoreReflVariableElement {
  1414         private final Field source;
  1416         protected CoreReflFieldVariableElement(Field source) {
  1417             this.source = Objects.requireNonNull(source);
  1420         @Override
  1421         public Field getSource() {
  1422             return source;
  1425         @Override
  1426         public TypeMirror asType() {
  1427             return createTypeMirror(getSource().getType());
  1430         @Override
  1431         public ElementKind getKind() {
  1432             if (source.isEnumConstant())
  1433                 return ElementKind.ENUM_CONSTANT;
  1434             else
  1435                 return ElementKind.FIELD;
  1438         @Override
  1439         public Set<Modifier> getModifiers() {
  1440             return ModifierUtil.instance(source.getModifiers() &
  1441                                          java.lang.reflect.Modifier.fieldModifiers(), false);
  1444         @Override
  1445         public Name getSimpleName() {
  1446             return StringName.instance(source.getName());
  1449         @Override
  1450         public ReflectionElement getEnclosingElement() {
  1451             return createMirror(source.getDeclaringClass());
  1454         @Override
  1455         public boolean equals(Object o) {
  1456             if (o instanceof CoreReflFieldVariableElement) {
  1457                 return Objects.equals(source,
  1458                                       ((CoreReflFieldVariableElement)o).getSource());
  1459             } else {
  1460                 return false;
  1464         @Override
  1465         public Object getConstantValue() {
  1466             Field target = source;
  1468             // The api says only Strings and primitives may be compile time constants.
  1469             // Ensure field is that, and final.
  1470             //
  1471             // Also, we don't have an instance so restrict to static Fields
  1472             //
  1473             if (!(source.getType().equals(java.lang.String.class)
  1474                   || source.getType().isPrimitive())) {
  1475                 return null;
  1477             final int modifiers = target.getModifiers();
  1478             if (!( java.lang.reflect.Modifier.isFinal(modifiers) &&
  1479                    java.lang.reflect.Modifier.isStatic(modifiers))) {
  1480                 return null;
  1483             try {
  1484                 return target.get(null);
  1485             } catch (IllegalAccessException e) {
  1486                 try {
  1487                     target.setAccessible(true);
  1488                     return target.get(null);
  1489                 } catch (IllegalAccessException i) {
  1490                     throw new SecurityException(i);
  1496     private static class CoreReflParameterVariableElement
  1497         extends CoreReflVariableElement {
  1498         private final Parameter source;
  1500         protected CoreReflParameterVariableElement(Parameter source) {
  1501             this.source = Objects.requireNonNull(source);
  1504         @Override
  1505         public Parameter getSource() {
  1506             return source;
  1509         @Override
  1510         public Set<Modifier> getModifiers() {
  1511             return ModifierUtil.instance(source.getModifiers() &
  1512                                          java.lang.reflect.Modifier.parameterModifiers(), false);
  1515         @Override
  1516         public TypeMirror asType() {
  1517             // TODO : switch to parameterized type
  1518             return createTypeMirror(source.getType());
  1521         @Override
  1522         public ElementKind getKind() {
  1523             return ElementKind.PARAMETER;
  1526         @Override
  1527         public Name getSimpleName() {
  1528             return StringName.instance(source.getName());
  1531         @Override
  1532         public ReflectionElement getEnclosingElement() {
  1533             Executable enclosing = source.getDeclaringExecutable();
  1534             if (enclosing instanceof Method)
  1535                 return createMirror((Method)enclosing);
  1536             else if (enclosing instanceof Constructor)
  1537                 return createMirror((Constructor<?>)enclosing);
  1538             else
  1539                 throw new AssertionError("Bad enclosing value.");
  1542         @Override
  1543         public boolean equals(Object o) {
  1544             if (o instanceof CoreReflParameterVariableElement) {
  1545                 return source.equals(((CoreReflParameterVariableElement) o).getSource());
  1546             } else
  1547                 return false;
  1550         // VariableElement methods
  1551         @Override
  1552         public Object getConstantValue() {
  1553             return null;
  1556         @Override
  1557         public boolean isSynthetic() {
  1558             return source.isSynthetic();
  1561         @Override
  1562         public boolean isImplicit() {
  1563             return source.isImplicit();
  1567     private static class CoreReflPackageElement extends CoreReflElement
  1568         implements ReflectionPackageElement {
  1570         private final Package source;
  1572         protected CoreReflPackageElement(Package source) {
  1573             this.source = source;
  1576         @Override
  1577         public Package getSource() {
  1578             return source;
  1581         @Override
  1582         public <R,P> R accept(ElementVisitor<R,P> v, P p) {
  1583             return v.visitPackage(this, p);
  1586         @Override
  1587         public <R,P> R accept(ReflectionElementVisitor<R,P> v, P p) {
  1588             return v.visitPackage(this, p);
  1591         @Override
  1592         public boolean equals(Object o) {
  1593             if (o instanceof CoreReflPackageElement) {
  1594                 return Objects.equals(source,
  1595                                       ((CoreReflPackageElement)o).getSource());
  1596             } else {
  1597                 return false;
  1601         @Override
  1602         public ElementKind getKind() {
  1603             return ElementKind.PACKAGE;
  1606         @Override
  1607         public ReflectionElement getEnclosingElement() {
  1608             return null;
  1611         @Override
  1612         public List<ReflectionElement> getEnclosedElements() {
  1613             throw new UnsupportedOperationException();
  1616         @Override
  1617         public Name getQualifiedName() {
  1618             return StringName.instance((source != null) ?
  1619                                        source.getName() :
  1620                                        "" );
  1623         @Override
  1624         public Name getSimpleName() {
  1625             String n = ((source != null) ?
  1626                         source.getName() :
  1627                         "");
  1628             int index = n.lastIndexOf('.');
  1629             if (index > 0) {
  1630                 return StringName.instance(n.substring(index + 1, n.length()));
  1631             } else {
  1632                 return StringName.instance(n);
  1636         @Override
  1637         public boolean isUnnamed() {
  1638             if (source != null) {
  1639                 String name = source.getName();
  1640                 return(name == null || name.isEmpty());
  1641             } else
  1642                 return true;
  1646     private static class CoreReflAnnotationMirror
  1647         implements javax.lang.model.element.AnnotationMirror {
  1648         private final Annotation annotation;
  1650         protected CoreReflAnnotationMirror(Annotation annotation) {
  1651             this.annotation = Objects.requireNonNull(annotation);
  1654         @Override
  1655         public DeclaredType getAnnotationType() {
  1656             return (DeclaredType)TypeFactory.instance(annotation.annotationType());
  1659         @Override
  1660         public Map<? extends ReflectionExecutableElement, ? extends AnnotationValue> getElementValues() {
  1661             // This differs from the javac implementation in that it returns default values
  1663             Method[] elems = annotation.annotationType().getDeclaredMethods();
  1664             int len = elems.length;
  1666             if (len > 0) {
  1667                 Map<ReflectionExecutableElement, AnnotationValue> res = new HashMap<>();
  1668                 for (Method m : elems) {
  1669                     AnnotationValue v;
  1670                     try {
  1671                         v = new CoreReflAnnotationValue(m.invoke(annotation));
  1672                     } catch (IllegalAccessException e) {
  1673                         try {
  1674                             m.setAccessible(true);
  1675                             v = new CoreReflAnnotationValue(m.invoke(annotation));
  1676                         } catch (IllegalAccessException i) {
  1677                             throw new SecurityException(i);
  1678                         } catch (InvocationTargetException ee) {
  1679                             throw new RuntimeException(ee);
  1681                     } catch (InvocationTargetException ee) {
  1682                         throw new RuntimeException(ee);
  1684                     ReflectionExecutableElement e = createMirror(m);
  1685                     res.put(e, v);
  1688                 return Collections.unmodifiableMap(res);
  1689             } else {
  1690                 return Collections.emptyMap();
  1694         @Override
  1695         public boolean equals(Object other) {
  1696             if (other instanceof CoreReflAnnotationMirror) {
  1697                 return annotation.equals(((CoreReflAnnotationMirror)other).annotation);
  1698             } else {
  1699                 return false;
  1703         @Override
  1704         public int hashCode() {
  1705             return Objects.hashCode(annotation);
  1708         @Override
  1709         public String toString() {
  1710             return annotation.toString();
  1714     private static class CoreReflAnnotationValue
  1715         implements javax.lang.model.element.AnnotationValue {
  1716         private Object value = null;
  1718         protected CoreReflAnnotationValue(Object value) {
  1719             // Is this constraint really necessary?
  1720             Objects.requireNonNull(value);
  1721             this.value = value;
  1724         @Override
  1725         public Object getValue() {
  1726             return value;
  1729         @Override
  1730         public String toString() {
  1731             return value.toString();
  1734         @Override
  1735         public <R,P> R accept(AnnotationValueVisitor<R,P> v, P p) {
  1736             return v.visit(this, p);
  1740     // Helper utility classes
  1742     private static class StringName implements Name {
  1743         private String name;
  1745         private StringName(String name) {
  1746             this.name = Objects.requireNonNull(name);
  1749         public static StringName instance(String name) {
  1750             return new StringName(name);
  1753         @Override
  1754         public int length() {
  1755             return name.length();
  1758         @Override
  1759         public char charAt(int index) {
  1760             return name.charAt(index);
  1763         @Override
  1764         public CharSequence subSequence(int start, int end) {
  1765             return name.subSequence(start, end);
  1768         @Override
  1769         public String toString() {
  1770             return name;
  1773         @Override
  1774         public boolean equals(Object other) {
  1775             if (other instanceof StringName) {
  1776                 return name.equals(((StringName) other).name);
  1777             } else {
  1778                 return false;
  1782         @Override
  1783         public int hashCode() {
  1784             return name.hashCode();
  1787         @Override
  1788         public boolean contentEquals(CharSequence cs) {
  1789             return name.contentEquals(cs);
  1793     /*
  1794      * Given an {@code int} value of modifiers, return a proper immutable set
  1795      * of {@code Modifier}s as a result.
  1796      */
  1797     private static class ModifierUtil {
  1798         private ModifierUtil() {
  1799             throw new AssertionError("No instances for you.");
  1802         // Exercise for the reader: explore if caching of sets of
  1803         // Modifiers would be helpful.
  1805         public static Set<Modifier> instance(int modifiers, boolean isDefault) {
  1806             Set<Modifier> modSet = EnumSet.noneOf(Modifier.class);
  1808             if (java.lang.reflect.Modifier.isAbstract(modifiers))
  1809                 modSet.add(Modifier.ABSTRACT);
  1811             if (java.lang.reflect.Modifier.isFinal(modifiers))
  1812                 modSet.add(Modifier.FINAL);
  1814             if (java.lang.reflect.Modifier.isNative(modifiers))
  1815                 modSet.add(Modifier.NATIVE);
  1817             if (java.lang.reflect.Modifier.isPrivate(modifiers))
  1818                 modSet.add(Modifier.PRIVATE);
  1820             if (java.lang.reflect.Modifier.isProtected(modifiers))
  1821                 modSet.add(Modifier.PROTECTED);
  1823             if (java.lang.reflect.Modifier.isPublic(modifiers))
  1824                 modSet.add(Modifier.PUBLIC);
  1826             if (java.lang.reflect.Modifier.isStatic(modifiers))
  1827                 modSet.add(Modifier.STATIC);
  1829             if (java.lang.reflect.Modifier.isStrict(modifiers))
  1830                 modSet.add(Modifier.STRICTFP);
  1832             if (java.lang.reflect.Modifier.isSynchronized(modifiers))
  1833                 modSet.add(Modifier.SYNCHRONIZED);
  1835             if (java.lang.reflect.Modifier.isTransient(modifiers))
  1836                 modSet.add(Modifier.TRANSIENT);
  1838             if (java.lang.reflect.Modifier.isVolatile(modifiers))
  1839                 modSet.add(Modifier.VOLATILE);
  1841             if (isDefault)
  1842                 modSet.add(Modifier.DEFAULT);
  1844             return Collections.unmodifiableSet(modSet);
  1848     private abstract static class AbstractTypeMirror implements TypeMirror {
  1849         private final TypeKind kind;
  1851         protected AbstractTypeMirror(TypeKind kind) {
  1852             this.kind = Objects.requireNonNull(kind);
  1855         @Override
  1856         public TypeKind getKind() {
  1857             return kind;
  1860         @Override
  1861         public <R,P> R accept(TypeVisitor<R,P> v, P p) {
  1862             return v.visit(this, p);
  1865         //Types methods
  1866         abstract List<? extends TypeMirror> directSuperTypes();
  1868         TypeMirror capture() {
  1869             // Exercise for the reader: make this abstract and implement in subtypes
  1870             throw new UnsupportedOperationException();
  1873         TypeMirror erasure() {
  1874             // Exercise for the reader: make this abstract and implement in subtypes
  1875             throw new UnsupportedOperationException();
  1878         // Exercise for the reader: implement the AnnotatedConstruct methods
  1879         @Override
  1880         public List<? extends AnnotationMirror> getAnnotationMirrors() {
  1881             throw new UnsupportedOperationException();
  1884         @Override
  1885         public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
  1886             throw new UnsupportedOperationException();
  1889         @Override
  1890         public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
  1891             throw new UnsupportedOperationException();
  1895     private static class CoreReflArrayType extends AbstractTypeMirror
  1896         implements javax.lang.model.type.ArrayType,
  1897                    Reifiable {
  1898         private Class<?> source = null;
  1899         private Class<?> component = null;
  1900         private TypeMirror eagerComponent = null;
  1902         protected CoreReflArrayType(Class<?> source) {
  1903             super(TypeKind.ARRAY);
  1904             this.source = source;
  1905             this.component = source.getComponentType();
  1906             this.eagerComponent = TypeFactory.instance(component);
  1909         public TypeMirror getComponentType() {
  1910             return eagerComponent;
  1913         @Override
  1914         public Class<?> getSource() {
  1915             return source;
  1918         @Override
  1919         List<? extends TypeMirror> directSuperTypes() {
  1920             final TypeMirror componentType = getComponentType();
  1921             final TypeMirror[] directSupers;
  1923             // JLS v4 4.10.3
  1924             if (componentType.getKind().isPrimitive() ||
  1925                 component.equals(java.lang.Object.class)) {
  1926                 directSupers = new TypeMirror[3];
  1927                 directSupers[0] = TypeFactory.instance(java.lang.Object.class);
  1928                 directSupers[1] = TypeFactory.instance(java.lang.Cloneable.class);
  1929                 directSupers[2] = TypeFactory.instance(java.io.Serializable.class);
  1930             } else if (componentType.getKind() == TypeKind.ARRAY) {
  1931                 List<? extends TypeMirror> componentDirectSupertypes = CoreReflTypes.instance().directSupertypes(componentType);
  1932                 directSupers = new TypeMirror[componentDirectSupertypes.size()];
  1933                 for (int i = 0; i < directSupers.length; i++) {
  1934                     directSupers[i] = new CoreReflArrayType(Array.newInstance(((Reifiable)componentDirectSupertypes.get(i)).getSource(), 0).getClass());
  1936             } else {
  1937                 Class<?> superClass = component.getSuperclass();
  1938                 Class<?>[] interfaces = component.getInterfaces();
  1939                 directSupers = new TypeMirror[1 + interfaces.length];
  1941                 directSupers[0] = TypeFactory.instance(Array.newInstance(superClass, 0).getClass());
  1943                 for (int i = 0; i < interfaces.length; i++) {
  1944                     directSupers[i + 1] = TypeFactory.instance(Array.newInstance(interfaces[i],0).getClass());
  1948             return Collections.unmodifiableList(Arrays.asList(directSupers));
  1951         @Override
  1952         public String toString() {
  1953             return getKind() + " of " + getComponentType().toString();
  1957     private static class CaptureTypeVariable extends AbstractTypeMirror implements javax.lang.model.type.TypeVariable {
  1958         private TypeMirror source = null;
  1959         private TypeMirror upperBound = null;
  1960         private TypeMirror lowerBound = null;
  1962         CaptureTypeVariable(TypeMirror source,
  1963                             TypeMirror upperBound,
  1964                             TypeMirror lowerBound) {
  1965             super(TypeKind.TYPEVAR);
  1967             this.source = Objects.requireNonNull(source);
  1968             this.upperBound = (upperBound == null ? CoreReflTypes.instance().getNullType() : upperBound);
  1969             this.lowerBound = (lowerBound == null ? CoreReflTypes.instance().getNullType() : lowerBound);
  1972         protected Class<?> getSource() {
  1973             if (source instanceof CoreReflDeclaredType) {
  1974                 return ((CoreReflDeclaredType)source).getSource();
  1975             } else {
  1976                 return null;
  1980         @Override
  1981         public TypeMirror getUpperBound() {
  1982             return upperBound;
  1985         @Override
  1986         public TypeMirror getLowerBound() {
  1987             return lowerBound;
  1990         @Override
  1991         public Element asElement() {
  1992             if (null == getSource()) {
  1993                 return null;
  1995             return CoreReflectionFactory.createMirror(getSource());
  1998         @Override
  1999         List<? extends TypeMirror> directSuperTypes() {
  2000             throw new UnsupportedOperationException();
  2004         @Override
  2005         public String toString() {
  2006             return getKind() + " CAPTURE of: " + source.toString();
  2010     private static class CoreReflElements implements ReflectionElements {
  2011         private CoreReflElements() {} // mostly one instance for you
  2013         private static CoreReflElements instance = new CoreReflElements();
  2015         static CoreReflElements instance() {
  2016             return instance;
  2019         /**
  2020          * {@inheritDoc}
  2021          */
  2022         @Override
  2023         public ReflectionPackageElement getPackageElement(CharSequence name) {
  2024             return createMirror(Package.getPackage(name.toString()));
  2027         /**
  2028          * {@inheritDoc}
  2029          */
  2030         @Override
  2031         public ReflectionTypeElement getTypeElement(CharSequence name) {
  2032             // where name is a Canonical Name jls 6.7
  2033             // but this method will probably accept an equivalent FQN
  2034             // depending on Class.forName(String)
  2036             ReflectionTypeElement tmp = null;
  2038             // Filter out arrays
  2039             String n = name.toString();
  2040             if (n.contains("[")) return null;
  2041             if (n.equals("")) return null;
  2043             // The intention of this loop is to handle nested
  2044             // elements.  If finding the element using Class.forName
  2045             // fails, an attempt is made to find the element as an
  2046             // enclosed element by trying fo find a prefix of the name
  2047             // (dropping a trailing ".xyz") and looking for "xyz" as
  2048             // an enclosed element.
  2050             Deque<String> parts = new ArrayDeque<>();
  2051             boolean again;
  2052             do {
  2053                 again = false;
  2054                 try {
  2055                     tmp = createMirror(Class.forName(n));
  2056                 } catch (ClassNotFoundException e) {
  2057                     tmp = null;
  2060                 if (tmp != null) {
  2061                     if (parts.isEmpty()) {
  2062                         return tmp;
  2065                     tmp = findInner(tmp, parts);
  2066                     if (tmp != null) {
  2067                         return tmp;
  2071                 int indx = n.lastIndexOf('.');
  2072                 if (indx > -1) {
  2073                     parts.addFirst(n.substring(indx + 1));
  2074                     n = n.substring(0, indx);
  2075                     again = true;
  2077             } while (again);
  2079             return null;
  2082         // Recursively finds enclosed type elements named as part.top() popping part and repeating
  2083         private ReflectionTypeElement findInner(ReflectionTypeElement e, Deque<String> parts) {
  2084             if (parts.isEmpty()) {
  2085                 return e;
  2088             String part = parts.removeFirst();
  2089             List<ReflectionElement> enclosed = e.getEnclosedElements();
  2090             for (ReflectionElement elm : enclosed) {
  2091                 if ((elm.getKind() == ElementKind.CLASS ||
  2092                      elm.getKind() == ElementKind.INTERFACE ||
  2093                      elm.getKind() == ElementKind.ENUM ||
  2094                      elm.getKind() == ElementKind.ANNOTATION_TYPE)
  2095                     && elm.getSimpleName().toString().equals(part)) {
  2096                     ReflectionTypeElement t = findInner((ReflectionTypeElement)elm, parts);
  2097                     if (t != null) {
  2098                         return t;
  2102             return null;
  2105         /**
  2106          * {@inheritDoc}
  2107          */
  2108         @Override
  2109         public Map<? extends ReflectionExecutableElement, ? extends AnnotationValue>
  2110             getElementValuesWithDefaults(AnnotationMirror a) {
  2111             if (a instanceof CoreReflAnnotationMirror) {
  2112                 return ((CoreReflAnnotationMirror)a).getElementValues();
  2113             } else {
  2114                 throw new IllegalArgumentException();
  2118         /**
  2119          * {@inheritDoc}
  2120          */
  2121         @Override
  2122         public String getDocComment(Element e) {
  2123             checkElement(e);
  2124             return null; // As per the doc
  2127         /**
  2128          * {@inheritDoc}
  2129          */
  2130         @Override
  2131         public boolean isDeprecated(Element e) {
  2132             checkElement(e);
  2133             return ((CoreReflElement)e).getSource().isAnnotationPresent(java.lang.Deprecated.class);
  2136         /**
  2137          * {@inheritDoc}
  2138          */
  2139         @Override
  2140         public Name getBinaryName(TypeElement type) {
  2141             checkElement(type);
  2142             return StringName.instance(((CoreReflTypeElement)type)
  2143                                        .getSource()
  2144                                        .getName());
  2147         /**
  2148          * {@inheritDoc}
  2149          */
  2150         @Override
  2151         public ReflectionPackageElement getPackageOf(Element type) {
  2152             checkElement(type);
  2153             if (type instanceof ReflectionPackageElement) {
  2154                 return (ReflectionPackageElement)type;
  2157             Package p;
  2158             if (type instanceof CoreReflTypeElement) {
  2159                 p = ((CoreReflTypeElement)type).getSource().getPackage();
  2160             } else {
  2161                 CoreReflTypeElement enclosingTypeElement = (CoreReflTypeElement)getEnclosingTypeElement((ReflectionElement)type);
  2162                 p = enclosingTypeElement.getSource().getPackage();
  2165             return createMirror(p);
  2168         /**
  2169          * {@inheritDoc}
  2170          */
  2171         @Override
  2172         public List<? extends ReflectionElement> getAllMembers(TypeElement type) {
  2173             checkElement(type);
  2174             return getAllMembers((ReflectionTypeElement)type);
  2177         // Exercise for the reader: should this method, and similar
  2178         // ones that specialize on the more specific argument types,
  2179         // be addd to the public ReflectionElements API?
  2180         public List<? extends ReflectionElement> getAllMembers(ReflectionTypeElement type) {
  2181             return type.getAllMembers();
  2184         /**
  2185          * {@inheritDoc}
  2186          */
  2187         @Override
  2188         public List<? extends AnnotationMirror> getAllAnnotationMirrors(Element e) {
  2189             checkElement(e);
  2190             AnnotatedElement ae = CoreReflElement.class.cast(e).getSource();
  2191             Annotation[] annotations = ae.getAnnotations();
  2192             int len = annotations.length;
  2194             if (len > 0) {
  2195                 List<AnnotationMirror> res = new ArrayList<>(len);
  2196                 for (Annotation a : annotations) {
  2197                     res.add(createMirror(a));
  2199                 return Collections.unmodifiableList(res);
  2200             } else {
  2201                 List<AnnotationMirror> ret = Collections.emptyList();
  2202                 return ret;
  2206         /**
  2207          * {@inheritDoc}
  2208          */
  2209         @Override
  2210         public boolean hides(Element hider, Element hidden) {
  2211             checkElement(hider);
  2212             checkElement(hidden);
  2214             // Names must be equal
  2215             if (!hider.getSimpleName().equals(hidden.getSimpleName())) {
  2216                 return false;
  2219             // Hides isn't reflexive
  2220             if (hider.equals(hidden)) {
  2221                 return false;
  2224             // Hider and hidden needs to be field, method or type
  2225             // and fields hide fields, types hide types, methods hide methods
  2226             // IE a Field doesn't hide a Methods etc
  2227             ElementKind hiderKind = hider.getKind();
  2228             ElementKind hiddenKind = hidden.getKind();
  2229             if (hiderKind.isField() && !hiddenKind.isField()) {
  2230                 return false;
  2231             } else if (hiderKind.isClass() &&
  2232                        !(hiddenKind.isClass() || hiddenKind.isInterface())) {
  2233                 return false;
  2234             } else if (hiderKind.isInterface() &&
  2235                        !(hiddenKind.isClass() || hiddenKind.isInterface())) {
  2236                 return false;
  2237             } else if (hiderKind == ElementKind.METHOD && hiddenKind != ElementKind.METHOD) {
  2238                 return false;
  2239             } else if (!(hiderKind.isClass() ||
  2240                          hiderKind.isInterface() ||
  2241                          hiderKind.isField() ||
  2242                          hiderKind == ElementKind.METHOD)) {
  2243                 return false;
  2246             Set<Modifier> hm = hidden.getModifiers();
  2247             // jls 8.4.8.2 only static methods can hide methods
  2248             if (hider.getKind() == ElementKind.METHOD) {
  2249                 if (!hider.getModifiers().contains(Modifier.STATIC)) {
  2250                     return false; // hider not static
  2251                 } else if (!hm.contains(Modifier.STATIC)) { // we know it's a method
  2252                     return false; // hidden not static
  2255                 // For methods we also need to check parameter types
  2256                 Class<?>[] h1 = ((CoreReflMethodExecutableElement)hider).getSource().getParameterTypes();
  2257                 Class<?>[] h2 = ((CoreReflMethodExecutableElement)hidden).getSource().getParameterTypes();
  2258                 if (h1.length != h2.length) {
  2259                     return false;
  2261                 for (int i = 0; i < h1.length; i++) {
  2262                     if (h1[i] != h2[i]) {
  2263                         return false;
  2268             // You can only hide visible elements
  2269             if (hm.contains(Modifier.PRIVATE)) {
  2270                 return false; // hidden private, can't be hidden
  2271             } else if ((!(hm.contains(Modifier.PUBLIC) || hm.contains(Modifier.PROTECTED))) && // not private, not (public or protected) IE package private
  2272                        (!getPackageOf(hider).equals(getPackageOf(hidden)))) {
  2273                 return false; // hidden package private, and different packages, IE not visible
  2276             // Ok so now hider actually hides hidden if hider is
  2277             // declared on a subtype of hidden.
  2278             //
  2279             // TODO: should this be a proper subtype or is that taken
  2280             // care of by the reflexive check in the beginning?
  2281             //
  2282             TypeMirror hiderType = getEnclosingTypeElement((ReflectionElement)hider).asType();
  2283             TypeMirror hiddenType = getEnclosingTypeElement((ReflectionElement)hidden).asType();
  2285             return getTypes().isSubtype(hiderType, hiddenType);
  2288         /**
  2289          * {@inheritDoc}
  2290          */
  2291         @Override
  2292         public ReflectionTypeElement getEnclosingTypeElement(ReflectionElement e) {
  2293             if (e.getKind() == ElementKind.PACKAGE) {
  2294                 return null;
  2297             if(e instanceof CoreReflTypeParameterElement) {
  2298                 ReflectionElement encElem = ((CoreReflTypeParameterElement)e).getEnclosingElement();
  2299                 if (encElem instanceof ReflectionTypeElement) {
  2300                     return (ReflectionTypeElement)encElem;
  2301                 } else  {
  2302                     return getEnclosingTypeElement(encElem);
  2306             Class<?> encl = null;
  2307             if (e instanceof CoreReflTypeElement) {
  2308                 encl = ((CoreReflTypeElement)e).getSource().getDeclaringClass();
  2309             } else if (e instanceof CoreReflExecutableElement) {
  2310                 encl = (((CoreReflExecutableElement)e).getSource()).getDeclaringClass();
  2311             } else if (e instanceof CoreReflFieldVariableElement) {
  2312                 encl = ((CoreReflFieldVariableElement)e).getSource().getDeclaringClass();
  2313             } else if (e instanceof CoreReflParameterVariableElement) {
  2314                 encl = ((CoreReflParameterVariableElement)e).getSource().getDeclaringExecutable().getDeclaringClass();
  2317             return encl == null ? null : createMirror(encl);
  2320         /**
  2321          *{@inheritDoc}
  2323          * Note that this implementation does not handle the situation
  2324          * where A overrides B and B overrides C but A does not
  2325          * directly override C. In this case, this implementation will
  2326          * erroneously return false.
  2327          */
  2328         @Override
  2329         public boolean overrides(ExecutableElement overrider, ExecutableElement overridden,
  2330                                  TypeElement type) {
  2331             checkElement(overrider);
  2332             checkElement(overridden);
  2333             checkElement(type);
  2335             // TODO handle transitive overrides
  2336             return overridesDirect(overrider, overridden, type);
  2339         private boolean overridesDirect(ExecutableElement overrider, ExecutableElement overridden,
  2340                                          TypeElement type) {
  2341             // Should we check that at least one of the types
  2342             // overrider has is in fact a supertype of the TypeElement
  2343             // 'type' supplied?
  2345             CoreReflExecutableElement rider = (CoreReflExecutableElement)overrider;
  2346             CoreReflExecutableElement ridden = (CoreReflExecutableElement)overridden;
  2347             CoreReflTypeElement riderType = (CoreReflTypeElement)type;
  2349             // Names must match, redundant - see subsignature below
  2350             if (!rider.getSimpleName().equals(ridden.getSimpleName())) {
  2351                 return false;
  2354             // Constructors don't override
  2355             // TODO: verify this fact
  2356             if (rider.getKind() == ElementKind.CONSTRUCTOR ||
  2357                 ridden.getKind() == ElementKind.CONSTRUCTOR) {
  2358                 return false;
  2361             // Overridden must be visible to be overridden
  2362             // TODO Fix transitive visibility/override
  2363             Set<Modifier> rm = ridden.getModifiers();
  2364             if (rm.contains(Modifier.PRIVATE)) {
  2365                 return false; // overridden private, can't be overridden
  2366             } else if ((!(rm.contains(Modifier.PUBLIC) || rm.contains(Modifier.PROTECTED))) && // not private, not (public or protected) IE package private
  2367                        (!getPackageOf(rider).equals(getPackageOf(ridden)))) {
  2368                 return false; // ridden package private, and different packages, IE not visible
  2371             // Static methods doesn't override
  2372             if (rm.contains(Modifier.STATIC) ||
  2373                 rider.getModifiers().contains(Modifier.STATIC)) {
  2374                 return false;
  2377             // Declaring class of overrider must be a subclass of declaring class of overridden
  2378             // except we use the parameter type as declaring class of overrider
  2379             if (!getTypes().isSubtype(riderType.asType(), getEnclosingTypeElement(ridden).asType())) {
  2380                 return false;
  2383             // Now overrider overrides overridden if the signature of rider is a subsignature of ridden
  2384             return getTypes().isSubsignature(rider.asType(), ridden.asType());
  2387         /**
  2388          *{@inheritDoc}
  2389          */
  2390         @Override
  2391         public String getConstantExpression(Object value) {
  2392             return Constants.format(value);
  2395         // If CoreReflectionFactory were a proper part of the JDK, the
  2396         // analogous functionality in javac could be reused.
  2397         private static class Constants {
  2398             /**
  2399              * Returns a string representation of a constant value (given in
  2400              * standard wrapped representation), quoted and formatted as in
  2401              * Java source.
  2402              */
  2403             public static String format(Object value) {
  2404                 if (value instanceof Byte)      return formatByte((Byte) value);
  2405                 if (value instanceof Short)     return formatShort((Short) value);
  2406                 if (value instanceof Long)      return formatLong((Long) value);
  2407                 if (value instanceof Float)     return formatFloat((Float) value);
  2408                 if (value instanceof Double)    return formatDouble((Double) value);
  2409                 if (value instanceof Character) return formatChar((Character) value);
  2410                 if (value instanceof String)    return formatString((String) value);
  2411                 if (value instanceof Integer ||
  2412                     value instanceof Boolean)   return value.toString();
  2413                 else
  2414                     throw new IllegalArgumentException("Argument is not a primitive type or a string; it " +
  2415                                                        ((value == null) ?
  2416                                                         "is a null value." :
  2417                                                         "has class " +
  2418                                                         value.getClass().getName()) + "." );
  2421             private static String formatByte(byte b) {
  2422                 return String.format("(byte)0x%02x", b);
  2425             private static String formatShort(short s) {
  2426                 return String.format("(short)%d", s);
  2429             private static String formatLong(long lng) {
  2430                 return lng + "L";
  2433             private static String formatFloat(float f) {
  2434                 if (Float.isNaN(f))
  2435                     return "0.0f/0.0f";
  2436                 else if (Float.isInfinite(f))
  2437                     return (f < 0) ? "-1.0f/0.0f" : "1.0f/0.0f";
  2438                 else
  2439                     return f + "f";
  2442             private static String formatDouble(double d) {
  2443                 if (Double.isNaN(d))
  2444                     return "0.0/0.0";
  2445                 else if (Double.isInfinite(d))
  2446                     return (d < 0) ? "-1.0/0.0" : "1.0/0.0";
  2447                 else
  2448                     return d + "";
  2451             private static String formatChar(char c) {
  2452                 return '\'' + quote(c) + '\'';
  2455             private static String formatString(String s) {
  2456                 return '"' + quote(s) + '"';
  2459             /**
  2460              * Escapes each character in a string that has an escape sequence or
  2461              * is non-printable ASCII.  Leaves non-ASCII characters alone.
  2462              */
  2463             private static String quote(String s) {
  2464                 StringBuilder buf = new StringBuilder();
  2465                 for (int i = 0; i < s.length(); i++) {
  2466                     buf.append(quote(s.charAt(i)));
  2468                 return buf.toString();
  2471             /**
  2472              * Escapes a character if it has an escape sequence or is
  2473              * non-printable ASCII.  Leaves ASCII characters alone.
  2474              */
  2475             private static String quote(char ch) {
  2476                 switch (ch) {
  2477                 case '\b':  return "\\b";
  2478                 case '\f':  return "\\f";
  2479                 case '\n':  return "\\n";
  2480                 case '\r':  return "\\r";
  2481                 case '\t':  return "\\t";
  2482                 case '\'':  return "\\'";
  2483                 case '\"':  return "\\\"";
  2484                 case '\\':  return "\\\\";
  2485                 default:
  2486                     return (isPrintableAscii(ch))
  2487                         ? String.valueOf(ch)
  2488                         : String.format("\\u%04x", (int) ch);
  2492             /**
  2493              * Is a character printable ASCII?
  2494              */
  2495             private static boolean isPrintableAscii(char ch) {
  2496                 return ch >= ' ' && ch <= '~';
  2500         /**
  2501          * {@inheritDoc}
  2502          */
  2503         @Override
  2504         public void printElements(Writer w, Element... elements) {
  2505             ElementVisitor<?, ?> printer = getPrinter(w);
  2506             try {
  2507                 for (Element e : elements) {
  2508                     checkElement(e);
  2509                     printer.visit(e);
  2511             } finally {
  2512                 try {
  2513                     w.flush();
  2514                 } catch (java.io.IOException e) { /* Ignore */;}
  2518         private ElementVisitor<?, ?> getPrinter(Writer w) {
  2519             // First try a reflective call into javac and if that
  2520             // fails, fallback to a very simple toString-based
  2521             // scanner.
  2522             try {
  2523                 //reflective form of
  2524                 // return new com.sun.tools.javac.processing.PrintingProcessor.PrintingElementVisitor(w, getElements());
  2525                 Class<?> printProcClass =
  2526                     ClassLoader.getSystemClassLoader().loadClass("com.sun.tools.javac.processing.PrintingProcessor$PrintingElementVisitor");
  2527                 Constructor<?> printProcCtor = printProcClass.getConstructor(Writer.class, Elements.class);
  2528                 return (ElementVisitor) printProcCtor.newInstance(w, getElements());
  2529             } catch (ReflectiveOperationException | SecurityException e) {
  2530                 return new ElementScanner8<Writer, Void>(w){
  2531                     @Override
  2532                     public Writer scan(Element e, Void v) {
  2533                         try {
  2534                             DEFAULT_VALUE.append(e.toString());
  2535                             DEFAULT_VALUE.append("\n");
  2536                         } catch (java.io.IOException ioe) {
  2537                             throw new RuntimeException(ioe);
  2539                         return DEFAULT_VALUE;
  2541                 };
  2545         /**
  2546          * {@inheritDoc}
  2547          */
  2548         @Override
  2549         public Name getName(CharSequence cs) {
  2550             return StringName.instance(cs.toString());
  2553         private void checkElement(Element e) {
  2554             if(!(e instanceof CoreReflElement)) {
  2555                 throw new IllegalArgumentException();
  2559         @Override
  2560         public boolean isFunctionalInterface(TypeElement e) {
  2561             throw new UnsupportedOperationException();
  2562             // Update once this functionality is in core reflection
  2566     private static class CoreReflTypes implements javax.lang.model.util.Types {
  2567         private static Types instance = new CoreReflTypes();
  2569         public static Types instance() {
  2570             return instance;
  2573         // Private to suppress instantiation
  2574         private CoreReflTypes() {}
  2576         // Types methods
  2577         @Override
  2578         public Element asElement(TypeMirror t) {
  2579             checkType(t);
  2580             if (t instanceof javax.lang.model.type.TypeVariable) {
  2581                 ((javax.lang.model.type.TypeVariable)t).asElement();
  2582             } else if (t instanceof DeclaredType) {
  2583                 return ((DeclaredType)t).asElement();
  2585             return null;
  2588         @Override
  2589         public boolean isSameType(TypeMirror t1, TypeMirror t2) {
  2590             if (t1.getKind() != t2.getKind()) {
  2591                 return false;
  2594             if (t1.getKind() == TypeKind.WILDCARD ||
  2595                 t2.getKind() == TypeKind.WILDCARD) {
  2596                 // Wildcards are not equal to any type
  2597                 return false;
  2600             if (t1 instanceof CoreReflDeclaredType &&
  2601                 t2 instanceof CoreReflDeclaredType) {
  2602                 return ((CoreReflDeclaredType)t1).isSameType((CoreReflDeclaredType)t2);
  2603             } else if (t1 instanceof PrimitiveType &&
  2604                        t2 instanceof PrimitiveType) {
  2605                 return t1.getKind() == t2.getKind();
  2606             } else if (t1 instanceof NoType &&
  2607                        t2 instanceof NoType) {
  2608                 return true;
  2609             } else if (t1 instanceof NullType &&
  2610                        t2 instanceof NullType) {
  2611                 return true;
  2612             } else if (t1 instanceof ArrayType &&
  2613                        t2 instanceof ArrayType) {
  2614                 return isSameType(((ArrayType)t1).getComponentType(), ((ArrayType)t2).getComponentType());
  2617             return false;
  2620         @Override
  2621         public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
  2622             checkType(t1);
  2623             checkType(t2);
  2625             if (isSameType(t1, t2)) {
  2626                 return true;
  2627             } else if(t1.getKind() == TypeKind.NULL) {
  2628                 return true;
  2631             // This depth first traversal should terminate due to the ban on circular inheritance
  2632             List<? extends TypeMirror> directSupertypes = directSupertypes(t1);
  2633             if (directSupertypes.isEmpty()) {
  2634                 return false;
  2636             for (TypeMirror ti : directSupertypes) {
  2637                 if (isSameType(ti, t2) || isSubtype(ti, t2)) {
  2638                     return true;
  2641             return false;
  2644         @Override
  2645         public boolean isAssignable(TypeMirror t1, TypeMirror t2) {
  2646             throw new UnsupportedOperationException();
  2649         @Override
  2650         public boolean contains(TypeMirror t1, TypeMirror t2) {
  2651             throw new UnsupportedOperationException();
  2654         @Override
  2655         public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
  2656             checkType(m1);
  2657             checkType(m2);
  2659             ExecutableMethodType m0 = (ExecutableMethodType)m1;
  2661             return m0.sameSignature((ExecutableMethodType)m2) || m0.sameSignature((ExecutableMethodType)erasure(m2));
  2664         @Override
  2665         public List<? extends TypeMirror> directSupertypes(TypeMirror t) {
  2666             checkType(t);
  2667             if (t instanceof ExecutableType ||
  2668                 t.getKind() == TypeKind.PACKAGE) {
  2669                 throw new IllegalArgumentException("You can't ask for direct supertypes for type: " + t);
  2671             return ((AbstractTypeMirror)t).directSuperTypes();
  2674         @Override
  2675         public TypeMirror erasure(TypeMirror t) {
  2676             checkType(t);
  2677             return ((AbstractTypeMirror)t).erasure();
  2680         @Override
  2681         public TypeElement boxedClass(javax.lang.model.type.PrimitiveType p) {
  2682             throw new UnsupportedOperationException();
  2685         @Override
  2686         public PrimitiveType unboxedType(TypeMirror t) {
  2687             throw new UnsupportedOperationException();
  2690         @Override
  2691         public TypeMirror capture(TypeMirror t) {
  2692             checkType(t);
  2693             return ((AbstractTypeMirror)t).capture();
  2696         @Override
  2697         public PrimitiveType getPrimitiveType(TypeKind kind) {
  2698             return PrimitiveType.instance(kind);
  2701         @Override
  2702         public NullType getNullType() {
  2703             return CoreReflNullType.getInstance();
  2706         @Override
  2707         public javax.lang.model.type.NoType getNoType(TypeKind kind) {
  2708             if (kind == TypeKind.NONE) {
  2709                 return NoType.getNoneInstance();
  2710             } else if (kind == TypeKind.VOID) {
  2711                 return NoType.getVoidInstance();
  2712             } else {
  2713                 throw new IllegalArgumentException("No NoType of kind: " + kind);
  2717         @Override
  2718         public ArrayType getArrayType(TypeMirror componentType) {
  2719             throw new UnsupportedOperationException();
  2722         @Override
  2723         public javax.lang.model.type.WildcardType getWildcardType(TypeMirror extendsBound,
  2724                                                                   TypeMirror superBound) {
  2725             throw new UnsupportedOperationException();
  2728         @Override
  2729         public DeclaredType getDeclaredType(TypeElement typeElem, TypeMirror... typeArgs) {
  2730             throw new UnsupportedOperationException();
  2733         @Override
  2734         public javax.lang.model.type.DeclaredType getDeclaredType(javax.lang.model.type.DeclaredType containing,
  2735                                                                   TypeElement typeElem,
  2736                                                                   TypeMirror... typeArgs) {
  2737             throw new UnsupportedOperationException();
  2740         @Override
  2741         public TypeMirror asMemberOf(javax.lang.model.type.DeclaredType containing, Element element) {
  2742             throw new UnsupportedOperationException();
  2745         private void checkType(TypeMirror t) {
  2746             if (!(t instanceof AbstractTypeMirror)) {
  2747                 throw new IllegalArgumentException("This Types implementation can only operate on CoreReflectionFactory type classes");
  2752     private abstract static class CoreReflDeclaredType extends AbstractTypeMirror
  2753         implements javax.lang.model.type.DeclaredType {
  2754         private Class<?> source = null;
  2756         private CoreReflDeclaredType(Class<?> source) {
  2757             super(TypeKind.DECLARED);
  2758             this.source = source;
  2761         static DeclaredType instance(Class<?> source, Type genericSource) {
  2762             if (genericSource instanceof ParameterizedType) {
  2763                 return new ParameterizedDeclaredType(source, (ParameterizedType)genericSource);
  2764             } else if (genericSource instanceof Class) { // This happens when a field has a raw type
  2765                 if (!source.equals(genericSource)) {
  2766                     throw new IllegalArgumentException("Don't know how to handle this");
  2768                 return instance(source);
  2770             throw new IllegalArgumentException("Don't know how to create a declared type from: " +
  2771                                                source +
  2772                                                " and genericSource " +
  2773                                                genericSource);
  2776         static DeclaredType instance(Class<?> source) {
  2777             return new RawDeclaredType(source);
  2780         protected Class<?> getSource() {
  2781             return source;
  2784         @Override
  2785         public Element asElement() {
  2786             return CoreReflectionFactory.createMirror(getSource());
  2789         abstract boolean isSameType(DeclaredType other);
  2791         @Override
  2792         TypeMirror capture() {
  2793             return new CaptureDeclaredType(this);
  2796         private static class CaptureDeclaredType extends CoreReflDeclaredType {
  2797             CoreReflDeclaredType cap;
  2798             CaptureDeclaredType(CoreReflDeclaredType t) {
  2799                 super(t.source);
  2800                 this.cap = t;
  2803             @Override
  2804             public List<? extends TypeMirror> getTypeArguments() {
  2805                 List<? extends TypeMirror> wrapped = cap.getTypeArguments();
  2806                 ArrayList<TypeMirror> res = new ArrayList<>(wrapped.size());
  2807                 res.ensureCapacity(wrapped.size());
  2809                 for (int i = 0; i < wrapped.size(); i++) {
  2810                     TypeMirror t = wrapped.get(i);
  2812                     if (t instanceof javax.lang.model.type.WildcardType) {
  2813                         res.add(i, convert(t));
  2814                     } else {
  2815                         res.add(i, t);
  2818                 return Collections.unmodifiableList(res);
  2821             private TypeMirror convert(TypeMirror t) {
  2822                 if (!(t instanceof javax.lang.model.type.WildcardType)) {
  2823                     throw new IllegalArgumentException();
  2824                 } else {
  2825                     javax.lang.model.type.WildcardType w = (javax.lang.model.type.WildcardType)t;
  2826                     return TypeFactory.typeVariableInstance(w, w.getExtendsBound(), w.getSuperBound());
  2830             @Override
  2831             public TypeMirror getEnclosingType() {
  2832                 return cap.getEnclosingType();
  2835             @Override
  2836             List<? extends TypeMirror> directSuperTypes() {
  2837                 return cap.directSuperTypes();
  2840             @Override
  2841             boolean isSameType(DeclaredType other) {
  2842                 return other == this;
  2845             @Override
  2846             public String toString() {
  2847                 return " CAPTURE of: " + cap.toString();
  2851         private static class RawDeclaredType extends CoreReflDeclaredType
  2852             implements Reifiable {
  2853             private RawDeclaredType(Class<?> source) {
  2854                 super(source);
  2857             @Override
  2858             public Class<?> getSource() {
  2859                 return super.getSource();
  2862             @Override
  2863             public TypeMirror getEnclosingType() {
  2864                 Class<?> enclosing = getSource().getEnclosingClass();
  2865                 if (null == enclosing) {
  2866                     return NoType.getNoneInstance();
  2867                 } else {
  2868                     return TypeFactory.instance(enclosing);
  2872             @Override
  2873             public List<? extends TypeMirror> getTypeArguments() {
  2874                 return Collections.emptyList();
  2877             @Override
  2878             List<? extends TypeMirror> directSuperTypes() {
  2879                 if (getSource().isEnum()) {
  2880                     return enumSuper();
  2883                 if (getSource() == java.lang.Object.class) {
  2884                     return Collections.emptyList();
  2886                 List<TypeMirror> res = new ArrayList<>();
  2887                 Type[] superInterfaces = getSource().getInterfaces();
  2888                 if (!getSource().isInterface()) {
  2889                     res.add(TypeFactory.instance(getSource().getSuperclass()));
  2890                 } else if (superInterfaces.length == 0) {
  2891                     // Interfaces that don't extend another interface
  2892                     // have java.lang.Object as a direct supertype.
  2893                     return Collections.unmodifiableList(Arrays.asList(TypeFactory.instance(java.lang.Object.class)));
  2896                 for (Type t : superInterfaces) {
  2897                     res.add(TypeFactory.instance(t));
  2899                 return Collections.unmodifiableList(res);
  2902             private List<? extends TypeMirror> enumSuper() {
  2903                 Class<?> rawSuper = getSource().getSuperclass();
  2904                 Type[] actualArgs = ((ParameterizedTypeImpl)getSource().getGenericSuperclass()).getActualTypeArguments();
  2906                 // Reconsider this : assume the problem is making
  2907                 // Enum<MyEnum> rather than just a raw enum.
  2908                 return Collections.unmodifiableList(Arrays.asList(TypeFactory.instance(ParameterizedTypeImpl.make(rawSuper,
  2909                                                                                                                   Arrays.copyOf(actualArgs,
  2910                                                                                                                                 actualArgs.length),
  2911                                                                                                                   null))));
  2914             @Override
  2915             boolean isSameType(DeclaredType other) {
  2916                 if (other instanceof RawDeclaredType) {
  2917                     return Objects.equals(getSource(), ((RawDeclaredType)other).getSource());
  2918                 } else {
  2919                     return false;
  2923             @Override
  2924             public String toString() {
  2925                 return getSource().toString();
  2929         private static class ParameterizedDeclaredType extends CoreReflDeclaredType {
  2930             private ParameterizedType genericSource = null;
  2931             private ParameterizedDeclaredType(Class<?> source, ParameterizedType genericSource) {
  2932                 super(source);
  2933                 this.genericSource = genericSource;
  2936             @Override
  2937             public TypeMirror getEnclosingType() {
  2938                 Type me = genericSource;
  2939                 Type owner = GenericTypes.getEnclosingType(me);
  2940                 if (owner == null) {
  2941                     return NoType.getNoneInstance();
  2943                 return TypeFactory.instance(owner);
  2946             @Override
  2947             public List<? extends TypeMirror> getTypeArguments() {
  2948                 Type[] typeArgs = genericSource.getActualTypeArguments();
  2950                 int length = typeArgs.length;
  2951                 if (length == 0)
  2952                     return Collections.emptyList();
  2953                 else {
  2954                     List<TypeMirror> tmp = new ArrayList<>(length);
  2955                     for (Type t : typeArgs) {
  2956                         tmp.add(TypeFactory.instance(t));
  2958                     return Collections.unmodifiableList(tmp);
  2962             @Override
  2963             List<? extends TypeMirror> directSuperTypes() {
  2964                 if (getSource() == java.lang.Object.class) {
  2965                     return Collections.emptyList();
  2968                 List<TypeMirror> res = new ArrayList<>();
  2969                 Type[] superInterfaces = getSource().getGenericInterfaces();
  2970                 if (!getSource().isInterface()) {
  2971                     // Replace actual type arguments with our type arguments
  2972                     res.add(TypeFactory.instance(substituteTypeArgs(getSource().getGenericSuperclass())));
  2973                 } else if (superInterfaces.length == 0) {
  2974                     // Interfaces that don't extend another interface
  2975                     // have java.lang.Object as a direct supertype, plus
  2976                     // possibly the interface's raw type
  2977                     res.add(TypeFactory.instance(java.lang.Object.class));
  2980                 for (Type t : superInterfaces) {
  2981                     res.add(TypeFactory.instance(substituteTypeArgs(t)));
  2984                 res.add(TypeFactory.instance(getSource())); // Add raw type
  2985                 return Collections.unmodifiableList(res);
  2988             private Type substituteTypeArgs(Type type) {
  2989                 if (!(type instanceof ParameterizedType)) {
  2990                     return type;
  2993                 ParameterizedType target = (ParameterizedType)type;
  2994                 // Cast to get a Class instead of a plain type.
  2995                 Class<?> raw = ((ParameterizedTypeImpl)target).getRawType();
  2996                 Type[] actualArgs = genericSource.getActualTypeArguments();
  2998                 return  ParameterizedTypeImpl.make(raw, Arrays.copyOf(actualArgs, actualArgs.length), null);
  3001             @Override
  3002             boolean isSameType(DeclaredType other) {
  3003                 if (other instanceof ParameterizedDeclaredType) {
  3004                     return GenericTypes.isSameGenericType(genericSource,
  3005                                                           ((ParameterizedDeclaredType)other).genericSource);
  3006                 } else {
  3007                     return false;
  3011             @Override
  3012             public String toString() {
  3013                 return getKind().toString() + " " + genericSource.toString();
  3017         /**
  3018          * Implementing class for ParameterizedType interface.
  3019          * Derived from sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
  3020          */
  3022         private static class ParameterizedTypeImpl implements ParameterizedType {
  3023             private Type[] actualTypeArguments;
  3024             private Class<?>  rawType;
  3025             private Type   ownerType;
  3027             private ParameterizedTypeImpl(Class<?> rawType,
  3028                                           Type[] actualTypeArguments,
  3029                                           Type ownerType) {
  3030                 this.actualTypeArguments = actualTypeArguments;
  3031                 this.rawType             = rawType;
  3032                 if (ownerType != null) {
  3033                     this.ownerType = ownerType;
  3034                 } else {
  3035                     this.ownerType = rawType.getDeclaringClass();
  3037                 validateConstructorArguments();
  3040             private void validateConstructorArguments() {
  3041                 java.lang.reflect.TypeVariable/*<?>*/[] formals = rawType.getTypeParameters();
  3042                 // check correct arity of actual type args
  3043                 if (formals.length != actualTypeArguments.length){
  3044                     throw new MalformedParameterizedTypeException();
  3048             /**
  3049              * Static factory. Given a (generic) class, actual type arguments
  3050              * and an owner type, creates a parameterized type.
  3051              * This class can be instantiated with a a raw type that does not
  3052              * represent a generic type, provided the list of actual type
  3053              * arguments is empty.
  3054              * If the ownerType argument is null, the declaring class of the
  3055              * raw type is used as the owner type.
  3056              * <p> This method throws a MalformedParameterizedTypeException
  3057              * under the following circumstances:
  3058              * If the number of actual type arguments (i.e., the size of the
  3059              * array {@code typeArgs}) does not correspond to the number of
  3060              * formal type arguments.
  3061              * If any of the actual type arguments is not an instance of the
  3062              * bounds on the corresponding formal.
  3063              * @param rawType the Class representing the generic type declaration being
  3064              * instantiated
  3065              * @param actualTypeArguments - a (possibly empty) array of types
  3066              * representing the actual type arguments to the parameterized type
  3067              * @param ownerType - the enclosing type, if known.
  3068              * @return An instance of {@code ParameterizedType}
  3069              * @throws MalformedParameterizedTypeException - if the instantiation
  3070              * is invalid
  3071              */
  3072             public static ParameterizedTypeImpl make(Class<?> rawType,
  3073                                                      Type[] actualTypeArguments,
  3074                                                      Type ownerType) {
  3075                 return new ParameterizedTypeImpl(rawType, actualTypeArguments,
  3076                                                  ownerType);
  3080             /**
  3081              * Returns an array of {@code Type} objects representing the actual type
  3082              * arguments to this type.
  3084              * <p>Note that in some cases, the returned array be empty. This can occur
  3085              * if this type represents a non-parameterized type nested within
  3086              * a parameterized type.
  3088              * @return an array of {@code Type} objects representing the actual type
  3089              *     arguments to this type
  3090              * @throws {@code TypeNotPresentException} if any of the
  3091              *     actual type arguments refers to a non-existent type declaration
  3092              * @throws {@code MalformedParameterizedTypeException} if any of the
  3093              *     actual type parameters refer to a parameterized type that cannot
  3094              *     be instantiated for any reason
  3095              * @since 1.5
  3096              */
  3097             public Type[] getActualTypeArguments() {
  3098                 return actualTypeArguments.clone();
  3101             /**
  3102              * Returns the {@code Type} object representing the class or interface
  3103              * that declared this type.
  3105              * @return the {@code Type} object representing the class or interface
  3106              *     that declared this type
  3107              */
  3108             public Class<?> getRawType() {
  3109                 return rawType;
  3113             /**
  3114              * Returns a {@code Type} object representing the type that this type
  3115              * is a member of.  For example, if this type is {@code O<T>.I<S>},
  3116              * return a representation of {@code O<T>}.
  3118              * <p>If this type is a top-level type, {@code null} is returned.
  3120              * @return a {@code Type} object representing the type that
  3121              *     this type is a member of. If this type is a top-level type,
  3122              *     {@code null} is returned
  3123              */
  3124             public Type getOwnerType() {
  3125                 return ownerType;
  3128             /*
  3129              * From the JavaDoc for java.lang.reflect.ParameterizedType
  3130              * "Instances of classes that implement this interface must
  3131              * implement an equals() method that equates any two instances
  3132              * that share the same generic type declaration and have equal
  3133              * type parameters."
  3134              */
  3135             @Override
  3136             public boolean equals(Object o) {
  3137                 if (o instanceof ParameterizedType) {
  3138                     // Check that information is equivalent
  3139                     ParameterizedType that = (ParameterizedType) o;
  3141                     if (this == that)
  3142                         return true;
  3144                     Type thatOwner   = that.getOwnerType();
  3145                     Type thatRawType = that.getRawType();
  3147                     return Objects.equals(ownerType, thatOwner) &&
  3148                         Objects.equals(rawType, thatRawType) &&
  3149                         Arrays.equals(actualTypeArguments, // avoid clone
  3150                                       that.getActualTypeArguments());
  3151                 } else
  3152                     return false;
  3155             @Override
  3156             public int hashCode() {
  3157                 return
  3158                     Arrays.hashCode(actualTypeArguments) ^
  3159                     Objects.hashCode(ownerType) ^
  3160                     Objects.hashCode(rawType);
  3163             public String toString() {
  3164                 StringBuilder sb = new StringBuilder();
  3166                 if (ownerType != null) {
  3167                     if (ownerType instanceof Class)
  3168                         sb.append(((Class)ownerType).getName());
  3169                     else
  3170                         sb.append(ownerType.toString());
  3172                     sb.append(".");
  3174                     if (ownerType instanceof ParameterizedTypeImpl) {
  3175                         // Find simple name of nested type by removing the
  3176                         // shared prefix with owner.
  3177                         sb.append(rawType.getName().replace( ((ParameterizedTypeImpl)ownerType).rawType.getName() + "$",
  3178                                                              ""));
  3179                     } else
  3180                         sb.append(rawType.getName());
  3181                 } else
  3182                     sb.append(rawType.getName());
  3184                 if (actualTypeArguments != null &&
  3185                     actualTypeArguments.length > 0) {
  3186                     sb.append("<");
  3187                     boolean first = true;
  3188                     for (Type t: actualTypeArguments) {
  3189                         if (!first)
  3190                             sb.append(", ");
  3191                         if (t instanceof Class)
  3192                             sb.append(((Class)t).getName());
  3193                         else
  3194                             sb.append(t.toString());
  3195                         first = false;
  3197                     sb.append(">");
  3200                 return sb.toString();
  3206     private static class ErasedMethodType extends ExecutableMethodType implements javax.lang.model.type.ExecutableType {
  3207         private final Method m;
  3209         ErasedMethodType(Method m) {
  3210             super(m);
  3211             this.m = Objects.requireNonNull(m);
  3214         @Override
  3215         public List<javax.lang.model.type.TypeVariable> getTypeVariables() {
  3216             return Collections.emptyList();
  3219         @Override
  3220         public List<? extends TypeMirror> getThrownTypes() {
  3221             Class<?>[] exceptions = m.getExceptionTypes();
  3222             int len = exceptions.length;
  3224             if (len > 0) {
  3225                 List<TypeMirror> res = new ArrayList<TypeMirror>(len);
  3226                 for (Class<?> t : exceptions) {
  3227                     res.add(TypeFactory.instance(t));
  3229                 return Collections.unmodifiableList(res);
  3230             } else {
  3231                 List<TypeMirror> ret = Collections.emptyList();
  3232                 return ret;
  3236         @Override
  3237         public List<? extends TypeMirror> getParameterTypes() {
  3238             Class<?>[] params = m.getParameterTypes();
  3239             int len = params.length;
  3241             if (len > 0) {
  3242                 List<TypeMirror> res = new ArrayList<TypeMirror>(len);
  3243                 for (Class<?> t : params) {
  3244                     res.add(TypeFactory.instance(t));
  3246                 return Collections.unmodifiableList(res);
  3247             } else {
  3248                 List<TypeMirror> ret = Collections.emptyList();
  3249                 return ret;
  3253         @Override
  3254         public TypeMirror getReturnType() {
  3255             return TypeFactory.instance(m.getReturnType());
  3258         @Override
  3259         TypeMirror erasure() {
  3260             return this;
  3264     private static class ErrorType extends AbstractTypeMirror implements javax.lang.model.type.ErrorType {
  3265         private static ErrorType errorType = new ErrorType();
  3267         public static ErrorType getErrorInstance() {
  3268             return errorType;
  3271         private ErrorType() {
  3272             super(TypeKind.ERROR);
  3275         @Override
  3276         public List<? extends TypeMirror> getTypeArguments() {
  3277             throw new UnsupportedOperationException();
  3280         @Override
  3281         public TypeMirror getEnclosingType() {
  3282             throw new UnsupportedOperationException();
  3285         @Override
  3286         public Element asElement() {
  3287             throw new UnsupportedOperationException();
  3290         @Override
  3291         List<? extends TypeMirror> directSuperTypes() {
  3292             throw new UnsupportedOperationException();
  3296     private static class ExecutableMethodType extends AbstractTypeMirror
  3297         implements javax.lang.model.type.ExecutableType {
  3298         private final Method m;
  3300         ExecutableMethodType(Method m) {
  3301             super(TypeKind.EXECUTABLE);
  3302             this.m = Objects.requireNonNull(m);
  3305         @Override
  3306         public List<? extends TypeMirror> getThrownTypes() {
  3307             Type[] exceptions = m.getGenericExceptionTypes();
  3308             int len = exceptions.length;
  3310             if (len > 0) {
  3311                 List<TypeMirror> res = new ArrayList<TypeMirror>(len);
  3312                 for (Type t : exceptions) {
  3313                     res.add(TypeFactory.instance(t));
  3315                 return Collections.unmodifiableList(res);
  3316             } else {
  3317                 List<TypeMirror> ret = Collections.emptyList();
  3318                 return ret;
  3322         @Override
  3323         public List<javax.lang.model.type.TypeVariable> getTypeVariables() {
  3324             java.lang.reflect.TypeVariable[] variables = m.getTypeParameters();
  3325             int len = variables.length;
  3327             if (len > 0) {
  3328                 List<javax.lang.model.type.TypeVariable> res = new ArrayList<>(len);
  3329                 for (java.lang.reflect.TypeVariable<?> t : variables) {
  3330                     res.add(TypeFactory.typeVariableInstance(t));
  3332                 return Collections.unmodifiableList(res);
  3333             } else {
  3334                 return Collections.emptyList();
  3338         @Override
  3339         public TypeMirror getReturnType() {
  3340             return TypeFactory.instance(m.getGenericReturnType());
  3343         @Override
  3344         public List<? extends TypeMirror> getParameterTypes() {
  3345             Type[] params = m.getGenericParameterTypes();
  3346             int len = params.length;
  3348             if (len > 0) {
  3349                 List<TypeMirror> res = new ArrayList<TypeMirror>(len);
  3350                 for (Type t : params) {
  3351                     res.add(TypeFactory.instance(t));
  3353                 return Collections.unmodifiableList(res);
  3354             } else {
  3355                 return Collections.emptyList();
  3359         @Override
  3360         List<? extends TypeMirror> directSuperTypes() {
  3361             // Spec says we don't need this
  3362             throw new UnsupportedOperationException();
  3365         @Override
  3366         TypeMirror erasure() {
  3367             return new ErasedMethodType(m);
  3370         @Override
  3371         public TypeMirror getReceiverType() {
  3372             throw new UnsupportedOperationException();
  3375         boolean sameSignature(ExecutableMethodType other){
  3376             if (!m.getName().equals(other.m.getName())) {
  3377                 return false;
  3380             List<? extends TypeMirror> thisParams = getParameterTypes();
  3381             List<? extends TypeMirror> otherParams = other.getParameterTypes();
  3382             if (thisParams.size() != otherParams.size()) {
  3383                 return false;
  3385             for (int i = 0; i < thisParams.size(); i++) {
  3386                 if (!CoreReflTypes.instance().isSameType(thisParams.get(i), otherParams.get(i))) {
  3387                     return false;
  3390             return true;
  3394     private static class GenericTypes {
  3395         public static boolean isSameGenericType(Type t1, Type t2) {
  3396             if (t1 instanceof Class) {
  3397                 return ((Class)t1).equals(t2);
  3398             } else if (t1 instanceof ParameterizedType) {
  3399                 return ((ParameterizedType)t1).equals(t2);
  3401             throw new UnsupportedOperationException();
  3404         public static Type getEnclosingType(Type t1) {
  3405             if (t1 instanceof Class) {
  3406                 return ((Class)t1).getEnclosingClass();
  3407             } else if (t1 instanceof ParameterizedType) {
  3408                 return ((ParameterizedType)t1).getOwnerType();
  3410             throw new UnsupportedOperationException();
  3414     private static class IntersectionDeclaredType extends AbstractTypeMirror
  3415         implements javax.lang.model.type.DeclaredType {
  3416         private Type[] sources = null;
  3418         IntersectionDeclaredType(Type[] sources) {
  3419             super(TypeKind.DECLARED);
  3420             this.sources = Arrays.copyOf(Objects.requireNonNull(sources),
  3421                                          sources.length);
  3424         @Override
  3425         public TypeMirror getEnclosingType() {
  3426             return NoType.getNoneInstance();
  3429         @Override
  3430         public  Element asElement() {
  3431             throw new UnsupportedOperationException();
  3434         @Override
  3435         public List<? extends TypeMirror> getTypeArguments() {
  3436             throw new UnsupportedOperationException();
  3439         @Override
  3440         List<? extends TypeMirror> directSuperTypes() {
  3441             int len = sources.length;
  3443             if (len > 0) {
  3444                 List<TypeMirror> res = new ArrayList<TypeMirror>(len);
  3445                 for (Type c : sources) {
  3446                     res.add(TypeFactory.instance(c));
  3448                 return Collections.unmodifiableList(res);
  3449             } else {
  3450                 return Collections.emptyList();
  3455     private static class ModelWildcardType extends AbstractTypeMirror
  3456         implements javax.lang.model.type.WildcardType {
  3457         private java.lang.reflect.WildcardType genericSource;
  3459         ModelWildcardType(java.lang.reflect.WildcardType genericSource) {
  3460             super(TypeKind.WILDCARD);
  3461             this.genericSource = Objects.requireNonNull(genericSource);
  3464         @Override
  3465         List<? extends TypeMirror> directSuperTypes() {
  3466             // TODO Add support for this operation
  3467             throw new UnsupportedOperationException();
  3470         @Override
  3471         public TypeMirror getExtendsBound() {
  3472             Type[] t = genericSource.getUpperBounds();
  3474             if (t.length == 1) {
  3475                 if (t[0].equals(Object.class) && getSuperBound() != null) { // can't have both lower and upper explicit
  3476                     return null;
  3478                 return TypeFactory.instance(t[0]);
  3480             throw new UnsupportedOperationException(); // TODO: intersection type?
  3483         @Override
  3484         public TypeMirror getSuperBound() {
  3485             Type[] t = genericSource.getLowerBounds();
  3487             if (t.length == 0) { // bound is null
  3488                 return null;
  3489             } else if (t.length == 1) {
  3490                 return TypeFactory.instance(t[0]);
  3492             throw new UnsupportedOperationException(); // TODO: intersection type?
  3495         @Override
  3496         public String toString() {
  3497             return getKind() + " " + genericSource.toString();
  3501     private static class NoType extends AbstractTypeMirror
  3502         implements javax.lang.model.type.NoType {
  3503         private static NoType noneType = new NoType(TypeKind.NONE, "none");
  3504         private static NoType packageType = new NoType(TypeKind.PACKAGE, "package");
  3505         private static NoType voidType = new NoType(TypeKind.VOID, "void");
  3507         private String str;
  3509         public static NoType getNoneInstance() {
  3510             return noneType;
  3513         public static NoType getPackageInstance() {
  3514             return packageType;
  3517         public static NoType getVoidInstance() {
  3518             return voidType;
  3521         private NoType(TypeKind k, String str) {
  3522             super(k);
  3523             this.str = str;
  3526         @Override
  3527         List<? extends TypeMirror> directSuperTypes() {
  3528             // TODO We don't need this for the Package instance, how about the others?
  3529             throw new UnsupportedOperationException();
  3532         @Override
  3533         public String toString() {
  3534             return str;
  3538     private static class CoreReflNullType extends AbstractTypeMirror
  3539         implements javax.lang.model.type.NullType {
  3540         private static CoreReflNullType nullType = new CoreReflNullType();
  3542         public static NullType getInstance() {
  3543             return nullType;
  3546         private CoreReflNullType() {
  3547             super(TypeKind.NULL);
  3550         @Override
  3551         List<? extends TypeMirror> directSuperTypes() {
  3552             // JLS 4.10.2 says:
  3553             // "The direct supertypes of the null type are all reference types other than the null type itself."
  3554             // TODO return null? an empty list? the error type? anyhow fix this
  3555             throw new UnsupportedOperationException();
  3559     private static interface Reifiable {
  3560         Class<?> getSource();
  3563     private static class PrimitiveType extends AbstractTypeMirror
  3564         implements javax.lang.model.type.PrimitiveType,
  3565                    Reifiable {
  3566         private Class<?> source;
  3568         private static PrimitiveType booleanInstance = new PrimitiveType(TypeKind.BOOLEAN, boolean.class);
  3569         private static PrimitiveType byteInstance =    new PrimitiveType(TypeKind.BYTE, byte.class);
  3570         private static PrimitiveType charInstance =    new PrimitiveType(TypeKind.CHAR, char.class);
  3571         private static PrimitiveType shortInstance =   new PrimitiveType(TypeKind.SHORT, short.class);
  3572         private static PrimitiveType intInstance =     new PrimitiveType(TypeKind.INT, int.class);
  3573         private static PrimitiveType longInstance =    new PrimitiveType(TypeKind.LONG, long.class);
  3574         private static PrimitiveType floatInstance =   new PrimitiveType(TypeKind.FLOAT, float.class);
  3575         private static PrimitiveType doubleInstance =  new PrimitiveType(TypeKind.DOUBLE, double.class);
  3577         private PrimitiveType(TypeKind kind, Class<?> source) {
  3578             super(kind);
  3579             this.source = source;
  3582         @Override
  3583         public Class<?> getSource() {
  3584             return source;
  3587         static PrimitiveType instance(Class<?> c) {
  3588             switch(c.getName()) {
  3589             case "boolean":
  3590                 return booleanInstance;
  3591             case "byte":
  3592                 return byteInstance;
  3593             case "char":
  3594                 return charInstance;
  3595             case "short":
  3596                 return shortInstance;
  3597             case "int":
  3598                 return intInstance;
  3599             case "long":
  3600                 return longInstance;
  3601             case "float":
  3602                 return floatInstance;
  3603             case "double":
  3604                 return doubleInstance;
  3605             default:
  3606                 throw new IllegalArgumentException();
  3610         static PrimitiveType instance(TypeKind k) {
  3611             switch(k) {
  3612             case BOOLEAN:
  3613                 return booleanInstance;
  3614             case BYTE:
  3615                 return byteInstance;
  3616             case CHAR:
  3617                 return charInstance;
  3618             case SHORT:
  3619                 return shortInstance;
  3620             case INT:
  3621                 return intInstance;
  3622             case LONG:
  3623                 return longInstance;
  3624             case FLOAT:
  3625                 return floatInstance;
  3626             case DOUBLE:
  3627                 return doubleInstance;
  3628             default:
  3629                 throw new IllegalArgumentException();
  3633         @Override
  3634         public String toString() {
  3635             return source.getName();
  3638         //Types methods
  3639         @Override
  3640         List<? extends TypeMirror> directSuperTypes() {
  3641             switch (getKind()) {
  3642             case DOUBLE:
  3643                 return Collections.emptyList();
  3644             case FLOAT:
  3645                 return Arrays.asList(doubleInstance);
  3646             case LONG:
  3647                 return Arrays.asList(floatInstance);
  3648             case INT:
  3649                 return Arrays.asList(longInstance);
  3650             case CHAR:
  3651                 return Arrays.asList(intInstance);
  3652             case SHORT:
  3653                 return Arrays.asList(intInstance);
  3654             case BYTE:
  3655                 return Arrays.asList(shortInstance);
  3656             default:
  3657                 return Collections.emptyList();
  3662     private static class TypeFactory {
  3663         private TypeFactory() { }// no instances for you
  3665         public static TypeMirror instance(Class<?> c) {
  3666             if (c.isPrimitive()) {
  3667                 if (c.getName().equals("void")) {
  3668                     return NoType.getVoidInstance();
  3669                 } else {
  3670                     return PrimitiveType.instance(c);
  3672             } else if (c.isArray()) {
  3673                 return new CoreReflArrayType(c);
  3674             } else if (c.isAnonymousClass() ||
  3675                        c.isLocalClass() ||
  3676                        c.isMemberClass() ||
  3677                        c.isInterface() || // covers annotations
  3678                        c.isEnum()) {
  3679                 return CoreReflDeclaredType.instance(c);
  3680             } else { // plain old class ??
  3681                 return CoreReflDeclaredType.instance(c);
  3685         public static TypeMirror instance(Type t) {
  3686             if (t instanceof Class) {
  3687                 return instance((Class)t);
  3688             } else if (t instanceof ParameterizedType) {
  3689                 ParameterizedType tmp = (ParameterizedType)t;
  3690                 Type raw = tmp.getRawType();
  3691                 if (!(raw instanceof Class)) {
  3692                     throw new IllegalArgumentException(t + " " + raw );
  3694                 return CoreReflDeclaredType.instance((Class)raw, tmp);
  3695             } else if (t instanceof java.lang.reflect.WildcardType) {
  3696                 return new ModelWildcardType((java.lang.reflect.WildcardType)t);
  3697             } else if (t instanceof java.lang.reflect.TypeVariable) {
  3698             return new CoreReflTypeVariable((java.lang.reflect.TypeVariable)t);
  3700             throw new IllegalArgumentException("Don't know how to make instance from: " + t.getClass());
  3703         public static TypeMirror instance(Field f) {
  3704             return CoreReflDeclaredType.instance(f.getType(), f.getGenericType());
  3707         public static ExecutableType instance(Method m) {
  3708             return new ExecutableMethodType(m);
  3711         public static javax.lang.model.type.TypeVariable typeVariableInstance(java.lang.reflect.TypeVariable<?> v) {
  3712             return new CoreReflTypeVariable(v);
  3715         public static javax.lang.model.type.TypeVariable typeVariableInstance(TypeMirror source,
  3716                                                         TypeMirror upperBound,
  3717                                                         TypeMirror lowerBound) {
  3718             return new CaptureTypeVariable(source, upperBound, lowerBound);
  3722     private static class CoreReflTypeVariable extends AbstractTypeMirror
  3723         implements javax.lang.model.type.TypeVariable {
  3724         private final java.lang.reflect.TypeVariable<?> source;
  3725         private boolean isCapture = false;
  3727         protected CoreReflTypeVariable(java.lang.reflect.TypeVariable<?> source) {
  3728             super(TypeKind.TYPEVAR);
  3729             Objects.requireNonNull(source);
  3730             this.source = source;
  3733         @Override
  3734         public TypeMirror getUpperBound() {
  3735             return new IntersectionDeclaredType(source.getBounds());
  3738         @Override
  3739         public TypeMirror getLowerBound() {
  3740             return CoreReflTypes.instance().getNullType();
  3743         @Override
  3744         public Element asElement() {
  3745             return CoreReflectionFactory.createMirror(source);
  3748         @Override
  3749         List<? extends TypeMirror> directSuperTypes() {
  3750             return ((AbstractTypeMirror)getUpperBound()).directSuperTypes();
  3753         @Override
  3754         public int hashCode() {
  3755             return source.hashCode();
  3758         @Override
  3759         public boolean equals(Object other) {
  3760             if (other instanceof CoreReflTypeVariable) {
  3761                 return this.source.equals(((CoreReflTypeVariable)other).source);
  3762             } else {
  3763                 return false;

mercurial