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