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