< prev index next >

test/hotspot/jtreg/runtime/RedefineTests/RedefineAnnotations.java

Print this page
rev 52456 : imported patch asm.additional


  46 import java.lang.annotation.Target;
  47 import java.lang.instrument.ClassFileTransformer;
  48 import java.lang.instrument.IllegalClassFormatException;
  49 import java.lang.instrument.Instrumentation;
  50 import java.lang.instrument.UnmodifiableClassException;
  51 import java.lang.reflect.AnnotatedArrayType;
  52 import java.lang.reflect.AnnotatedParameterizedType;
  53 import java.lang.reflect.AnnotatedType;
  54 import java.lang.reflect.AnnotatedWildcardType;
  55 import java.lang.reflect.Executable;
  56 import java.lang.reflect.TypeVariable;
  57 import java.security.ProtectionDomain;
  58 import java.util.Arrays;
  59 import java.util.LinkedList;
  60 import java.util.List;
  61 import java.util.Map;
  62 import jdk.internal.org.objectweb.asm.ClassReader;
  63 import jdk.internal.org.objectweb.asm.ClassVisitor;
  64 import jdk.internal.org.objectweb.asm.ClassWriter;
  65 import jdk.internal.org.objectweb.asm.FieldVisitor;
  66 import static jdk.internal.org.objectweb.asm.Opcodes.ASM5;
  67 
  68 @Retention(RetentionPolicy.RUNTIME)
  69 @Target(ElementType.TYPE_USE)
  70 @interface TestAnn {
  71     String site();
  72 }
  73 
  74 public class RedefineAnnotations {
  75     static Instrumentation inst;
  76     public static void premain(String agentArgs, Instrumentation inst) {
  77         RedefineAnnotations.inst = inst;
  78     }
  79 
  80     static class Transformer implements ClassFileTransformer {
  81 
  82         public byte[] asm(ClassLoader loader, String className,
  83                 Class<?> classBeingRedefined,
  84                 ProtectionDomain protectionDomain, byte[] classfileBuffer)
  85             throws IllegalClassFormatException {
  86 
  87             ClassWriter cw = new ClassWriter(0);
  88             ClassVisitor cv = new ReAddDummyFieldsClassVisitor(ASM5, cw) { };
  89             ClassReader cr = new ClassReader(classfileBuffer);
  90             cr.accept(cv, 0);
  91             return cw.toByteArray();
  92         }
  93 
  94         public class ReAddDummyFieldsClassVisitor extends ClassVisitor {
  95 
  96             LinkedList<F> fields = new LinkedList<>();
  97 
  98             public ReAddDummyFieldsClassVisitor(int api, ClassVisitor cv) {
  99                 super(api, cv);
 100             }
 101 
 102             @Override public FieldVisitor visitField(int access, String name,
 103                     String desc, String signature, Object value) {
 104                 if (name.startsWith("dummy")) {
 105                     // Remove dummy field
 106                     fields.addLast(new F(access, name, desc, signature, value));
 107                     return null;
 108                 }




  46 import java.lang.annotation.Target;
  47 import java.lang.instrument.ClassFileTransformer;
  48 import java.lang.instrument.IllegalClassFormatException;
  49 import java.lang.instrument.Instrumentation;
  50 import java.lang.instrument.UnmodifiableClassException;
  51 import java.lang.reflect.AnnotatedArrayType;
  52 import java.lang.reflect.AnnotatedParameterizedType;
  53 import java.lang.reflect.AnnotatedType;
  54 import java.lang.reflect.AnnotatedWildcardType;
  55 import java.lang.reflect.Executable;
  56 import java.lang.reflect.TypeVariable;
  57 import java.security.ProtectionDomain;
  58 import java.util.Arrays;
  59 import java.util.LinkedList;
  60 import java.util.List;
  61 import java.util.Map;
  62 import jdk.internal.org.objectweb.asm.ClassReader;
  63 import jdk.internal.org.objectweb.asm.ClassVisitor;
  64 import jdk.internal.org.objectweb.asm.ClassWriter;
  65 import jdk.internal.org.objectweb.asm.FieldVisitor;
  66 import static jdk.internal.org.objectweb.asm.Opcodes.ASM7;
  67 
  68 @Retention(RetentionPolicy.RUNTIME)
  69 @Target(ElementType.TYPE_USE)
  70 @interface TestAnn {
  71     String site();
  72 }
  73 
  74 public class RedefineAnnotations {
  75     static Instrumentation inst;
  76     public static void premain(String agentArgs, Instrumentation inst) {
  77         RedefineAnnotations.inst = inst;
  78     }
  79 
  80     static class Transformer implements ClassFileTransformer {
  81 
  82         public byte[] asm(ClassLoader loader, String className,
  83                 Class<?> classBeingRedefined,
  84                 ProtectionDomain protectionDomain, byte[] classfileBuffer)
  85             throws IllegalClassFormatException {
  86 
  87             ClassWriter cw = new ClassWriter(0);
  88             ClassVisitor cv = new ReAddDummyFieldsClassVisitor(ASM7, cw) { };
  89             ClassReader cr = new ClassReader(classfileBuffer);
  90             cr.accept(cv, 0);
  91             return cw.toByteArray();
  92         }
  93 
  94         public class ReAddDummyFieldsClassVisitor extends ClassVisitor {
  95 
  96             LinkedList<F> fields = new LinkedList<>();
  97 
  98             public ReAddDummyFieldsClassVisitor(int api, ClassVisitor cv) {
  99                 super(api, cv);
 100             }
 101 
 102             @Override public FieldVisitor visitField(int access, String name,
 103                     String desc, String signature, Object value) {
 104                 if (name.startsWith("dummy")) {
 105                     // Remove dummy field
 106                     fields.addLast(new F(access, name, desc, signature, value));
 107                     return null;
 108                 }


< prev index next >