1 /*
   2  * Copyright (c) 2020, 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 8237528
  27  * @summary Verify there are no unnecessary checkcasts and conditions generated
  28  *          for the pattern matching in instanceof.
  29  * @modules jdk.jdeps/com.sun.tools.classfile
  30  * @compile --enable-preview -source ${jdk.version} NoUnnecessaryCast.java
  31  * @run main/othervm --enable-preview NoUnnecessaryCast
  32  */
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 
  37 import com.sun.tools.classfile.Attribute;
  38 import com.sun.tools.classfile.ClassFile;
  39 import com.sun.tools.classfile.Code_attribute;
  40 import com.sun.tools.classfile.Code_attribute.InvalidIndex;
  41 import com.sun.tools.classfile.ConstantPool;
  42 import com.sun.tools.classfile.ConstantPoolException;
  43 import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
  44 import com.sun.tools.classfile.Instruction;
  45 import com.sun.tools.classfile.Method;
  46 import java.util.Arrays;
  47 import java.util.stream.Collectors;
  48 import java.util.stream.StreamSupport;
  49 
  50 public class NoUnnecessaryCast {
  51     public static void main(String[] args)
  52             throws IOException, ConstantPoolException, InvalidDescriptor, InvalidIndex {
  53         new NoUnnecessaryCast()
  54                 .checkClassFile(new File(System.getProperty("test.classes", "."),
  55                     NoUnnecessaryCast.class.getName() + ".class"));
  56     }
  57 
  58     void checkClassFile(File file)
  59             throws IOException, ConstantPoolException, InvalidDescriptor, InvalidIndex {
  60         ClassFile classFile = ClassFile.read(file);
  61         ConstantPool constantPool = classFile.constant_pool;
  62 
  63         Method method = Arrays.stream(classFile.methods)
  64                               .filter(m -> getName(m, constantPool)
  65                                                .equals("test"))
  66                               .findAny()
  67                               .get();
  68         String expectedInstructions = """
  69                                       aload_1
  70                                       astore_3
  71                                       aload_3
  72                                       instanceof
  73                                       ifeq
  74                                       aload_3
  75                                       checkcast
  76                                       astore_2
  77                                       aload_2
  78                                       invokevirtual
  79                                       ifeq
  80                                       iconst_1
  81                                       goto
  82                                       iconst_0
  83                                       ireturn
  84                                       """;
  85         Code_attribute code = (Code_attribute) method.attributes
  86                 .get(Attribute.Code);
  87         String actualInstructions = printCode(code);
  88         if (!expectedInstructions.equals(actualInstructions)) {
  89             throw new AssertionError("Unexpected instructions found:\n" +
  90                                      actualInstructions);
  91         }
  92     }
  93 
  94     String printCode(Code_attribute code) {
  95         return StreamSupport.stream(code.getInstructions().spliterator(), false)
  96                             .map(Instruction::getMnemonic)
  97                             .collect(Collectors.joining("\n", "", "\n"));
  98     }
  99 
 100     String getName(Method m, ConstantPool constantPool) {
 101         try {
 102             return m.getName(constantPool);
 103         } catch (ConstantPoolException ex) {
 104             throw new IllegalStateException(ex);
 105         }
 106     }
 107 
 108     boolean test(Object o) {
 109         return o instanceof String s && s.isEmpty();
 110     }
 111 }