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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 2012, 2013, 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 8011052 8025087
    27  * @summary Test basic properties of javax.lang.element.ExecutableElement
    28  * @author  Joseph D. Darcy
    29  * @library /tools/javac/lib
    30  * @build   JavacTestingAbstractProcessor TestExecutableElement
    31  * @compile -processor TestExecutableElement -proc:only -AexpectedMethodCount=7 TestExecutableElement.java
    32  * @compile/process -processor TestExecutableElement -proc:only -AexpectedMethodCount=3 ProviderOfDefault
    33  */
    35 import java.lang.annotation.*;
    36 import java.util.Formatter;
    37 import java.util.Set;
    38 import java.util.regex.*;
    39 import javax.annotation.processing.*;
    40 import javax.lang.model.element.*;
    41 import static javax.lang.model.util.ElementFilter.*;
    42 import static javax.tools.Diagnostic.Kind.*;
    44 /**
    45  * Test some basic workings of javax.lang.element.ExecutableElement
    46  */
    47 @SupportedOptions("expectedMethodCount")
    48 public class TestExecutableElement extends JavacTestingAbstractProcessor implements ProviderOfDefault {
    49     private int seenMethods = 0;
    50     @IsDefault(false)
    51     public boolean process(Set<? extends TypeElement> annotations,
    52                            RoundEnvironment roundEnv) {
    53         if (!roundEnv.processingOver()) {
    54             for (Element element : roundEnv.getRootElements()) {
    55                 for (ExecutableElement method : methodsIn(element.getEnclosedElements())) {
    56                     checkIsDefault(method);
    57                     seenMethods++;
    58                 }
    59             }
    60         } else {
    61             String expectedMethodCountStr = processingEnv.getOptions().get("expectedMethodCount");
    62             if (expectedMethodCountStr == null) {
    63                 messager.printMessage(ERROR, "No expected method count specified.");
    64             } else {
    65                 int expectedMethodCount = Integer.parseInt(expectedMethodCountStr);
    67                 if (seenMethods != expectedMethodCount) {
    68                     messager.printMessage(ERROR, "Wrong number of seen methods: " + seenMethods);
    69                 }
    70             }
    71         }
    72         return true;
    73     }
    75     @IsDefault(false)
    76     void checkIsDefault(ExecutableElement method) {
    77         System.out.println("Testing " + method);
    78         IsDefault expectedIsDefault = method.getAnnotation(IsDefault.class);
    80         boolean expectedDefault = (expectedIsDefault != null) ?
    81             expectedIsDefault.value() :
    82             false;
    84         boolean methodIsDefault = method.isDefault();
    86         if (expectedDefault) {
    87             if (!method.getModifiers().contains(Modifier.DEFAULT)) {
    88                 messager.printMessage(ERROR,
    89                                       "Modifier \"default\" not present as expected.",
    90                                       method);
    91             }
    93             // Check printing output
    94             java.io.Writer stringWriter = new java.io.StringWriter();
    95             eltUtils.printElements(stringWriter, method);
    96             Pattern p = Pattern.compile(expectedIsDefault.expectedTextRegex(), Pattern.DOTALL);
    98             if (! p.matcher(stringWriter.toString()).matches()) {
    99                 messager.printMessage(ERROR,
   100                                       new Formatter().format("Unexpected printing ouptput:%n\tgot %s,%n\texpected pattern %s.",
   101                                                              stringWriter.toString(),
   102                                                              expectedIsDefault.expectedTextRegex()).toString(),
   103                                       method);
   104             }
   106             System.out.println("\t" + stringWriter.toString());
   108         } else {
   109             if (method.getModifiers().contains(Modifier.DEFAULT)) {
   110                 messager.printMessage(ERROR,
   111                                       "Modifier \"default\" present when not expected.",
   112                                       method);
   113             }
   114         }
   116         if (methodIsDefault != expectedDefault) {
   117             messager.printMessage(ERROR,
   118                                   new Formatter().format("Unexpected Executable.isDefault result: got ``%s'', expected ``%s''.",
   119                                                          expectedDefault,
   120                                                          methodIsDefault).toString(),
   121                                   method);
   122         }
   123     }
   124 }
   126 /**
   127  * Expected value of the ExecutableElement.isDefault method.
   128  */
   129 @Retention(RetentionPolicy.RUNTIME)
   130 @Target(ElementType.METHOD)
   131 @interface IsDefault {
   132     boolean value();
   133     String expectedTextRegex() default "";
   134 }
   136 /**
   137  * Test interface to provide a default method.
   138  */
   139 interface ProviderOfDefault {
   140     @IsDefault(false)
   141     boolean process(Set<? extends TypeElement> annotations,
   142                     RoundEnvironment roundEnv);
   144     @IsDefault(value=true, expectedTextRegex="\\s*@IsDefault\\(.*\\)\\s*default strictfp void quux\\(\\);\\s*$")
   145     default strictfp void quux() {};
   146     @IsDefault(false)
   147     static void statik() {}
   148 }

mercurial