return null;
}
final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(context, node, baseInterface);
- defaultImplementedInterace(genType);
+ addConcreteInterfaceMethods(genType);
annotateDeprecatedIfNecessary(node, genType);
final Module module = context.module();
addImplementedInterfaceFromUses(schema, outType);
outType.addImplementsType(type);
outType.addImplementsType(augmentable(outType));
- defaultImplementedInterace(outType);
+ addConcreteInterfaceMethods(outType);
annotateDeprecatedIfNecessary(rpc, outType);
resolveDataSchemaNodes(context, outType, outType, schema.getChildNodes(), false);
context.addChildNodeType(schema, outType);
final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition(
context.modulePackageName(), notification, DATA_OBJECT, context);
- defaultImplementedInterace(notificationInterface);
+ addConcreteInterfaceMethods(notificationInterface);
annotateDeprecatedIfNecessary(notification, notificationInterface);
notificationInterface.addImplementsType(NOTIFICATION);
context.addChildNodeType(notification, notificationInterface);
final GeneratedTypeBuilder notifInterface = addDefaultInterfaceDefinition(
packageNameForGeneratedType(context.modulePackageName(), notif.getPath()), notif, DATA_OBJECT, context);
- defaultImplementedInterace(notifInterface);
+ addConcreteInterfaceMethods(notifInterface);
annotateDeprecatedIfNecessary(notif, notifInterface);
notifInterface.addImplementsType(keyType != null ? keyedListNotification(notifInterface, parent, keyType)
JavaTypeName.create(augmentPackageName, augTypeName));
augTypeBuilder.addImplementsType(augmentation(targetTypeRef));
- defaultImplementedInterace(augTypeBuilder);
+ addConcreteInterfaceMethods(augTypeBuilder);
annotateDeprecatedIfNecessary(augSchema, augTypeBuilder);
addImplementedInterfaceFromUses(augSchema, augTypeBuilder);
if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(context, caseNode);
caseTypeBuilder.addImplementsType(refChoiceType);
- defaultImplementedInterace(caseTypeBuilder);
+ addConcreteInterfaceMethods(caseTypeBuilder);
annotateDeprecatedIfNecessary(caseNode, caseTypeBuilder);
context.addCaseType(caseNode.getPath(), caseTypeBuilder);
context.addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
if (caseNode != null) {
final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(context, caseNode);
caseTypeBuilder.addImplementsType(targetType);
- defaultImplementedInterace(caseTypeBuilder);
+ addConcreteInterfaceMethods(caseTypeBuilder);
CaseSchemaNode node = null;
final String caseLocalName = caseNode.getQName().getLocalName();
}
}
+ private static void addConcreteInterfaceMethods(final GeneratedTypeBuilder typeBuilder) {
+ defaultImplementedInterace(typeBuilder);
+
+ typeBuilder.addMethod(BindingMapping.BINDING_TO_STRING_NAME)
+ .setAccessModifier(AccessModifier.PUBLIC)
+ .setStatic(true)
+ .setReturnType(STRING);
+ }
+
private static void narrowImplementedInterface(final GeneratedTypeBuilder typeBuilder) {
defineImplementedInterfaceMethod(typeBuilder, wildcardTypeFor(typeBuilder.getIdentifier()));
}
assertNotNull("Generated Interface cannot contain NULL reference for Method Signature Definitions!", methods);
- assertEquals("Expected count of method signature definitions is 16", 16, methods.size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(17, methods.size());
Enumeration ianaIfType = null;
for (final MethodSignature method : methods) {
if (method.getName().equals("getType")) {
Type operStatus = null;
final List<MethodSignature> methods = genInterface.getMethodDefinitions();
assertNotNull("Generated Type Interface cannot contain NULL reference to Enumeration types!", methods);
- assertEquals("Generated Type Interface MUST contain 6 Methods ", 6, methods.size());
+
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(7, methods.size());
for (final MethodSignature method : methods) {
if (method.getName().equals("getLinkUpDownTrapEnable")) {
linkUpDownTrapEnable = method.getReturnType();
}
assertNotNull(simpleContainer);
assertNotNull(nestedContainer);
- assertEquals(4, simpleContainer.getMethodDefinitions().size());
- assertEquals(3, nestedContainer.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(5, simpleContainer.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(4, nestedContainer.getMethodDefinitions().size());
int getFooMethodCounter = 0;
int getBarMethodCounter = 0;
}
assertNotNull(simpleContainer);
assertNotNull(nestedContainer);
- assertEquals(4, simpleContainer.getMethodDefinitions().size());
- assertEquals(3, nestedContainer.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(5, simpleContainer.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(4, nestedContainer.getMethodDefinitions().size());
int getFooMethodCounter = 0;
int getBarMethodCounter = 0;
}
}
- assertEquals(3, listParentContainerMethodsCount);
- assertEquals(2, listChildContainerMethodsCount);
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(4, listParentContainerMethodsCount);
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(3, listChildContainerMethodsCount);
assertEquals(1, getSimpleListKeyMethodCount);
assertEquals(1, listKeyClassCount);
assertEquals(0, setSimpleLeafListMethodCount);
assertEquals(1, getBarMethodCount);
- assertEquals(7, simpleListMethodsCount);
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(8, simpleListMethodsCount);
}
@Test
import org.opendaylight.mdsal.binding.model.api.GeneratedType;
import org.opendaylight.mdsal.binding.model.api.MethodSignature;
import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class Mdsal320Test {
assertTrue(getImplIface.isDefault());
assertTrue(it.hasNext());
+ final MethodSignature bindingToString = it.next();
+ assertEquals(BindingMapping.BINDING_TO_STRING_NAME, bindingToString.getName());
+
final MethodSignature getBar = it.next();
assertFalse(it.hasNext());
final Type getBarType = getBar.getReturnType();
"org.opendaylight.yang.gen.v1.urn.grouping.uses._case.rev130718", groupingCaseTest.getPackageName());
containsInterface("GroupingCaseTest", caseC);
- assertEquals("Case C shouldn't contain any method.", 1, caseC.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(2, caseC.getMethodDefinitions().size());
assertEquals("Number of method in GroupingCaseTest is incorrect", 2, groupingCaseTest.getMethodDefinitions()
.size());
assertEquals("Number of method in GroupingContainerTestis incorrect", 3, groupingContainerTest
.getMethodDefinitions().size());
- assertEquals("Number of method in ContainerTest is incorrect", 2, containerTest.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(3, containerTest.getMethodDefinitions().size());
containsMethods(groupingContainerTest.getMethodDefinitions(), new NameTypePattern(
"getLeafGroupingContainerTest1", "String"), new NameTypePattern("getLeafGroupingContainerTest2",
assertEquals("Number of method in GroupingListTest is incorrect", 6, groupingListTest.getMethodDefinitions()
.size());
- assertEquals("Number of method in ListTest is incorrect", 2, listTest.getMethodDefinitions().size());
- assertEquals("Number of method in ContainerGroupingListTest is incorrect", 2, containerGroupingListTest
- .getMethodDefinitions().size());
- assertEquals("Number of method in ListGroupingListTest is incorrect", 2, listGroupingListTest
- .getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(3, listTest.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(3, containerGroupingListTest.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(3, listGroupingListTest.getMethodDefinitions().size());
containsMethods(groupingListTest.getMethodDefinitions(), new NameTypePattern("getContainerGroupingListTest",
"ContainerGroupingListTest"), new NameTypePattern("getLeafGroupingListTest", "String"),
containsInterface("GroupingRpcInputTest", rpcTestInput);
containsInterface("GroupingRpcOutputTest", rpcTestOutput);
- assertEquals("Number of method in RpcTestInput is incorrect", 1, rpcTestInput.getMethodDefinitions().size());
- assertEquals("Number of method in RpcTestOutput is incorrect", 1, rpcTestOutput.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(2, rpcTestInput.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(2, rpcTestOutput.getMethodDefinitions().size());
assertEquals("Number of method in GroupingRpcInputTest is incorrect", 3, groupingRpcInputTest
.getMethodDefinitions().size());
assertEquals("Number of method in GroupingRpcOutputTest is incorrect", 2, groupingRpcOutputTest
.getMethodDefinitions().size());
- assertEquals("Number of method in ContainerGroupingRpcInputTest is incorrect", 2, containerGroupingRpcInputTest
- .getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(3, containerGroupingRpcInputTest.getMethodDefinitions().size());
containsMethods(groupingRpcInputTest.getMethodDefinitions(), new NameTypePattern(
"getContainerGroupingRpcInputTest", "ContainerGroupingRpcInputTest"), new NameTypePattern(
containsInterface("GroupingAugmentTest", containerAugment1);
- assertEquals("Number of method in GroupingCaseTest is incorrect", 1, containerAugment1.getMethodDefinitions()
- .size());
-
- assertEquals("Number of method in ContainerAugment1 is incorrect", 1, containerAugment1.getMethodDefinitions()
- .size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(2, containerAugment1.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(2, containerAugment1.getMethodDefinitions().size());
assertEquals("Number of method in GroupingCaseTest is incorrect", 2, groupingAugmentTest.getMethodDefinitions()
.size());
containsInterface("GroupingNotificationTest", notificationTest);
- assertEquals("Number of method in NotificationTest is incorrect", 2, notificationTest.getMethodDefinitions()
- .size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(3, notificationTest.getMethodDefinitions().size());
assertEquals("Number of method in GroupingNotificationTest is incorrect", 3, groupingNotificationTest
.getMethodDefinitions().size());
- assertEquals("Number of method in ContainerGroupingNotificationTest is incorrect", 2,
- containerGroupingNotificationTest.getMethodDefinitions().size());
+ // FIXME: split this into getter/default/static asserts
+ assertEquals(3, containerGroupingNotificationTest.getMethodDefinitions().size());
containsMethods(notificationTest.getMethodDefinitions(), new NameTypePattern("getLeafNotificationTest",
"String"));
*/
package org.opendaylight.mdsal.binding.java.api.generator
-import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
-import com.google.common.base.MoreObjects
import java.util.ArrayList
import java.util.Collection
import java.util.Collections
'''
}
- override generateToString(Collection<? extends GeneratedProperty> properties) '''
- «IF properties !== null»
- @«OVERRIDE.importedName»
- public «STRING.importedName» toString() {
- final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«targetType.name»");
- «FOR property : properties»
- «CODEHELPERS.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
- «ENDFOR»
- «IF augmentType !== null»
- «CODEHELPERS.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", augmentations().values());
- «ENDIF»
- return helper.toString();
- }
- «ENDIF»
- '''
-
/**
* Template method which generate getter methods for IMPL class.
*
package org.opendaylight.mdsal.binding.java.api.generator
import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
-import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
import com.google.common.base.CharMatcher
-import com.google.common.base.MoreObjects
import com.google.common.base.Splitter
import com.google.common.collect.ImmutableMap
import com.google.common.collect.Iterables
return sb.append(lineBuilder).append(NEW_LINE).toString
}
- def protected generateToString(Collection<? extends GeneratedProperty> properties) '''
- «IF !properties.empty»
- @«OVERRIDE.importedName»
- public «STRING.importedName» toString() {
- final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper(«type.importedName».class);
- «FOR property : properties»
- «CODEHELPERS.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
- «ENDFOR»
- return helper.toString();
- }
- «ENDIF»
- '''
-
/**
* Template method which generates method parameters with their types from <code>parameters</code>.
*
package org.opendaylight.mdsal.binding.java.api.generator
import static org.opendaylight.mdsal.binding.model.util.BindingTypes.DATA_OBJECT
+import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_TO_STRING_NAME
import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
import java.util.Collection
«generateEquals()»
- «generateToString(properties)»
+ «generateToString()»
}
'''
«ENDIF»
'''
+ /**
+ * Template method which generates the method <code>toString()</code>.
+ *
+ * @return string with the <code>toString()</code> method definition in JAVA format
+ */
+ def protected generateToString() '''
+ @«OVERRIDE.importedName»
+ public «STRING.importedName» toString() {
+ return «targetType.importedName».«BINDING_TO_STRING_NAME»(this);
+ }
+ '''
+
override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() != null) {
this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
import org.opendaylight.mdsal.binding.model.util.Types
import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
import org.opendaylight.yangtools.concepts.Builder
-import org.opendaylight.yangtools.yang.binding.AugmentationHolder
/**
* Template for generating JAVA builder classes.
'''
override protected generateCopyAugmentation(Type implType) {
- val augmentationHolderRef = AugmentationHolder.importedName
+ val augmentationHolderRef = AUGMENTATION_HOLDER.importedName
val typeRef = targetType.importedName
val hashMapRef = JU_HASHMAP.importedName
val augmentTypeRef = augmentType.importedName
import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
+import com.google.common.base.MoreObjects
import com.google.common.base.Preconditions
import com.google.common.collect.ImmutableList
import com.google.common.collect.Lists
import java.beans.ConstructorProperties
import java.util.ArrayList
import java.util.Base64;
+import java.util.Collection
import java.util.Comparator
import java.util.List
import java.util.Map
«ENDIF»
'''
+ def private generateToString(Collection<? extends GeneratedProperty> properties) '''
+ «IF !properties.empty»
+ @«OVERRIDE.importedName»
+ public «STRING.importedName» toString() {
+ final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper(«type.importedName».class);
+ «FOR property : properties»
+ «CODEHELPERS.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
+ «ENDFOR»
+ return helper.toString();
+ }
+ «ENDIF»
+ '''
+
def GeneratedProperty getPropByName(String name) {
for (GeneratedProperty prop : allProperties) {
if (prop.name.equals(name)) {
*/
package org.opendaylight.mdsal.binding.java.api.generator
-import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
+import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.getGetterMethodForNonnull
import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isGetterMethodName
import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isNonnullMethodName
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_TO_STRING_NAME
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
+import com.google.common.base.MoreObjects
import java.util.List
import java.util.Map.Entry
import java.util.Set
«FOR m : methods SEPARATOR "\n"»
«IF m.isDefault»
«generateDefaultMethod(m)»
+ «ELSEIF m.isStatic»
+ «generateStaticMethod(m)»
«ELSEIF m.parameters.empty && m.name.isGetterMethodName»
«generateAccessorMethod(m)»
«ELSE»
}
}
+ def private generateStaticMethod(MethodSignature method) {
+ switch method.name {
+ case BINDING_TO_STRING_NAME : generateBindingToString
+ }
+ }
+
def private generateMethod(MethodSignature method) '''
«method.comment.asJavadoc»
«method.annotations.generateAnnotations»
}
'''
+ def generateBindingToString() '''
+ «val augmentable = analyzeType»
+ /**
+ * Default implementation of {@link «Object.importedName»#toString()} contract for this interface.
+ * Implementations of this interface are encouraged to defer to this method to get consistent string
+ * representations across all implementation.
+ *
+ «IF augmentable»
+ * <p>
+ * @param <T$$> implementation type, which has to also implement «AUGMENTATION_HOLDER.importedName» interface
+ * contract.
+ «ENDIF»
+ * @param obj Object for which to generate toString() result.
+ * @return {@link «STRING.importedName»} value of data modeled by this interface.
+ * @throws «NPE.importedName» if {@code obj} is null
+ */
+ «IF augmentable»
+ static <T$$ extends «type.fullyQualifiedName» & «AUGMENTATION_HOLDER.importedName»<«type.fullyQualifiedName»>> «STRING.importedName» «BINDING_TO_STRING_NAME»(final @«NONNULL.importedName» T$$ obj) {
+ «ELSE»
+ static «STRING.importedName» «BINDING_TO_STRING_NAME»(final «type.fullyQualifiedName» obj) {
+ «ENDIF»
+ final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper("«type.name»");
+ «FOR property : typeAnalysis.value»
+ «CODEHELPERS.importedName».appendValue(helper, "«property.name»", obj.«property.getterName»());
+ «ENDFOR»
+ «IF augmentable»
+ «CODEHELPERS.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", obj.augmentations().values());
+ «ENDIF»
+ return helper.toString();
+ }
+ '''
+
def private generateNonnullMethod(MethodSignature method) '''
«val ret = method.returnType»
«val name = method.name»
return !type.getPackageName().isEmpty()
}
- def analyzeType() {
+ private def boolean analyzeType() {
if (typeAnalysis === null) {
typeAnalysis = analyzeTypeHierarchy(type)
}
+ typeAnalysis.key !== null
}
}
import org.opendaylight.mdsal.binding.model.util.Types;
import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
import org.opendaylight.yangtools.yang.binding.Augmentable;
+import org.opendaylight.yangtools.yang.binding.AugmentationHolder;
import org.opendaylight.yangtools.yang.binding.CodeHelpers;
* {@code java.lang.Deprecated} as a JavaTypeName.
*/
static final @NonNull JavaTypeName DEPRECATED = JavaTypeName.create(Deprecated.class);
+ /**
+ * {@code java.lang.NullPointerException} as a JavaTypeName.
+ */
+ static final @NonNull JavaTypeName NPE = JavaTypeName.create(NullPointerException.class);
/**
* {@code java.lang.Override} as a JavaTypeName.
*/
static final @NonNull JavaTypeName OVERRIDE = JavaTypeName.create(Override.class);
-
/**
* {@code java.lang.SuppressWarnings} as a JavaTypeName.
*/
* {@code org.opendaylight.yangtools.yang.binding.CodeHelpers} as a JavaTypeName.
*/
static final @NonNull JavaTypeName CODEHELPERS = JavaTypeName.create(CodeHelpers.class);
+ /**
+ * {@code org.opendaylight.yangtools.yang.binding.AugmentationHolder} as a JavaTypeName.
+ */
+ static final @NonNull JavaTypeName AUGMENTATION_HOLDER = JavaTypeName.create(AugmentationHolder.class);
private static final Comparator<MethodSignature> METHOD_COMPARATOR = new AlphabeticallyTypeMemberComparator<>();
private static final Type AUGMENTATION_RET_TYPE;
public void builderTemplateGenerateToStringWithPropertyTest() {
final GeneratedType genType = mockGenType("get" + TEST);
- assertEquals("@Override\n"
- + "public String toString() {\n"
+ assertEquals("/**\n"
+ + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
+ + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
+ + "\n * representations across all implementation.\n"
+ + " *\n"
+ + " * @param obj Object for which to generate toString() result.\n"
+ + " * @return {@link String} value of data modeled by this interface.\n"
+ + " * @throws NullPointerException if {@code obj} is null\n"
+ + " */\n"
+ + "static String bindingToString(final test.test obj) {\n"
+ " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
- + " CodeHelpers.appendValue(helper, \"_test\", _test);\n"
+ + " CodeHelpers.appendValue(helper, \"test\", obj.gettest());\n"
+ " return helper.toString();\n"
+ "}\n", genToString(genType).toString());
}
@Test
public void builderTemplateGenerateToStringWithoutAnyPropertyTest() throws Exception {
- assertEquals("@Override\n"
- + "public String toString() {\n"
+ assertEquals("/**\n"
+ + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
+ + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
+ + "\n * representations across all implementation.\n"
+ + " *\n"
+ + " * @param obj Object for which to generate toString() result.\n"
+ + " * @return {@link String} value of data modeled by this interface.\n"
+ + " * @throws NullPointerException if {@code obj} is null\n"
+ + " */\n"
+ + "static String bindingToString(final test.test obj) {\n"
+ " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
+ " return helper.toString();\n"
+ "}\n", genToString(mockGenType(TEST)).toString());
@Test
public void builderTemplateGenerateToStringWithMorePropertiesTest() throws Exception {
- assertEquals("@Override\n"
- + "public String toString() {\n"
+ assertEquals("/**\n"
+ + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
+ + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
+ + "\n * representations across all implementation.\n"
+ + " *\n"
+ + " * @param obj Object for which to generate toString() result.\n"
+ + " * @return {@link String} value of data modeled by this interface.\n"
+ + " * @throws NullPointerException if {@code obj} is null\n"
+ + " */\n"
+ + "static String bindingToString(final test.test obj) {\n"
+ " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
- + " CodeHelpers.appendValue(helper, \"_test1\", _test1);\n"
- + " CodeHelpers.appendValue(helper, \"_test2\", _test2);\n"
+ + " CodeHelpers.appendValue(helper, \"test1\", obj.gettest1());\n"
+ + " CodeHelpers.appendValue(helper, \"test2\", obj.gettest2());\n"
+ " return helper.toString();\n"
+ "}\n", genToString(mockGenTypeMoreMeth("get" + TEST)).toString());
}
@Test
public void builderTemplateGenerateToStringWithoutPropertyWithAugmentTest() throws Exception {
- assertEquals("@Override\n"
- + "public String toString() {\n"
+ assertEquals("/**\n"
+ + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
+ + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
+ + "\n * representations across all implementation.\n"
+ + " *\n"
+ + " * <p>\n"
+ + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
+ + " * contract.\n"
+ + " * @param obj Object for which to generate toString() result.\n"
+ + " * @return {@link String} value of data modeled by this interface.\n"
+ + " * @throws NullPointerException if {@code obj} is null\n"
+ + " */\n"
+ + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
+ + " T$$ obj) {\n"
+ " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
- + " CodeHelpers.appendValue(helper, \"augmentation\", augmentations().values());\n"
+ + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
+ " return helper.toString();\n"
+ "}\n", genToString(mockAugment(mockGenType(TEST))).toString());
}
@Test
public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
- assertEquals("@Override\n"
- + "public String toString() {\n"
+ assertEquals("/**\n"
+ + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
+ + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
+ + "\n * representations across all implementation.\n"
+ + " *\n"
+ + " * <p>\n"
+ + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
+ + " * contract.\n"
+ + " * @param obj Object for which to generate toString() result.\n"
+ + " * @return {@link String} value of data modeled by this interface.\n"
+ + " * @throws NullPointerException if {@code obj} is null\n"
+ + " */\n"
+ + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
+ + " T$$ obj) {\n"
+ " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
- + " CodeHelpers.appendValue(helper, \"_test\", _test);\n"
- + " CodeHelpers.appendValue(helper, \"augmentation\", augmentations().values());\n"
+ + " CodeHelpers.appendValue(helper, \"test\", obj.gettest());\n"
+ + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
+ " return helper.toString();\n"
+ "}\n", genToString(mockAugment(mockGenType("get" + TEST))).toString());
}
@Test
public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
- assertEquals("@Override\n"
- + "public String toString() {\n"
+ assertEquals("/**\n"
+ + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
+ + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
+ + "\n * representations across all implementation.\n"
+ + " *\n"
+ + " * <p>\n"
+ + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
+ + " * contract.\n"
+ + " * @param obj Object for which to generate toString() result.\n"
+ + " * @return {@link String} value of data modeled by this interface.\n"
+ + " * @throws NullPointerException if {@code obj} is null\n"
+ + " */\n"
+ + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
+ + " T$$ obj) {\n"
+ " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
- + " CodeHelpers.appendValue(helper, \"_test1\", _test1);\n"
- + " CodeHelpers.appendValue(helper, \"_test2\", _test2);\n"
- + " CodeHelpers.appendValue(helper, \"augmentation\", augmentations().values());\n"
+ + " CodeHelpers.appendValue(helper, \"test1\", obj.gettest1());\n"
+ + " CodeHelpers.appendValue(helper, \"test2\", obj.gettest2());\n"
+ + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
+ " return helper.toString();\n"
+ "}\n", genToString(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString());
}
}
private static CharSequence genToString(final GeneratedType genType) {
- final BuilderTemplate bt = BuilderGenerator.templateForType(genType);
- return bt.generateToString(bt.properties);
+ return new InterfaceTemplate(genType).generateBindingToString();
}
private static GeneratedType mockGenType(final String methodeName) {
*/
public static final @NonNull String DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME = "implementedInterface";
+ /**
+ * Name of default {@link Object#toString()} implementation for instantiated DataObjects. Each such generated
+ * interface contains this static method.
+ */
+ public static final @NonNull String BINDING_TO_STRING_NAME = "bindingToString";
+
/**
* Name of {@link ScalarTypeObject#getValue()}.
*/
import com.google.common.util.concurrent.ListenableFuture;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
return false;
}
for (Method potentialMethod : potential.getMethods()) {
+ if (Modifier.isStatic(potentialMethod.getModifiers())) {
+ // Skip any static methods, as we are not interested in those
+ continue;
+ }
+
try {
Method targetMethod = target.getMethod(potentialMethod.getName(), potentialMethod.getParameterTypes());
if (!potentialMethod.getReturnType().equals(targetMethod.getReturnType())) {