2 * Copyright (c) 2017 Pantheon Technologies 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.javav2.dom.codec.generator.spi.source;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import org.opendaylight.mdsal.binding.javav2.dom.codec.generator.spi.generator.AbstractGenerator;
14 import org.opendaylight.mdsal.binding.javav2.dom.codec.generator.spi.generator.AbstractStreamWriterGenerator;
15 import org.opendaylight.mdsal.binding.javav2.generator.api.ClassLoadingStrategy;
16 import org.opendaylight.mdsal.binding.javav2.generator.impl.GeneratedClassLoadingStrategy;
17 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
18 import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
19 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
20 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingStreamEventWriter;
21 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializerRegistry;
24 public abstract class AbstractTreeNodeSerializerSource extends AbstractSource {
26 private static final ClassLoadingStrategy STRATEGY = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
28 static final String SERIALIZER = "_serializer";
29 static final String STREAM = "_stream";
30 static final String ITERATOR = "_iterator";
31 static final String CURRENT = "_current";
32 static final String REGISTRY = "_registry";
34 private final AbstractGenerator generator;
42 AbstractTreeNodeSerializerSource(final AbstractGenerator generator) {
43 this.generator = requireNonNull(generator);
46 @SuppressWarnings("unchecked")
47 Class<? extends Instantiable<?>> loadClass(final Type childType) {
49 return (Class<? extends Instantiable<?>>) STRATEGY.loadClass(childType);
50 } catch (final ClassNotFoundException e) {
51 throw new IllegalStateException("Could not load referenced class ", e);
56 * Returns body of static serialize method.
59 * <li>{@link TreeNodeSerializerRegistry} - registry of serializers
60 * <li>{@link TreeNode} - node to be serialized
61 * <li>{@link BindingStreamEventWriter} - writer to which events should be
65 * @return valid javassist code describing static serialization body
67 public abstract CharSequence getSerializerBody();
70 * Invoking leafNode method of stream with arguments local name and value.
73 * - argument for invoking leafNode
75 * - argument for invoking leafNode
76 * @return invoking leafNode method as String
78 static final CharSequence leafNode(final String localName, final CharSequence value) {
79 return invoke(STREAM, "leafNode", escape(localName), value);
83 * Invoking startLeafSet method of stream with arguments local name and
87 * - argument for invoking startLeafSet
89 * - argument for invoking startLeafSet
90 * @return invoking startLeafSet method as String
92 static final CharSequence startLeafSet(final String localName, final CharSequence expected) {
93 return invoke(STREAM, "startLeafSet", escape(localName), expected);
97 * Invoking startOrderedLeafSet method of stream with arguments localname
101 * - argument for invoking startOrderedLeafSet
103 * - argument for invoking startOrderedLeafSet
104 * @return invoking startOrderedLeafSet method as String
106 static final CharSequence startOrderedLeafSet(final String localName, final CharSequence expected) {
107 return invoke(STREAM, "startOrderedLeafSet", escape(localName), expected);
111 * Bound local name by quotes.
115 * @return bounded local name
117 static final CharSequence escape(final String localName) {
118 return '"' + localName + '"';
122 * Invoking leafSetEntryNode method of stream with argument value.
125 * - argument for invoking leafSetEntryNode
126 * @return invoking leafSetEntryNode method as String
128 static final CharSequence leafSetEntryNode(final CharSequence value) {
129 return invoke(STREAM, "leafSetEntryNode", value);
133 * Invoking startContainerNode method of stream with arguments type and
137 * - argument for invoking startContainerNode
139 * - argument for invoking startContainerNode
140 * @return invoking startContainerNode method as String
142 public static final CharSequence startContainerNode(final CharSequence type, final CharSequence expected) {
143 return invoke(STREAM, "startContainerNode", type, expected);
147 * Invoking startUnkeyedList method of stream with arguments type and
151 * - argument for invoking startUnkeyedList
153 * - argument for invoking startUnkeyedList
154 * @return invoking startUnkeyedList method as String
156 static final CharSequence startUnkeyedList(final CharSequence type, final CharSequence expected) {
157 return invoke(STREAM, "startUnkeyedList", type, expected);
161 * Invoking startUnkeyedListItem of stream with argument expected.
164 * - argument for invoking startUnkeyedListItem
165 * @return invoking startUnkeyedListItem method as String
167 protected static final CharSequence startUnkeyedListItem(final CharSequence expected) {
168 return invoke(STREAM, "startUnkeyedListItem", expected);
172 * Invoking startMapNode method of stream with arguments type and expected.
175 * - argument for invoking startMapNode
177 * - argument for invoking startMapNode
178 * @return invoking startMapNode method as String
180 static final CharSequence startMapNode(final CharSequence type, final CharSequence expected) {
181 return invoke(STREAM, "startMapNode", type, expected);
185 * Invoking startOrderedMapNode method of stream with arguments type and
189 * - argument for invoking startOrderedMapNode
191 * - argument for invoking startOrderedMapNode
192 * @return invoking startOrderedMapNode method as String
194 static final CharSequence startOrderedMapNode(final CharSequence type, final CharSequence expected) {
195 return invoke(STREAM, "startOrderedMapNode", type, expected);
199 * Invoking startMapEntryNode method of stream with arguments key and
203 * - argument for invoking startMapEntryNode
205 * - argument for invoking startMapEntryNode
206 * @return invoking startMapEntryNode method as String
208 protected static final CharSequence startMapEntryNode(final CharSequence key, final CharSequence expected) {
209 return invoke(STREAM, "startMapEntryNode", key, expected);
213 * Invoking startAugmentationNode of stream with argument key.
216 * - argument for invoking startAugmentationNode
217 * @return invoking startAugmentationNode method as String
219 protected static final CharSequence startAugmentationNode(final CharSequence key) {
220 return invoke(STREAM, "startAugmentationNode", key);
224 * Invoking startChoiceNode method of stream with arguments localname and
228 * - argument for invoking startChoiceNode
230 * - argument for invoking startChoiceNode
231 * @return invoking startChoiceNode method as String
233 static final CharSequence startChoiceNode(final CharSequence localName, final CharSequence expected) {
234 return invoke(STREAM, "startChoiceNode", localName, expected);
238 * Invoking startCaseNode method of stream with arguments localname and
242 * - argument for invoking startCaseNode
244 * - argument for invoking startCaseNode
245 * @return invoking startCaseNode method as String
247 protected static final CharSequence startCaseNode(final CharSequence localName, final CharSequence expected) {
248 return invoke(STREAM, "startCase", localName, expected);
252 * Invoking anyxmlNode method of stream with arguments name and
256 * - argument for invoking anyxmlNode
258 * - argument for invoking anyxmlNode
259 * @return invoking anyxmlNode method as String
261 static final CharSequence anyxmlNode(final String name, final String value)
262 throws IllegalArgumentException {
263 return invoke(STREAM, "anyxmlNode", escape(name), value);
267 * Invoking anydataNode method of stream with arguments name and
271 * - argument for invoking anydataNode
273 * - argument for invoking anydataNode
274 * @return invoking anydataNode method as String
276 static final CharSequence anydataNode(final String name, final String value)
277 throws IllegalArgumentException {
278 return invoke(STREAM, "anydataNode", escape(name), name);
282 * Invoking endNode method of stream without any arguments.
284 * @return invoking andNode method as String
286 static final CharSequence endNode() {
287 return invoke(STREAM, "endNode");
291 * Prepare loop through iterable object with specific body.
294 * - name of iterable object
296 * - type of iterate objects
298 * - specific body of loop
299 * @return loop through iterable object as String
301 static final CharSequence forEach(final String iterable, final Type valueType, final CharSequence body) {
302 return forEach(iterable, ITERATOR, valueType.getFullyQualifiedName(), CURRENT, body);
306 * Returns class reference for type.
309 * - type for referencing class
310 * @return referenced class of type
312 protected static final CharSequence classReference(final Type type) {
313 return type.getFullyQualifiedName() + ".class";
317 * After getting class of childType from class loader, prepare invoking of
318 * serialize() method from instance of reached class of childType with
319 * arguments {@link #REGISTRY}, name and {@link #STREAM}.
322 * - type of child for getting class from classloader
324 * - argument for invoking serialize method of instance childType
326 * @return invoking serialize method with specific arguments as String
328 final CharSequence staticInvokeEmitter(final Type childType, final String name) {
331 cls = STRATEGY.loadClass(childType);
332 } catch (final ClassNotFoundException e) {
333 throw new IllegalStateException("Failed to invoke emitter", e);
336 final String className = this.generator.loadSerializerFor(cls) + ".getInstance()";
337 return invoke(className, AbstractStreamWriterGenerator.SERIALIZE_METHOD_NAME, REGISTRY, name, STREAM);