test/tools/javac/7166455/CheckACC_STRICTFlagOnclinitTest.java

changeset 1555
762d0af062f5
parent 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/7166455/CheckACC_STRICTFlagOnclinitTest.java	Fri Feb 08 09:12:37 2013 +0000
     1.3 @@ -0,0 +1,107 @@
     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.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * @test
    1.31 + * @bug 7166455
    1.32 + * @summary javac doesn't set ACC_STRICT bit on <clinit> for strictfp class
    1.33 + * @run main CheckACC_STRICTFlagOnclinitTest
    1.34 + */
    1.35 +
    1.36 +import java.util.ArrayList;
    1.37 +import java.util.List;
    1.38 +import java.io.File;
    1.39 +import java.io.IOException;
    1.40 +import com.sun.tools.classfile.ClassFile;
    1.41 +import com.sun.tools.classfile.ConstantPoolException;
    1.42 +import com.sun.tools.classfile.Descriptor;
    1.43 +import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
    1.44 +import com.sun.tools.classfile.Method;
    1.45 +
    1.46 +import static com.sun.tools.classfile.AccessFlags.ACC_STRICT;
    1.47 +
    1.48 +public strictfp class CheckACC_STRICTFlagOnclinitTest {
    1.49 +    private static final String AssertionErrorMessage =
    1.50 +        "All methods should have the ACC_STRICT access flag " +
    1.51 +        "please check output";
    1.52 +    private static final String offendingMethodErrorMessage =
    1.53 +        "Method %s of class %s doesn't have the ACC_STRICT access flag";
    1.54 +
    1.55 +    static {
    1.56 +        class Foo {
    1.57 +            class Bar {
    1.58 +                void m11() {}
    1.59 +            }
    1.60 +            void m1() {}
    1.61 +        }
    1.62 +    }
    1.63 +    void m2() {
    1.64 +        class Any {
    1.65 +            void m21() {}
    1.66 +        }
    1.67 +    }
    1.68 +
    1.69 +    private List<String> errors = new ArrayList<>();
    1.70 +
    1.71 +    public static void main(String[] args)
    1.72 +            throws IOException, ConstantPoolException, InvalidDescriptor {
    1.73 +        new CheckACC_STRICTFlagOnclinitTest().run();
    1.74 +    }
    1.75 +
    1.76 +    private void run()
    1.77 +            throws IOException, ConstantPoolException, InvalidDescriptor {
    1.78 +        String testClasses = System.getProperty("test.classes");
    1.79 +        check(testClasses,
    1.80 +              "CheckACC_STRICTFlagOnclinitTest.class",
    1.81 +              "CheckACC_STRICTFlagOnclinitTest$1Foo.class",
    1.82 +              "CheckACC_STRICTFlagOnclinitTest$1Foo$Bar.class",
    1.83 +              "CheckACC_STRICTFlagOnclinitTest$1Any.class");
    1.84 +        if (errors.size() > 0) {
    1.85 +            for (String error: errors) {
    1.86 +                System.err.println(error);
    1.87 +            }
    1.88 +            throw new AssertionError(AssertionErrorMessage);
    1.89 +        }
    1.90 +    }
    1.91 +
    1.92 +    void check(String dir, String... fileNames)
    1.93 +        throws
    1.94 +            IOException,
    1.95 +            ConstantPoolException,
    1.96 +            Descriptor.InvalidDescriptor {
    1.97 +        for (String fileName : fileNames) {
    1.98 +            ClassFile classFileToCheck = ClassFile.read(new File(dir, fileName));
    1.99 +
   1.100 +            for (Method method : classFileToCheck.methods) {
   1.101 +                if ((method.access_flags.flags & ACC_STRICT) == 0) {
   1.102 +                    errors.add(String.format(offendingMethodErrorMessage,
   1.103 +                            method.getName(classFileToCheck.constant_pool),
   1.104 +                            classFileToCheck.getName()));
   1.105 +                }
   1.106 +            }
   1.107 +        }
   1.108 +    }
   1.109 +
   1.110 +}

mercurial