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

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 495
fe17a9dbef03
child 699
d2aaaec153e8
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2006, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * 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 @SupportedAnnotationTypes("*")
    44 public class OverridesSpecEx extends AbstractProcessor {
    46     Elements elements;
    47     Types types;
    49     public void init(ProcessingEnvironment penv) {
    50         super.init(penv);
    51         elements = penv.getElementUtils();
    52         types =  penv.getTypeUtils();
    53     }
    55     public boolean process(Set<? extends TypeElement> annoTypes,
    56                            RoundEnvironment round) {
    57         if (!round.processingOver())
    58             doit(annoTypes, round);
    59         return true;
    60     }
    62     @Override
    63     public SourceVersion getSupportedSourceVersion() {
    64         return SourceVersion.latest();
    65     }
    67     private void doit(Set<? extends TypeElement> annoTypes,
    68                       RoundEnvironment round) {
    69         TypeElement string = elements.getTypeElement("java.lang.String");
    70         TypeElement object = elements.getTypeElement("java.lang.Object");
    72         ExecutableElement m1 = null;
    73         ExecutableElement m2 = null;
    74         for (ExecutableElement m : methodsIn(string.getEnclosedElements())) {
    75             if (m.getSimpleName().contentEquals("hashCode")) {
    76                 m1 = m;
    77                 break;
    78             }
    79         }
    80         for (ExecutableElement m : methodsIn(object.getEnclosedElements())) {
    81             if (m.getSimpleName().contentEquals("hashCode")) {
    82                 m2 = m;
    83                 break;
    84             }
    85         }
    87         boolean res =
    88             elements.overrides(m1, m2, (TypeElement) m1.getEnclosingElement());
    89         System.out.println("String.hashCode overrides Object.hashCode?  " + res);
    90         checkResult(res);
    92         TypeElement a = elements.getTypeElement("OverridesSpecEx.A");
    93         TypeElement b = elements.getTypeElement("OverridesSpecEx.B");
    94         TypeElement c = elements.getTypeElement("OverridesSpecEx.C");
    96         m1 = null;
    97         m2 = null;
    98         for (ExecutableElement m : methodsIn(a.getEnclosedElements()))
    99             m1 = m;
   100         for (ExecutableElement m : methodsIn(b.getEnclosedElements()))
   101             m2 = m;
   103         res = elements.overrides(m1, m2, a);
   104         System.out.println("A.m overrides B.m in B?  " + res);
   105         checkResult(!res);
   106         res = elements.overrides(m1, m2, c);
   107         System.out.println("A.m overrides B.m in C?  " + res);
   108         checkResult(res);
   109     }
   111     private static void checkResult(boolean truthiness) {
   112         if (!truthiness)
   113             throw new AssertionError("Bogus result");
   114     }
   117     // Fodder for the processor
   119     class A {
   120         public void m() {}
   121     }
   122     interface B {
   123         void m();
   124     }
   125     class C extends A implements B {
   126     }
   127 }

mercurial