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