2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13 import static net.bytebuddy.implementation.bytecode.member.MethodVariableAccess.loadThis;
14 import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.getField;
15 import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.invokeMethod;
16 import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.putField;
18 import com.google.common.collect.ImmutableMap;
19 import java.lang.invoke.MethodHandles;
20 import java.lang.invoke.MethodHandles.Lookup;
21 import java.lang.invoke.VarHandle;
22 import java.lang.reflect.Method;
24 import java.util.Map.Entry;
25 import java.util.Optional;
26 import net.bytebuddy.ByteBuddy;
27 import net.bytebuddy.description.field.FieldDescription;
28 import net.bytebuddy.description.type.TypeDefinition;
29 import net.bytebuddy.description.type.TypeDescription;
30 import net.bytebuddy.description.type.TypeDescription.ForLoadedType;
31 import net.bytebuddy.description.type.TypeDescription.Generic;
32 import net.bytebuddy.dynamic.DynamicType.Builder;
33 import net.bytebuddy.dynamic.scaffold.InstrumentedType;
34 import net.bytebuddy.implementation.Implementation;
35 import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
36 import net.bytebuddy.implementation.bytecode.StackManipulation;
37 import net.bytebuddy.implementation.bytecode.assign.TypeCasting;
38 import net.bytebuddy.implementation.bytecode.constant.ClassConstant;
39 import net.bytebuddy.implementation.bytecode.constant.TextConstant;
40 import net.bytebuddy.implementation.bytecode.member.MethodReturn;
41 import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
42 import net.bytebuddy.jar.asm.Opcodes;
43 import org.eclipse.jdt.annotation.Nullable;
44 import org.opendaylight.mdsal.binding.dom.codec.impl.ClassGeneratorBridge.LocalNameProvider;
45 import org.opendaylight.mdsal.binding.dom.codec.impl.ClassGeneratorBridge.NodeContextSupplierProvider;
46 import org.opendaylight.mdsal.binding.dom.codec.loader.CodecClassLoader;
47 import org.opendaylight.mdsal.binding.dom.codec.loader.CodecClassLoader.ClassGenerator;
48 import org.opendaylight.mdsal.binding.dom.codec.loader.CodecClassLoader.GeneratorResult;
49 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * Private support for generating {@link CodecDataObject} and {@link AugmentableCodecDataObject} specializations.
58 * Code generation here is probably more involved than usual mainly due to the fact we *really* want to express the
59 * strong connection between a generated class to the extent possible. In most cases (grouping-generated types) this
60 * involves one level of indirection, which is a safe approach. If we are dealing with a type generated outside of a
61 * grouping statement, though, we are guaranteed instantiation-invariance and hence can hard-wire to a runtime-constant
62 * {@link NodeContextSupplier} -- which provides significant boost to JITs ability to optimize code -- especially with
63 * inlining and constant propagation.
66 * The accessor mapping performance is critical due to users typically not taking care of storing the results acquired
67 * by an invocation, assuming the accessors are backed by a normal field -- which of course is not true, as the results
68 * are lazily computed.
71 * The design is such that for a particular structure like:
79 * we end up generating a class with the following layout:
81 * public final class Foo$$$codecImpl extends CodecDataObject implements Foo {
82 * private static final VarHandle getBar$$$V;
83 * private volatile Object getBar;
85 * public Foo$$$codecImpl(DistinctNodeContainer data) {
89 * public Bar getBar() {
90 * return (Bar) codecMember(getBar$$$V, "bar");
96 * This strategy minimizes the bytecode footprint and follows the generally good idea of keeping common logic in a
97 * single place in a maintainable form. The glue code is extremely light (~6 instructions), which is beneficial on both
98 * sides of invocation:
100 * <li>generated method can readily be inlined into the caller</li>
101 * <li>it forms a call site into which codeMember() can be inlined with VarHandle being constant</li>
105 * The second point is important here, as it allows the invocation logic around VarHandle to completely disappear,
106 * becoming synonymous with operations on a field. Even though the field itself is declared as volatile, it is only ever
107 * accessed through helper method using VarHandles -- and those helpers are using relaxed field ordering
108 * of {@code getAcquire()}/{@code setRelease()} memory semantics.
111 * Furthermore there are distinct {@code codecMember} methods, each of which supports a different invocation style:
113 * <li>with {@code String}, which ends up looking up a {@link ValueNodeCodecContext}</li>
114 * <li>with {@code Class}, which ends up looking up a {@link DataContainerCodecContext}</li>
115 * <li>with {@code NodeContextSupplier}, which performs a direct load</li>
117 * The third mode of operation requires that the object being implemented is not defined in a {@code grouping}, because
118 * it welds the object to a particular namespace -- hence it trades namespace mobility for access speed.
121 * The sticky point here is the NodeContextSupplier, as it is a heap object which cannot normally be looked up from the
122 * static context in which the static class initializer operates -- so we need perform some sort of a trick here.
123 * Even though ByteBuddy provides facilities for bridging references to type fields, those facilities operate on
124 * volatile fields -- hence they do not quite work for us.
127 * Another alternative, which we used in Javassist-generated DataObjectSerializers, is to muck with the static field
128 * using reflection -- which works, but requires redefinition of Field.modifiers, which is something Java 9+ complains
129 * about quite noisily.
132 * We take a different approach here, which takes advantage of the fact we are in control of both code generation (here)
133 * and class loading (in {@link CodecClassLoader}). The process is performed in four steps:
135 * <li>During code generation, the context fields are pointed towards
136 * {@link ClassGeneratorBridge#resolveNodeContextSupplier(String)} and
137 * {@link ClassGeneratorBridge#resolveKey(String)} methods, which are public and static, hence perfectly usable
138 * in the context of a class initializer.</li>
139 * <li>During class loading of generated byte code, the original instance of the generator is called to wrap the actual
140 * class loading operation. At this point the generator installs itself as the current generator for this thread via
141 * {@link ClassGeneratorBridge#setup(CodecDataObjectGenerator)} and allows the class to be loaded.
142 * <li>After the class has been loaded, but before the call returns, we will force the class to initialize, at which
143 * point the static invocations will be redirected to {@link #resolveNodeContextSupplier(String)} and
144 * {@link #resolveKey(String)} methods, thus initializing the fields to the intended constants.</li>
145 * <li>Before returning from the class loading call, the generator will detach itself via
146 * {@link ClassGeneratorBridge#tearDown(CodecDataObjectGenerator)}.</li>
150 * This strategy works due to close cooperation with the target ClassLoader, as the entire code generation and loading
151 * block runs with the class loading lock for this FQCN and the reference is not leaked until the process completes.
153 abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements ClassGenerator<T> {
154 // Not reusable definition: we can inline NodeContextSuppliers without a problem
155 // FIXME: MDSAL-443: wire this implementation, which requires that BindingRuntimeTypes provides information about
156 // types being generated from within a grouping
157 private static final class Fixed<T extends CodecDataObject<?>> extends CodecDataObjectGenerator<T>
158 implements NodeContextSupplierProvider<T> {
159 private final ImmutableMap<Method, NodeContextSupplier> properties;
161 Fixed(final TypeDescription superClass, final ImmutableMap<Method, NodeContextSupplier> properties,
162 final @Nullable Method keyMethod) {
163 super(superClass, keyMethod);
164 this.properties = requireNonNull(properties);
168 Builder<T> generateGetters(final Builder<T> builder) {
169 Builder<T> tmp = builder;
170 for (Method method : properties.keySet()) {
171 LOG.trace("Generating for fixed method {}", method);
172 final String methodName = method.getName();
173 final TypeDescription retType = TypeDescription.ForLoadedType.of(method.getReturnType());
174 tmp = tmp.defineMethod(methodName, retType, PUB_FINAL).intercept(
175 new SupplierGetterMethodImplementation(methodName, retType));
181 public NodeContextSupplier resolveNodeContextSupplier(final String methodName) {
182 final Optional<Entry<Method, NodeContextSupplier>> found = properties.entrySet().stream()
183 .filter(entry -> methodName.equals(entry.getKey().getName())).findAny();
184 verify(found.isPresent(), "Failed to find property for %s in %s", methodName, this);
185 return verifyNotNull(found.get().getValue());
189 // Reusable definition: we have to rely on context lookups
190 private static final class Reusable<T extends CodecDataObject<?>> extends CodecDataObjectGenerator<T>
191 implements LocalNameProvider<T> {
192 private final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties;
193 private final Map<Method, Class<?>> daoProperties;
195 Reusable(final TypeDescription superClass, final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
196 final Map<Method, Class<?>> daoProperties, final @Nullable Method keyMethod) {
197 super(superClass, keyMethod);
198 this.simpleProperties = requireNonNull(simpleProperties);
199 this.daoProperties = requireNonNull(daoProperties);
203 Builder<T> generateGetters(final Builder<T> builder) {
204 Builder<T> tmp = builder;
205 for (Method method : simpleProperties.keySet()) {
206 LOG.trace("Generating for simple method {}", method);
207 final String methodName = method.getName();
208 final TypeDescription retType = TypeDescription.ForLoadedType.of(method.getReturnType());
209 tmp = tmp.defineMethod(methodName, retType, PUB_FINAL).intercept(
210 new SimpleGetterMethodImplementation(methodName, retType));
212 for (Entry<Method, Class<?>> entry : daoProperties.entrySet()) {
213 final Method method = entry.getKey();
214 LOG.trace("Generating for structured method {}", method);
215 final String methodName = method.getName();
216 final TypeDescription retType = TypeDescription.ForLoadedType.of(method.getReturnType());
217 tmp = tmp.defineMethod(methodName, retType, PUB_FINAL).intercept(
218 new StructuredGetterMethodImplementation(methodName, retType, entry.getValue()));
225 public String resolveLocalName(final String methodName) {
226 final Optional<Entry<Method, ValueNodeCodecContext>> found = simpleProperties.entrySet().stream()
227 .filter(entry -> methodName.equals(entry.getKey().getName())).findAny();
228 verify(found.isPresent(), "Failed to find property for %s in %s", methodName, this);
229 return found.get().getValue().getSchema().getQName().getLocalName();
233 private static final Logger LOG = LoggerFactory.getLogger(CodecDataObjectGenerator.class);
234 private static final Generic BB_BOOLEAN = TypeDefinition.Sort.describe(boolean.class);
235 private static final Generic BB_OBJECT = TypeDefinition.Sort.describe(Object.class);
236 private static final Generic BB_INT = TypeDefinition.Sort.describe(int.class);
237 private static final Generic BB_STRING = TypeDefinition.Sort.describe(String.class);
238 private static final TypeDescription BB_CDO = ForLoadedType.of(CodecDataObject.class);
239 private static final TypeDescription BB_ACDO = ForLoadedType.of(AugmentableCodecDataObject.class);
241 private static final StackManipulation FIRST_ARG_REF = MethodVariableAccess.REFERENCE.loadFrom(1);
243 private static final int PROT_FINAL = Opcodes.ACC_PROTECTED | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC;
244 private static final int PUB_FINAL = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC;
246 private static final ByteBuddy BB = new ByteBuddy();
248 private final TypeDescription superClass;
249 private final Method keyMethod;
251 CodecDataObjectGenerator(final TypeDescription superClass, final @Nullable Method keyMethod) {
252 this.superClass = requireNonNull(superClass);
253 this.keyMethod = keyMethod;
256 static <D extends DataObject, T extends CodecDataObject<T>> Class<T> generate(final CodecClassLoader loader,
257 final Class<D> bindingInterface, final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
258 final Map<Method, Class<?>> daoProperties, final Method keyMethod) {
259 return loader.generateClass(bindingInterface, "codecImpl",
260 new Reusable<>(BB_CDO, simpleProperties, daoProperties, keyMethod));
263 static <D extends DataObject, T extends CodecDataObject<T>> Class<T> generateAugmentable(
264 final CodecClassLoader loader, final Class<D> bindingInterface,
265 final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
266 final Map<Method, Class<?>> daoProperties, final Method keyMethod) {
267 return loader.generateClass(bindingInterface, "codecImpl",
268 new Reusable<>(BB_ACDO, simpleProperties, daoProperties, keyMethod));
272 public final GeneratorResult<T> generateClass(final CodecClassLoader loeader, final String fqcn,
273 final Class<?> bindingInterface) {
274 LOG.trace("Generating class {}", fqcn);
276 final Generic bindingDef = TypeDefinition.Sort.describe(bindingInterface);
277 @SuppressWarnings("unchecked")
278 Builder<T> builder = (Builder<T>) BB.subclass(Generic.Builder.parameterizedType(superClass, bindingDef).build())
279 .name(fqcn).implement(bindingDef);
281 builder = generateGetters(builder);
283 if (keyMethod != null) {
284 LOG.trace("Generating for key {}", keyMethod);
285 final String methodName = keyMethod.getName();
286 final TypeDescription retType = TypeDescription.ForLoadedType.of(keyMethod.getReturnType());
287 builder = builder.defineMethod(methodName, retType, PUB_FINAL).intercept(
288 new KeyMethodImplementation(methodName, retType));
292 return GeneratorResult.of(builder
293 // codecHashCode() ...
294 .defineMethod("codecHashCode", BB_INT, PROT_FINAL)
295 .intercept(codecHashCode(bindingInterface))
296 // ... equals(Object) ...
297 .defineMethod("codecEquals", BB_BOOLEAN, PROT_FINAL).withParameter(BB_OBJECT)
298 .intercept(codecEquals(bindingInterface))
299 // ... toString() ...
300 .defineMethod("toString", BB_STRING, PUB_FINAL)
301 .intercept(toString(bindingInterface))
306 abstract Builder<T> generateGetters(Builder<T> builder);
308 private static Implementation codecHashCode(final Class<?> bindingInterface) {
309 return new Implementation.Simple(
310 // return Foo.bindingHashCode(this);
312 invokeMethod(bindingInterface, BindingMapping.BINDING_HASHCODE_NAME, bindingInterface),
313 MethodReturn.INTEGER);
316 private static Implementation codecEquals(final Class<?> bindingInterface) {
317 return new Implementation.Simple(
318 // return Foo.bindingEquals(this, obj);
321 invokeMethod(bindingInterface, BindingMapping.BINDING_EQUALS_NAME, bindingInterface, Object.class),
322 MethodReturn.INTEGER);
325 private static Implementation toString(final Class<?> bindingInterface) {
326 return new Implementation.Simple(
327 // return Foo.bindingToString(this);
329 invokeMethod(bindingInterface, BindingMapping.BINDING_TO_STRING_NAME, bindingInterface),
330 MethodReturn.REFERENCE);
333 private abstract static class AbstractMethodImplementation implements Implementation {
334 private static final Generic BB_HANDLE = TypeDefinition.Sort.describe(VarHandle.class);
335 private static final Generic BB_OBJECT = TypeDefinition.Sort.describe(Object.class);
336 private static final StackManipulation OBJECT_CLASS = ClassConstant.of(TypeDescription.OBJECT);
337 private static final StackManipulation LOOKUP = invokeMethod(MethodHandles.class, "lookup");
338 private static final StackManipulation FIND_VAR_HANDLE = invokeMethod(Lookup.class,
339 "findVarHandle", Class.class, String.class, Class.class);
341 static final int PRIV_CONST = Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL
342 | Opcodes.ACC_SYNTHETIC;
343 private static final int PRIV_VOLATILE = Opcodes.ACC_PRIVATE | Opcodes.ACC_VOLATILE | Opcodes.ACC_SYNTHETIC;
345 final TypeDescription retType;
347 final String methodName;
349 final String handleName;
351 AbstractMethodImplementation(final String methodName, final TypeDescription retType) {
352 this.methodName = requireNonNull(methodName);
353 this.retType = requireNonNull(retType);
354 this.handleName = methodName + "$$$V";
358 public InstrumentedType prepare(final InstrumentedType instrumentedType) {
359 final InstrumentedType tmp = instrumentedType
360 // private static final VarHandle getFoo$$$V;
361 .withField(new FieldDescription.Token(handleName, PRIV_CONST, BB_HANDLE))
362 // private volatile Object getFoo;
363 .withField(new FieldDescription.Token(methodName, PRIV_VOLATILE, BB_OBJECT));
365 return tmp.withInitializer(new ByteCodeAppender.Simple(
366 // TODO: acquiring lookup is expensive, we should share it across all initialization
367 // getFoo$$$V = MethodHandles.lookup().findVarHandle(This.class, "getFoo", Object.class);
369 ClassConstant.of(tmp),
370 new TextConstant(methodName),
373 putField(tmp, handleName)));
377 private static final class KeyMethodImplementation extends AbstractMethodImplementation {
378 private static final StackManipulation CODEC_KEY = invokeMethod(CodecDataObject.class,
379 "codecKey", VarHandle.class);
381 KeyMethodImplementation(final String methodName, final TypeDescription retType) {
382 super(methodName, retType);
386 public ByteCodeAppender appender(final Target implementationTarget) {
387 return new ByteCodeAppender.Simple(
388 // return (FooType) codecKey(getFoo$$$V);
390 getField(implementationTarget.getInstrumentedType(), handleName),
392 TypeCasting.to(retType),
393 MethodReturn.REFERENCE);
398 * A simple leaf method, which looks up child by a String constant. This is slightly more complicated because we
399 * want to make sure we are using the same String instance as the one stored in associated DataObjectCodecContext,
400 * so that during lookup we perform an identity check instead of comparing content -- speeding things up as well
401 * as minimizing footprint. Since that string is not guaranteed to be interned in the String Pool, we cannot rely
402 * on the constant pool entry to resolve to the same object.
404 private static final class SimpleGetterMethodImplementation extends AbstractMethodImplementation {
405 private static final StackManipulation CODEC_MEMBER = invokeMethod(CodecDataObject.class,
406 "codecMember", VarHandle.class, String.class);
407 private static final StackManipulation BRIDGE_RESOLVE = invokeMethod(ClassGeneratorBridge.class,
408 "resolveLocalName", String.class);
409 private static final Generic BB_STRING = TypeDefinition.Sort.describe(String.class);
412 private final String stringName;
414 SimpleGetterMethodImplementation(final String methodName, final TypeDescription retType) {
415 super(methodName, retType);
416 this.stringName = methodName + "$$$S";
420 public InstrumentedType prepare(final InstrumentedType instrumentedType) {
421 final InstrumentedType tmp = super.prepare(instrumentedType)
422 // private static final String getFoo$$$S;
423 .withField(new FieldDescription.Token(stringName, PRIV_CONST, BB_STRING));
425 return tmp.withInitializer(new ByteCodeAppender.Simple(
426 // getFoo$$$S = CodecDataObjectBridge.resolveString("getFoo");
427 new TextConstant(methodName),
429 putField(tmp, stringName)));
433 public ByteCodeAppender appender(final Target implementationTarget) {
434 final TypeDescription instrumentedType = implementationTarget.getInstrumentedType();
435 return new ByteCodeAppender.Simple(
436 // return (FooType) codecMember(getFoo$$$V, getFoo$$$S);
438 getField(instrumentedType, handleName),
439 getField(instrumentedType, stringName),
441 TypeCasting.to(retType),
442 MethodReturn.REFERENCE);
446 private static final class StructuredGetterMethodImplementation extends AbstractMethodImplementation {
447 private static final StackManipulation CODEC_MEMBER = invokeMethod(CodecDataObject.class,
448 "codecMember", VarHandle.class, Class.class);
450 private final Class<?> bindingClass;
452 StructuredGetterMethodImplementation(final String methodName, final TypeDescription retType,
453 final Class<?> bindingClass) {
454 super(methodName, retType);
455 this.bindingClass = requireNonNull(bindingClass);
459 public ByteCodeAppender appender(final Target implementationTarget) {
460 return new ByteCodeAppender.Simple(
461 // return (FooType) codecMember(getFoo$$$V, FooType.class);
463 getField(implementationTarget.getInstrumentedType(), handleName),
464 ClassConstant.of(TypeDefinition.Sort.describe(bindingClass).asErasure()),
466 TypeCasting.to(retType),
467 MethodReturn.REFERENCE);
471 private static final class SupplierGetterMethodImplementation extends AbstractMethodImplementation {
472 private static final StackManipulation CODEC_MEMBER = invokeMethod(CodecDataObject.class,
473 "codecMember", VarHandle.class, NodeContextSupplier.class);
474 private static final StackManipulation BRIDGE_RESOLVE = invokeMethod(ClassGeneratorBridge.class,
475 "resolveNodeContextSupplier", String.class);
476 private static final Generic BB_NCS = TypeDefinition.Sort.describe(NodeContextSupplier.class);
479 private final String contextName;
481 SupplierGetterMethodImplementation(final String methodName, final TypeDescription retType) {
482 super(methodName, retType);
483 contextName = methodName + "$$$C";
487 public InstrumentedType prepare(final InstrumentedType instrumentedType) {
488 final InstrumentedType tmp = super.prepare(instrumentedType)
489 // private static final NodeContextSupplier getFoo$$$C;
490 .withField(new FieldDescription.Token(contextName, PRIV_CONST, BB_NCS));
492 return tmp.withInitializer(new ByteCodeAppender.Simple(
493 // getFoo$$$C = CodecDataObjectBridge.resolve("getFoo");
494 new TextConstant(methodName),
496 putField(tmp, contextName)));
500 public ByteCodeAppender appender(final Target implementationTarget) {
501 final TypeDescription instrumentedType = implementationTarget.getInstrumentedType();
502 return new ByteCodeAppender.Simple(
503 // return (FooType) codecMember(getFoo$$$V, getFoo$$$C);
505 getField(instrumentedType, handleName),
506 getField(instrumentedType, contextName),
508 TypeCasting.to(retType),
509 MethodReturn.REFERENCE);