1 /* 2 * Copyright (c) 2023, 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.ModuleAttribute; 56 import java.lang.classfile.attribute.ModuleProvideInfo; 57 import java.lang.classfile.attribute.NestHostAttribute; 58 import java.lang.classfile.attribute.NestMembersAttribute; 59 import java.lang.classfile.attribute.PermittedSubclassesAttribute; 60 import java.lang.classfile.attribute.RecordAttribute; 61 import java.lang.classfile.attribute.RecordComponentInfo; 62 import java.lang.classfile.attribute.RuntimeInvisibleAnnotationsAttribute; 63 import java.lang.classfile.attribute.RuntimeInvisibleParameterAnnotationsAttribute; 64 import java.lang.classfile.attribute.RuntimeInvisibleTypeAnnotationsAttribute; 65 import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute; 66 import java.lang.classfile.attribute.RuntimeVisibleParameterAnnotationsAttribute; 67 import java.lang.classfile.attribute.RuntimeVisibleTypeAnnotationsAttribute; 68 import java.lang.classfile.attribute.SignatureAttribute; 69 import java.lang.classfile.components.ClassRemapper; 70 import java.lang.classfile.constantpool.Utf8Entry; 71 import java.lang.classfile.instruction.ConstantInstruction.LoadConstantInstruction; 72 import java.lang.classfile.instruction.ExceptionCatch; 73 import java.lang.classfile.instruction.FieldInstruction; 74 import java.lang.classfile.instruction.InvokeDynamicInstruction; 75 import java.lang.classfile.instruction.InvokeInstruction; 76 import java.lang.classfile.instruction.LocalVariable; 77 import java.lang.classfile.instruction.LocalVariableType; 78 import java.lang.classfile.instruction.NewMultiArrayInstruction; 79 import java.lang.classfile.instruction.NewObjectInstruction; 80 import java.lang.classfile.instruction.NewReferenceArrayInstruction; 81 import java.lang.classfile.instruction.TypeCheckInstruction; 82 83 import java.lang.constant.ClassDesc; 84 import java.lang.constant.ConstantDesc; 85 import java.lang.constant.DirectMethodHandleDesc; 86 import java.lang.constant.DynamicCallSiteDesc; 87 import java.lang.constant.DynamicConstantDesc; 88 import java.lang.constant.MethodHandleDesc; 89 import java.lang.constant.MethodTypeDesc; 90 import java.util.List; 91 import java.util.function.Function; 92 93 public record ClassRemapperImpl(Function<ClassDesc, ClassDesc> mapFunction) implements ClassRemapper { 94 95 @Override 96 public void accept(ClassBuilder clb, ClassElement cle) { 97 switch (cle) { 98 case FieldModel fm -> 99 clb.withField(fm.fieldName().stringValue(), map( 100 fm.fieldTypeSymbol()), fb -> 101 fm.forEachElement(asFieldTransform().resolve(fb).consumer())); 102 case MethodModel mm -> 103 clb.withMethod(mm.methodName().stringValue(), mapMethodDesc( 104 mm.methodTypeSymbol()), mm.flags().flagsMask(), mb -> 105 mm.forEachElement(asMethodTransform().resolve(mb).consumer())); 106 case Superclass sc -> 107 clb.withSuperclass(map(sc.superclassEntry().asSymbol())); 108 case Interfaces ins -> 109 clb.withInterfaceSymbols(Util.mappedList(ins.interfaces(), in -> 110 map(in.asSymbol()))); 111 case SignatureAttribute sa -> 112 clb.with(SignatureAttribute.of(mapClassSignature(sa.asClassSignature()))); 113 case InnerClassesAttribute ica -> 114 clb.with(InnerClassesAttribute.of(ica.classes().stream().map(ici -> 115 InnerClassInfo.of(map(ici.innerClass().asSymbol()), 116 ici.outerClass().map(oc -> map(oc.asSymbol())), 117 ici.innerName().map(Utf8Entry::stringValue), 118 ici.flagsMask())).toList())); 119 case EnclosingMethodAttribute ema -> 120 clb.with(EnclosingMethodAttribute.of(map(ema.enclosingClass().asSymbol()), 121 ema.enclosingMethodName().map(Utf8Entry::stringValue), 122 ema.enclosingMethodTypeSymbol().map(this::mapMethodDesc))); 123 case RecordAttribute ra -> 124 clb.with(RecordAttribute.of(ra.components().stream() 125 .map(this::mapRecordComponent).toList())); 126 case ModuleAttribute ma -> 127 clb.with(ModuleAttribute.of(ma.moduleName(), ma.moduleFlagsMask(), 128 ma.moduleVersion().orElse(null), 129 ma.requires(), ma.exports(), ma.opens(), 130 ma.uses().stream().map(ce -> 131 clb.constantPool().classEntry(map(ce.asSymbol()))).toList(), 132 ma.provides().stream().map(mp -> 133 ModuleProvideInfo.of(map(mp.provides().asSymbol()), 134 mp.providesWith().stream().map(pw -> 135 map(pw.asSymbol())).toList())).toList())); 136 case NestHostAttribute nha -> 137 clb.with(NestHostAttribute.of(map(nha.nestHost().asSymbol()))); 138 case NestMembersAttribute nma -> 139 clb.with(NestMembersAttribute.ofSymbols(nma.nestMembers().stream() 140 .map(nm -> map(nm.asSymbol())).toList())); 141 case PermittedSubclassesAttribute psa -> 142 clb.with(PermittedSubclassesAttribute.ofSymbols( 143 psa.permittedSubclasses().stream().map(ps -> 144 map(ps.asSymbol())).toList())); 145 case RuntimeVisibleAnnotationsAttribute aa -> 146 clb.with(RuntimeVisibleAnnotationsAttribute.of( 147 mapAnnotations(aa.annotations()))); 148 case RuntimeInvisibleAnnotationsAttribute aa -> 149 clb.with(RuntimeInvisibleAnnotationsAttribute.of( 150 mapAnnotations(aa.annotations()))); 151 case RuntimeVisibleTypeAnnotationsAttribute aa -> 152 clb.with(RuntimeVisibleTypeAnnotationsAttribute.of( 153 mapTypeAnnotations(aa.annotations()))); 154 case RuntimeInvisibleTypeAnnotationsAttribute aa -> 155 clb.with(RuntimeInvisibleTypeAnnotationsAttribute.of( 156 mapTypeAnnotations(aa.annotations()))); 157 default -> 158 clb.with(cle); 159 } 160 } 161 162 @Override 163 public FieldTransform asFieldTransform() { 164 return (FieldBuilder fb, FieldElement fe) -> { 165 switch (fe) { 166 case SignatureAttribute sa -> 167 fb.with(SignatureAttribute.of( 168 mapSignature(sa.asTypeSignature()))); 169 case RuntimeVisibleAnnotationsAttribute aa -> 170 fb.with(RuntimeVisibleAnnotationsAttribute.of( 171 mapAnnotations(aa.annotations()))); 172 case RuntimeInvisibleAnnotationsAttribute aa -> 173 fb.with(RuntimeInvisibleAnnotationsAttribute.of( 174 mapAnnotations(aa.annotations()))); 175 case RuntimeVisibleTypeAnnotationsAttribute aa -> 176 fb.with(RuntimeVisibleTypeAnnotationsAttribute.of( 177 mapTypeAnnotations(aa.annotations()))); 178 case RuntimeInvisibleTypeAnnotationsAttribute aa -> 179 fb.with(RuntimeInvisibleTypeAnnotationsAttribute.of( 180 mapTypeAnnotations(aa.annotations()))); 181 default -> 182 fb.with(fe); 183 } 184 }; 185 } 186 187 @Override 188 public MethodTransform asMethodTransform() { 189 return (MethodBuilder mb, MethodElement me) -> { 190 switch (me) { 191 case AnnotationDefaultAttribute ada -> 192 mb.with(AnnotationDefaultAttribute.of( 193 mapAnnotationValue(ada.defaultValue()))); 194 case CodeModel com -> 195 mb.transformCode(com, asCodeTransform()); 196 case ExceptionsAttribute ea -> 197 mb.with(ExceptionsAttribute.ofSymbols( 198 ea.exceptions().stream().map(ce -> 199 map(ce.asSymbol())).toList())); 200 case SignatureAttribute sa -> 201 mb.with(SignatureAttribute.of( 202 mapMethodSignature(sa.asMethodSignature()))); 203 case RuntimeVisibleAnnotationsAttribute aa -> 204 mb.with(RuntimeVisibleAnnotationsAttribute.of( 205 mapAnnotations(aa.annotations()))); 206 case RuntimeInvisibleAnnotationsAttribute aa -> 207 mb.with(RuntimeInvisibleAnnotationsAttribute.of( 208 mapAnnotations(aa.annotations()))); 209 case RuntimeVisibleParameterAnnotationsAttribute paa -> 210 mb.with(RuntimeVisibleParameterAnnotationsAttribute.of( 211 paa.parameterAnnotations().stream() 212 .map(this::mapAnnotations).toList())); 213 case RuntimeInvisibleParameterAnnotationsAttribute paa -> 214 mb.with(RuntimeInvisibleParameterAnnotationsAttribute.of( 215 paa.parameterAnnotations().stream() 216 .map(this::mapAnnotations).toList())); 217 case RuntimeVisibleTypeAnnotationsAttribute aa -> 218 mb.with(RuntimeVisibleTypeAnnotationsAttribute.of( 219 mapTypeAnnotations(aa.annotations()))); 220 case RuntimeInvisibleTypeAnnotationsAttribute aa -> 221 mb.with(RuntimeInvisibleTypeAnnotationsAttribute.of( 222 mapTypeAnnotations(aa.annotations()))); 223 default -> 224 mb.with(me); 225 } 226 }; 227 } 228 229 @Override 230 public CodeTransform asCodeTransform() { 231 return (CodeBuilder cob, CodeElement coe) -> { 232 switch (coe) { 233 case FieldInstruction fai -> 234 cob.fieldInstruction(fai.opcode(), map(fai.owner().asSymbol()), 235 fai.name().stringValue(), map(fai.typeSymbol())); 236 case InvokeInstruction ii -> 237 cob.invokeInstruction(ii.opcode(), map(ii.owner().asSymbol()), 238 ii.name().stringValue(), mapMethodDesc(ii.typeSymbol()), 239 ii.isInterface()); 240 case InvokeDynamicInstruction idi -> 241 cob.invokeDynamicInstruction(DynamicCallSiteDesc.of( 242 idi.bootstrapMethod(), idi.name().stringValue(), 243 mapMethodDesc(idi.typeSymbol()), 244 idi.bootstrapArgs().stream().map(this::mapConstantValue).toArray(ConstantDesc[]::new))); 245 case NewObjectInstruction c -> 246 cob.newObjectInstruction(map(c.className().asSymbol())); 247 case NewReferenceArrayInstruction c -> 248 cob.anewarray(map(c.componentType().asSymbol())); 249 case NewMultiArrayInstruction c -> 250 cob.multianewarray(map(c.arrayType().asSymbol()), c.dimensions()); 251 case TypeCheckInstruction c -> 252 cob.typeCheckInstruction(c.opcode(), map(c.type().asSymbol())); 253 case ExceptionCatch c -> 254 cob.exceptionCatch(c.tryStart(), c.tryEnd(), c.handler(),c.catchType() 255 .map(d -> TemporaryConstantPool.INSTANCE.classEntry(map(d.asSymbol())))); 256 case LocalVariable c -> 257 cob.localVariable(c.slot(), c.name().stringValue(), map(c.typeSymbol()), 258 c.startScope(), c.endScope()); 259 case LocalVariableType c -> 260 cob.localVariableType(c.slot(), c.name().stringValue(), 261 mapSignature(c.signatureSymbol()), c.startScope(), c.endScope()); 262 case LoadConstantInstruction ldc -> 263 cob.constantInstruction(ldc.opcode(), 264 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.RefTypeSig[]::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() 373 .map(ta -> Signature.TypeArg.of( 374 ta.wildcardIndicator(), 375 ta.boundType().map(this::mapSignature))) 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(), map(a.classSymbol()), 407 a.elements().stream().map(el -> AnnotationElement.of(el.name(), 408 mapAnnotationValue(el.value()))).toList())).toList(); 409 } 410 411 List<Signature.TypeParam> mapTypeParams(List<Signature.TypeParam> typeParams) { 412 return typeParams.stream().map(tp -> Signature.TypeParam.of(tp.identifier(), 413 tp.classBound().map(this::mapSignature), 414 tp.interfaceBounds().stream() 415 .map(this::mapSignature).toArray(Signature.RefTypeSig[]::new))).toList(); 416 } 417 418 }