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 Smoke test for nestmate classfile support
  27  * @run main CheckNestmateAttrs
  28  * @modules
  29  *      jdk.compiler
  30  *      jdk.jdeps/com.sun.tools.javap
  31  */
  32 
  33 import java.io.PrintWriter;
  34 import java.io.StringWriter;
  35 import java.nio.file.Paths;
  36 
  37 public class CheckNestmateAttrs {
  38 
  39     private void test() { }
  40 
  41     class Inner {
  42         void m() {
  43             class LocalInner {
  44                 void testInner() {
  45                     test();
  46                 }
  47             }
  48         }
  49     }
  50 
  51     static class Nested {
  52         void s() {
  53             class LocalNested { }
  54         }
  55     }
  56 
  57     public static void main(String[] args) {
  58         new CheckNestmateAttrs().run();
  59     }
  60 
  61     void run() {
  62         String [] params = new String [] { "-v",
  63                                             Paths.get(System.getProperty("test.classes"),
  64                                                 "CheckNestmateAttrs.class").toString() };
  65         runCheck(params, new String [] {
  66                         "NestMembers:" +
  67                         "  CheckNestmateAttrs$Nested" +
  68                         "  CheckNestmateAttrs$Nested$1LocalNested" +
  69                         "  CheckNestmateAttrs$Inner" +
  70                         "  CheckNestmateAttrs$Inner$1LocalInner"
  71                         });
  72 
  73         params = new String [] { "-v",
  74                                  Paths.get(System.getProperty("test.classes"),
  75                                      "CheckNestmateAttrs$Inner.class").toString() };
  76 
  77         runCheck(params, new String [] { "NestHost: class CheckNestmateAttrs" });
  78 
  79         params = new String [] { "-v",
  80                                  Paths.get(System.getProperty("test.classes"),
  81                                      "CheckNestmateAttrs$Nested.class").toString() };
  82 
  83         runCheck(params, new String [] { "NestHost: class CheckNestmateAttrs" });
  84 
  85         params = new String [] { "-v",
  86                                  Paths.get(System.getProperty("test.classes"),
  87                                      "CheckNestmateAttrs$Inner$1LocalInner.class").toString() };
  88 
  89         runCheck(params, new String [] {
  90                         "NestHost: class CheckNestmateAttrs",
  91                         "0: aload_0",
  92                         "1: getfield      #1                  // Field this$1:LCheckNestmateAttrs$Inner;",
  93                         "4: getfield      #3                  // Field CheckNestmateAttrs$Inner.this$0:LCheckNestmateAttrs;",
  94                         "7: invokevirtual #4                  // Method CheckNestmateAttrs.test:()V",
  95                         "10: return"
  96         });
  97 
  98         params = new String [] { "-v",
  99                                  Paths.get(System.getProperty("test.classes"),
 100                                      "CheckNestmateAttrs$Nested$1LocalNested.class").toString() };
 101 
 102         runCheck(params, new String [] { "NestHost: class CheckNestmateAttrs" });
 103      }
 104 
 105      void runCheck(String [] params, String [] expectedOut) {
 106         StringWriter s;
 107         String out;
 108 
 109         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
 110             com.sun.tools.javap.Main.run(params, pw);
 111             out = s.toString();
 112         }
 113         for (String eo: expectedOut) {
 114             if (!out.contains(eo)) {
 115                 System.out.println("Actual output: " + out);
 116                 throw new AssertionError("Missing output: " + eo);
 117             }
 118         }
 119     }
 120 }