langtools/test/tools/javac/TestBootstrapMethodsCount.java
changeset 32334 fd65e32e16b3
equal deleted inserted replaced
32256:3966bd3b8167 32334:fd65e32e16b3
       
     1 /*
       
     2  * Copyright (c) 2012, 2015, 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 8129547
       
    27  * @summary Excess entries in BootstrapMethods with the same (bsm, bsmKind, bsmStaticArgs), but different dynamicArgs
       
    28  * @library lib
       
    29  * @modules jdk.jdeps/com.sun.tools.classfile
       
    30  *          jdk.compiler/com.sun.tools.javac.api
       
    31  *          jdk.compiler/com.sun.tools.javac.code
       
    32  *          jdk.compiler/com.sun.tools.javac.jvm
       
    33  *          jdk.compiler/com.sun.tools.javac.tree
       
    34  *          jdk.compiler/com.sun.tools.javac.util
       
    35  * @build JavacTestingAbstractThreadedTest
       
    36  * @run main/othervm TestBootstrapMethodsCount
       
    37  */
       
    38 
       
    39 import java.io.File;
       
    40 import java.net.URI;
       
    41 import java.util.ArrayList;
       
    42 import java.util.Arrays;
       
    43 import java.util.Locale;
       
    44 
       
    45 import javax.tools.Diagnostic;
       
    46 import javax.tools.JavaFileObject;
       
    47 import javax.tools.SimpleJavaFileObject;
       
    48 
       
    49 import com.sun.source.tree.MethodInvocationTree;
       
    50 import com.sun.source.tree.MethodTree;
       
    51 import com.sun.source.util.TaskEvent;
       
    52 import com.sun.source.util.TaskListener;
       
    53 import com.sun.source.util.TreeScanner;
       
    54 
       
    55 import com.sun.tools.classfile.Attribute;
       
    56 import com.sun.tools.classfile.BootstrapMethods_attribute;
       
    57 import com.sun.tools.classfile.ClassFile;
       
    58 
       
    59 import com.sun.tools.javac.api.JavacTaskImpl;
       
    60 import com.sun.tools.javac.code.Symbol;
       
    61 import com.sun.tools.javac.code.Symbol.MethodSymbol;
       
    62 import com.sun.tools.javac.code.Symtab;
       
    63 import com.sun.tools.javac.code.Types;
       
    64 import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
       
    65 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
       
    66 import com.sun.tools.javac.tree.JCTree.JCIdent;
       
    67 import com.sun.tools.javac.util.Context;
       
    68 import com.sun.tools.javac.util.Names;
       
    69 
       
    70 import static com.sun.tools.javac.jvm.ClassFile.*;
       
    71 
       
    72 public class TestBootstrapMethodsCount
       
    73         extends JavacTestingAbstractThreadedTest
       
    74         implements Runnable {
       
    75 
       
    76 
       
    77     public static void main(String... args) throws Exception {
       
    78         pool.execute(new TestBootstrapMethodsCount());
       
    79         checkAfterExec();
       
    80     }
       
    81 
       
    82     DiagChecker dc;
       
    83 
       
    84     TestBootstrapMethodsCount() {
       
    85         dc = new DiagChecker();
       
    86     }
       
    87 
       
    88     public void run() {
       
    89         int id = checkCount.incrementAndGet();
       
    90         JavaSource source = new JavaSource(id);
       
    91         JavacTaskImpl ct = (JavacTaskImpl)comp.getTask(null, fm.get(), dc,
       
    92                 Arrays.asList("-g"), null, Arrays.asList(source));
       
    93         Context context = ct.getContext();
       
    94         Symtab syms = Symtab.instance(context);
       
    95         Names names = Names.instance(context);
       
    96         Types types = Types.instance(context);
       
    97         ct.addTaskListener(new Indifier(syms, names, types));
       
    98         try {
       
    99             ct.generate();
       
   100         } catch (Throwable t) {
       
   101             t.printStackTrace();
       
   102             throw new AssertionError(
       
   103                     String.format("Error thrown when compiling following code\n%s",
       
   104                             source.source));
       
   105         }
       
   106         if (dc.diagFound) {
       
   107             throw new AssertionError(
       
   108                     String.format("Diags found when compiling following code\n%s\n\n%s",
       
   109                             source.source, dc.printDiags()));
       
   110         }
       
   111         verifyBytecode(id);
       
   112     }
       
   113 
       
   114     void verifyBytecode(int id) {
       
   115         File compiledTest = new File(String.format("Test%d.class", id));
       
   116         try {
       
   117             ClassFile cf = ClassFile.read(compiledTest);
       
   118             BootstrapMethods_attribute bsm_attr =
       
   119                     (BootstrapMethods_attribute)cf
       
   120                             .getAttribute(Attribute.BootstrapMethods);
       
   121             int length = bsm_attr.bootstrap_method_specifiers.length;
       
   122             if (length != 1) {
       
   123                 throw new Error("Bad number of method specifiers " +
       
   124                         "in BootstrapMethods attribute: " + length);
       
   125             }
       
   126         } catch (Exception e) {
       
   127             e.printStackTrace();
       
   128             throw new Error("error reading " + compiledTest +": " + e);
       
   129         }
       
   130     }
       
   131 
       
   132     class JavaSource extends SimpleJavaFileObject {
       
   133 
       
   134         static final String source_template = "import java.lang.invoke.*;\n" +
       
   135                 "class Bootstrap {\n" +
       
   136                 "   public static CallSite bsm(MethodHandles.Lookup lookup, " +
       
   137                 "String name, MethodType methodType) {\n" +
       
   138                 "       return null;\n" +
       
   139                 "   }\n" +
       
   140                 "}\n" +
       
   141                 "class Test#ID {\n" +
       
   142                 "   void m1() { }\n" +
       
   143                 "   void m2(Object arg1) { }\n" +
       
   144                 "   void test1() {\n" +
       
   145                 "      Object o = this; // marker statement \n" +
       
   146                 "      m1();\n" +
       
   147                 "   }\n" +
       
   148                 "   void test2(Object arg1) {\n" +
       
   149                 "      Object o = this; // marker statement \n" +
       
   150                 "      m2(arg1);\n" +
       
   151                 "   }\n" +
       
   152                 "}";
       
   153 
       
   154         String source;
       
   155 
       
   156         JavaSource(int id) {
       
   157             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
       
   158             source = source_template.replace("#ID", String.valueOf(id));
       
   159         }
       
   160 
       
   161         @Override
       
   162         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
       
   163             return source;
       
   164         }
       
   165     }
       
   166 
       
   167     class Indifier extends TreeScanner<Void, Void> implements TaskListener {
       
   168 
       
   169         MethodSymbol bsm;
       
   170         Symtab syms;
       
   171         Names names;
       
   172         Types types;
       
   173 
       
   174         Indifier(Symtab syms, Names names, Types types) {
       
   175             this.syms = syms;
       
   176             this.names = names;
       
   177             this.types = types;
       
   178         }
       
   179 
       
   180         @Override
       
   181         public void started(TaskEvent e) {
       
   182             //do nothing
       
   183         }
       
   184 
       
   185         @Override
       
   186         public void finished(TaskEvent e) {
       
   187             if (e.getKind() == TaskEvent.Kind.ANALYZE) {
       
   188                 scan(e.getCompilationUnit(), null);
       
   189             }
       
   190         }
       
   191 
       
   192         @Override
       
   193         public Void visitMethodInvocation(MethodInvocationTree node, Void p) {
       
   194             super.visitMethodInvocation(node, p);
       
   195             JCMethodInvocation apply = (JCMethodInvocation)node;
       
   196             JCIdent ident = (JCIdent)apply.meth;
       
   197             Symbol oldSym = ident.sym;
       
   198             if (!oldSym.isConstructor()) {
       
   199                 ident.sym = new Symbol.DynamicMethodSymbol(oldSym.name,
       
   200                         oldSym.owner, REF_invokeStatic, bsm, oldSym.type, new Object[0]);
       
   201             }
       
   202             return null;
       
   203         }
       
   204 
       
   205         @Override
       
   206         public Void visitMethod(MethodTree node, Void p) {
       
   207             super.visitMethod(node, p);
       
   208             if (node.getName().toString().equals("bsm")) {
       
   209                 bsm = ((JCMethodDecl)node).sym;
       
   210             }
       
   211             return null;
       
   212         }
       
   213     }
       
   214 
       
   215     static class DiagChecker
       
   216             implements javax.tools.DiagnosticListener<JavaFileObject> {
       
   217 
       
   218         boolean diagFound;
       
   219         ArrayList<String> diags = new ArrayList<>();
       
   220 
       
   221         public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
       
   222             diags.add(diagnostic.getMessage(Locale.getDefault()));
       
   223             diagFound = true;
       
   224         }
       
   225 
       
   226         String printDiags() {
       
   227             StringBuilder buf = new StringBuilder();
       
   228             for (String s : diags) {
       
   229                 buf.append(s);
       
   230                 buf.append("\n");
       
   231             }
       
   232             return buf.toString();
       
   233         }
       
   234     }
       
   235 
       
   236 }