test/tools/javac/processing/model/element/TestExecutableElement.java

Fri, 21 Dec 2012 08:45:43 -0800

author
darcy
date
Fri, 21 Dec 2012 08:45:43 -0800
changeset 1466
b52a38d4536c
parent 1459
bc74006c2d8d
child 1672
0d47e6131490
permissions
-rw-r--r--

8005282: Use @library tag with non-relative path for javac tests
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2012, 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 8005046
    27  * @summary Test basic properties of javax.lang.element.Element
    28  * @author  Joseph D. Darcy
    29  * @library /tools/javac/lib
    30  * @build   JavacTestingAbstractProcessor TestExecutableElement
    31  * @compile -processor TestExecutableElement -proc:only TestExecutableElement.java
    32  */
    34 import java.lang.annotation.*;
    35 import java.util.Formatter;
    36 import java.util.Set;
    37 import java.util.Objects;
    38 import javax.annotation.processing.*;
    39 import javax.lang.model.SourceVersion;
    40 import static javax.lang.model.SourceVersion.*;
    41 import javax.lang.model.element.*;
    42 import javax.lang.model.util.*;
    43 import static javax.lang.model.util.ElementFilter.*;
    44 import static javax.tools.Diagnostic.Kind.*;
    45 import static javax.tools.StandardLocation.*;
    47 /**
    48  * Test some basic workings of javax.lang.element.ExecutableElement
    49  */
    50 public class TestExecutableElement extends JavacTestingAbstractProcessor implements ProviderOfDefault {
    51     @IsDefault(false)
    52     public boolean process(Set<? extends TypeElement> annotations,
    53                            RoundEnvironment roundEnv) {
    54         int errors = 0;
    55         if (!roundEnv.processingOver()) {
    56             boolean hasRun = false;
    57             for (Element element : roundEnv.getRootElements()) {
    58                 for (ExecutableElement method : methodsIn(element.getEnclosedElements())) {
    59                     hasRun = true;
    60                     errors += checkIsDefault(method);
    61                 }
    62             }
    64             if (!hasRun) {
    65                 messager.printMessage(ERROR, "No test cases run; test fails.");
    66             }
    67         }
    68         return true;
    69     }
    71     @IsDefault(false)
    72     int checkIsDefault(ExecutableElement method) {
    73         System.out.println("Testing " + method);
    74         IsDefault expectedIsDefault = method.getAnnotation(IsDefault.class);
    76         boolean expectedDefault = (expectedIsDefault != null) ?
    77             expectedIsDefault.value() :
    78             false;
    80         boolean methodIsDefault = method.isDefault();
    82         if (methodIsDefault != expectedDefault) {
    83             messager.printMessage(ERROR,
    84                                   new Formatter().format("Unexpected Executable.isDefault result: got %s, expected %s",
    85                                                          expectedDefault,
    86                                                          methodIsDefault).toString(),
    87                                   method);
    88             return 1;
    89         }
    90         return 0;
    91     }
    92 }
    94 /**
    95  * Expected value of the ExecutableElement.isDefault method.
    96  */
    97 @Retention(RetentionPolicy.RUNTIME)
    98 @Target(ElementType.METHOD)
    99 @interface IsDefault {
   100     boolean value();
   101 }
   103 /**
   104  * Test interface to provide a default method.
   105  */
   106 interface ProviderOfDefault {
   107     @IsDefault(false)
   108     boolean process(Set<? extends TypeElement> annotations,
   109                     RoundEnvironment roundEnv);
   111     @IsDefault(true)
   112     default void quux() {};
   113 }

mercurial