1 /*
  2  * Copyright (c) 2018, 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 Check code generation for value creation ops
 27  * @modules jdk.compiler/com.sun.tools.javac.util jdk.jdeps/com.sun.tools.javap
 28  * @compile -XDallowWithFieldOperator WithFieldOfImplicitThis.java
 29  * @run main/othervm -Xverify:none WithFieldOfImplicitThis
 30  * @modules jdk.compiler
 31  */
 32 
 33 import java.io.PrintWriter;
 34 import java.io.StringWriter;
 35 import java.nio.file.Paths;
 36 
 37 public class WithFieldOfImplicitThis {
 38 
 39     final primitive class X.val {
 40 
 41         final int x;
 42 
 43         X() {
 44             x = 10;
 45         }
 46         
 47         X.val getX(Integer xVal, int xi) {
 48             X.val xl = X.val.default;
 49             xl = __WithField(x, xi);
 50             xl = __WithField(x, xVal);
 51             return xl;
 52         }
 53     }
 54 
 55     public static void main(String[] args) {
 56         new WithFieldOfImplicitThis().run();
 57     }
 58 
 59     void run() {
 60         String [] params = new String [] { "-v",
 61                                             Paths.get(System.getProperty("test.classes"),
 62                                                 "WithFieldOfImplicitThis$X.class").toString() };
 63         runCheck(params, new String [] {
 64 
 65          "0: defaultvalue  #1                  // class WithFieldOfImplicitThis$X",
 66          "3: astore_3",
 67          "4: aload_0",
 68          "5: iload_2",
 69          "6: withfield     #7                  // Field x:I",
 70          "9: astore_3",
 71         "10: aload_0",
 72         "11: aload_1",
 73         "12: invokevirtual #11                 // Method java/lang/Integer.intValue:()I",
 74         "15: withfield     #7                  // Field x:I",
 75         "18: astore_3",
 76         "19: aload_3",
 77         "20: areturn"
 78          });
 79      }
 80 
 81      void runCheck(String [] params, String [] expectedOut) {
 82         StringWriter s;
 83         String out;
 84 
 85         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
 86             com.sun.tools.javap.Main.run(params, pw);
 87             out = s.toString();
 88         }
 89         int errors = 0;
 90         for (String eo: expectedOut) {
 91             if (!out.contains(eo)) {
 92                 System.err.println("Match not found for string: " + eo);
 93                 errors++;
 94             }
 95         }
 96          if (errors > 0) {
 97              throw new AssertionError("Unexpected javap output: " + out);
 98          }
 99     }
100 }