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

Print this page


   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  */
  31 
  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.*;
  41 
  42 import static javax.lang.model.SourceVersion.RELEASE_8;
  43 import static java.util.Objects.*;
  44 
  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


 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     }
 476 
 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     }
 486 
 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     }
 497 
 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> {
 505 
 506         protected SimpleReflectionElementVisitor8(){
 507             super();
 508         }
 509 
 510         protected SimpleReflectionElementVisitor8(R defaultValue) {
 511             super(defaultValue);
 512         }
 513 
 514         // Create manual "bridge methods" for now.
 515 
 516         @Override
 517         public final R visitPackage(PackageElement e, P p) {
 518             return visitPackage((ReflectionPackageElement) e , p);
 519         }
 520 
 521         @Override
 522         public final R visitType(TypeElement e, P p) {
 523             return visitType((ReflectionTypeElement) e , p);
 524         }
 525 
 526         @Override
 527         public final R visitVariable(VariableElement e, P p) {
 528             return visitVariable((ReflectionVariableElement) e , p);
 529         }
 530 


2510                 }
2511             } finally {
2512                 try {
2513                     w.flush();
2514                 } catch (java.io.IOException e) { /* Ignore */;}
2515             }
2516         }
2517 
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);
2538                         }
2539                         return DEFAULT_VALUE;
2540                     }
2541                 };
2542             }
2543         }
2544 
2545         /**
2546          * {@inheritDoc}
2547          */
2548         @Override
2549         public Name getName(CharSequence cs) {
2550             return StringName.instance(cs.toString());


   1 /*
   2  * Copyright (c) 2012, 2014, 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  */
  31 
  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.*;
  41 
  42 import static javax.lang.model.SourceVersion.RELEASE_9;
  43 import static java.util.Objects.*;
  44 
  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


 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     }
 476 
 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     }
 486 
 487     /**
 488      * Base class for concrete visitors of elements backed by core reflection.
 489      */
 490     public static abstract class AbstractReflectionElementVisitor9<R, P>
 491         extends AbstractElementVisitor9<R, P>
 492         implements ReflectionElementVisitor<R, P> {
 493         protected AbstractReflectionElementVisitor9() {
 494             super();
 495         }
 496     }
 497 
 498     /**
 499      * Base class for simple visitors of elements that are backed by core reflection.
 500      */
 501     @SupportedSourceVersion(value=RELEASE_9)
 502     public static abstract class SimpleReflectionElementVisitor9<R, P>
 503         extends SimpleElementVisitor9<R, P>
 504         implements ReflectionElementVisitor<R, P> {
 505 
 506         protected SimpleReflectionElementVisitor9(){
 507             super();
 508         }
 509 
 510         protected SimpleReflectionElementVisitor9(R defaultValue) {
 511             super(defaultValue);
 512         }
 513 
 514         // Create manual "bridge methods" for now.
 515 
 516         @Override
 517         public final R visitPackage(PackageElement e, P p) {
 518             return visitPackage((ReflectionPackageElement) e , p);
 519         }
 520 
 521         @Override
 522         public final R visitType(TypeElement e, P p) {
 523             return visitType((ReflectionTypeElement) e , p);
 524         }
 525 
 526         @Override
 527         public final R visitVariable(VariableElement e, P p) {
 528             return visitVariable((ReflectionVariableElement) e , p);
 529         }
 530 


2510                 }
2511             } finally {
2512                 try {
2513                     w.flush();
2514                 } catch (java.io.IOException e) { /* Ignore */;}
2515             }
2516         }
2517 
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 ElementScanner9<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);
2538                         }
2539                         return DEFAULT_VALUE;
2540                     }
2541                 };
2542             }
2543         }
2544 
2545         /**
2546          * {@inheritDoc}
2547          */
2548         @Override
2549         public Name getName(CharSequence cs) {
2550             return StringName.instance(cs.toString());