X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-dom-codec%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fdom%2Fcodec%2Fimpl%2FCodecDataObjectGenerator.java;h=f224471e5ec499780e6787339f94cc81396b4fa6;hb=134f46b88b407b67695bdf1d52999515e723b489;hp=688c4ec90e57f327466eef65d894574fd0f5c7a9;hpb=383464c2832e94e1e2ce7772deae2b1e13d0b613;p=mdsal.git
diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CodecDataObjectGenerator.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CodecDataObjectGenerator.java
index 688c4ec90e..f224471e5e 100644
--- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CodecDataObjectGenerator.java
+++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CodecDataObjectGenerator.java
@@ -10,10 +10,9 @@ package org.opendaylight.mdsal.binding.dom.codec.impl;
import static com.google.common.base.Verify.verify;
import static com.google.common.base.Verify.verifyNotNull;
import static java.util.Objects.requireNonNull;
-import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.computeFrames;
+import static net.bytebuddy.implementation.bytecode.member.MethodVariableAccess.loadThis;
import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.getField;
import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.invokeMethod;
-import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.loadThis;
import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.putField;
import com.google.common.collect.ImmutableMap;
@@ -42,13 +41,13 @@ import net.bytebuddy.implementation.bytecode.member.MethodReturn;
import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
import net.bytebuddy.jar.asm.Opcodes;
import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.mdsal.binding.dom.codec.impl.ClassGeneratorBridge.CodecContextSupplierProvider;
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;
-import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
+import org.opendaylight.mdsal.binding.loader.BindingClassLoader;
+import org.opendaylight.mdsal.binding.loader.BindingClassLoader.ClassGenerator;
+import org.opendaylight.mdsal.binding.loader.BindingClassLoader.GeneratorResult;
import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.contract.Naming;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -60,7 +59,7 @@ import org.slf4j.LoggerFactory;
* strong connection between a generated class to the extent possible. In most cases (grouping-generated types) this
* involves one level of indirection, which is a safe approach. If we are dealing with a type generated outside of a
* grouping statement, though, we are guaranteed instantiation-invariance and hence can hard-wire to a runtime-constant
- * {@link NodeContextSupplier} -- which provides significant boost to JITs ability to optimize code -- especially with
+ * {@link CodecContextSupplier} -- which provides significant boost to JITs ability to optimize code -- especially with
* inlining and constant propagation.
*
*
@@ -131,17 +130,17 @@ import org.slf4j.LoggerFactory;
*
*
* 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:
+ * and class loading (in {@link BindingClassLoader}). The process is performed in four steps:
*
* - During code generation, the context fields are pointed towards
- * {@link ClassGeneratorBridge#resolveNodeContextSupplier(String)} and
+ * {@link ClassGeneratorBridge#resolveCodecContextSupplier(String)} and
* {@link ClassGeneratorBridge#resolveKey(String)} methods, which are public and static, hence perfectly usable
* in the context of a class initializer.
* - 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 ClassGeneratorBridge#setup(CodecDataObjectGenerator)} and allows the class to be loaded.
*
- 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 redirected to {@link #resolveNodeContextSupplier(String)} and
+ * point the static invocations will be redirected to {@link #resolveCodecContextSupplier(String)} and
* {@link #resolveKey(String)} methods, thus initializing the fields to the intended constants.
* - Before returning from the class loading call, the generator will detach itself via
* {@link ClassGeneratorBridge#tearDown(CodecDataObjectGenerator)}.
@@ -156,10 +155,10 @@ abstract class CodecDataObjectGenerator> implements
// FIXME: MDSAL-443: wire this implementation, which requires that BindingRuntimeTypes provides information about
// types being generated from within a grouping
private static final class Fixed> extends CodecDataObjectGenerator
- implements NodeContextSupplierProvider {
- private final ImmutableMap properties;
+ implements CodecContextSupplierProvider {
+ private final ImmutableMap properties;
- Fixed(final TypeDescription superClass, final ImmutableMap properties,
+ Fixed(final TypeDescription superClass, final ImmutableMap properties,
final @Nullable Method keyMethod) {
super(superClass, keyMethod);
this.properties = requireNonNull(properties);
@@ -171,7 +170,7 @@ abstract class CodecDataObjectGenerator> implements
for (Method method : properties.keySet()) {
LOG.trace("Generating for fixed method {}", method);
final String methodName = method.getName();
- final TypeDescription retType = TypeDescription.ForLoadedType.of(method.getReturnType());
+ final TypeDescription retType = ForLoadedType.of(method.getReturnType());
tmp = tmp.defineMethod(methodName, retType, PUB_FINAL).intercept(
new SupplierGetterMethodImplementation(methodName, retType));
}
@@ -179,11 +178,11 @@ abstract class CodecDataObjectGenerator> implements
}
@Override
- public NodeContextSupplier resolveNodeContextSupplier(final String methodName) {
- final Optional> found = properties.entrySet().stream()
+ public CodecContextSupplier resolveCodecContextSupplier(final String methodName) {
+ final Optional> 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);
- return verifyNotNull(found.get().getValue());
+ return verifyNotNull(found.orElseThrow().getValue());
}
}
@@ -191,10 +190,10 @@ abstract class CodecDataObjectGenerator> implements
private static final class Reusable> extends CodecDataObjectGenerator
implements LocalNameProvider {
private final ImmutableMap simpleProperties;
- private final Map> daoProperties;
+ private final Map, PropertyInfo> daoProperties;
Reusable(final TypeDescription superClass, final ImmutableMap simpleProperties,
- final Map> daoProperties, final @Nullable Method keyMethod) {
+ final Map, PropertyInfo> daoProperties, final @Nullable Method keyMethod) {
super(superClass, keyMethod);
this.simpleProperties = requireNonNull(simpleProperties);
this.daoProperties = requireNonNull(daoProperties);
@@ -206,17 +205,24 @@ abstract class CodecDataObjectGenerator> implements
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());
+ final TypeDescription retType = ForLoadedType.of(method.getReturnType());
tmp = tmp.defineMethod(methodName, retType, PUB_FINAL).intercept(
new SimpleGetterMethodImplementation(methodName, retType));
}
- for (Entry> entry : daoProperties.entrySet()) {
- final Method method = entry.getKey();
+ for (Entry, PropertyInfo> entry : daoProperties.entrySet()) {
+ final PropertyInfo info = entry.getValue();
+ final Method method = info.getterMethod();
LOG.trace("Generating for structured method {}", method);
final String methodName = method.getName();
- final TypeDescription retType = TypeDescription.ForLoadedType.of(method.getReturnType());
+ final TypeDescription retType = ForLoadedType.of(method.getReturnType());
tmp = tmp.defineMethod(methodName, retType, PUB_FINAL).intercept(
- new StructuredGetterMethodImplementation(methodName, retType, entry.getValue()));
+ new StructuredGetterMethodImplementation(methodName, retType, entry.getKey()));
+
+ if (info instanceof PropertyInfo.GetterAndNonnull orEmpty) {
+ final String nonnullName = orEmpty.nonnullMethod().getName();
+ tmp = tmp.defineMethod(nonnullName, retType, PUB_FINAL).intercept(
+ new NonnullMethodImplementation(nonnullName, retType, entry.getKey(), method));
+ }
}
return tmp;
@@ -227,7 +233,7 @@ abstract class CodecDataObjectGenerator> implements
final Optional> 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();
+ return found.orElseThrow().getValue().getSchema().getQName().getLocalName();
}
}
@@ -254,37 +260,37 @@ abstract class CodecDataObjectGenerator> implements
this.keyMethod = keyMethod;
}
- static > Class generate(final CodecClassLoader loader,
+ static > Class generate(final BindingClassLoader loader,
final Class bindingInterface, final ImmutableMap simpleProperties,
- final Map> daoProperties, final Method keyMethod) {
- return loader.generateClass(bindingInterface, "codecImpl",
+ final Map, PropertyInfo> daoProperties, final Method keyMethod) {
+ return CodecPackage.CODEC.generateClass(loader, bindingInterface,
new Reusable<>(BB_CDO, simpleProperties, daoProperties, keyMethod));
}
static > Class generateAugmentable(
- final CodecClassLoader loader, final Class bindingInterface,
+ final BindingClassLoader loader, final Class bindingInterface,
final ImmutableMap simpleProperties,
- final Map> daoProperties, final Method keyMethod) {
- return loader.generateClass(bindingInterface, "codecImpl",
+ final Map, PropertyInfo> daoProperties, final Method keyMethod) {
+ return CodecPackage.CODEC.generateClass(loader, bindingInterface,
new Reusable<>(BB_ACDO, simpleProperties, daoProperties, keyMethod));
}
@Override
- public final GeneratorResult generateClass(final CodecClassLoader loeader, final String fqcn,
+ public final GeneratorResult generateClass(final BindingClassLoader loader, final String fqcn,
final Class> bindingInterface) {
LOG.trace("Generating class {}", fqcn);
final Generic bindingDef = TypeDefinition.Sort.describe(bindingInterface);
@SuppressWarnings("unchecked")
Builder builder = (Builder) BB.subclass(Generic.Builder.parameterizedType(superClass, bindingDef).build())
- .visit(computeFrames()).name(fqcn).implement(bindingDef);
+ .name(fqcn).implement(bindingDef);
builder = generateGetters(builder);
if (keyMethod != null) {
LOG.trace("Generating for key {}", keyMethod);
final String methodName = keyMethod.getName();
- final TypeDescription retType = TypeDescription.ForLoadedType.of(keyMethod.getReturnType());
+ final TypeDescription retType = ForLoadedType.of(keyMethod.getReturnType());
builder = builder.defineMethod(methodName, retType, PUB_FINAL).intercept(
new KeyMethodImplementation(methodName, retType));
}
@@ -310,7 +316,7 @@ abstract class CodecDataObjectGenerator> implements
return new Implementation.Simple(
// return Foo.bindingHashCode(this);
loadThis(),
- invokeMethod(bindingInterface, BindingMapping.BINDING_HASHCODE_NAME, bindingInterface),
+ invokeMethod(bindingInterface, Naming.BINDING_HASHCODE_NAME, bindingInterface),
MethodReturn.INTEGER);
}
@@ -319,7 +325,7 @@ abstract class CodecDataObjectGenerator> implements
// return Foo.bindingEquals(this, obj);
loadThis(),
FIRST_ARG_REF,
- invokeMethod(bindingInterface, BindingMapping.BINDING_EQUALS_NAME, bindingInterface, Object.class),
+ invokeMethod(bindingInterface, Naming.BINDING_EQUALS_NAME, bindingInterface, Object.class),
MethodReturn.INTEGER);
}
@@ -327,14 +333,25 @@ abstract class CodecDataObjectGenerator> implements
return new Implementation.Simple(
// return Foo.bindingToString(this);
loadThis(),
- invokeMethod(bindingInterface, BindingMapping.BINDING_TO_STRING_NAME, bindingInterface),
+ invokeMethod(bindingInterface, Naming.BINDING_TO_STRING_NAME, bindingInterface),
MethodReturn.REFERENCE);
}
private abstract static class AbstractMethodImplementation implements Implementation {
+ final TypeDescription retType;
+ // getFoo, usually
+ final String methodName;
+
+ AbstractMethodImplementation(final String methodName, final TypeDescription retType) {
+ this.methodName = requireNonNull(methodName);
+ this.retType = requireNonNull(retType);
+ }
+ }
+
+ private abstract static class AbstractCachedMethodImplementation extends AbstractMethodImplementation {
private static final Generic BB_HANDLE = TypeDefinition.Sort.describe(VarHandle.class);
private static final Generic BB_OBJECT = TypeDefinition.Sort.describe(Object.class);
- private static final StackManipulation OBJECT_CLASS = ClassConstant.of(TypeDescription.OBJECT);
+ private static final StackManipulation OBJECT_CLASS = ClassConstant.of(ForLoadedType.of(Object.class));
private static final StackManipulation LOOKUP = invokeMethod(MethodHandles.class, "lookup");
private static final StackManipulation FIND_VAR_HANDLE = invokeMethod(Lookup.class,
"findVarHandle", Class.class, String.class, Class.class);
@@ -343,16 +360,12 @@ abstract class CodecDataObjectGenerator> implements
| Opcodes.ACC_SYNTHETIC;
private static final int PRIV_VOLATILE = Opcodes.ACC_PRIVATE | Opcodes.ACC_VOLATILE | Opcodes.ACC_SYNTHETIC;
- final TypeDescription retType;
- // getFoo
- final String methodName;
// getFoo$$$V
final String handleName;
- AbstractMethodImplementation(final String methodName, final TypeDescription retType) {
- this.methodName = requireNonNull(methodName);
- this.retType = requireNonNull(retType);
- this.handleName = methodName + "$$$V";
+ AbstractCachedMethodImplementation(final String methodName, final TypeDescription retType) {
+ super(methodName, retType);
+ handleName = methodName + "$$$V";
}
@Override
@@ -375,7 +388,7 @@ abstract class CodecDataObjectGenerator> implements
}
}
- private static final class KeyMethodImplementation extends AbstractMethodImplementation {
+ private static final class KeyMethodImplementation extends AbstractCachedMethodImplementation {
private static final StackManipulation CODEC_KEY = invokeMethod(CodecDataObject.class,
"codecKey", VarHandle.class);
@@ -395,6 +408,40 @@ abstract class CodecDataObjectGenerator> implements
}
}
+ private static final class NonnullMethodImplementation extends AbstractMethodImplementation {
+ private static final StackManipulation NONNULL_MEMBER = invokeMethod(CodecDataObject.class,
+ "codecMemberOrEmpty", Object.class, Class.class);
+
+ private final Class> bindingClass;
+ private final Method getterMethod;
+
+ NonnullMethodImplementation(final String methodName, final TypeDescription retType,
+ final Class> bindingClass, final Method getterMethod) {
+ super(methodName, retType);
+ this.bindingClass = requireNonNull(bindingClass);
+ this.getterMethod = requireNonNull(getterMethod);
+ }
+
+ @Override
+ public ByteCodeAppender appender(final Target implementationTarget) {
+ return new ByteCodeAppender.Simple(
+ // return (FooType) codecMemberOrEmpty(getFoo(), FooType.class)
+ loadThis(),
+ loadThis(),
+ invokeMethod(getterMethod),
+ ClassConstant.of(TypeDefinition.Sort.describe(bindingClass).asErasure()),
+ NONNULL_MEMBER,
+ TypeCasting.to(retType),
+ MethodReturn.REFERENCE);
+ }
+
+ @Override
+ public InstrumentedType prepare(final InstrumentedType instrumentedType) {
+ // No-op
+ return instrumentedType;
+ }
+ }
+
/*
* 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,
@@ -402,7 +449,7 @@ abstract class CodecDataObjectGenerator> implements
* 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 class SimpleGetterMethodImplementation extends AbstractCachedMethodImplementation {
private static final StackManipulation CODEC_MEMBER = invokeMethod(CodecDataObject.class,
"codecMember", VarHandle.class, String.class);
private static final StackManipulation BRIDGE_RESOLVE = invokeMethod(ClassGeneratorBridge.class,
@@ -414,7 +461,7 @@ abstract class CodecDataObjectGenerator> implements
SimpleGetterMethodImplementation(final String methodName, final TypeDescription retType) {
super(methodName, retType);
- this.stringName = methodName + "$$$S";
+ stringName = methodName + "$$$S";
}
@Override
@@ -444,7 +491,7 @@ abstract class CodecDataObjectGenerator> implements
}
}
- private static final class StructuredGetterMethodImplementation extends AbstractMethodImplementation {
+ private static final class StructuredGetterMethodImplementation extends AbstractCachedMethodImplementation {
private static final StackManipulation CODEC_MEMBER = invokeMethod(CodecDataObject.class,
"codecMember", VarHandle.class, Class.class);
@@ -469,12 +516,12 @@ abstract class CodecDataObjectGenerator> implements
}
}
- private static final class SupplierGetterMethodImplementation extends AbstractMethodImplementation {
+ private static final class SupplierGetterMethodImplementation extends AbstractCachedMethodImplementation {
private static final StackManipulation CODEC_MEMBER = invokeMethod(CodecDataObject.class,
- "codecMember", VarHandle.class, NodeContextSupplier.class);
+ "codecMember", VarHandle.class, CodecContextSupplier.class);
private static final StackManipulation BRIDGE_RESOLVE = invokeMethod(ClassGeneratorBridge.class,
"resolveNodeContextSupplier", String.class);
- private static final Generic BB_NCS = TypeDefinition.Sort.describe(NodeContextSupplier.class);
+ private static final Generic BB_NCS = TypeDefinition.Sort.describe(CodecContextSupplier.class);
// getFoo$$$C
private final String contextName;