test/tools/javac/processing/model/util/OverridesSpecEx.java

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 495
fe17a9dbef03
permissions
-rw-r--r--

Initial load

     1 /*
     2  * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  */
    24 /*
    25  * @test
    26  * @bug     6453386
    27  * @summary Verify that example code in Elements.overrides works as spec'ed.
    28  * @author  Scott Seligman
    29  * @compile -g OverridesSpecEx.java
    30  * @compile -processor OverridesSpecEx -proc:only OverridesSpecEx.java
    31  */
    33 import java.util.Set;
    34 import javax.annotation.processing.*;
    35 import javax.lang.model.SourceVersion;
    36 import javax.lang.model.element.*;
    37 import javax.lang.model.type.*;
    38 import javax.lang.model.util.*;
    40 import static javax.lang.model.util.ElementFilter.*;
    43 @SupportedSourceVersion(SourceVersion.RELEASE_6)
    44 @SupportedAnnotationTypes("*")
    45 public class OverridesSpecEx extends AbstractProcessor {
    47     Elements elements;
    48     Types types;
    50     public void init(ProcessingEnvironment penv) {
    51         super.init(penv);
    52         elements = penv.getElementUtils();
    53         types =  penv.getTypeUtils();
    54     }
    56     public boolean process(Set<? extends TypeElement> annoTypes,
    57                            RoundEnvironment round) {
    58         if (!round.processingOver())
    59             doit(annoTypes, round);
    60         return true;
    61     }
    63     private void doit(Set<? extends TypeElement> annoTypes,
    64                       RoundEnvironment round) {
    65         TypeElement string = elements.getTypeElement("java.lang.String");
    66         TypeElement object = elements.getTypeElement("java.lang.Object");
    68         ExecutableElement m1 = null;
    69         ExecutableElement m2 = null;
    70         for (ExecutableElement m : methodsIn(string.getEnclosedElements())) {
    71             if (m.getSimpleName().contentEquals("hashCode")) {
    72                 m1 = m;
    73                 break;
    74             }
    75         }
    76         for (ExecutableElement m : methodsIn(object.getEnclosedElements())) {
    77             if (m.getSimpleName().contentEquals("hashCode")) {
    78                 m2 = m;
    79                 break;
    80             }
    81         }
    83         boolean res =
    84             elements.overrides(m1, m2, (TypeElement) m1.getEnclosingElement());
    85         System.out.println("String.hashCode overrides Object.hashCode?  " + res);
    86         checkResult(res);
    88         TypeElement a = elements.getTypeElement("OverridesSpecEx.A");
    89         TypeElement b = elements.getTypeElement("OverridesSpecEx.B");
    90         TypeElement c = elements.getTypeElement("OverridesSpecEx.C");
    92         m1 = null;
    93         m2 = null;
    94         for (ExecutableElement m : methodsIn(a.getEnclosedElements()))
    95             m1 = m;
    96         for (ExecutableElement m : methodsIn(b.getEnclosedElements()))
    97             m2 = m;
    99         res = elements.overrides(m1, m2, a);
   100         System.out.println("A.m overrides B.m in B?  " + res);
   101         checkResult(!res);
   102         res = elements.overrides(m1, m2, c);
   103         System.out.println("A.m overrides B.m in C?  " + res);
   104         checkResult(res);
   105     }
   107     private static void checkResult(boolean truthiness) {
   108         if (!truthiness)
   109             throw new AssertionError("Bogus result");
   110     }
   113     // Fodder for the processor
   115     class A {
   116         public void m() {}
   117     }
   118     interface B {
   119         void m();
   120     }
   121     class C extends A implements B {
   122     }
   123 }

mercurial