test/tools/javac/defaultMethods/DefaultMethodFlags.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/defaultMethods/DefaultMethodFlags.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,111 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8011383
    1.30 + * @summary Symbol.getModifiers omits ACC_ABSTRACT from interface with default methods
    1.31 + */
    1.32 +
    1.33 +import java.io.File;
    1.34 +import java.io.IOException;
    1.35 +import java.util.Arrays;
    1.36 +
    1.37 +import javax.lang.model.element.*;
    1.38 +import javax.tools.JavaCompiler;
    1.39 +import javax.tools.JavaFileObject;
    1.40 +import javax.tools.StandardJavaFileManager;
    1.41 +import javax.tools.ToolProvider;
    1.42 +
    1.43 +import com.sun.source.util.JavacTask;
    1.44 +import com.sun.source.util.TaskEvent;
    1.45 +import com.sun.source.util.TaskListener;
    1.46 +import com.sun.tools.javac.util.Assert;
    1.47 +
    1.48 +public class DefaultMethodFlags {
    1.49 +
    1.50 +    public static void main(String[] args) throws IOException {
    1.51 +        new DefaultMethodFlags().run(args);
    1.52 +    }
    1.53 +
    1.54 +    void run(String[] args) throws IOException {
    1.55 +        checkDefaultMethodFlags();
    1.56 +    }
    1.57 +
    1.58 +    void checkDefaultMethodFlags() throws IOException {
    1.59 +        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    1.60 +        StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
    1.61 +        Iterable<? extends JavaFileObject> fos =
    1.62 +                fm.getJavaFileObjectsFromFiles(
    1.63 +                Arrays.asList(new File(
    1.64 +                System.getProperty("test.src"),
    1.65 +                this.getClass().getSimpleName() + ".java")));
    1.66 +        JavacTask task = (JavacTask) c.getTask(null, fm, null, null, null, fos);
    1.67 +
    1.68 +        task.addTaskListener(new TaskListener() {
    1.69 +
    1.70 +            @Override
    1.71 +            public void started(TaskEvent e) {}
    1.72 +
    1.73 +            @Override
    1.74 +            public void finished(TaskEvent e) {
    1.75 +                if (e.getKind() == TaskEvent.Kind.ANALYZE) {
    1.76 +                    TypeElement te = e.getTypeElement();
    1.77 +                    if (te.getSimpleName().toString().equals("I")) {
    1.78 +                        checkDefaultInterface(te);
    1.79 +                    }
    1.80 +                }
    1.81 +            }
    1.82 +        });
    1.83 +
    1.84 +        task.analyze();
    1.85 +    }
    1.86 +
    1.87 +    void checkDefaultInterface(TypeElement te) {
    1.88 +        System.err.println("Checking " + te.getSimpleName());
    1.89 +        Assert.check(te.getModifiers().contains(Modifier.ABSTRACT));
    1.90 +        for (Element e : te.getEnclosedElements()) {
    1.91 +            if (e.getSimpleName().toString().matches("(\\w)_(default|static|abstract)")) {
    1.92 +                boolean abstractExpected = false;
    1.93 +                String methodKind = e.getSimpleName().toString().substring(2);
    1.94 +                switch (methodKind) {
    1.95 +                    case "default":
    1.96 +                    case "static":
    1.97 +                        break;
    1.98 +                    case "abstract":
    1.99 +                        abstractExpected = true;
   1.100 +                        break;
   1.101 +                    default:
   1.102 +                        Assert.error("Cannot get here!" + methodKind);
   1.103 +                }
   1.104 +                Assert.check(e.getModifiers().contains(Modifier.ABSTRACT) == abstractExpected);
   1.105 +            }
   1.106 +        }
   1.107 +    }
   1.108 +}
   1.109 +
   1.110 +interface I {
   1.111 +    default void m_default() { }
   1.112 +    static void m_static() { }
   1.113 +    void m_abstract();
   1.114 +}

mercurial