langtools/test/tools/javac/defaultMethods/BadClassfile.java
author ksrini
Wed, 20 Jul 2016 12:49:32 -0700
changeset 39917 c51a8950f278
parent 36526 3b41f1c69604
child 42407 f3702cff2933
permissions -rw-r--r--
8154705: invalid use of ALL-MODULE-PATH causes crash Summary: Introducing Modules.initModules as a clear point where the module graphs is initialized in javac Reviewed-by: jjg, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21006
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 24604
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
21006
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
     4
 *
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
     8
 *
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    14
 *
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    18
 *
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    21
 * questions.
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    22
 */
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    23
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    24
/*
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    25
 * @test
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    26
 * @bug 8025087
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    27
 * @summary Verify that pre-JDK8 classfiles with default and/or static methods
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    28
 *          are refused correctly.
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    29
 * @modules jdk.jdeps/com.sun.tools.classfile
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 24604
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 24604
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.code
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    32
 *          jdk.compiler/com.sun.tools.javac.comp
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 24604
diff changeset
    33
 *          jdk.compiler/com.sun.tools.javac.jvm
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 24604
diff changeset
    34
 *          jdk.compiler/com.sun.tools.javac.main
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 24604
diff changeset
    35
 *          jdk.compiler/com.sun.tools.javac.util
21006
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    36
 * @build BadClassfile
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    37
 * @run main BadClassfile
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    38
 */
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    39
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    40
import com.sun.tools.classfile.*;
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    41
import com.sun.tools.javac.api.JavacTaskImpl;
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 21006
diff changeset
    42
import com.sun.tools.javac.code.ClassFinder.BadClassFile;
21006
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    43
import com.sun.tools.javac.code.Symbol;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    44
import com.sun.tools.javac.code.Symtab;
21006
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    45
import com.sun.tools.javac.jvm.Target;
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    46
import com.sun.tools.javac.util.Assert;
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    47
import com.sun.tools.javac.util.JCDiagnostic;
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    48
import java.io.File;
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    49
import java.util.Arrays;
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    50
import java.util.Objects;
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    51
import javax.tools.JavaCompiler;
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    52
import javax.tools.ToolProvider;
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    53
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    54
public class BadClassfile {
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    55
    public static void main(String... args) throws Exception {
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    56
        test("BadClassfile$DefaultMethodTest", "compiler.misc.invalid.default.interface");
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    57
        test("BadClassfile$StaticMethodTest", "compiler.misc.invalid.static.interface");
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    58
    }
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    59
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    60
    private static void test(String classname, String expected) throws Exception {
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    61
        File classfile = new File(System.getProperty("test.classes", "."), classname + ".class");
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    62
        ClassFile cf = ClassFile.read(classfile);
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    63
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    64
        cf = new ClassFile(cf.magic, Target.JDK1_7.minorVersion,
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    65
                 Target.JDK1_7.majorVersion, cf.constant_pool, cf.access_flags,
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    66
                cf.this_class, cf.super_class, cf.interfaces, cf.fields,
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    67
                cf.methods, cf.attributes);
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    68
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    69
        new ClassWriter().write(cf, classfile);
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    70
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    71
        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    72
        JavacTaskImpl task = (JavacTaskImpl) c.getTask(null, null, null, Arrays.asList("-classpath", System.getProperty("test.classes", ".")), null, null);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    73
        Symtab syms = Symtab.instance(task.getContext());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    74
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 36526
diff changeset
    75
        task.ensureEntered();
21006
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    76
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    77
        try {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    78
            Symbol clazz = com.sun.tools.javac.main.JavaCompiler.instance(task.getContext()).resolveIdent(syms.unnamedModule, classname);
21006
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    79
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    80
            clazz.complete();
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    81
        } catch (BadClassFile f) {
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    82
            JCDiagnostic embeddedDiag = (JCDiagnostic) f.diag.getArgs()[1];
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    83
            assertEquals(expected, embeddedDiag.getCode());
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    84
            assertEquals(Integer.toString(Target.JDK1_7.majorVersion), embeddedDiag.getArgs()[0]);
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    85
            assertEquals(Integer.toString(Target.JDK1_7.minorVersion), embeddedDiag.getArgs()[1]);
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    86
        }
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    87
    }
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    88
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    89
    private static void assertEquals(Object expected, Object actual) {
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    90
        Assert.check(Objects.equals(expected, actual),
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    91
                     "expected: " + expected + ", but was: " + actual);
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    92
    }
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    93
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    94
    interface DefaultMethodTest {
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    95
        default void test() { }
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    96
    }
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    97
    interface StaticMethodTest {
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    98
        static void test() { }
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
    99
    }
534673718919 8025087: Annotation processing api returns default modifier for interface static method
jlahoda
parents:
diff changeset
   100
}