import net.bytebuddy.jar.asm.Opcodes;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.mdsal.binding.dom.codec.impl.ClassGeneratorBridge.BridgeProvider;
+import org.opendaylight.mdsal.binding.dom.codec.impl.ClassGeneratorBridge.LocalNameProvider;
+import org.opendaylight.mdsal.binding.dom.codec.impl.ClassGeneratorBridge.NodeContextSupplierProvider;
import org.opendaylight.mdsal.binding.dom.codec.loader.CodecClassLoader;
import org.opendaylight.mdsal.binding.dom.codec.loader.CodecClassLoader.ClassGenerator;
import org.opendaylight.mdsal.binding.dom.codec.loader.CodecClassLoader.GeneratorResult;
* We take a different approach here, which takes advantage of the fact we are in control of both code generation (here)
* and class loading (in {@link CodecClassLoader}). The process is performed in four steps:
* <ul>
- * <li>During code generation, the context fields are pointed towards {@link CodecDataObjectBridge#resolve(String)} and
- * {@link CodecDataObjectBridge#resolveKey(String)} methods, which are public and static, hence perfectly usable
+ * <li>During code generation, the context fields are pointed towards
+ * {@link ClassGeneratorBridge#resolveNodeContextSupplier(String)} and
+ * {@link ClassGeneratorBridge#resolveKey(String)} methods, which are public and static, hence perfectly usable
* in the context of a class initializer.</li>
* <li>During class loading of generated byte code, the original instance of the generator is called to wrap the actual
* class loading operation. At this point the generator installs itself as the current generator for this thread via
- * {@link CodecDataObjectBridge#setup(CodecDataObjectGenerator)} and allows the class to be loaded.
+ * {@link ClassGeneratorBridge#setup(CodecDataObjectGenerator)} and allows the class to be loaded.
* <li>After the class has been loaded, but before the call returns, we will force the class to initialize, at which
- * point the static invocations will be redirect to {@link #resolve(String)} and {@link #resolveKey(String)}
- * methods, thus initializing the fields to the intended constants.</li>
+ * point the static invocations will be redirect to {@link #resolveNodeContextSupplier(String)} and
+ * {@link #resolveKey(String)} methods, thus initializing the fields to the intended constants.</li>
* <li>Before returning from the class loading call, the generator will detach itself via
- * {@link CodecDataObjectBridge#tearDown(CodecDataObjectGenerator)}.</li>
+ * {@link ClassGeneratorBridge#tearDown(CodecDataObjectGenerator)}.</li>
* </ul>
*
* <p>
* This strategy works due to close cooperation with the target ClassLoader, as the entire code generation and loading
* block runs with the class loading lock for this FQCN and the reference is not leaked until the process completes.
*/
-abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements ClassGenerator<T> {
+abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements ClassGenerator<T>, BridgeProvider {
// Not reusable defintion: we can inline NodeContextSuppliers without a problem
- static final class Fixed<T extends CodecDataObject<?>> extends CodecDataObjectGenerator<T> {
+ static final class Fixed<T extends CodecDataObject<?>> extends CodecDataObjectGenerator<T>
+ implements NodeContextSupplierProvider {
private final ImmutableMap<Method, NodeContextSupplier> properties;
private Fixed(final Builder<?> template, final ImmutableMap<Method, NodeContextSupplier> properties,
}
@Override
- public Class<T> customizeLoading(final @NonNull Supplier<Class<T>> loader) {
- final Fixed<?> prev = CodecDataObjectBridge.setup(this);
- try {
- final Class<T> result = loader.get();
-
- /*
- * This a bit of magic to support NodeContextSupplier constants. These constants need to be resolved
- * while we have the information needed to find them -- that information is being held in this instance
- * and we leak it to a thread-local variable held by CodecDataObjectBridge.
- *
- * By default the JVM will defer class initialization to first use, which unfortunately is too late for
- * us, and hence we need to force class to initialize.
- */
- try {
- Class.forName(result.getName(), true, result.getClassLoader());
- } catch (ClassNotFoundException e) {
- throw new LinkageError("Failed to find newly-defined " + result, e);
- }
-
- return result;
- } finally {
- CodecDataObjectBridge.tearDown(prev);
- }
- }
-
- @NonNull NodeContextSupplier resolve(final @NonNull String methodName) {
+ public NodeContextSupplier resolveNodeContextSupplier(final String methodName) {
final Optional<Entry<Method, NodeContextSupplier>> found = properties.entrySet().stream()
.filter(entry -> methodName.equals(entry.getKey().getName())).findAny();
verify(found.isPresent(), "Failed to find property for %s in %s", methodName, this);
}
// Reusable definition: we have to rely on context lookups
- private static final class Reusable<T extends CodecDataObject<?>> extends CodecDataObjectGenerator<T> {
+ static final class Reusable<T extends CodecDataObject<?>> extends CodecDataObjectGenerator<T>
+ implements LocalNameProvider {
private final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties;
private final Map<Method, Class<?>> daoProperties;
@Override
Builder<T> generateGetters(final Builder<T> builder) {
Builder<T> tmp = builder;
- for (Entry<Method, ValueNodeCodecContext> entry : simpleProperties.entrySet()) {
- final Method method = entry.getKey();
+ for (Method method : simpleProperties.keySet()) {
LOG.trace("Generating for simple method {}", method);
final String methodName = method.getName();
final TypeDescription retType = TypeDescription.ForLoadedType.of(method.getReturnType());
tmp = tmp.defineMethod(methodName, retType, PUB_FINAL).intercept(
- new SimpleGetterMethodImplementation(methodName, retType,
- entry.getValue().getSchema().getQName().getLocalName()));
+ new SimpleGetterMethodImplementation(methodName, retType));
}
for (Entry<Method, Class<?>> entry : daoProperties.entrySet()) {
final Method method = entry.getKey();
ret.addAll(daoProperties.keySet());
return ret;
}
+
+ @Override
+ public String resolveLocalName(final String methodName) {
+ final Optional<Entry<Method, ValueNodeCodecContext>> found = simpleProperties.entrySet().stream()
+ .filter(entry -> methodName.equals(entry.getKey().getName())).findAny();
+ verify(found.isPresent(), "Failed to find property for %s in %s", methodName, this);
+ return found.get().getValue().getSchema().getQName().getLocalName();
+ }
}
private static final Logger LOG = LoggerFactory.getLogger(CodecDataObjectGenerator.class);
.make());
}
+ @Override
+ public Class<T> customizeLoading(final @NonNull Supplier<Class<T>> loader) {
+ final BridgeProvider prev = ClassGeneratorBridge.setup(this);
+ try {
+ final Class<T> result = loader.get();
+
+ /*
+ * This a bit of magic to support NodeContextSupplier constants. These constants need to be resolved
+ * while we have the information needed to find them -- that information is being held in this instance
+ * and we leak it to a thread-local variable held by CodecDataObjectBridge.
+ *
+ * By default the JVM will defer class initialization to first use, which unfortunately is too late for
+ * us, and hence we need to force class to initialize.
+ */
+ try {
+ Class.forName(result.getName(), true, result.getClassLoader());
+ } catch (ClassNotFoundException e) {
+ throw new LinkageError("Failed to find newly-defined " + result, e);
+ }
+
+ return result;
+ } finally {
+ ClassGeneratorBridge.tearDown(prev);
+ }
+ }
+
abstract Builder<T> generateGetters(Builder<T> builder);
abstract ArrayList<Method> getterMethods();
}
}
+ /*
+ * A simple leaf method, which looks up child by a String constant. This is slightly more complicated because we
+ * want to make sure we are using the same String instance as the one stored in associated DataObjectCodecContext,
+ * so that during lookup we perform an identity check instead of comparing content -- speeding things up as well
+ * as minimizing footprint. Since that string is not guaranteed to be interned in the String Pool, we cannot rely
+ * on the constant pool entry to resolve to the same object.
+ */
private static final class SimpleGetterMethodImplementation extends AbstractMethodImplementation {
private static final StackManipulation CODEC_MEMBER = invokeMethod(CodecDataObject.class,
"codecMember", AtomicReferenceFieldUpdater.class, String.class);
+ private static final StackManipulation BRIDGE_RESOLVE = invokeMethod(ClassGeneratorBridge.class,
+ "resolveLocalName", String.class);
+ private static final Generic BB_STRING = TypeDefinition.Sort.describe(String.class);
- private final String localName;
+ // getFoo$$$S
+ private final String stringName;
- SimpleGetterMethodImplementation(final String methodName, final TypeDescription retType,
- final String localName) {
+ SimpleGetterMethodImplementation(final String methodName, final TypeDescription retType) {
super(methodName, retType);
- this.localName = requireNonNull(localName);
+ this.stringName = methodName + "$$$S";
+ }
+
+ @Override
+ public InstrumentedType prepare(final InstrumentedType instrumentedType) {
+ final InstrumentedType tmp = super.prepare(instrumentedType)
+ // private static final String getFoo$$$S;
+ .withField(new FieldDescription.Token(stringName, PRIV_CONST, BB_STRING));
+
+ return tmp.withInitializer(new ByteCodeAppender.Simple(
+ // getFoo$$$S = CodecDataObjectBridge.resolveString("getFoo");
+ new TextConstant(methodName),
+ BRIDGE_RESOLVE,
+ putField(tmp, stringName)));
}
@Override
public ByteCodeAppender appender(final Target implementationTarget) {
final TypeDescription instrumentedType = implementationTarget.getInstrumentedType();
return new ByteCodeAppender.Simple(
- // return (FooType) codecMember(getFoo$$$A, "foo");
+ // return (FooType) codecMember(getFoo$$$A, getFoo$$$S);
THIS,
getField(instrumentedType, arfuName),
- new TextConstant(localName),
+ getField(instrumentedType, stringName),
CODEC_MEMBER,
TypeCasting.to(retType),
MethodReturn.REFERENCE);
private static final class SupplierGetterMethodImplementation extends AbstractMethodImplementation {
private static final StackManipulation CODEC_MEMBER = invokeMethod(CodecDataObject.class,
"codecMember", AtomicReferenceFieldUpdater.class, NodeContextSupplier.class);
- private static final StackManipulation BRIDGE_RESOLVE = invokeMethod(CodecDataObjectBridge.class,
- "resolve", String.class);
+ private static final StackManipulation BRIDGE_RESOLVE = invokeMethod(ClassGeneratorBridge.class,
+ "resolveNodeContextSupplier", String.class);
private static final Generic BB_NCS = TypeDefinition.Sort.describe(NodeContextSupplier.class);
// getFoo$$$C