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.  Oracle designates this
   8 * particular file as subject to the "Classpath" exception as provided
   9 * by Oracle in the LICENSE file that accompanied this code.
  10 *
  11 * This code is distributed in the hope that it will be useful, but WITHOUT
  12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14 * version 2 for more details (a copy is included in the LICENSE file that
  15 * accompanied this code).
  16 *
  17 * You should have received a copy of the GNU General Public License version
  18 * 2 along with this work; if not, write to the Free Software Foundation,
  19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20 *
  21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22 * or visit www.oracle.com if you need additional information or have any
  23 * questions.
  24 */
  25 
  26 /*
  27  * Create class file using ASM, slightly modified the ASMifier output
  28 */
  29 
  30 
  31 
  32 import java.io.File;
  33 import java.io.FileOutputStream;
  34 import java.io.IOException;
  35 import jdk.internal.org.objectweb.asm.*;
  36 
  37 
  38 public class ClassFileGenerator {
  39 
  40     public static void main(String... args) throws Exception {
  41         classFileWriter("AnnoationWithVoidReturn.class", AnnoationWithVoidReturnDump.dump());
  42         classFileWriter("AnnoationWithParameter.class", AnnoationWithParameterDump.dump());
  43         classFileWriter("AnnoationWithStaticModifier.class", AnnoationWithStaticModifierDump.dump());
  44         classFileWriter("AnnoationWithPrivateModifier.class", AnnoationWithPrivateModifierDump.dump());
  45     }
  46 
  47     private static void classFileWriter(String name, byte[] contents) throws IOException {
  48         try (FileOutputStream fos = new FileOutputStream(new File(System.getProperty("test.classes"),
  49                 name))) {
  50             fos.write(contents);
  51         }
  52     }
  53 
  54     /*
  55     Following code create equivalent classfile, 
  56     which is not allowed by javac.
  57     @Retention(RetentionPolicy.RUNTIME)
  58     public @interface AnnoationWithVoidReturn {
  59     void m() default 1;
  60     }
  61     */
  62 
  63     private static class AnnoationWithVoidReturnDump implements Opcodes {
  64         public static byte[] dump() throws Exception {
  65             ClassWriter cw = new ClassWriter(0);
  66             MethodVisitor mv;
  67             AnnotationVisitor av0;
  68 
  69             cw.visit(52, ACC_PUBLIC + ACC_ANNOTATION + ACC_ABSTRACT + +ACC_INTERFACE,
  70                     "AnnoationWithVoidReturn", null,
  71                     "java/lang/Object", new String[]{"java/lang/annotation/Annotation"});
  72 
  73             {
  74                 av0 = cw.visitAnnotation("Ljava/lang/annotation/Retention;", true);
  75                 av0.visitEnum("value", "Ljava/lang/annotation/RetentionPolicy;",
  76                         "RUNTIME");
  77                 av0.visitEnd();
  78             }
  79             {
  80                 mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "m", "()V", null, null);
  81                 mv.visitEnd();
  82             }
  83             {
  84                 av0 = mv.visitAnnotationDefault();
  85                 av0.visit(null, new Integer(1));
  86                 av0.visitEnd();
  87             }
  88             cw.visitEnd();
  89 
  90             return cw.toByteArray();
  91 
  92         }
  93     }
  94 
  95     /*
  96     Following code create equivalent classfile, 
  97     which is not allowed by javac.
  98     @Retention(RetentionPolicy.RUNTIME)
  99     public @interface AnnoationWithParameter {
 100        int m(int x);
 101     }
 102     */
 103 
 104     private static class AnnoationWithParameterDump implements Opcodes {
 105         public static byte[] dump() throws Exception {
 106 
 107             ClassWriter cw = new ClassWriter(0);
 108             MethodVisitor mv;
 109             AnnotationVisitor av0;
 110 
 111             cw.visit(52, ACC_PUBLIC + ACC_ANNOTATION + ACC_ABSTRACT + ACC_INTERFACE,
 112                     "AnnoationWithParameter", null,
 113                     "java/lang/Object", new String[]{"java/lang/annotation/Annotation"});
 114 
 115             {
 116                 av0 = cw.visitAnnotation("Ljava/lang/annotation/Retention;", true);
 117                 av0.visitEnum("value", "Ljava/lang/annotation/RetentionPolicy;",
 118                         "RUNTIME");
 119                 av0.visitEnd();
 120             }
 121             {
 122                 mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT,
 123                         "badValue",
 124                         "(I)I", // Bad method with a parameter
 125                         null, null);
 126                 mv.visitEnd();
 127             }
 128             {
 129                 av0 = mv.visitAnnotationDefault();
 130                 av0.visit(null, new Integer(-1));
 131                 av0.visitEnd();
 132             }
 133             cw.visitEnd();
 134 
 135             return cw.toByteArray();
 136         }
 137     }
 138 
 139     /*
 140     Following code create equivalent classfile,
 141     which is not allowed by javac.
 142     @Retention(RetentionPolicy.RUNTIME)
 143     public @interface AnnoationWithStaticModifier {
 144         static int m() {
 145             return 1;
 146         }
 147     }
 148     */
 149 
 150     private static class AnnoationWithStaticModifierDump implements Opcodes {
 151         public static byte[] dump() throws Exception {
 152             ClassWriter cw = new ClassWriter(0);
 153             MethodVisitor mv;
 154             AnnotationVisitor av0;
 155 
 156             cw.visit(52, ACC_PUBLIC + ACC_ANNOTATION + ACC_ABSTRACT + ACC_INTERFACE,
 157                     "AnnoationWithStaticModifier", null,
 158                     "java/lang/Object", new String[]{"java/lang/annotation/Annotation"});
 159 
 160             {
 161                 av0 = cw.visitAnnotation("Ljava/lang/annotation/Retention;", true);
 162                 av0.visitEnum("value", "Ljava/lang/annotation/RetentionPolicy;",
 163                         "RUNTIME");
 164                 av0.visitEnd();
 165             }
 166             {
 167                 mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "m", "()I", null, null);
 168                 {
 169                     av0 = mv.visitAnnotationDefault();
 170                     av0.visit(null, new Integer(1));
 171                     av0.visitEnd();
 172                 }
 173                 mv.visitCode();
 174                 mv.visitInsn(ICONST_M1);
 175                 mv.visitInsn(IRETURN);
 176                 mv.visitMaxs(1, 1);
 177                 mv.visitEnd();
 178             }
 179             cw.visitEnd();
 180 
 181             return cw.toByteArray();
 182 
 183         }
 184     }
 185 
 186     /*
 187     Following code create equivalent classfile, 
 188     which is not allowed by javac.
 189     @Retention(RetentionPolicy.RUNTIME)
 190     public @interface AnnoationWithPrivateModifier {
 191         private int m() {
 192             return 1;
 193         }
 194     }
 195     */
 196 
 197     private static class AnnoationWithPrivateModifierDump implements Opcodes {
 198         public static byte[] dump() throws Exception {
 199             ClassWriter cw = new ClassWriter(0);
 200             MethodVisitor mv;
 201             AnnotationVisitor av0;
 202 
 203             cw.visit(V1_8,
 204                     ACC_PUBLIC + ACC_ANNOTATION + ACC_ABSTRACT + ACC_INTERFACE,
 205                     "AnnoationWithPrivateModifier",
 206                     null, "java/lang/Object",
 207                     new String[]{"java/lang/annotation/Annotation"});
 208 
 209             {
 210                 av0 = cw.visitAnnotation("Ljava/lang/annotation/Retention;", true);
 211                 av0.visitEnum("value", "Ljava/lang/annotation/RetentionPolicy;",
 212                         "RUNTIME");
 213                 av0.visitEnd();
 214             }
 215             {
 216 
 217                 mv = cw.visitMethod(ACC_PRIVATE, // Bad modifier, not abstract
 218                         "badMethod",
 219                         "()I",
 220                         null,
 221                         null);
 222                 {
 223                     av0 = mv.visitAnnotationDefault();
 224                     av0.visit(null, new Integer(1));
 225                     av0.visitEnd();
 226                 }
 227 
 228                 {
 229                     mv.visitCode();
 230                     mv.visitInsn(ICONST_M1);
 231                     mv.visitInsn(IRETURN);
 232                     mv.visitMaxs(1, 1);
 233                     mv.visitEnd();
 234                 }
 235                 mv.visitEnd();
 236             }
 237             cw.visitEnd();
 238 
 239             return cw.toByteArray();
 240 
 241         }
 242     }
 243 }