|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | + * @bug 8315575 |
| 27 | + * @summary test that records with invisible annotation can be retransformed |
| 28 | + * |
| 29 | + * @library /test/lib |
| 30 | + * @run shell MakeJAR.sh retransformAgent |
| 31 | + * @run main/othervm -javaagent:retransformAgent.jar -Xlog:redefine+class=trace RetransformRecordAnnotation |
| 32 | + */ |
| 33 | + |
| 34 | +import java.io.File; |
| 35 | +import java.lang.annotation.ElementType; |
| 36 | +import java.lang.annotation.Retention; |
| 37 | +import java.lang.annotation.RetentionPolicy; |
| 38 | +import java.lang.annotation.Target; |
| 39 | +import java.lang.instrument.ClassFileTransformer; |
| 40 | +import java.nio.file.Files; |
| 41 | +import java.security.ProtectionDomain; |
| 42 | + |
| 43 | +public class RetransformRecordAnnotation extends AInstrumentationTestCase { |
| 44 | + |
| 45 | + @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) |
| 46 | + @Retention(RetentionPolicy.RUNTIME) |
| 47 | + @interface RuntimeTypeAnno {} |
| 48 | + |
| 49 | + @Retention(RetentionPolicy.RUNTIME) |
| 50 | + @interface RuntimeParamAnno { |
| 51 | + String s() default "foo"; |
| 52 | + } |
| 53 | + |
| 54 | + @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) |
| 55 | + @Retention(RetentionPolicy.CLASS) |
| 56 | + @interface ClassTypeAnno {} |
| 57 | + |
| 58 | + @Retention(RetentionPolicy.CLASS) |
| 59 | + @interface ClassParamAnno { |
| 60 | + String s() default "bar"; |
| 61 | + } |
| 62 | + |
| 63 | + @RuntimeTypeAnno |
| 64 | + @RuntimeParamAnno(s = "1") |
| 65 | + public record VisibleAnnos(@RuntimeTypeAnno @RuntimeParamAnno(s = "2") Object o, Object other) { |
| 66 | + } |
| 67 | + |
| 68 | + @ClassTypeAnno |
| 69 | + @ClassParamAnno(s = "3") |
| 70 | + public record InvisibleAnnos(@ClassTypeAnno @ClassParamAnno(s = "4") Object o, Object other) { |
| 71 | + } |
| 72 | + |
| 73 | + @RuntimeTypeAnno |
| 74 | + @RuntimeParamAnno(s = "5") |
| 75 | + @ClassTypeAnno |
| 76 | + @ClassParamAnno(s = "6") |
| 77 | + public record MixedAnnos(@RuntimeTypeAnno @RuntimeParamAnno(s = "7") |
| 78 | + @ClassTypeAnno @ClassParamAnno(s = "8") Object o, Object other) { |
| 79 | + } |
| 80 | + |
| 81 | + public static void main (String[] args) throws Throwable { |
| 82 | + ATestCaseScaffold test = new RetransformRecordAnnotation(); |
| 83 | + test.beVerbose(); |
| 84 | + test.runTest(); |
| 85 | + } |
| 86 | + |
| 87 | + private Transformer transformer; |
| 88 | + |
| 89 | + public RetransformRecordAnnotation() throws Throwable { |
| 90 | + super("RetransformRecordAnnotation"); |
| 91 | + } |
| 92 | + |
| 93 | + private void log(Object o) { |
| 94 | + System.out.println(String.valueOf(o)); |
| 95 | + } |
| 96 | + |
| 97 | + // Retransforms target class using provided class bytes; |
| 98 | + private void retransform(Class targetClass, byte[] classBytes) throws Throwable { |
| 99 | + transformer.prepare(targetClass, classBytes); |
| 100 | + fInst.retransformClasses(targetClass); |
| 101 | + assertTrue(targetClass.getName() + " was not seen by transform()", |
| 102 | + transformer.getSeenClassBytes() != null); |
| 103 | + } |
| 104 | + |
| 105 | + protected final void doRunTest() throws Throwable { |
| 106 | + transformer = new Transformer(); |
| 107 | + fInst.addTransformer(transformer, true); |
| 108 | + |
| 109 | + { |
| 110 | + log("Sanity: retransform to original class bytes"); |
| 111 | + retransform(InvisibleAnnos.class, loadClassBytes(InvisibleAnnos.class)); |
| 112 | + log(""); |
| 113 | + } |
| 114 | + |
| 115 | + // The following testcases use null as new class bytes (i.e. no transform is performed). |
| 116 | + // However, it is enough for testing purposes as the JvmtiClassFileReconstituter is still involved |
| 117 | + // in preparation of the initial class bytes. |
| 118 | + { |
| 119 | + log("Test: retransform VisibleAnnos to null"); |
| 120 | + retransform(VisibleAnnos.class, null); |
| 121 | + log(""); |
| 122 | + } |
| 123 | + |
| 124 | + { |
| 125 | + log("Test: retransform InvisibleAnnos to null"); |
| 126 | + retransform(InvisibleAnnos.class, null); |
| 127 | + log(""); |
| 128 | + } |
| 129 | + |
| 130 | + { |
| 131 | + log("Test: retransform MixedAnnos to null"); |
| 132 | + retransform(MixedAnnos.class, null); |
| 133 | + log(""); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private byte[] loadClassBytes(Class cls) throws Exception { |
| 138 | + String classFileName = cls.getName() + ".class"; |
| 139 | + File classFile = new File(System.getProperty("test.classes", "."), classFileName); |
| 140 | + log("Reading test class from " + classFile); |
| 141 | + byte[] classBytes = Files.readAllBytes(classFile.toPath()); |
| 142 | + log("Read " + classBytes.length + " bytes."); |
| 143 | + return classBytes; |
| 144 | + } |
| 145 | + |
| 146 | + public class Transformer implements ClassFileTransformer { |
| 147 | + private String targetClassName; |
| 148 | + private byte[] seenClassBytes; |
| 149 | + private byte[] newClassBytes; |
| 150 | + |
| 151 | + public Transformer() { |
| 152 | + } |
| 153 | + |
| 154 | + // Prepares transformer for Instrumentation.retransformClasses. |
| 155 | + public void prepare(Class targetClass, byte[] classBytes) { |
| 156 | + targetClassName = targetClass.getName(); |
| 157 | + newClassBytes = classBytes; |
| 158 | + seenClassBytes = null; |
| 159 | + } |
| 160 | + |
| 161 | + byte[] getSeenClassBytes() { |
| 162 | + return seenClassBytes; |
| 163 | + } |
| 164 | + |
| 165 | + public String toString() { |
| 166 | + return Transformer.this.getClass().getName(); |
| 167 | + } |
| 168 | + |
| 169 | + public byte[] transform(ClassLoader loader, String className, |
| 170 | + Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { |
| 171 | + |
| 172 | + if (className.equals(targetClassName)) { |
| 173 | + log(this + ".transform() sees '" + className |
| 174 | + + "' of " + classfileBuffer.length + " bytes."); |
| 175 | + seenClassBytes = classfileBuffer; |
| 176 | + if (newClassBytes != null) { |
| 177 | + log(this + ".transform() sets new classbytes for '" + className |
| 178 | + + "' of " + newClassBytes.length + " bytes."); |
| 179 | + } |
| 180 | + return newClassBytes; |
| 181 | + } |
| 182 | + |
| 183 | + return null; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments