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