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

Wed, 03 Apr 2013 12:27:12 -0700

author
darcy
date
Wed, 03 Apr 2013 12:27:12 -0700
changeset 1672
0d47e6131490
parent 1459
bc74006c2d8d
child 2099
1b469fd31e35
permissions
-rw-r--r--

8011052: Add DEFAULT to javax.lang.model.Modifier
Reviewed-by: abuckley, jjg

darcy@1459 1 /*
darcy@1672 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
darcy@1459 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
darcy@1459 4 *
darcy@1459 5 * This code is free software; you can redistribute it and/or modify it
darcy@1459 6 * under the terms of the GNU General Public License version 2 only, as
darcy@1459 7 * published by the Free Software Foundation.
darcy@1459 8 *
darcy@1459 9 * This code is distributed in the hope that it will be useful, but WITHOUT
darcy@1459 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
darcy@1459 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
darcy@1459 12 * version 2 for more details (a copy is included in the LICENSE file that
darcy@1459 13 * accompanied this code).
darcy@1459 14 *
darcy@1459 15 * You should have received a copy of the GNU General Public License version
darcy@1459 16 * 2 along with this work; if not, write to the Free Software Foundation,
darcy@1459 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
darcy@1459 18 *
darcy@1459 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
darcy@1459 20 * or visit www.oracle.com if you need additional information or have any
darcy@1459 21 * questions.
darcy@1459 22 */
darcy@1459 23
darcy@1459 24 /*
darcy@1459 25 * @test
darcy@1672 26 * @bug 8005046 8011052
darcy@1459 27 * @summary Test basic properties of javax.lang.element.Element
darcy@1459 28 * @author Joseph D. Darcy
darcy@1459 29 * @library /tools/javac/lib
darcy@1459 30 * @build JavacTestingAbstractProcessor TestExecutableElement
darcy@1459 31 * @compile -processor TestExecutableElement -proc:only TestExecutableElement.java
darcy@1459 32 */
darcy@1459 33
darcy@1459 34 import java.lang.annotation.*;
darcy@1459 35 import java.util.Formatter;
darcy@1459 36 import java.util.Set;
darcy@1459 37 import java.util.Objects;
darcy@1672 38 import java.util.regex.*;
darcy@1459 39 import javax.annotation.processing.*;
darcy@1459 40 import javax.lang.model.SourceVersion;
darcy@1459 41 import static javax.lang.model.SourceVersion.*;
darcy@1459 42 import javax.lang.model.element.*;
darcy@1459 43 import javax.lang.model.util.*;
darcy@1459 44 import static javax.lang.model.util.ElementFilter.*;
darcy@1459 45 import static javax.tools.Diagnostic.Kind.*;
darcy@1459 46 import static javax.tools.StandardLocation.*;
darcy@1459 47
darcy@1459 48 /**
darcy@1459 49 * Test some basic workings of javax.lang.element.ExecutableElement
darcy@1459 50 */
darcy@1459 51 public class TestExecutableElement extends JavacTestingAbstractProcessor implements ProviderOfDefault {
darcy@1459 52 @IsDefault(false)
darcy@1459 53 public boolean process(Set<? extends TypeElement> annotations,
darcy@1459 54 RoundEnvironment roundEnv) {
darcy@1459 55 int errors = 0;
darcy@1459 56 if (!roundEnv.processingOver()) {
darcy@1459 57 boolean hasRun = false;
darcy@1459 58 for (Element element : roundEnv.getRootElements()) {
darcy@1459 59 for (ExecutableElement method : methodsIn(element.getEnclosedElements())) {
darcy@1459 60 hasRun = true;
darcy@1459 61 errors += checkIsDefault(method);
darcy@1459 62 }
darcy@1459 63 }
darcy@1459 64
darcy@1459 65 if (!hasRun) {
darcy@1459 66 messager.printMessage(ERROR, "No test cases run; test fails.");
darcy@1459 67 }
darcy@1459 68 }
darcy@1459 69 return true;
darcy@1459 70 }
darcy@1459 71
darcy@1459 72 @IsDefault(false)
darcy@1459 73 int checkIsDefault(ExecutableElement method) {
darcy@1459 74 System.out.println("Testing " + method);
darcy@1459 75 IsDefault expectedIsDefault = method.getAnnotation(IsDefault.class);
darcy@1459 76
darcy@1459 77 boolean expectedDefault = (expectedIsDefault != null) ?
darcy@1459 78 expectedIsDefault.value() :
darcy@1459 79 false;
darcy@1459 80
darcy@1459 81 boolean methodIsDefault = method.isDefault();
darcy@1459 82
darcy@1672 83 if (expectedDefault) {
darcy@1672 84 if (!method.getModifiers().contains(Modifier.DEFAULT)) {
darcy@1672 85 messager.printMessage(ERROR,
darcy@1672 86 "Modifier \"default\" not present as expected.",
darcy@1672 87 method);
darcy@1672 88 }
darcy@1672 89
darcy@1672 90 // Check printing output
darcy@1672 91 java.io.Writer stringWriter = new java.io.StringWriter();
darcy@1672 92 eltUtils.printElements(stringWriter, method);
darcy@1672 93 Pattern p = Pattern.compile(expectedIsDefault.expectedTextRegex(), Pattern.DOTALL);
darcy@1672 94
darcy@1672 95 if (! p.matcher(stringWriter.toString()).matches()) {
darcy@1672 96 messager.printMessage(ERROR,
darcy@1672 97 new Formatter().format("Unexpected printing ouptput:%n\tgot %s,%n\texpected pattern %s.",
darcy@1672 98 stringWriter.toString(),
darcy@1672 99 expectedIsDefault.expectedTextRegex()).toString(),
darcy@1672 100 method);
darcy@1672 101 }
darcy@1672 102
darcy@1672 103 System.out.println("\t" + stringWriter.toString());
darcy@1672 104
darcy@1672 105 } else {
darcy@1672 106 if (method.getModifiers().contains(Modifier.DEFAULT)) {
darcy@1672 107 messager.printMessage(ERROR,
darcy@1672 108 "Modifier \"default\" present when not expected.",
darcy@1672 109 method);
darcy@1672 110 }
darcy@1672 111 }
darcy@1672 112
darcy@1459 113 if (methodIsDefault != expectedDefault) {
darcy@1459 114 messager.printMessage(ERROR,
darcy@1672 115 new Formatter().format("Unexpected Executable.isDefault result: got ``%s'', expected ``%s''.",
darcy@1459 116 expectedDefault,
darcy@1459 117 methodIsDefault).toString(),
darcy@1459 118 method);
darcy@1459 119 return 1;
darcy@1459 120 }
darcy@1459 121 return 0;
darcy@1459 122 }
darcy@1459 123 }
darcy@1459 124
darcy@1459 125 /**
darcy@1459 126 * Expected value of the ExecutableElement.isDefault method.
darcy@1459 127 */
darcy@1459 128 @Retention(RetentionPolicy.RUNTIME)
darcy@1459 129 @Target(ElementType.METHOD)
darcy@1459 130 @interface IsDefault {
darcy@1459 131 boolean value();
darcy@1672 132 String expectedTextRegex() default "";
darcy@1459 133 }
darcy@1459 134
darcy@1459 135 /**
darcy@1459 136 * Test interface to provide a default method.
darcy@1459 137 */
darcy@1459 138 interface ProviderOfDefault {
darcy@1459 139 @IsDefault(false)
darcy@1459 140 boolean process(Set<? extends TypeElement> annotations,
darcy@1459 141 RoundEnvironment roundEnv);
darcy@1459 142
darcy@1672 143 @IsDefault(value=true, expectedTextRegex="\\s*@IsDefault\\(.*\\)\\s*default strictfp void quux\\(\\);\\s*$")
darcy@1672 144 default strictfp void quux() {};
darcy@1459 145 }

mercurial