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

Tue, 18 Dec 2012 00:24:54 -0800

author
darcy
date
Tue, 18 Dec 2012 00:24:54 -0800
changeset 1459
bc74006c2d8d
child 1672
0d47e6131490
permissions
-rw-r--r--

8005046: Provide checking for a default method in javax.lang.model
Reviewed-by: jjg

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

mercurial