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 com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.mdsal.binding.javav2.dom.codec.generator.spi.generator.AbstractGenerator;
13 import org.opendaylight.mdsal.binding.javav2.dom.codec.generator.spi.generator.AbstractStreamWriterGenerator;
14 import org.opendaylight.mdsal.binding.javav2.generator.api.ClassLoadingStrategy;
15 import org.opendaylight.mdsal.binding.javav2.generator.impl.GeneratedClassLoadingStrategy;
16 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
17 import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
18 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
19 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingStreamEventWriter;
20 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializerRegistry;
23 public abstract class AbstractTreeNodeSerializerSource extends AbstractSource {
25 private static final ClassLoadingStrategy STRATEGY = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
27 static final String SERIALIZER = "_serializer";
28 static final String STREAM = "_stream";
29 static final String ITERATOR = "_iterator";
30 static final String CURRENT = "_current";
31 static final String REGISTRY = "_registry";
33 private final AbstractGenerator generator;
41 AbstractTreeNodeSerializerSource(final AbstractGenerator generator) {
42 this.generator = Preconditions.checkNotNull(generator);
45 @SuppressWarnings("unchecked")
46 Class<? extends Instantiable<?>> loadClass(final Type childType) {
48 return (Class<? extends Instantiable<?>>) STRATEGY.loadClass(childType);
49 } catch (final ClassNotFoundException e) {
50 throw new IllegalStateException("Could not load referenced class ", e);
55 * Returns body of static serialize method.
58 * <li>{@link TreeNodeSerializerRegistry} - registry of serializers
59 * <li>{@link TreeNode} - node to be serialized
60 * <li>{@link BindingStreamEventWriter} - writer to which events should be
64 * @return valid javassist code describing static serialization body
66 public abstract CharSequence getSerializerBody();
69 * Invoking leafNode method of stream with arguments local name and value.
72 * - argument for invoking leafNode
74 * - argument for invoking leafNode
75 * @return invoking leafNode method as String
77 static final CharSequence leafNode(final String localName, final CharSequence value) {
78 return invoke(STREAM, "leafNode", escape(localName), value);
82 * Invoking startLeafSet method of stream with arguments local name and
86 * - argument for invoking startLeafSet
88 * - argument for invoking startLeafSet
89 * @return invoking startLeafSet method as String
91 static final CharSequence startLeafSet(final String localName, final CharSequence expected) {
92 return invoke(STREAM, "startLeafSet", escape(localName), expected);
96 * Invoking startOrderedLeafSet method of stream with arguments localname
100 * - argument for invoking startOrderedLeafSet
102 * - argument for invoking startOrderedLeafSet
103 * @return invoking startOrderedLeafSet method as String
105 static final CharSequence startOrderedLeafSet(final String localName, final CharSequence expected) {
106 return invoke(STREAM, "startOrderedLeafSet", escape(localName), expected);
110 * Bound local name by quotes.
114 * @return bounded local name
116 static final CharSequence escape(final String localName) {
117 return '"' + localName + '"';
121 * Invoking leafSetEntryNode method of stream with argument value.
124 * - argument for invoking leafSetEntryNode
125 * @return invoking leafSetEntryNode method as String
127 static final CharSequence leafSetEntryNode(final CharSequence value) {
128 return invoke(STREAM, "leafSetEntryNode", value);
132 * Invoking startContainerNode method of stream with arguments type and
136 * - argument for invoking startContainerNode
138 * - argument for invoking startContainerNode
139 * @return invoking startContainerNode method as String
141 public static final CharSequence startContainerNode(final CharSequence type, final CharSequence expected) {
142 return invoke(STREAM, "startContainerNode", type, expected);
146 * Invoking startUnkeyedList method of stream with arguments type and
150 * - argument for invoking startUnkeyedList
152 * - argument for invoking startUnkeyedList
153 * @return invoking startUnkeyedList method as String
155 static final CharSequence startUnkeyedList(final CharSequence type, final CharSequence expected) {
156 return invoke(STREAM, "startUnkeyedList", type, expected);
160 * Invoking startUnkeyedListItem of stream with argument expected.
163 * - argument for invoking startUnkeyedListItem
164 * @return invoking startUnkeyedListItem method as String
166 protected static final CharSequence startUnkeyedListItem(final CharSequence expected) {
167 return invoke(STREAM, "startUnkeyedListItem", expected);
171 * Invoking startMapNode method of stream with arguments type and expected.
174 * - argument for invoking startMapNode
176 * - argument for invoking startMapNode
177 * @return invoking startMapNode method as String
179 static final CharSequence startMapNode(final CharSequence type, final CharSequence expected) {
180 return invoke(STREAM, "startMapNode", type, expected);
184 * Invoking startOrderedMapNode method of stream with arguments type and
188 * - argument for invoking startOrderedMapNode
190 * - argument for invoking startOrderedMapNode
191 * @return invoking startOrderedMapNode method as String
193 static final CharSequence startOrderedMapNode(final CharSequence type, final CharSequence expected) {
194 return invoke(STREAM, "startOrderedMapNode", type, expected);
198 * Invoking startMapEntryNode method of stream with arguments key and
202 * - argument for invoking startMapEntryNode
204 * - argument for invoking startMapEntryNode
205 * @return invoking startMapEntryNode method as String
207 protected static final CharSequence startMapEntryNode(final CharSequence key, final CharSequence expected) {
208 return invoke(STREAM, "startMapEntryNode", key, expected);
212 * Invoking startAugmentationNode of stream with argument key.
215 * - argument for invoking startAugmentationNode
216 * @return invoking startAugmentationNode method as String
218 protected static final CharSequence startAugmentationNode(final CharSequence key) {
219 return invoke(STREAM, "startAugmentationNode", key);
223 * Invoking startChoiceNode method of stream with arguments localname and
227 * - argument for invoking startChoiceNode
229 * - argument for invoking startChoiceNode
230 * @return invoking startChoiceNode method as String
232 static final CharSequence startChoiceNode(final CharSequence localName, final CharSequence expected) {
233 return invoke(STREAM, "startChoiceNode", localName, expected);
237 * Invoking startCaseNode method of stream with arguments localname and
241 * - argument for invoking startCaseNode
243 * - argument for invoking startCaseNode
244 * @return invoking startCaseNode method as String
246 protected static final CharSequence startCaseNode(final CharSequence localName, final CharSequence expected) {
247 return invoke(STREAM, "startCase", localName, expected);
251 * Invoking anyxmlNode method of stream with arguments name and
255 * - argument for invoking anyxmlNode
257 * - argument for invoking anyxmlNode
258 * @return invoking anyxmlNode method as String
260 static final CharSequence anyxmlNode(final String name, final String value)
261 throws IllegalArgumentException {
262 return invoke(STREAM, "anyxmlNode", escape(name), value);
266 * Invoking anydataNode method of stream with arguments name and
270 * - argument for invoking anydataNode
272 * - argument for invoking anydataNode
273 * @return invoking anydataNode method as String
275 static final CharSequence anydataNode(final String name, final String value)
276 throws IllegalArgumentException {
277 return invoke(STREAM, "anydataNode", escape(name), name);
281 * Invoking endNode method of stream without any arguments.
283 * @return invoking andNode method as String
285 static final CharSequence endNode() {
286 return invoke(STREAM, "endNode");
290 * Prepare loop through iterable object with specific body.
293 * - name of iterable object
295 * - type of iterate objects
297 * - specific body of loop
298 * @return loop through iterable object as String
300 static final CharSequence forEach(final String iterable, final Type valueType, final CharSequence body) {
301 return forEach(iterable, ITERATOR, valueType.getFullyQualifiedName(), CURRENT, body);
305 * Returns class reference for type.
308 * - type for referencing class
309 * @return referenced class of type
311 protected static final CharSequence classReference(final Type type) {
312 return type.getFullyQualifiedName() + ".class";
316 * After getting class of childType from class loader, prepare invoking of
317 * serialize() method from instance of reached class of childType with
318 * arguments {@link #REGISTRY}, name and {@link #STREAM}.
321 * - type of child for getting class from classloader
323 * - argument for invoking serialize method of instance childType
325 * @return invoking serialize method with specific arguments as String
327 final CharSequence staticInvokeEmitter(final Type childType, final String name) {
330 cls = STRATEGY.loadClass(childType);
331 } catch (final ClassNotFoundException e) {
332 throw new IllegalStateException("Failed to invoke emitter", e);
335 final String className = this.generator.loadSerializerFor(cls) + ".getInstance()";
336 return invoke(className, AbstractStreamWriterGenerator.SERIALIZE_METHOD_NAME, REGISTRY, name, STREAM);