diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2018-09-14 17:26:28 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2018-09-14 17:26:28 +0200 |
commit | e0eff2edc1bd975f34759618c8023498f70561e6 (patch) | |
tree | 78804fd718164b77f1e17c637c93b3973fc0e359 | |
parent | a769712c6ad927ba51b15024bd368a19b01dade0 (diff) |
Ensure that we do not get duplicates or have conflicting field definitions when using multiple inheritance.
3 files changed, 53 insertions, 10 deletions
diff --git a/vespa-documentgen-plugin/etc/complex/common2.sd b/vespa-documentgen-plugin/etc/complex/common2.sd new file mode 100644 index 00000000000..e32d3ed6751 --- /dev/null +++ b/vespa-documentgen-plugin/etc/complex/common2.sd @@ -0,0 +1,9 @@ +# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +search common2 { + document { + field com2 type string { + + } + } +} + diff --git a/vespa-documentgen-plugin/etc/complex/music3.sd b/vespa-documentgen-plugin/etc/complex/music3.sd new file mode 100644 index 00000000000..65f37029d04 --- /dev/null +++ b/vespa-documentgen-plugin/etc/complex/music3.sd @@ -0,0 +1,8 @@ +# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +search music3 { + document music3 inherits music2, common2 { + field mu3 type string { + + } + } +} diff --git a/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java b/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java index 309b9b73aa6..acefa3fa461 100644 --- a/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java +++ b/vespa-documentgen-plugin/src/main/java/com/yahoo/vespa/DocumentGenMojo.java @@ -1,6 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa; +import com.yahoo.collections.Pair; import com.yahoo.document.*; import com.yahoo.document.annotation.AnnotationReferenceDataType; import com.yahoo.document.annotation.AnnotationType; @@ -405,7 +406,9 @@ public class DocumentGenMojo extends AbstractMojo { */ private void exportDocumentClass(NewDocumentType docType, Writer out, String packageName) throws IOException { String className = className(docType.getName()); - String superType = javaSuperType(docType); + Pair<String, Boolean> extendInfo = javaSuperType(docType); + String superType = extendInfo.getFirst(); + Boolean multiExtends = extendInfo.getSecond(); out.write( "package "+packageName+";\n\n" + exportInnerImportsFromSuperTypes(docType, packageName) + @@ -441,12 +444,14 @@ public class DocumentGenMojo extends AbstractMojo { exportStructTypeGetter(docType.getName()+".header", docType.allHeader().getFields(), out, 1, "getHeaderStructType", "com.yahoo.document.StructDataType"); exportStructTypeGetter(docType.getName()+".body", docType.allBody().getFields(), out, 1, "getBodyStructType", "com.yahoo.document.StructDataType"); - exportExtendedStructTypeGetter(className, docType.getName(), docType.getAllFields(), out, 1, "getDocumentType", "com.yahoo.document.DocumentType"); - exportCopyConstructor(className, docType.getAllFields(), out, 1, true); - exportFieldsAndAccessors(className, "com.yahoo.document.Document".equals(superType) ? docType.getAllFields() : docType.getFields(), out, 1, true); - exportDocumentMethods(docType.getAllFields(), out, 1); - exportHashCode(docType.getAllFields(), out, 1, "(getDataType() != null ? getDataType().hashCode() : 0) + getId().hashCode()"); - exportEquals(className, docType.getAllFields(), out, 1); + Collection<Field> allUniqueFields = getAllUniqueFields(multiExtends, docType.getAllFields()); + exportExtendedStructTypeGetter(className, docType.getName(), allUniqueFields, out, 1, "getDocumentType", "com.yahoo.document.DocumentType"); + exportCopyConstructor(className, allUniqueFields, out, 1, true); + + exportFieldsAndAccessors(className, "com.yahoo.document.Document".equals(superType) ? allUniqueFields : docType.getFields(), out, 1, true); + exportDocumentMethods(allUniqueFields, out, 1); + exportHashCode(allUniqueFields, out, 1, "(getDataType() != null ? getDataType().hashCode() : 0) + getId().hashCode()"); + exportEquals(className, allUniqueFields, out, 1); Set<DataType> exportedStructs = exportStructTypes(docType.getTypes(), out, 1, null); docTypes.put(docType.getName(), packageName+"."+className); for (DataType exportedStruct : exportedStructs) { @@ -455,15 +460,36 @@ public class DocumentGenMojo extends AbstractMojo { out.write("}\n"); } + private Collection<Field> getAllUniqueFields(Boolean multipleInheritance, Collection<Field> allFields) { + if (multipleInheritance) { + Map<String, Field> seen = new HashMap<>(); + List<Field> unique = new ArrayList<>(allFields.size()); + for (Field f : allFields) { + if (seen.containsKey(f.getName())) { + if ( ! f.equals(seen.get(f.getName()))) { + throw new IllegalArgumentException("Field '" + f.getName() + "' has conflicting definitions in multiple inheritance." + + "First defined as '" + seen.get(f.getName()) + "', then as '" + f + "'."); + } + } else { + unique.add(f); + seen.put(f.getName(), f); + } + } + return unique; + } + return allFields; + } + /** * The Java class the class of the given type should inherit from. If the input type inherits from _one_ * other type, use that, otherwise Document. */ - private static String javaSuperType(NewDocumentType docType) { + private static Pair<String,Boolean> javaSuperType(NewDocumentType docType) { String ret = "com.yahoo.document.Document"; Collection<NewDocumentType> specInheriteds = specificInheriteds(docType); - if (!specInheriteds.isEmpty() && singleInheritance(specInheriteds)) ret = className(specInheriteds.iterator().next().getName()); - return ret; + boolean singleExtends = singleInheritance(specInheriteds); + if (!specInheriteds.isEmpty() && singleExtends) ret = className(specInheriteds.iterator().next().getName()); + return new Pair<>(ret, !singleExtends); } private static boolean singleInheritance(Collection<NewDocumentType> specInheriteds) { |