Convert mdsal-binding-dom-codec to a JPMS module
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / CodecDataObjectGenerator.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
9
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;
17
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;
23 import java.util.Map;
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.impl.loader.CodecClassLoader;
47 import org.opendaylight.mdsal.binding.dom.codec.impl.loader.CodecClassLoader.ClassGenerator;
48 import org.opendaylight.mdsal.binding.dom.codec.impl.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;
53
54 /**
55  * Private support for generating {@link CodecDataObject} and {@link AugmentableCodecDataObject} specializations.
56  *
57  * <p>
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.
64  *
65  * <p>
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.
69  *
70  * <p>
71  * The design is such that for a particular structure like:
72  * <pre>
73  *     container foo {
74  *         leaf bar {
75  *             type string;
76  *         }
77  *     }
78  * </pre>
79  * we end up generating a class with the following layout:
80  * <pre>
81  *     public final class Foo$$$codecImpl extends CodecDataObject implements Foo {
82  *         private static final VarHandle getBar$$$V;
83  *         private volatile Object getBar;
84  *
85  *         public Foo$$$codecImpl(DistinctNodeContainer data) {
86  *             super(data);
87  *         }
88  *
89  *         public Bar getBar() {
90  *             return (Bar) codecMember(getBar$$$V, "bar");
91  *         }
92  *     }
93  * </pre>
94  *
95  * <p>
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:
99  * <ul>
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>
102  * </ul>
103  *
104  * <p>
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.
109  *
110  * <p>
111  * Furthermore there are distinct {@code codecMember} methods, each of which supports a different invocation style:
112  * <ul>
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>
116  * </ul>
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.
119  *
120  * <p>
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.
125  *
126  * <p>
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.
130  *
131  * <p>
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:
134  * <ul>
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>
147  * </ul>
148  *
149  * <p>
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.
152  */
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;
160
161         Fixed(final TypeDescription superClass, final ImmutableMap<Method, NodeContextSupplier> properties,
162                 final @Nullable Method keyMethod) {
163             super(superClass, keyMethod);
164             this.properties = requireNonNull(properties);
165         }
166
167         @Override
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));
176             }
177             return tmp;
178         }
179
180         @Override
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());
186         }
187     }
188
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;
194
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);
200         }
201
202         @Override
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));
211             }
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()));
219             }
220
221             return tmp;
222         }
223
224         @Override
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();
230         }
231     }
232
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);
240
241     private static final StackManipulation FIRST_ARG_REF = MethodVariableAccess.REFERENCE.loadFrom(1);
242
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;
245
246     private static final ByteBuddy BB = new ByteBuddy();
247
248     private final TypeDescription superClass;
249     private final Method keyMethod;
250
251     CodecDataObjectGenerator(final TypeDescription superClass, final @Nullable Method keyMethod) {
252         this.superClass = requireNonNull(superClass);
253         this.keyMethod = keyMethod;
254     }
255
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));
261     }
262
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));
269     }
270
271     @Override
272     public final GeneratorResult<T> generateClass(final CodecClassLoader loeader, final String fqcn,
273             final Class<?> bindingInterface) {
274         LOG.trace("Generating class {}", fqcn);
275
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);
280
281         builder = generateGetters(builder);
282
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));
289         }
290
291         // Final bits:
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))
302                 // ... and build it
303                 .make());
304     }
305
306     abstract Builder<T> generateGetters(Builder<T> builder);
307
308     private static Implementation codecHashCode(final Class<?> bindingInterface) {
309         return new Implementation.Simple(
310             // return Foo.bindingHashCode(this);
311             loadThis(),
312             invokeMethod(bindingInterface, BindingMapping.BINDING_HASHCODE_NAME, bindingInterface),
313             MethodReturn.INTEGER);
314     }
315
316     private static Implementation codecEquals(final Class<?> bindingInterface) {
317         return new Implementation.Simple(
318             // return Foo.bindingEquals(this, obj);
319             loadThis(),
320             FIRST_ARG_REF,
321             invokeMethod(bindingInterface, BindingMapping.BINDING_EQUALS_NAME, bindingInterface, Object.class),
322             MethodReturn.INTEGER);
323     }
324
325     private static Implementation toString(final Class<?> bindingInterface) {
326         return new Implementation.Simple(
327             // return Foo.bindingToString(this);
328             loadThis(),
329             invokeMethod(bindingInterface, BindingMapping.BINDING_TO_STRING_NAME, bindingInterface),
330             MethodReturn.REFERENCE);
331     }
332
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);
340
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;
344
345         final TypeDescription retType;
346         // getFoo
347         final String methodName;
348         // getFoo$$$V
349         final String handleName;
350
351         AbstractMethodImplementation(final String methodName, final TypeDescription retType) {
352             this.methodName = requireNonNull(methodName);
353             this.retType = requireNonNull(retType);
354             this.handleName = methodName + "$$$V";
355         }
356
357         @Override
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));
364
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);
368                 LOOKUP,
369                 ClassConstant.of(tmp),
370                 new TextConstant(methodName),
371                 OBJECT_CLASS,
372                 FIND_VAR_HANDLE,
373                 putField(tmp, handleName)));
374         }
375     }
376
377     private static final class KeyMethodImplementation extends AbstractMethodImplementation {
378         private static final StackManipulation CODEC_KEY = invokeMethod(CodecDataObject.class,
379             "codecKey", VarHandle.class);
380
381         KeyMethodImplementation(final String methodName, final TypeDescription retType) {
382             super(methodName, retType);
383         }
384
385         @Override
386         public ByteCodeAppender appender(final Target implementationTarget) {
387             return new ByteCodeAppender.Simple(
388                 // return (FooType) codecKey(getFoo$$$V);
389                 loadThis(),
390                 getField(implementationTarget.getInstrumentedType(), handleName),
391                 CODEC_KEY,
392                 TypeCasting.to(retType),
393                 MethodReturn.REFERENCE);
394         }
395     }
396
397     /*
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.
403      */
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);
410
411         // getFoo$$$S
412         private final String stringName;
413
414         SimpleGetterMethodImplementation(final String methodName, final TypeDescription retType) {
415             super(methodName, retType);
416             this.stringName = methodName + "$$$S";
417         }
418
419         @Override
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));
424
425             return tmp.withInitializer(new ByteCodeAppender.Simple(
426                 // getFoo$$$S = CodecDataObjectBridge.resolveString("getFoo");
427                 new TextConstant(methodName),
428                 BRIDGE_RESOLVE,
429                 putField(tmp, stringName)));
430         }
431
432         @Override
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);
437                 loadThis(),
438                 getField(instrumentedType, handleName),
439                 getField(instrumentedType, stringName),
440                 CODEC_MEMBER,
441                 TypeCasting.to(retType),
442                 MethodReturn.REFERENCE);
443         }
444     }
445
446     private static final class StructuredGetterMethodImplementation extends AbstractMethodImplementation {
447         private static final StackManipulation CODEC_MEMBER = invokeMethod(CodecDataObject.class,
448             "codecMember", VarHandle.class, Class.class);
449
450         private final Class<?> bindingClass;
451
452         StructuredGetterMethodImplementation(final String methodName, final TypeDescription retType,
453                 final Class<?> bindingClass) {
454             super(methodName, retType);
455             this.bindingClass = requireNonNull(bindingClass);
456         }
457
458         @Override
459         public ByteCodeAppender appender(final Target implementationTarget) {
460             return new ByteCodeAppender.Simple(
461                 // return (FooType) codecMember(getFoo$$$V, FooType.class);
462                 loadThis(),
463                 getField(implementationTarget.getInstrumentedType(), handleName),
464                 ClassConstant.of(TypeDefinition.Sort.describe(bindingClass).asErasure()),
465                 CODEC_MEMBER,
466                 TypeCasting.to(retType),
467                 MethodReturn.REFERENCE);
468         }
469     }
470
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);
477
478         // getFoo$$$C
479         private final String contextName;
480
481         SupplierGetterMethodImplementation(final String methodName, final TypeDescription retType) {
482             super(methodName, retType);
483             contextName = methodName + "$$$C";
484         }
485
486         @Override
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));
491
492             return tmp.withInitializer(new ByteCodeAppender.Simple(
493                 // getFoo$$$C = CodecDataObjectBridge.resolve("getFoo");
494                 new TextConstant(methodName),
495                 BRIDGE_RESOLVE,
496                 putField(tmp, contextName)));
497         }
498
499         @Override
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);
504                 loadThis(),
505                 getField(instrumentedType, handleName),
506                 getField(instrumentedType, contextName),
507                 CODEC_MEMBER,
508                 TypeCasting.to(retType),
509                 MethodReturn.REFERENCE);
510         }
511     }
512 }