test/tools/javac/8005931/CheckACC_STRICTFlagOnPkgAccessClassTest.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/8005931/CheckACC_STRICTFlagOnPkgAccessClassTest.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,130 @@
     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 8005931
    1.32 + * @summary javac doesn't set ACC_STRICT for classes with package access
    1.33 + * @run main CheckACC_STRICTFlagOnPkgAccessClassTest
    1.34 + */
    1.35 +
    1.36 +import java.io.File;
    1.37 +import java.io.IOException;
    1.38 +import java.net.URI;
    1.39 +import java.util.ArrayList;
    1.40 +import java.util.Arrays;
    1.41 +import java.util.List;
    1.42 +import javax.tools.JavaCompiler;
    1.43 +import javax.tools.JavaFileObject;
    1.44 +import javax.tools.SimpleJavaFileObject;
    1.45 +import javax.tools.ToolProvider;
    1.46 +import com.sun.source.util.JavacTask;
    1.47 +import com.sun.tools.classfile.ClassFile;
    1.48 +import com.sun.tools.classfile.ConstantPoolException;
    1.49 +import com.sun.tools.classfile.Descriptor;
    1.50 +import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
    1.51 +import com.sun.tools.classfile.Method;
    1.52 +
    1.53 +import static com.sun.tools.classfile.AccessFlags.ACC_STRICT;
    1.54 +
    1.55 +public class CheckACC_STRICTFlagOnPkgAccessClassTest {
    1.56 +
    1.57 +    private static final String AssertionErrorMessage =
    1.58 +        "All methods should have the ACC_STRICT access flag " +
    1.59 +        "please check output";
    1.60 +    private static final String CompilationErrorMessage =
    1.61 +        "Error thrown when compiling the following source:\n";
    1.62 +    private static final String offendingMethodErrorMessage =
    1.63 +        "Method %s of class %s doesn't have the ACC_STRICT access flag";
    1.64 +
    1.65 +    JavaSource source = new JavaSource();
    1.66 +
    1.67 +    private List<String> errors = new ArrayList<>();
    1.68 +
    1.69 +    public static void main(String[] args)
    1.70 +            throws IOException, ConstantPoolException, InvalidDescriptor {
    1.71 +        JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    1.72 +        new CheckACC_STRICTFlagOnPkgAccessClassTest().run(comp);
    1.73 +    }
    1.74 +
    1.75 +    private void run(JavaCompiler comp)
    1.76 +            throws IOException, ConstantPoolException, InvalidDescriptor {
    1.77 +        compile(comp);
    1.78 +        check();
    1.79 +        if (errors.size() > 0) {
    1.80 +            for (String error: errors) {
    1.81 +                System.err.println(error);
    1.82 +            }
    1.83 +            throw new AssertionError(AssertionErrorMessage);
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    private void compile(JavaCompiler comp) {
    1.88 +        JavacTask ct = (JavacTask)comp.getTask(null, null, null, null, null,
    1.89 +                Arrays.asList(source));
    1.90 +        try {
    1.91 +            if (!ct.call()) {
    1.92 +                throw new AssertionError(CompilationErrorMessage +
    1.93 +                        source.getCharContent(true));
    1.94 +            }
    1.95 +        } catch (Throwable ex) {
    1.96 +            throw new AssertionError(CompilationErrorMessage +
    1.97 +                    source.getCharContent(true));
    1.98 +        }
    1.99 +    }
   1.100 +
   1.101 +    void check()
   1.102 +        throws
   1.103 +            IOException,
   1.104 +            ConstantPoolException,
   1.105 +            Descriptor.InvalidDescriptor {
   1.106 +        ClassFile classFileToCheck = ClassFile.read(new File("Test.class"));
   1.107 +
   1.108 +        for (Method method : classFileToCheck.methods) {
   1.109 +            if ((method.access_flags.flags & ACC_STRICT) == 0) {
   1.110 +                errors.add(String.format(offendingMethodErrorMessage,
   1.111 +                        method.getName(classFileToCheck.constant_pool),
   1.112 +                        classFileToCheck.getName()));
   1.113 +            }
   1.114 +        }
   1.115 +    }
   1.116 +
   1.117 +    class JavaSource extends SimpleJavaFileObject {
   1.118 +
   1.119 +        String source = "strictfp class Test {" +
   1.120 +                "    Test(){}" +
   1.121 +                "    void m(){}" +
   1.122 +                "}";
   1.123 +
   1.124 +        public JavaSource() {
   1.125 +            super(URI.create("Test.java"), JavaFileObject.Kind.SOURCE);
   1.126 +        }
   1.127 +
   1.128 +        @Override
   1.129 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   1.130 +            return source;
   1.131 +        }
   1.132 +    }
   1.133 +}

mercurial