1 /*
   2  * Copyright (c) 2014, 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 SyntheticParameters
  26  * @summary Test generation of annotations on inner class parameters.
  27  * @build ClassfileInspector
  28  * @run main SyntheticParameters
  29  */
  30 
  31 import java.io.*;
  32 import java.lang.annotation.*;
  33 
  34 import com.sun.tools.classfile.*;
  35 
  36 public class SyntheticParameters extends ClassfileInspector {
  37 
  38     private static final String Inner_class = "SyntheticParameters$Inner.class";
  39     private static final String Foo_class = "SyntheticParameters$Foo.class";
  40     private static final Expected Inner_expected =
  41         new Expected("SyntheticParameters$Inner",
  42                      null,
  43                      new ExpectedMethodTypeAnnotation[] {
  44                          (ExpectedMethodTypeAnnotation)
  45                          // Assert there is no annotation on the
  46                          // this$0 parameter.
  47                          new ExpectedMethodTypeAnnotation.Builder(
  48                              "<init>",
  49                              "A",
  50                              TypeAnnotation.TargetType.METHOD_FORMAL_PARAMETER,
  51                              false,
  52                              0).setParameterIndex(0).build(),
  53                          (ExpectedMethodTypeAnnotation)
  54                          // Assert there is an annotation on the
  55                          // first parameter.
  56                          new ExpectedMethodTypeAnnotation.Builder(
  57                              "<init>",
  58                              "A",
  59                              TypeAnnotation.TargetType.METHOD_FORMAL_PARAMETER,
  60                              false,
  61                              1).setParameterIndex(1).build(),
  62                          (ExpectedMethodTypeAnnotation)
  63                          new ExpectedMethodTypeAnnotation.Builder(
  64                              "foo",
  65                              "A",
  66                              TypeAnnotation.TargetType.METHOD_FORMAL_PARAMETER,
  67                              false,
  68                              1).setParameterIndex(0).build(),
  69                          (ExpectedMethodTypeAnnotation)
  70                          new ExpectedMethodTypeAnnotation.Builder(
  71                              "foo",
  72                              "A",
  73                              TypeAnnotation.TargetType.METHOD_FORMAL_PARAMETER,
  74                              false,
  75                              0).setParameterIndex(1).build()
  76                      },
  77                      null);
  78     private static final Expected Foo_expected =
  79         new Expected("SyntheticParameters$Foo",
  80                      null,
  81                      new ExpectedMethodTypeAnnotation[] {
  82                          (ExpectedMethodTypeAnnotation)
  83                          // Assert there is no annotation on the
  84                          // $enum$name parameter.
  85                          new ExpectedMethodTypeAnnotation.Builder(
  86                              "<init>",
  87                              "A",
  88                              TypeAnnotation.TargetType.METHOD_FORMAL_PARAMETER,
  89                              false,
  90                              0).setParameterIndex(0).build(),
  91                          (ExpectedMethodTypeAnnotation)
  92                          // Assert there is no annotation on the
  93                          // $enum$ordinal parameter.
  94                          new ExpectedMethodTypeAnnotation.Builder(
  95                              "<init>",
  96                              "A",
  97                              TypeAnnotation.TargetType.METHOD_FORMAL_PARAMETER,
  98                              false,
  99                              0).setParameterIndex(1).build(),
 100                          (ExpectedMethodTypeAnnotation)
 101                          // Assert there is an annotation on the
 102                          // first parameter.
 103                          new ExpectedMethodTypeAnnotation.Builder(
 104                              "<init>",
 105                              "A",
 106                              TypeAnnotation.TargetType.METHOD_FORMAL_PARAMETER,
 107                              false,
 108                              1).setParameterIndex(2).build()
 109                      },
 110                      null);
 111 
 112     public static void main(String... args) throws Exception {
 113         new SyntheticParameters().run(
 114             new ClassFile[] { getClassFile(Inner_class), getClassFile(Foo_class) },
 115             new Expected[] { Inner_expected, Foo_expected });
 116     }
 117 
 118     public class Inner {
 119         public Inner(@A int a) {}
 120         public void foo(@A int a, int b) {}
 121     }
 122 
 123     public static enum Foo {
 124         ONE(null);
 125         Foo(@A Object a) {}
 126     }
 127 }
 128 
 129 @Target({ElementType.TYPE_USE})
 130 @interface A {}