1 /*
2  * Copyright (c) 2013, 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 8011383
27  * @summary Symbol.getModifiers omits ACC_ABSTRACT from interface with default methods
28  * @modules jdk.compiler/com.sun.tools.javac.util
29  */
30 
31 import java.io.File;
32 import java.io.IOException;
33 import java.util.Arrays;
34 
35 import javax.lang.model.element.*;
36 import javax.tools.JavaCompiler;
37 import javax.tools.JavaFileObject;
38 import javax.tools.StandardJavaFileManager;
39 import javax.tools.ToolProvider;
40 
41 import com.sun.source.util.JavacTask;
42 import com.sun.source.util.TaskEvent;
43 import com.sun.source.util.TaskListener;
44 import com.sun.tools.javac.util.Assert;
45 
46 public class DefaultMethodFlags {
47 
main(String[] args)48     public static void main(String[] args) throws IOException {
49         new DefaultMethodFlags().run(args);
50     }
51 
run(String[] args)52     void run(String[] args) throws IOException {
53         checkDefaultMethodFlags();
54     }
55 
checkDefaultMethodFlags()56     void checkDefaultMethodFlags() throws IOException {
57         JavaCompiler c = ToolProvider.getSystemJavaCompiler();
58         try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) {
59             Iterable<? extends JavaFileObject> fos =
60                     fm.getJavaFileObjectsFromFiles(
61                     Arrays.asList(new File(
62                     System.getProperty("test.src"),
63                     this.getClass().getSimpleName() + ".java")));
64             JavacTask task = (JavacTask) c.getTask(null, fm, null, null, null, fos);
65 
66             task.addTaskListener(new TaskListener() {
67 
68                 @Override
69                 public void started(TaskEvent e) {}
70 
71                 @Override
72                 public void finished(TaskEvent e) {
73                     if (e.getKind() == TaskEvent.Kind.ANALYZE) {
74                         TypeElement te = e.getTypeElement();
75                         if (te.getSimpleName().toString().equals("I")) {
76                             checkDefaultInterface(te);
77                         }
78                     }
79                 }
80             });
81 
82             task.analyze();
83         }
84     }
85 
checkDefaultInterface(TypeElement te)86     void checkDefaultInterface(TypeElement te) {
87         System.err.println("Checking " + te.getSimpleName());
88         Assert.check(te.getModifiers().contains(Modifier.ABSTRACT));
89         for (Element e : te.getEnclosedElements()) {
90             if (e.getSimpleName().toString().matches("(\\w)_(default|static|abstract)")) {
91                 boolean abstractExpected = false;
92                 String methodKind = e.getSimpleName().toString().substring(2);
93                 switch (methodKind) {
94                     case "default":
95                     case "static":
96                         break;
97                     case "abstract":
98                         abstractExpected = true;
99                         break;
100                     default:
101                         Assert.error("Cannot get here!" + methodKind);
102                 }
103                 Assert.check(e.getModifiers().contains(Modifier.ABSTRACT) == abstractExpected);
104             }
105         }
106     }
107 }
108 
109 interface I {
m_default()110     default void m_default() { }
m_static()111     static void m_static() { }
m_abstract()112     void m_abstract();
113 }
114