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 NoSuchMethodError
  28  * @modules java.base/jdk.internal.org.objectweb.asm
  29  * @library /runtime/SelectionResolution/classes
  30  * @build selectionresolution.*
  31  * @run main NoSuchMethodErrorTest
  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.Result;
  40 import selectionresolution.SelectionResolutionTest;
  41 import selectionresolution.SelectionResolutionTestCase;
  42 import selectionresolution.Template;
  43 
  44 public class NoSuchMethodErrorTest extends SelectionResolutionTest {
  45 
  46     private static final SelectionResolutionTestCase.Builder initBuilder =
  47         new SelectionResolutionTestCase.Builder();
  48 
  49     static {
  50         initBuilder.setResult(Result.NSME);
  51     }
  52 
  53     private static final MethodData concreteMethod =
  54         new MethodData(MethodData.Access.PUBLIC, MethodData.Context.INSTANCE);
  55 
  56     private static final MethodData staticMethod =
  57         new MethodData(MethodData.Access.PUBLIC, MethodData.Context.STATIC);
  58 
  59     private static final MethodData privateMethod =
  60         new MethodData(MethodData.Access.PRIVATE, MethodData.Context.INSTANCE);
  61 
  62     private static final ClassData withDef =
  63         new ClassData(ClassData.Package.SAME, concreteMethod);
  64 
  65     private static final ClassData withStaticDef =
  66         new ClassData(ClassData.Package.SAME, staticMethod);
  67 
  68     private static final ClassData withPrivateDef =
  69         new ClassData(ClassData.Package.SAME, staticMethod);
  70 
  71     private static final Template NoMethodResolutionTemplateClassBottom =
  72         new Template("NoMethodResolutionTemplate",
  73                     /* Empty single class
  74                      *
  75                      * C[]() = mref
  76                      */
  77                     (final SelectionResolutionTestCase.Builder builder) -> {
  78                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
  79                         builder.methodref = C;
  80                     },
  81                     /* Class bottom, inherit empty class
  82                      *
  83                      * C2[]()
  84                      * C1[C2]() = mref
  85                      */
  86                     (final SelectionResolutionTestCase.Builder builder) -> {
  87                         final int C1 = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
  88                         final int C2 = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
  89                         builder.hier.addInherit(C1, C2);
  90                         builder.methodref = C1;
  91                     },
  92                     /* Class bottom, inherit empty interface
  93                      *
  94                      * I[]()
  95                      * C[I]() = mref
  96                      */
  97                     (final SelectionResolutionTestCase.Builder builder) -> {
  98                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
  99                         final int I = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 100                         builder.hier.addInherit(C, I);
 101                         builder.methodref = C;
 102                     },
 103                     /* Class bottom, inherit empty class and interface
 104                      *
 105                      * C2[](), I[]()
 106                      * C1[C2,I]() = mref
 107                      */
 108                     (final SelectionResolutionTestCase.Builder builder) -> {
 109                         final int C1 = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 110                         final int C2 = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 111                         final int I = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 112                         builder.hier.addInherit(C1, C2);
 113                         builder.hier.addInherit(C1, I);
 114                         builder.methodref = C1;
 115                     },
 116                     /* Class bottom, unrelated class defines
 117                      *
 118                      * C20[](con)
 119                      * C1[]()
 120                      */
 121                     (final SelectionResolutionTestCase.Builder builder) -> {
 122                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 123                         builder.addClass(withDef);
 124                         builder.methodref = C;
 125                     },
 126                     /* Class bottom, interface defines static
 127                      *
 128                      * I[](stat)
 129                      * C[]()
 130                      */
 131                     (final SelectionResolutionTestCase.Builder builder) -> {
 132                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 133                         final int I = builder.addInterface(withStaticDef);
 134                         builder.hier.addInherit(C, I);
 135                         builder.methodref = C;
 136                     },
 137                     /* Class bottom, interface defines private
 138                      *
 139                      * I[](priv)
 140                      * C[]()
 141                      */
 142                     (final SelectionResolutionTestCase.Builder builder) -> {
 143                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 144                         final int I = builder.addInterface(withPrivateDef);
 145                         builder.hier.addInherit(C, I);
 146                         builder.methodref = C;
 147                     });
 148 
 149     private static final Template NoMethodResolutionTemplateIfaceBottom =
 150         new Template("NoMethodResolutionTemplate",
 151                     /* Empty single interface
 152                      *
 153                      * I[]() = mref
 154                      */
 155                     (final SelectionResolutionTestCase.Builder builder) -> {
 156                         final int I = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 157                         builder.methodref = I;
 158                     },
 159                     /* Interface bottom, inherit empty interface
 160                      *
 161                      * I2[]()
 162                      * I1[I2]() = mref
 163                      */
 164                     (final SelectionResolutionTestCase.Builder builder) -> {
 165                         final int I1 = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 166                         final int I2 = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 167                         builder.hier.addInherit(I1, I2);
 168                         builder.methodref = I1;
 169                     },
 170                     /* Interface bottom, unrelated class defines
 171                      *
 172                      * C0[](con)
 173                      * I[]() = mref
 174                      */
 175                     (final SelectionResolutionTestCase.Builder builder) -> {
 176                         final int I = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 177                         builder.addClass(withDef);
 178                         builder.methodref = I;
 179                     },
 180                     /* Interface bottom, interface defines static
 181                      *
 182                      * I2[](stat)
 183                      * I1[I2]() = mref
 184                      */
 185                     (final SelectionResolutionTestCase.Builder builder) -> {
 186                         final int I1 = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 187                         final int I2 = builder.addInterface(withStaticDef);
 188                         builder.hier.addInherit(I1, I2);
 189                         builder.methodref = I1;
 190                     },
 191                     /* Interface bottom, interface defines private
 192                      *
 193                      * I2[](stat)
 194                      * I1[I2]() = mref
 195                      */
 196                     (final SelectionResolutionTestCase.Builder builder) -> {
 197                         final int I1 = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 198                         final int I2 = builder.addInterface(withPrivateDef);
 199                         builder.hier.addInherit(I1, I2);
 200                         builder.methodref = I1;
 201                     });
 202 
 203     private static final Template NoMethodSelectionTemplateClassMethodref =
 204         new Template("NoMethodSelectionTemplate",
 205                     /* objectref = methodref
 206                      *
 207                      * C[]() = mref = oref
 208                      */
 209                     (final SelectionResolutionTestCase.Builder builder) -> {
 210                         builder.objectref = builder.methodref;
 211                     },
 212                     /* Inherit methodref
 213                      *
 214                      * C2[]() = mref
 215                      * C1[C2]() = oref
 216                      */
 217                     (final SelectionResolutionTestCase.Builder builder) -> {
 218                         final int C1 = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 219                         final int C2 = builder.methodref;
 220                         builder.hier.addInherit(C1, C2);
 221                         builder.objectref = C1;
 222                     },
 223                     /* Inherit methodref and interface
 224                      *
 225                      * C2[]() = mref, I[]()
 226                      * C1[C2,I]() = oref
 227                      */
 228                     (final SelectionResolutionTestCase.Builder builder) -> {
 229                         final int C2 = builder.methodref;
 230                         final int C1 = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 231                         final int I = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 232                         builder.hier.addInherit(C1, C2);
 233                         builder.hier.addInherit(C1, I);
 234                         builder.objectref = C1;
 235                     },
 236                     /* objectref = methodref, unrelated class defines
 237                      *
 238                      * C0[](def)
 239                      * C[]() = mref = oref
 240                      */
 241                     (final SelectionResolutionTestCase.Builder builder) -> {
 242                         builder.addClass(withDef);
 243                         builder.objectref = builder.methodref;
 244                     },
 245                     /* Inherit methodref, unrelated class defines
 246                      *
 247                      * C0[](def)
 248                      * C2[]() = mref
 249                      * C1[C2]() = oref
 250                      */
 251                     (final SelectionResolutionTestCase.Builder builder) -> {
 252                         final int C1 = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 253                         final int C2 = builder.methodref;
 254                         builder.addClass(withDef);
 255                         builder.hier.addInherit(C1, C2);
 256                         builder.objectref = C1;
 257                     },
 258                     /* Inherit methodref and interface, unrelated class defines.
 259                      *
 260                      * C0[](def)
 261                      * C2[]() = mref, I[]()
 262                      * C1[C2,I]() = oref
 263                      */
 264                     (final SelectionResolutionTestCase.Builder builder) -> {
 265                         final int C2 = builder.methodref;
 266                         final int C1 = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 267                         final int I = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 268                         builder.addClass(withDef);
 269                         builder.hier.addInherit(C1, C2);
 270                         builder.hier.addInherit(C1, I);
 271                         builder.objectref = C1;
 272                     },
 273                     /* objectref = methodref, unrelated interface defines
 274                      *
 275                      * I0[](def)
 276                      * C[]() = mref = oref
 277                      */
 278                     (final SelectionResolutionTestCase.Builder builder) -> {
 279                         builder.addInterface(withDef);
 280                         builder.objectref = builder.methodref;
 281                     },
 282                     /* Inherit methodref, interface defines static
 283                      *
 284                      * C2[]() = mref, I0[](stat)
 285                      * C1[C2]() = oref
 286                      */
 287                     (final SelectionResolutionTestCase.Builder builder) -> {
 288                         final int C1 = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 289                         final int C2 = builder.methodref;
 290                         final int I0 = builder.addInterface(withStaticDef);
 291                         builder.hier.addInherit(C1, C2);
 292                         builder.hier.addInherit(C1, I0);
 293                         builder.objectref = C1;
 294                     },
 295                     /* Inherit methodref, interface defines private
 296                      *
 297                      * C2[]() = mref, I0[](stat)
 298                      * C1[C2]() = oref
 299                      */
 300                     (final SelectionResolutionTestCase.Builder builder) -> {
 301                         final int C1 = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 302                         final int C2 = builder.methodref;
 303                         final int I0 = builder.addInterface(withPrivateDef);
 304                         builder.hier.addInherit(C1, C2);
 305                         builder.hier.addInherit(C1, I0);
 306                         builder.objectref = C1;
 307                     });
 308 
 309     private static final Template NoMethodSelectionTemplateIfaceMethodref =
 310         new Template("NoMethodSelectionTemplate",
 311                     /* Inherit methodref
 312                      *
 313                      * I[]() = mref
 314                      * C[I]() = oref
 315                      */
 316                     (final SelectionResolutionTestCase.Builder builder) -> {
 317                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 318                         final int I = builder.methodref;
 319                         builder.hier.addInherit(C, I);
 320                         builder.objectref = C;
 321                     },
 322                     /* Inherit methodref and interface
 323                      *
 324                      * I1[]() = mref, I2[]()
 325                      * C[T,I]() = oref
 326                      */
 327                     (final SelectionResolutionTestCase.Builder builder) -> {
 328                         final int I1 = builder.methodref;
 329                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 330                         final int I2 = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 331                         builder.hier.addInherit(C, I1);
 332                         builder.hier.addInherit(C, I2);
 333                         builder.objectref = C;
 334                     },
 335                     /* Inherit methodref, unrelated class defines
 336                      *
 337                      * C0[](def)
 338                      * I[]() = mref
 339                      * C[I]() = oref
 340                      */
 341                     (final SelectionResolutionTestCase.Builder builder) -> {
 342                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 343                         final int I = builder.methodref;
 344                         builder.addClass(withDef);
 345                         builder.hier.addInherit(C, I);
 346                         builder.objectref = C;
 347                     },
 348                     /* Inherit methodref and interface, unrelated class defines
 349                      *
 350                      * C0[](def)
 351                      * I1[]() = mref, I2[]()
 352                      * C[I1,I2]() = oref
 353                      */
 354                     (final SelectionResolutionTestCase.Builder builder) -> {
 355                         final int I1 = builder.methodref;
 356                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 357                         final int I2 = builder.addInterface(Template.emptyClass(ClassData.Package.SAME));
 358                         builder.addClass(withDef);
 359                         builder.hier.addInherit(C, I1);
 360                         builder.hier.addInherit(C, I2);
 361                         builder.objectref = C;
 362                     },
 363                     /* Inherit methodref, interface defines static
 364                      *
 365                      * I[]() = mref, I0[](stat)
 366                      * C[I,I0]() = oref
 367                      */
 368                     (final SelectionResolutionTestCase.Builder builder) -> {
 369                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 370                         final int I = builder.methodref;
 371                         final int I0 = builder.addInterface(withStaticDef);
 372                         builder.hier.addInherit(C, I);
 373                         builder.hier.addInherit(C, I0);
 374                         builder.objectref = C;
 375                     },
 376                     /* Inherit methodref, unrelated class defines private
 377                      *
 378                      * I[]() = mref, I0[](priv)
 379                      * C[I,I0]() = oref
 380                      */
 381                     (final SelectionResolutionTestCase.Builder builder) -> {
 382                         final int C = builder.addClass(Template.emptyClass(ClassData.Package.SAME));
 383                         final int I = builder.methodref;
 384                         final int I0 = builder.addInterface(withPrivateDef);
 385                         builder.hier.addInherit(C, I);
 386                         builder.hier.addInherit(C, I0);
 387                         builder.objectref = C;
 388                     });
 389 
 390     private static final Collection<TestGroup> testgroups =
 391         Arrays.asList(
 392                 /* invokestatic tests */
 393                 new TestGroup.Simple(initBuilder,
 394                         Template.SetInvoke(SelectionResolutionTestCase.InvokeInstruction.INVOKESTATIC),
 395                         NoMethodResolutionTemplateClassBottom,
 396                         Template.AllCallsiteCases,
 397                         Template.TrivialObjectref),
 398                 new TestGroup.Simple(initBuilder,
 399                         Template.SetInvoke(SelectionResolutionTestCase.InvokeInstruction.INVOKESTATIC),
 400                         NoMethodResolutionTemplateIfaceBottom,
 401                         Template.CallsiteNotEqualsMethodref,
 402                         Template.TrivialObjectref),
 403                 /* invokevirtual tests */
 404                 new TestGroup.Simple(initBuilder,
 405                         Template.SetInvoke(SelectionResolutionTestCase.InvokeInstruction.INVOKEVIRTUAL),
 406                         NoMethodResolutionTemplateClassBottom,
 407                         Template.AllCallsiteCases,
 408                         NoMethodSelectionTemplateClassMethodref),
 409                 /* invokeinterface tests */
 410                 new TestGroup.Simple(initBuilder,
 411                         Template.SetInvoke(SelectionResolutionTestCase.InvokeInstruction.INVOKEINTERFACE),
 412                         NoMethodResolutionTemplateIfaceBottom,
 413                         Template.CallsiteNotEqualsMethodref,
 414                         NoMethodSelectionTemplateIfaceMethodref),
 415 
 416                 /* Hiding of private interface methods */
 417                 /* invokevirtual */
 418                 new TestGroup.Simple(initBuilder,
 419                         Template.SetInvoke(SelectionResolutionTestCase.InvokeInstruction.INVOKEVIRTUAL),
 420                         Template.ResultCombo(EnumSet.of(Template.Kind.INTERFACE),
 421                                              EnumSet.of(MethodData.Access.PRIVATE),
 422                                              EnumSet.of(MethodData.Context.INSTANCE,
 423                                                         MethodData.Context.ABSTRACT),
 424                                              EnumSet.of(ClassData.Package.SAME,
 425                                                         ClassData.Package.DIFFERENT)),
 426                         Template.MethodrefNotEqualsExpectedIface,
 427                         Template.AllCallsiteCases,
 428                         Template.TrivialObjectref),
 429                 /* invokeinterface */
 430                 new TestGroup.Simple(initBuilder,
 431                         Template.SetInvoke(SelectionResolutionTestCase.InvokeInstruction.INVOKEINTERFACE),
 432                         Template.ResultCombo(EnumSet.of(Template.Kind.INTERFACE),
 433                                              EnumSet.of(MethodData.Access.PRIVATE),
 434                                              EnumSet.of(MethodData.Context.INSTANCE,
 435                                                         MethodData.Context.ABSTRACT),
 436                                              EnumSet.of(ClassData.Package.SAME,
 437                                                         ClassData.Package.DIFFERENT)),
 438                         Template.IfaceMethodrefNotEqualsExpected,
 439                         Template.AllCallsiteCases,
 440                         Template.TrivialObjectrefNotEqualMethodref)
 441             );
 442 
 443     private NoSuchMethodErrorTest() {
 444         super(testgroups);
 445     }
 446 
 447     public static void main(final String... args) {
 448         new NoSuchMethodErrorTest().run();
 449     }
 450 }