import com.google.common.base.Preconditions;
import java.util.HashMap;
import java.util.Map;
+import org.opendaylight.mdsal.binding.dom.codec.util.BindingSchemaMapping;
import org.opendaylight.mdsal.binding.dom.codec.util.ChoiceDispatchSerializer;
import org.opendaylight.mdsal.binding.model.api.GeneratedType;
import org.opendaylight.mdsal.binding.model.api.MethodSignature;
import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
import org.opendaylight.mdsal.binding.model.api.Type;
-import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
import org.opendaylight.yangtools.yang.binding.BindingSerializer;
import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
-import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
return hashMap;
}
- private static String getGetterName(final DataSchemaNode node) {
- final TypeDefinition<?> type;
- if (node instanceof TypedDataSchemaNode) {
- type = ((TypedDataSchemaNode) node).getType();
- } else {
- type = null;
- }
-
- final String prefix;
- // Bug 8903: If it is a derived type of boolean or empty, not an inner type, then the return type
- // of method would be the generated type of typedef not build-in types, so here it should be 'get'.
- if ((type instanceof BooleanTypeDefinition || type instanceof EmptyTypeDefinition)
- && (type.getPath().equals(node.getPath()) || type.getBaseType() == null)) {
- prefix = "is";
- } else {
- prefix = "get";
- }
- return prefix + BindingMapping.getGetterSuffix(node.getQName());
- }
-
private void emitBody(final StringBuilder sb) {
final Map<String, Type> getterToType = collectAllProperties(dtoType, new HashMap<String, Type>());
for (final DataSchemaNode schemaChild : schemaNode.getChildNodes()) {
if (!schemaChild.isAugmenting()) {
- final String getter = getGetterName(schemaChild);
+ final String getter = BindingSchemaMapping.getGetterMethodName(schemaChild);
final Type childType = getterToType.get(getter);
if (childType == null) {
// FIXME AnyXml nodes are ignored, since their type cannot be found in generated bindnig
import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
import org.opendaylight.mdsal.binding.dom.codec.impl.NodeCodecContext.CodecContextFactory;
+import org.opendaylight.mdsal.binding.dom.codec.util.BindingSchemaMapping;
import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
import org.opendaylight.mdsal.binding.model.api.GeneratedType;
-import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
import org.opendaylight.yangtools.concepts.Codec;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
final Map<String, DataSchemaNode> getterToLeafSchema = new HashMap<>();
for (final DataSchemaNode leaf : childSchema.getChildNodes()) {
if (leaf instanceof TypedDataSchemaNode) {
- getterToLeafSchema.put(getGetterName(leaf, ((TypedDataSchemaNode) leaf).getType()), leaf);
+ getterToLeafSchema.put(BindingSchemaMapping.getGetterMethodName((TypedDataSchemaNode) leaf), leaf);
}
}
return getLeafNodesUsingReflection(parentClass, getterToLeafSchema);
}
- private static String getGetterName(final SchemaNode node, final TypeDefinition<?> typeDef) {
- final String suffix = BindingMapping.getGetterSuffix(node.getQName());
- // Bug 8903: If it is a derived type of boolean or empty, not an inner type, then the return type
- // of method would be the generated type of typedef not build-in types, so here it should be 'get'.
- if ((typeDef instanceof BooleanTypeDefinition || typeDef instanceof EmptyTypeDefinition)
- && (typeDef.getPath().equals(node.getPath()) || typeDef.getBaseType() == null)) {
- return "is" + suffix;
- }
- return "get" + suffix;
- }
-
private ImmutableMap<String, LeafNodeCodecContext<?>> getLeafNodesUsingReflection(final Class<?> parentClass,
final Map<String, DataSchemaNode> getterToLeafSchema) {
final Map<String, LeafNodeCodecContext<?>> leaves = new HashMap<>();
final Set<String> ctorArgs = new TreeSet<>();
for (Bit bit : rootType.getBits()) {
- final Method valueGetter = returnType.getMethod("is" + BindingMapping.getClassName(bit.getName()));
+ final Method valueGetter = returnType.getMethod(BindingMapping.BOOLEAN_GETTER_PREFIX
+ + BindingMapping.getClassName(bit.getName()));
ctorArgs.add(bit.getName());
getters.put(bit.getName(), valueGetter);
}
if (subtype instanceof LeafrefTypeDefinition) {
addLeafrefValueCodec(unionCls, unionType, bindingCodecContext, values, subtype);
} else {
- final Method valueGetter =
- unionCls.getMethod("get" + BindingMapping.getClassName(subtype.getQName()));
+ final Method valueGetter = unionCls.getMethod(BindingMapping.GETTER_PREFIX
+ + BindingMapping.getClassName(subtype.getQName()));
final Class<?> valueType = valueGetter.getReturnType();
final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
// get method via reflection from generated code according to
// get_TypeName_Value method
- final Method valueGetterParent = unionCls
- .getMethod(new StringBuilder("get").append(typeName).append(className).append("Value").toString());
+ final Method valueGetterParent = unionCls.getMethod(new StringBuilder().append(BindingMapping.GETTER_PREFIX)
+ .append(typeName).append(className).append("Value").toString());
final Class<?> returnType = valueGetterParent.getReturnType();
// prepare codec of union subtype according to return type of referenced
--- /dev/null
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.dom.codec.util;
+
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
+
+public final class BindingSchemaMapping {
+ private BindingSchemaMapping() {
+
+ }
+
+ public static String getGetterMethodName(final DataSchemaNode node) {
+ return node instanceof TypedDataSchemaNode ? getGetterMethodName((TypedDataSchemaNode) node)
+ : BindingMapping.getGetterMethodName(node.getQName(), false);
+ }
+
+ public static String getGetterMethodName(final TypedDataSchemaNode node) {
+ // Bug 8903: If it is a derived type of boolean or empty, not an inner type, then the return type
+ // of method would be the generated type of typedef not build-in types, so here it should be 'get'.
+ final TypeDefinition<?> type = node.getType();
+ return BindingMapping.getGetterMethodName(node.getQName(),
+ (type instanceof BooleanTypeDefinition || type instanceof EmptyTypeDefinition)
+ && (type.getPath().equals(node.getPath()) || type.getBaseType() == null));
+ }
+}
* @return string with the name of the getter method for <code>methodName</code> in JAVA method format
*/
public static String getterMethodName(final String localName, final Type returnType) {
- final StringBuilder method = new StringBuilder();
- if (BOOLEAN.equals(returnType)) {
- method.append("is");
- } else {
- method.append("get");
- }
- final String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName));
- method.append(name);
- return method.toString();
+ return BindingMapping.getGetterMethodName(localName, BOOLEAN.equals(returnType));
}
/**
final protected def fieldName(GeneratedProperty property) '''_«property.name»'''
final protected def propertyNameFromGetter(MethodSignature getter) {
- var int prefix;
- if (getter.name.startsWith("is")) {
- prefix = 2
- } else if (getter.name.startsWith("get")) {
- prefix = 3
+ var String prefix;
+ if (getter.name.startsWith(BindingMapping.BOOLEAN_GETTER_PREFIX)) {
+ prefix = BindingMapping.BOOLEAN_GETTER_PREFIX
+ } else if (getter.name.startsWith(BindingMapping.GETTER_PREFIX)) {
+ prefix = BindingMapping.GETTER_PREFIX
} else {
throw new IllegalArgumentException("Not a getter")
}
- return getter.name.substring(prefix).toFirstLower;
- }
-
- final protected def isAccessor(MethodSignature maybeGetter) {
- return maybeGetter.name.startsWith("is") || maybeGetter.name.startsWith("get");
+ return getter.name.substring(prefix.length).toFirstLower;
}
/**
checkArgument(method.getReturnType() != null);
checkArgument(method.getName() != null);
checkArgument(!method.getName().isEmpty());
- final String prefix = Types.BOOLEAN.equals(method.getReturnType()) ? "is" : "get";
+ final String prefix = BindingMapping.getGetterPrefix(Types.BOOLEAN.equals(method.getReturnType()));
if (!method.getName().startsWith(prefix)) {
return null;
}
*/
package org.opendaylight.mdsal.binding.java.api.generator
+import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isGetterMethodName
+
import java.util.List
import org.opendaylight.mdsal.binding.model.api.AnnotationType
import org.opendaylight.mdsal.binding.model.api.Constant
def private generateMethods() '''
«IF !methods.empty»
«FOR m : methods SEPARATOR "\n"»
- «val accessor = m.isAccessor»
+ «val accessor = m.name.isGetterMethodName»
«val ret = m.returnType»
«IF accessor»
«formatDataForJavaDoc(m, "@return " + asCode(ret.fullyQualifiedName) + " "
*/
public static final String IDENTIFIABLE_KEY_NAME = "key";
+ /**
+ * Prefix for getter methods working on top of boolean.
+ */
+ public static final String BOOLEAN_GETTER_PREFIX = "is";
+
+ /**
+ * Prefix for normal getter methods.
+ */
+ public static final String GETTER_PREFIX = "get";
+
public static final String RPC_INPUT_SUFFIX = "Input";
public static final String RPC_OUTPUT_SUFFIX = "Output";
return getMethodName(name.getLocalName());
}
+ public static String getGetterPrefix(final boolean isBoolean) {
+ return isBoolean ? BOOLEAN_GETTER_PREFIX : GETTER_PREFIX;
+ }
+
+ public static String getGetterMethodName(final String localName, final boolean isBoolean) {
+ return getGetterPrefix(isBoolean) + toFirstUpper(getPropertyName(localName));
+ }
+
+ public static String getGetterMethodName(final QName name, final boolean isBoolean) {
+ return getGetterPrefix(isBoolean) + getGetterSuffix(name);
+ }
+
+ public static boolean isGetterMethodName(final String methodName) {
+ return methodName.startsWith(GETTER_PREFIX) || methodName.startsWith(BOOLEAN_GETTER_PREFIX);
+ }
+
public static String getGetterSuffix(final QName name) {
checkArgument(name != null, "Name should not be null.");
final String candidate = toFirstUpper(toCamelCase(name.getLocalName()));
if (rawString == null || rawString.isEmpty()) {
return rawString;
}
- char firstChar = rawString.charAt(0);
- if (firstChar >= '0' && firstChar <= '9') {
- return "_" + rawString;
- } else {
- return rawString;
- }
+ final char firstChar = rawString.charAt(0);
+ return firstChar >= '0' && firstChar <= '9' ? "_" + rawString : rawString;
}
/**
@SuppressWarnings({ "unchecked", "rawtypes", "checkstyle:illegalCatch" })
private static Optional<Class<? extends DataContainer>> getYangModeledReturnType(final Method method) {
- if ("getClass".equals(method.getName()) || !method.getName().startsWith("get")
+ if ("getClass".equals(method.getName()) || !method.getName().startsWith(BindingMapping.GETTER_PREFIX)
|| method.getParameterTypes().length > 0) {
return Optional.empty();
}
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
import org.opendaylight.yangtools.yang.binding.Augmentable;
import org.opendaylight.yangtools.yang.binding.Augmentation;
import org.opendaylight.yangtools.yang.binding.DataContainer;
public abstract DataContainer read(DataContainer parent, Class<?> child);
private static Method resolveGetterMethod(final Class<? extends DataContainer> parent, final Class<?> child) {
- String methodName = "get" + child.getSimpleName();
+ String methodName = BindingMapping.GETTER_PREFIX + child.getSimpleName();
try {
return parent.getMethod(methodName);
} catch (NoSuchMethodException e) {