1 /* 2 * Copyright (c) 2023, 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. 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 package jdk.internal.classfile.impl; 26 27 import java.lang.classfile.Annotation; 28 import java.lang.classfile.AnnotationElement; 29 import java.lang.classfile.AnnotationValue; 30 import java.lang.classfile.ClassBuilder; 31 import java.lang.classfile.ClassElement; 32 import java.lang.classfile.ClassSignature; 33 import java.lang.classfile.CodeBuilder; 34 import java.lang.classfile.CodeElement; 35 import java.lang.classfile.CodeModel; 36 import java.lang.classfile.CodeTransform; 37 import java.lang.classfile.FieldBuilder; 38 import java.lang.classfile.FieldElement; 39 import java.lang.classfile.FieldModel; 40 import java.lang.classfile.FieldTransform; 41 import java.lang.classfile.Interfaces; 42 import java.lang.classfile.MethodBuilder; 43 import java.lang.classfile.MethodElement; 44 import java.lang.classfile.MethodModel; 45 import java.lang.classfile.MethodSignature; 46 import java.lang.classfile.MethodTransform; 47 import java.lang.classfile.Signature; 48 import java.lang.classfile.Superclass; 49 import java.lang.classfile.TypeAnnotation; 50 import java.lang.classfile.attribute.AnnotationDefaultAttribute; 51 import java.lang.classfile.attribute.EnclosingMethodAttribute; 52 import java.lang.classfile.attribute.ExceptionsAttribute; 53 import java.lang.classfile.attribute.InnerClassInfo; 54 import java.lang.classfile.attribute.InnerClassesAttribute; 55 import java.lang.classfile.attribute.LoadableDescriptorsAttribute; 56 import java.lang.classfile.attribute.ModuleAttribute; 57 import java.lang.classfile.attribute.ModuleProvideInfo; 58 import java.lang.classfile.attribute.NestHostAttribute; 59 import java.lang.classfile.attribute.NestMembersAttribute; 60 import java.lang.classfile.attribute.PermittedSubclassesAttribute; 61 import java.lang.classfile.attribute.RecordAttribute; 62 import java.lang.classfile.attribute.RecordComponentInfo; 63 import java.lang.classfile.attribute.RuntimeInvisibleAnnotationsAttribute; 64 import java.lang.classfile.attribute.RuntimeInvisibleParameterAnnotationsAttribute; 65 import java.lang.classfile.attribute.RuntimeInvisibleTypeAnnotationsAttribute; 66 import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute; 67 import java.lang.classfile.attribute.RuntimeVisibleParameterAnnotationsAttribute; 68 import java.lang.classfile.attribute.RuntimeVisibleTypeAnnotationsAttribute; 69 import java.lang.classfile.attribute.SignatureAttribute; 70 import java.lang.classfile.components.ClassRemapper; 71 import java.lang.classfile.constantpool.Utf8Entry; 72 import java.lang.classfile.instruction.ConstantInstruction.LoadConstantInstruction; 73 import java.lang.classfile.instruction.ExceptionCatch; 74 import java.lang.classfile.instruction.FieldInstruction; 75 import java.lang.classfile.instruction.InvokeDynamicInstruction; 76 import java.lang.classfile.instruction.InvokeInstruction; 77 import java.lang.classfile.instruction.LocalVariable; 78 import java.lang.classfile.instruction.LocalVariableType; 79 import java.lang.classfile.instruction.NewMultiArrayInstruction; 80 import java.lang.classfile.instruction.NewObjectInstruction; 81 import java.lang.classfile.instruction.NewReferenceArrayInstruction; 82 import java.lang.classfile.instruction.TypeCheckInstruction; 83 84 import java.lang.constant.ClassDesc; 85 import java.lang.constant.ConstantDesc; 86 import java.lang.constant.DirectMethodHandleDesc; 87 import java.lang.constant.DynamicCallSiteDesc; 88 import java.lang.constant.DynamicConstantDesc; 89 import java.lang.constant.MethodHandleDesc; 90 import java.lang.constant.MethodTypeDesc; 91 import java.util.List; 92 import java.util.function.Function; 93 94 public record ClassRemapperImpl(Function<ClassDesc, ClassDesc> mapFunction) implements ClassRemapper { 95 96 @Override 97 public void accept(ClassBuilder clb, ClassElement cle) { 98 switch (cle) { 99 case FieldModel fm -> 100 clb.withField(fm.fieldName().stringValue(), map( 101 fm.fieldTypeSymbol()), fb -> fb.transform(fm, asFieldTransform())); 102 case MethodModel mm -> 103 clb.withMethod(mm.methodName().stringValue(), mapMethodDesc( 104 mm.methodTypeSymbol()), mm.flags().flagsMask(), mb -> mb.transform(mm, asMethodTransform())); 105 case Superclass sc -> 106 clb.withSuperclass(map(sc.superclassEntry().asSymbol())); 107 case Interfaces ins -> 108 clb.withInterfaceSymbols(Util.mappedList(ins.interfaces(), in -> 109 map(in.asSymbol()))); 110 case SignatureAttribute sa -> 111 clb.with(SignatureAttribute.of(mapClassSignature(sa.asClassSignature()))); 112 case InnerClassesAttribute ica -> 113 clb.with(InnerClassesAttribute.of(ica.classes().stream().map(ici -> 114 InnerClassInfo.of(map(ici.innerClass().asSymbol()), 115 ici.outerClass().map(oc -> map(oc.asSymbol())), 116 ici.innerName().map(Utf8Entry::stringValue), 117 ici.flagsMask())).toList())); 118 case EnclosingMethodAttribute ema -> 119 clb.with(EnclosingMethodAttribute.of(map(ema.enclosingClass().asSymbol()), 120 ema.enclosingMethodName().map(Utf8Entry::stringValue), 121 ema.enclosingMethodTypeSymbol().map(this::mapMethodDesc))); 122 case RecordAttribute ra -> 123 clb.with(RecordAttribute.of(ra.components().stream() 124 .map(this::mapRecordComponent).toList())); 125 case ModuleAttribute ma -> 126 clb.with(ModuleAttribute.of(ma.moduleName(), ma.moduleFlagsMask(), 127 ma.moduleVersion().orElse(null), 128 ma.requires(), ma.exports(), ma.opens(), 129 ma.uses().stream().map(ce -> 130 clb.constantPool().classEntry(map(ce.asSymbol()))).toList(), 131 ma.provides().stream().map(mp -> 132 ModuleProvideInfo.of(map(mp.provides().asSymbol()), 133 mp.providesWith().stream().map(pw -> 134 map(pw.asSymbol())).toList())).toList())); 135 case NestHostAttribute nha -> 136 clb.with(NestHostAttribute.of(map(nha.nestHost().asSymbol()))); 137 case NestMembersAttribute nma -> 138 clb.with(NestMembersAttribute.ofSymbols(nma.nestMembers().stream() 139 .map(nm -> map(nm.asSymbol())).toList())); 140 case PermittedSubclassesAttribute psa -> 141 clb.with(PermittedSubclassesAttribute.ofSymbols( 142 psa.permittedSubclasses().stream().map(ps -> 143 map(ps.asSymbol())).toList())); 144 case LoadableDescriptorsAttribute pa -> 145 clb.with(LoadableDescriptorsAttribute.of(pa.loadableDescriptors())); 146 case RuntimeVisibleAnnotationsAttribute aa -> 147 clb.with(RuntimeVisibleAnnotationsAttribute.of( 148 mapAnnotations(aa.annotations()))); 149 case RuntimeInvisibleAnnotationsAttribute aa -> 150 clb.with(RuntimeInvisibleAnnotationsAttribute.of( 151 mapAnnotations(aa.annotations()))); 152 case RuntimeVisibleTypeAnnotationsAttribute aa -> 153 clb.with(RuntimeVisibleTypeAnnotationsAttribute.of( 154 mapTypeAnnotations(aa.annotations()))); 155 case RuntimeInvisibleTypeAnnotationsAttribute aa -> 156 clb.with(RuntimeInvisibleTypeAnnotationsAttribute.of( 157 mapTypeAnnotations(aa.annotations()))); 158 default -> 159 clb.with(cle); 160 } 161 } 162 163 @Override 164 public FieldTransform asFieldTransform() { 165 return (FieldBuilder fb, FieldElement fe) -> { 166 switch (fe) { 167 case SignatureAttribute sa -> 168 fb.with(SignatureAttribute.of( 169 mapSignature(sa.asTypeSignature()))); 170 case RuntimeVisibleAnnotationsAttribute aa -> 171 fb.with(RuntimeVisibleAnnotationsAttribute.of( 172 mapAnnotations(aa.annotations()))); 173 case RuntimeInvisibleAnnotationsAttribute aa -> 174 fb.with(RuntimeInvisibleAnnotationsAttribute.of( 175 mapAnnotations(aa.annotations()))); 176 case RuntimeVisibleTypeAnnotationsAttribute aa -> 177 fb.with(RuntimeVisibleTypeAnnotationsAttribute.of( 178 mapTypeAnnotations(aa.annotations()))); 179 case RuntimeInvisibleTypeAnnotationsAttribute aa -> 180 fb.with(RuntimeInvisibleTypeAnnotationsAttribute.of( 181 mapTypeAnnotations(aa.annotations()))); 182 default -> 183 fb.with(fe); 184 } 185 }; 186 } 187 188 @Override 189 public MethodTransform asMethodTransform() { 190 return (MethodBuilder mb, MethodElement me) -> { 191 switch (me) { 192 case AnnotationDefaultAttribute ada -> 193 mb.with(AnnotationDefaultAttribute.of( 194 mapAnnotationValue(ada.defaultValue()))); 195 case CodeModel com -> 196 mb.transformCode(com, asCodeTransform()); 197 case ExceptionsAttribute ea -> 198 mb.with(ExceptionsAttribute.ofSymbols( 199 ea.exceptions().stream().map(ce -> 200 map(ce.asSymbol())).toList())); 201 case SignatureAttribute sa -> 202 mb.with(SignatureAttribute.of( 203 mapMethodSignature(sa.asMethodSignature()))); 204 case RuntimeVisibleAnnotationsAttribute aa -> 205 mb.with(RuntimeVisibleAnnotationsAttribute.of( 206 mapAnnotations(aa.annotations()))); 207 case RuntimeInvisibleAnnotationsAttribute aa -> 208 mb.with(RuntimeInvisibleAnnotationsAttribute.of( 209 mapAnnotations(aa.annotations()))); 210 case RuntimeVisibleParameterAnnotationsAttribute paa -> 211 mb.with(RuntimeVisibleParameterAnnotationsAttribute.of( 212 paa.parameterAnnotations().stream() 213 .map(this::mapAnnotations).toList())); 214 case RuntimeInvisibleParameterAnnotationsAttribute paa -> 215 mb.with(RuntimeInvisibleParameterAnnotationsAttribute.of( 216 paa.parameterAnnotations().stream() 217 .map(this::mapAnnotations).toList())); 218 case RuntimeVisibleTypeAnnotationsAttribute aa -> 219 mb.with(RuntimeVisibleTypeAnnotationsAttribute.of( 220 mapTypeAnnotations(aa.annotations()))); 221 case RuntimeInvisibleTypeAnnotationsAttribute aa -> 222 mb.with(RuntimeInvisibleTypeAnnotationsAttribute.of( 223 mapTypeAnnotations(aa.annotations()))); 224 default -> 225 mb.with(me); 226 } 227 }; 228 } 229 230 @Override 231 public CodeTransform asCodeTransform() { 232 return (CodeBuilder cob, CodeElement coe) -> { 233 switch (coe) { 234 case FieldInstruction fai -> 235 cob.fieldAccess(fai.opcode(), map(fai.owner().asSymbol()), 236 fai.name().stringValue(), map(fai.typeSymbol())); 237 case InvokeInstruction ii -> 238 cob.invoke(ii.opcode(), map(ii.owner().asSymbol()), 239 ii.name().stringValue(), mapMethodDesc(ii.typeSymbol()), 240 ii.isInterface()); 241 case InvokeDynamicInstruction idi -> 242 cob.invokedynamic(DynamicCallSiteDesc.of( 243 mapDirectMethodHandle(idi.bootstrapMethod()), idi.name().stringValue(), 244 mapMethodDesc(idi.typeSymbol()), 245 idi.bootstrapArgs().stream().map(this::mapConstantValue).toArray(ConstantDesc[]::new))); 246 case NewObjectInstruction c -> 247 cob.new_(map(c.className().asSymbol())); 248 case NewReferenceArrayInstruction c -> 249 cob.anewarray(map(c.componentType().asSymbol())); 250 case NewMultiArrayInstruction c -> 251 cob.multianewarray(map(c.arrayType().asSymbol()), c.dimensions()); 252 case TypeCheckInstruction c -> 253 cob.with(TypeCheckInstruction.of(c.opcode(), map(c.type().asSymbol()))); 254 case ExceptionCatch c -> 255 cob.exceptionCatch(c.tryStart(), c.tryEnd(), c.handler(),c.catchType() 256 .map(d -> TemporaryConstantPool.INSTANCE.classEntry(map(d.asSymbol())))); 257 case LocalVariable c -> 258 cob.localVariable(c.slot(), c.name().stringValue(), map(c.typeSymbol()), 259 c.startScope(), c.endScope()); 260 case LocalVariableType c -> 261 cob.localVariableType(c.slot(), c.name().stringValue(), 262 mapSignature(c.signatureSymbol()), c.startScope(), c.endScope()); 263 case LoadConstantInstruction ldc -> 264 cob.ldc(mapConstantValue(ldc.constantValue())); 265 case RuntimeVisibleTypeAnnotationsAttribute aa -> 266 cob.with(RuntimeVisibleTypeAnnotationsAttribute.of( 267 mapTypeAnnotations(aa.annotations()))); 268 case RuntimeInvisibleTypeAnnotationsAttribute aa -> 269 cob.with(RuntimeInvisibleTypeAnnotationsAttribute.of( 270 mapTypeAnnotations(aa.annotations()))); 271 default -> 272 cob.with(coe); 273 } 274 }; 275 } 276 277 @Override 278 public ClassDesc map(ClassDesc desc) { 279 if (desc == null) return null; 280 if (desc.isArray()) return map(desc.componentType()).arrayType(); 281 if (desc.isPrimitive()) return desc; 282 return mapFunction.apply(desc); 283 } 284 285 MethodTypeDesc mapMethodDesc(MethodTypeDesc desc) { 286 return MethodTypeDesc.of(map(desc.returnType()), 287 desc.parameterList().stream().map(this::map).toArray(ClassDesc[]::new)); 288 } 289 290 ClassSignature mapClassSignature(ClassSignature signature) { 291 return ClassSignature.of(mapTypeParams(signature.typeParameters()), 292 mapSignature(signature.superclassSignature()), 293 signature.superinterfaceSignatures().stream() 294 .map(this::mapSignature).toArray(Signature.ClassTypeSig[]::new)); 295 } 296 297 MethodSignature mapMethodSignature(MethodSignature signature) { 298 return MethodSignature.of(mapTypeParams(signature.typeParameters()), 299 signature.throwableSignatures().stream().map(this::mapSignature).toList(), 300 mapSignature(signature.result()), 301 signature.arguments().stream() 302 .map(this::mapSignature).toArray(Signature[]::new)); 303 } 304 305 RecordComponentInfo mapRecordComponent(RecordComponentInfo component) { 306 return RecordComponentInfo.of(component.name().stringValue(), 307 map(component.descriptorSymbol()), 308 component.attributes().stream().map(atr -> 309 switch (atr) { 310 case SignatureAttribute sa -> 311 SignatureAttribute.of( 312 mapSignature(sa.asTypeSignature())); 313 case RuntimeVisibleAnnotationsAttribute aa -> 314 RuntimeVisibleAnnotationsAttribute.of( 315 mapAnnotations(aa.annotations())); 316 case RuntimeInvisibleAnnotationsAttribute aa -> 317 RuntimeInvisibleAnnotationsAttribute.of( 318 mapAnnotations(aa.annotations())); 319 case RuntimeVisibleTypeAnnotationsAttribute aa -> 320 RuntimeVisibleTypeAnnotationsAttribute.of( 321 mapTypeAnnotations(aa.annotations())); 322 case RuntimeInvisibleTypeAnnotationsAttribute aa -> 323 RuntimeInvisibleTypeAnnotationsAttribute.of( 324 mapTypeAnnotations(aa.annotations())); 325 default -> atr; 326 }).toList()); 327 } 328 329 DirectMethodHandleDesc mapDirectMethodHandle(DirectMethodHandleDesc dmhd) { 330 return switch (dmhd.kind()) { 331 case GETTER, SETTER, STATIC_GETTER, STATIC_SETTER -> 332 MethodHandleDesc.ofField(dmhd.kind(), map(dmhd.owner()), 333 dmhd.methodName(), 334 map(ClassDesc.ofDescriptor(dmhd.lookupDescriptor()))); 335 default -> 336 MethodHandleDesc.ofMethod(dmhd.kind(), map(dmhd.owner()), 337 dmhd.methodName(), 338 mapMethodDesc(MethodTypeDesc.ofDescriptor(dmhd.lookupDescriptor()))); 339 }; 340 } 341 342 ConstantDesc mapConstantValue(ConstantDesc value) { 343 return switch (value) { 344 case ClassDesc cd -> 345 map(cd); 346 case DynamicConstantDesc<?> dcd -> 347 mapDynamicConstant(dcd); 348 case DirectMethodHandleDesc dmhd -> 349 mapDirectMethodHandle(dmhd); 350 case MethodTypeDesc mtd -> 351 mapMethodDesc(mtd); 352 default -> value; 353 }; 354 } 355 356 DynamicConstantDesc<?> mapDynamicConstant(DynamicConstantDesc<?> dcd) { 357 return DynamicConstantDesc.ofNamed(mapDirectMethodHandle(dcd.bootstrapMethod()), 358 dcd.constantName(), 359 map(dcd.constantType()), 360 dcd.bootstrapArgsList().stream().map(this::mapConstantValue).toArray(ConstantDesc[]::new)); 361 } 362 363 @SuppressWarnings("unchecked") 364 <S extends Signature> S mapSignature(S signature) { 365 return (S) switch (signature) { 366 case Signature.ArrayTypeSig ats -> 367 Signature.ArrayTypeSig.of(mapSignature(ats.componentSignature())); 368 case Signature.ClassTypeSig cts -> 369 Signature.ClassTypeSig.of( 370 cts.outerType().map(this::mapSignature).orElse(null), 371 map(cts.classDesc()), 372 cts.typeArgs().stream().map(ta -> switch (ta) { 373 case Signature.TypeArg.Unbounded u -> u; 374 case Signature.TypeArg.Bounded bta -> Signature.TypeArg.bounded( 375 bta.wildcardIndicator(), mapSignature(bta.boundType())); 376 }).toArray(Signature.TypeArg[]::new)); 377 default -> signature; 378 }; 379 } 380 381 List<Annotation> mapAnnotations(List<Annotation> annotations) { 382 return annotations.stream().map(this::mapAnnotation).toList(); 383 } 384 385 Annotation mapAnnotation(Annotation a) { 386 return Annotation.of(map(a.classSymbol()), a.elements().stream().map(el -> 387 AnnotationElement.of(el.name(), mapAnnotationValue(el.value()))).toList()); 388 } 389 390 AnnotationValue mapAnnotationValue(AnnotationValue val) { 391 return switch (val) { 392 case AnnotationValue.OfAnnotation oa -> 393 AnnotationValue.ofAnnotation(mapAnnotation(oa.annotation())); 394 case AnnotationValue.OfArray oa -> 395 AnnotationValue.ofArray(oa.values().stream().map(this::mapAnnotationValue).toList()); 396 case AnnotationValue.OfConstant oc -> oc; 397 case AnnotationValue.OfClass oc -> 398 AnnotationValue.ofClass(map(oc.classSymbol())); 399 case AnnotationValue.OfEnum oe -> 400 AnnotationValue.ofEnum(map(oe.classSymbol()), oe.constantName().stringValue()); 401 }; 402 } 403 404 List<TypeAnnotation> mapTypeAnnotations(List<TypeAnnotation> typeAnnotations) { 405 return typeAnnotations.stream().map(a -> TypeAnnotation.of(a.targetInfo(), 406 a.targetPath(), mapAnnotation(a.annotation()))).toList(); 407 } 408 409 List<Signature.TypeParam> mapTypeParams(List<Signature.TypeParam> typeParams) { 410 return typeParams.stream().map(tp -> Signature.TypeParam.of(tp.identifier(), 411 tp.classBound().map(this::mapSignature), 412 tp.interfaceBounds().stream() 413 .map(this::mapSignature).toArray(Signature.RefTypeSig[]::new))).toList(); 414 } 415 416 }