1 /*
   2  * Copyright (c) 2016, 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  * @summary Test of method selection and resolution cases that
  27  * generate InvokeVirtualSuccessTest
  28  * @modules java.base/jdk.internal.org.objectweb.asm
  29  * @library /runtime/SelectionResolution/classes
  30  * @build selectionresolution.*
  31  * @run main/othervm/timeout=400 -XX:+IgnoreUnrecognizedVMOptions -XX:-VerifyDependencies InvokeVirtualSuccessTest
  32  */
  33 
  34 import java.util.Arrays;
  35 import java.util.Collection;
  36 import java.util.EnumSet;
  37 import selectionresolution.ClassData;
  38 import selectionresolution.MethodData;
  39 import selectionresolution.SelectionResolutionTest;
  40 import selectionresolution.SelectionResolutionTestCase;
  41 import selectionresolution.Template;
  42 
  43 public class InvokeVirtualSuccessTest extends SelectionResolutionTest {
  44 
  45     private static final SelectionResolutionTestCase.Builder initBuilder =
  46         new SelectionResolutionTestCase.Builder();
  47 
  48     static {
  49         initBuilder.invoke = SelectionResolutionTestCase.InvokeInstruction.INVOKEVIRTUAL;
  50     }
  51 
  52     private static final Collection<TestGroup> testgroups =
  53         Arrays.asList(
  54                 /* invokevirtual tests */
  55                 /* Group 16: callsite = methodref = expected, no override */
  56                 new TestGroup.Simple(initBuilder,
  57                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
  58                                              EnumSet.of(MethodData.Access.PRIVATE),
  59                                              EnumSet.of(MethodData.Context.INSTANCE),
  60                                              EnumSet.of(ClassData.Package.SAME)),
  61                         Template.OverrideAbstractExpectedClass,
  62                         Template.MethodrefEqualsExpected,
  63                         Template.IgnoredAbstract,
  64                         Template.CallsiteEqualsMethodref,
  65                         Template.MethodrefSelectionResolvedIsClassNoOverride),
  66                 /* Group 17: callsite = methodref = expected, override allowed */
  67                 new TestGroup.Simple(initBuilder,
  68                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
  69                                              EnumSet.of(MethodData.Access.PROTECTED,
  70                                                         MethodData.Access.PUBLIC),
  71                                              EnumSet.of(MethodData.Context.INSTANCE,
  72                                                         MethodData.Context.ABSTRACT),
  73                                              EnumSet.of(ClassData.Package.SAME)),
  74                         Template.OverrideAbstractExpectedClass,
  75                         Template.MethodrefEqualsExpected,
  76                         Template.IgnoredAbstract,
  77                         Template.CallsiteEqualsMethodref,
  78                         Template.MethodrefSelectionResolvedIsClass,
  79                         Template.SelectionOverrideAbstract),
  80                 /* Group 18: callsite = methodref = resolved, possibly
  81                  * skip different package in selection.
  82                  */
  83                 new TestGroup.Simple(initBuilder,
  84                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
  85                                              EnumSet.of(MethodData.Access.PACKAGE),
  86                                              EnumSet.of(MethodData.Context.INSTANCE,
  87                                                         MethodData.Context.ABSTRACT),
  88                                              EnumSet.of(ClassData.Package.SAME)),
  89                         Template.OverrideAbstractExpectedClass,
  90                         Template.MethodrefEqualsExpected,
  91                         Template.IgnoredAbstract,
  92                         Template.CallsiteEqualsMethodref,
  93                         Template.MethodrefSelectionPackageSkip,
  94                         Template.SelectionOverrideAbstract),
  95                 /* Group 19: callsite = methodref, methodref != expected,
  96                  * expected is class, expected and callsite in the same package
  97                  */
  98                 new TestGroup.Simple(initBuilder,
  99                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 100                                              EnumSet.of(MethodData.Access.PUBLIC,
 101                                                         MethodData.Access.PROTECTED),
 102                                              EnumSet.of(MethodData.Context.INSTANCE,
 103                                                         MethodData.Context.ABSTRACT),
 104                                              EnumSet.of(ClassData.Package.SAME)),
 105                         Template.OverrideAbstractExpectedClass,
 106                         Template.MethodrefNotEqualsExpectedClass,
 107                         Template.IgnoredAbstract,
 108                         Template.CallsiteEqualsMethodref,
 109                         Template.MethodrefSelectionResolvedIsClass,
 110                         Template.SelectionOverrideAbstract),
 111                 /* Group 20: callsite = methodref, methodref \!=
 112                  * expected, possibly skip different package in
 113                  * selection.
 114                  */
 115                 new TestGroup.Simple(initBuilder,
 116                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 117                                              EnumSet.of(MethodData.Access.PACKAGE),
 118                                              EnumSet.of(MethodData.Context.INSTANCE,
 119                                                         MethodData.Context.ABSTRACT),
 120                                              EnumSet.of(ClassData.Package.SAME)),
 121                         Template.OverrideAbstractExpectedClass,
 122                         Template.MethodrefNotEqualsExpectedClass,
 123                         Template.IgnoredAbstract,
 124                         Template.CallsiteEqualsMethodref,
 125                         Template.MethodrefSelectionPackageSkip,
 126                         Template.SelectionOverrideAbstract),
 127                 /* Group 21: callsite = methodref, methodref != expected,
 128                  * expected is interface, expected and callsite in the same package
 129                  */
 130                 new TestGroup.Simple(initBuilder,
 131                         Template.ResultCombo(EnumSet.of(Template.Kind.INTERFACE),
 132                                              EnumSet.of(MethodData.Access.PUBLIC),
 133                                              EnumSet.of(MethodData.Context.INSTANCE,
 134                                                         MethodData.Context.ABSTRACT),
 135                                              EnumSet.of(ClassData.Package.SAME)),
 136                         Template.OverrideAbstractExpectedIface,
 137                         Template.MethodrefNotEqualsExpectedIface,
 138                         Template.IgnoredAbstract,
 139                         Template.CallsiteEqualsMethodref,
 140                         Template.MethodrefSelectionResolvedIsIface,
 141                         Template.SelectionOverrideAbstract),
 142                 /* Group 22: callsite :> methodref, methodref = expected,
 143                  * expected is class, expected and callsite in the same package
 144                  */
 145                 new TestGroup.Simple(initBuilder,
 146                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 147                                              EnumSet.of(MethodData.Access.PUBLIC,
 148                                                         MethodData.Access.PROTECTED),
 149                                              EnumSet.of(MethodData.Context.INSTANCE,
 150                                                         MethodData.Context.ABSTRACT),
 151                                              EnumSet.of(ClassData.Package.SAME)),
 152                         Template.OverrideAbstractExpectedClass,
 153                         Template.MethodrefEqualsExpected,
 154                         Template.IgnoredAbstract,
 155                         Template.CallsiteSubclassMethodref,
 156                         Template.MethodrefSelectionResolvedIsClass,
 157                         Template.SelectionOverrideAbstract),
 158                 /* Group 23: callsite :>, methodref = expected,
 159                  * possibly skip different package in selection
 160                  */
 161                 new TestGroup.Simple(initBuilder,
 162                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 163                                              EnumSet.of(MethodData.Access.PACKAGE),
 164                                              EnumSet.of(MethodData.Context.INSTANCE,
 165                                                         MethodData.Context.ABSTRACT),
 166                                              EnumSet.of(ClassData.Package.SAME)),
 167                         Template.OverrideAbstractExpectedClass,
 168                         Template.MethodrefEqualsExpected,
 169                         Template.IgnoredAbstract,
 170                         Template.CallsiteSubclassMethodref,
 171                         Template.MethodrefSelectionPackageSkip,
 172                         Template.SelectionOverrideAbstract),
 173                 /* Group 24: callsite :> methodref, methodref != expected,
 174                  * expected is class, expected and callsite in the same package
 175                  */
 176                 new TestGroup.Simple(initBuilder,
 177                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 178                                              EnumSet.of(MethodData.Access.PUBLIC,
 179                                                         MethodData.Access.PROTECTED),
 180                                              EnumSet.of(MethodData.Context.INSTANCE,
 181                                                         MethodData.Context.ABSTRACT),
 182                                              EnumSet.of(ClassData.Package.SAME)),
 183                         Template.OverrideAbstractExpectedClass,
 184                         Template.MethodrefNotEqualsExpectedClass,
 185                         Template.IgnoredAbstract,
 186                         Template.CallsiteSubclassMethodref,
 187                         Template.MethodrefSelectionResolvedIsClass,
 188                         Template.SelectionOverrideAbstract),
 189                 /* Group 25: callsite :>, methodref = expected,
 190                  * possibly skip different package in selection
 191                  */
 192                 new TestGroup.Simple(initBuilder,
 193                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 194                                              EnumSet.of(MethodData.Access.PACKAGE),
 195                                              EnumSet.of(MethodData.Context.INSTANCE,
 196                                                         MethodData.Context.ABSTRACT),
 197                                              EnumSet.of(ClassData.Package.SAME)),
 198                         Template.OverrideAbstractExpectedClass,
 199                         Template.MethodrefNotEqualsExpectedClass,
 200                         Template.IgnoredAbstract,
 201                         Template.CallsiteSubclassMethodref,
 202                         Template.MethodrefSelectionPackageSkip,
 203                         Template.SelectionOverrideAbstract),
 204                 /* Group 26: callsite :> methodref, methodref != expected,
 205                  * expected is interface, expected and callsite in the same package
 206                  */
 207                 new TestGroup.Simple(initBuilder,
 208                         Template.ResultCombo(EnumSet.of(Template.Kind.INTERFACE),
 209                                              EnumSet.of(MethodData.Access.PUBLIC),
 210                                              EnumSet.of(MethodData.Context.INSTANCE,
 211                                                         MethodData.Context.ABSTRACT),
 212                                              EnumSet.of(ClassData.Package.SAME)),
 213                         Template.OverrideAbstractExpectedIface,
 214                         Template.MethodrefNotEqualsExpectedIface,
 215                         Template.IgnoredAbstract,
 216                         Template.CallsiteSubclassMethodref,
 217                         Template.MethodrefSelectionResolvedIsIface,
 218                         Template.SelectionOverrideAbstract),
 219                 /* Group 27: callsite unrelated to methodref, methodref = expected,
 220                  * expected is class, expected and callsite in the same package
 221                  */
 222                 new TestGroup.Simple(initBuilder,
 223                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 224                                              EnumSet.of(MethodData.Access.PUBLIC,
 225                                                         MethodData.Access.PROTECTED),
 226                                              EnumSet.of(MethodData.Context.INSTANCE,
 227                                                         MethodData.Context.ABSTRACT),
 228                                              EnumSet.of(ClassData.Package.SAME)),
 229                         Template.OverrideAbstractExpectedClass,
 230                         Template.MethodrefEqualsExpected,
 231                         Template.IgnoredAbstract,
 232                         Template.CallsiteUnrelatedToMethodref,
 233                         Template.MethodrefSelectionResolvedIsClass,
 234                         Template.SelectionOverrideAbstract),
 235                 /* Group 28: callsite unrelated to methodref,
 236                  * methodref = expected, possibly skip different
 237                  * package in selection
 238                  */
 239                 new TestGroup.Simple(initBuilder,
 240                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 241                                              EnumSet.of(MethodData.Access.PACKAGE),
 242                                              EnumSet.of(MethodData.Context.INSTANCE,
 243                                                         MethodData.Context.ABSTRACT),
 244                                              EnumSet.of(ClassData.Package.SAME)),
 245                         Template.OverrideAbstractExpectedClass,
 246                         Template.MethodrefEqualsExpected,
 247                         Template.IgnoredAbstract,
 248                         Template.CallsiteUnrelatedToMethodref,
 249                         Template.MethodrefSelectionPackageSkip,
 250                         Template.SelectionOverrideAbstract),
 251                 /* Group 29: callsite unrelated to methodref, methodref != expected,
 252                  * expected is class, expected and callsite in the same package
 253                  */
 254                 new TestGroup.Simple(initBuilder,
 255                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 256                                              EnumSet.of(MethodData.Access.PUBLIC,
 257                                                         MethodData.Access.PROTECTED),
 258                                              EnumSet.of(MethodData.Context.INSTANCE,
 259                                                         MethodData.Context.ABSTRACT),
 260                                              EnumSet.of(ClassData.Package.SAME)),
 261                         Template.OverrideAbstractExpectedClass,
 262                         Template.MethodrefNotEqualsExpectedClass,
 263                         Template.IgnoredAbstract,
 264                         Template.CallsiteUnrelatedToMethodref,
 265                         Template.MethodrefSelectionResolvedIsClass,
 266                         Template.SelectionOverrideAbstract),
 267                 /* Group 30: callsite unrelated to methodref,
 268                  * methodref \!= expected, possibly skip different
 269                  * package in selection
 270                  */
 271                 new TestGroup.Simple(initBuilder,
 272                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 273                                              EnumSet.of(MethodData.Access.PACKAGE),
 274                                              EnumSet.of(MethodData.Context.INSTANCE,
 275                                                         MethodData.Context.ABSTRACT),
 276                                              EnumSet.of(ClassData.Package.SAME)),
 277                         Template.OverrideAbstractExpectedClass,
 278                         Template.MethodrefNotEqualsExpectedClass,
 279                         Template.IgnoredAbstract,
 280                         Template.CallsiteUnrelatedToMethodref,
 281                         Template.MethodrefSelectionPackageSkip,
 282                         Template.SelectionOverrideAbstract),
 283                 /* Group 31: callsite unrelated to methodref, methodref != expected,
 284                  * expected is interface, expected and callsite in the same package
 285                  */
 286                 new TestGroup.Simple(initBuilder,
 287                         Template.ResultCombo(EnumSet.of(Template.Kind.INTERFACE),
 288                                              EnumSet.of(MethodData.Access.PUBLIC),
 289                                              EnumSet.of(MethodData.Context.INSTANCE,
 290                                                         MethodData.Context.ABSTRACT),
 291                                              EnumSet.of(ClassData.Package.SAME)),
 292                         Template.OverrideAbstractExpectedIface,
 293                         Template.MethodrefNotEqualsExpectedIface,
 294                         Template.IgnoredAbstract,
 295                         Template.CallsiteUnrelatedToMethodref,
 296                         Template.MethodrefSelectionResolvedIsIface,
 297                         Template.SelectionOverrideAbstract),
 298                 /* Group 32: callsite = methodref, methodref != expected,
 299                  * expected is class, expected and callsite not in the same package
 300                  */
 301                 new TestGroup.Simple(initBuilder,
 302                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 303                                              EnumSet.of(MethodData.Access.PUBLIC,
 304                                                         MethodData.Access.PROTECTED),
 305                                              EnumSet.of(MethodData.Context.INSTANCE,
 306                                                         MethodData.Context.ABSTRACT),
 307                                              EnumSet.of(ClassData.Package.DIFFERENT)),
 308                         Template.OverrideAbstractExpectedClass,
 309                         Template.MethodrefNotEqualsExpectedClass,
 310                         Template.IgnoredAbstract,
 311                         Template.CallsiteEqualsMethodref,
 312                         Template.MethodrefSelectionResolvedIsClass,
 313                         Template.SelectionOverrideAbstract),
 314                 /* Group 33: callsite = methodref, methodref != expected,
 315                  * expected is interface, expected and callsite not in the same package
 316                  */
 317                 new TestGroup.Simple(initBuilder,
 318                         Template.ResultCombo(EnumSet.of(Template.Kind.INTERFACE),
 319                                              EnumSet.of(MethodData.Access.PUBLIC),
 320                                              EnumSet.of(MethodData.Context.INSTANCE,
 321                                                         MethodData.Context.ABSTRACT),
 322                                              EnumSet.of(ClassData.Package.DIFFERENT)),
 323                         Template.OverrideAbstractExpectedIface,
 324                         Template.MethodrefNotEqualsExpectedIface,
 325                         Template.IgnoredAbstract,
 326                         Template.CallsiteEqualsMethodref,
 327                         Template.MethodrefSelectionResolvedIsIface,
 328                         Template.SelectionOverrideAbstract),
 329                 /* Group 34: callsite :> methodref, methodref = expected,
 330                  * expected is class, expected and callsite not in the same package
 331                  */
 332                 new TestGroup.Simple(initBuilder,
 333                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 334                                              EnumSet.of(MethodData.Access.PUBLIC),
 335                                              EnumSet.of(MethodData.Context.INSTANCE,
 336                                                         MethodData.Context.ABSTRACT),
 337                                              EnumSet.of(ClassData.Package.DIFFERENT)),
 338                         Template.OverrideAbstractExpectedClass,
 339                         Template.MethodrefEqualsExpected,
 340                         Template.IgnoredAbstract,
 341                         Template.CallsiteSubclassMethodref,
 342                         Template.MethodrefSelectionResolvedIsClass,
 343                         Template.SelectionOverrideAbstract),
 344 
 345                 /* Group 35: callsite :> methodref, methodref != expected,
 346                  * expected is class, expected and callsite not in the same package
 347                  */
 348                 new TestGroup.Simple(initBuilder,
 349                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 350                                              EnumSet.of(MethodData.Access.PUBLIC),
 351                                              EnumSet.of(MethodData.Context.INSTANCE,
 352                                                         MethodData.Context.ABSTRACT),
 353                                              EnumSet.of(ClassData.Package.DIFFERENT)),
 354                         Template.OverrideAbstractExpectedClass,
 355                         Template.MethodrefNotEqualsExpectedClass,
 356                         Template.IgnoredAbstract,
 357                         Template.CallsiteSubclassMethodref,
 358                         Template.MethodrefSelectionResolvedIsClass,
 359                         Template.SelectionOverrideAbstract),
 360                 /* Group 36: callsite :> methodref, methodref != expected,
 361                  * expected is interface, expected and callsite not in the same package
 362                  */
 363                 new TestGroup.Simple(initBuilder,
 364                         Template.ResultCombo(EnumSet.of(Template.Kind.INTERFACE),
 365                                              EnumSet.of(MethodData.Access.PUBLIC),
 366                                              EnumSet.of(MethodData.Context.INSTANCE,
 367                                                         MethodData.Context.ABSTRACT),
 368                                              EnumSet.of(ClassData.Package.DIFFERENT)),
 369                         Template.OverrideAbstractExpectedIface,
 370                         Template.MethodrefNotEqualsExpectedIface,
 371                         Template.IgnoredAbstract,
 372                         Template.CallsiteSubclassMethodref,
 373                         Template.MethodrefSelectionResolvedIsIface,
 374                         Template.SelectionOverrideAbstract),
 375                 /* Group 37: callsite unrelated to methodref, methodref = expected,
 376                  * expected is class, expected and callsite not in the same package
 377                  */
 378                 new TestGroup.Simple(initBuilder,
 379                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 380                                              EnumSet.of(MethodData.Access.PUBLIC),
 381                                              EnumSet.of(MethodData.Context.INSTANCE,
 382                                                         MethodData.Context.ABSTRACT),
 383                                              EnumSet.of(ClassData.Package.DIFFERENT)),
 384                         Template.OverrideAbstractExpectedClass,
 385                         Template.MethodrefEqualsExpected,
 386                         Template.IgnoredAbstract,
 387                         Template.CallsiteUnrelatedToMethodref,
 388                         Template.MethodrefSelectionResolvedIsClass,
 389                         Template.SelectionOverrideAbstract),
 390                 /* Group 38: callsite unrelated to methodref, methodref != expected,
 391                  * expected is class, expected and callsite not in the same package
 392                  */
 393                 new TestGroup.Simple(initBuilder,
 394                         Template.ResultCombo(EnumSet.of(Template.Kind.CLASS),
 395                                              EnumSet.of(MethodData.Access.PUBLIC),
 396                                              EnumSet.of(MethodData.Context.INSTANCE,
 397                                                         MethodData.Context.ABSTRACT),
 398                                              EnumSet.of(ClassData.Package.DIFFERENT)),
 399                         Template.OverrideAbstractExpectedClass,
 400                         Template.MethodrefNotEqualsExpectedClass,
 401                         Template.IgnoredAbstract,
 402                         Template.CallsiteUnrelatedToMethodref,
 403                         Template.MethodrefSelectionResolvedIsClass,
 404                         Template.SelectionOverrideAbstract),
 405                 /* Group 39: callsite unrelated to methodref, methodref != expected,
 406                  * expected is interface, expected and callsite not in the same package
 407                  */
 408                 new TestGroup.Simple(initBuilder,
 409                         Template.ResultCombo(EnumSet.of(Template.Kind.INTERFACE),
 410                                              EnumSet.of(MethodData.Access.PUBLIC),
 411                                              EnumSet.of(MethodData.Context.INSTANCE,
 412                                                         MethodData.Context.ABSTRACT),
 413                                              EnumSet.of(ClassData.Package.DIFFERENT)),
 414                         Template.OverrideAbstractExpectedIface,
 415                         Template.MethodrefNotEqualsExpectedIface,
 416                         Template.IgnoredAbstract,
 417                         Template.CallsiteUnrelatedToMethodref,
 418                         Template.MethodrefSelectionResolvedIsIface,
 419                         Template.SelectionOverrideAbstract)
 420             );
 421 
 422     private InvokeVirtualSuccessTest() {
 423         super(testgroups);
 424     }
 425 
 426     public static void main(final String... args) {
 427         new InvokeVirtualSuccessTest().run();
 428     }
 429 }