From 333c98c106f3bc39116bca3a07fb23955d7861ad Mon Sep 17 00:00:00 2001 From: Roberto Perez Alcolea Date: Thu, 30 May 2024 16:53:01 -0700 Subject: [PATCH 1/2] codegen: option to add @com.netflix.hollow.Generated annotation --- .../java/com/netflix/hollow/Generated.java | 35 +++++++++++++++++++ .../AbstractHollowAPIGeneratorBuilder.java | 9 +++++ .../api/codegen/CodeGeneratorConfig.java | 15 ++++++++ .../codegen/HollowAPIClassJavaGenerator.java | 2 ++ .../HollowAPIFactoryJavaGenerator.java | 1 + .../HollowConsumerJavaFileGenerator.java | 11 ++++++ .../api/HollowDataAccessorGenerator.java | 3 +- .../codegen/api/TypeAPIListJavaGenerator.java | 1 + .../codegen/api/TypeAPIMapJavaGenerator.java | 2 ++ .../api/TypeAPIObjectJavaGenerator.java | 3 +- .../codegen/api/TypeAPISetJavaGenerator.java | 1 + ...llowObjectDelegateCachedImplGenerator.java | 1 + ...llowObjectDelegateLookupImplGenerator.java | 1 + .../indexes/HollowHashIndexGenerator.java | 4 ++- .../HollowUniqueKeyIndexGenerator.java | 3 +- .../objects/HollowFactoryJavaGenerator.java | 2 ++ .../objects/HollowListJavaGenerator.java | 4 ++- .../objects/HollowMapJavaGenerator.java | 3 +- .../objects/HollowObjectJavaGenerator.java | 3 +- .../objects/HollowSetJavaGenerator.java | 3 +- ...HollowObjectTypePerfAPIClassGenerator.java | 2 +- .../HollowPerformanceAPIClassGenerator.java | 2 +- .../AbstractHollowAPIGeneratorTest.java | 10 +++++- .../api/codegen/HollowAPIGeneratorTest.java | 8 +++++ ...ooleanFieldErgonomicsAPIGeneratorTest.java | 3 +- .../HollowDataAccessorAPIGeneratorTest.java | 8 +++++ .../codegen/HollowMapAPIGeneratorTest.java | 7 ++++ ...llowPackageErgonomicsAPIGeneratorTest.java | 6 ++++ .../HollowPrimaryKeyAPIGeneratorTest.java | 9 +++++ .../HollowPrimitiveTypesAPIGeneratorTest.java | 3 +- .../api/codegen/ScalarFieldCodeGenTest.java | 4 ++- 31 files changed, 155 insertions(+), 14 deletions(-) create mode 100644 hollow/src/main/java/com/netflix/hollow/Generated.java diff --git a/hollow/src/main/java/com/netflix/hollow/Generated.java b/hollow/src/main/java/com/netflix/hollow/Generated.java new file mode 100644 index 0000000000..99b558eaa8 --- /dev/null +++ b/hollow/src/main/java/com/netflix/hollow/Generated.java @@ -0,0 +1,35 @@ +/* + * Copyright 2016-2024 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.netflix.hollow; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.CONSTRUCTOR; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.TYPE; + +/** + * This represents code that is generated by HollowAPIGenerator + * This is helpful for tools that instrument code, such as JaCoCo. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(value = {CONSTRUCTOR, METHOD, TYPE}) +@Documented +public @interface Generated {} diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorBuilder.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorBuilder.java index 6dad1c7835..c3e7c5618d 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorBuilder.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorBuilder.java @@ -154,6 +154,15 @@ public B withMetaInfo(Path metaInfoPath) { return getBuilder(); } + /** + * Enable @Generated annotation in the generated API + * @return + */ + public B withGeneratedAnnotation() { + config.setUseGeneratedAnnotation(true); + return getBuilder(); + } + public G build() { if (apiClassname == null) throw new IllegalStateException("Please specify an API classname (.withAPIClassname()) before calling .build()"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/CodeGeneratorConfig.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/CodeGeneratorConfig.java index 4292641964..89506d3e89 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/CodeGeneratorConfig.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/CodeGeneratorConfig.java @@ -29,6 +29,7 @@ public class CodeGeneratorConfig { private boolean useHollowPrimitiveTypes = false; private boolean restrictApiToFieldType = false; private boolean useVerboseToString = false; + private boolean useGeneratedAnnotation = false; private boolean useMetaInfo = false; private Path metaInfoPath; @@ -139,6 +140,15 @@ public void setUseVerboseToString(boolean useVerboseToString) { this.useVerboseToString = useVerboseToString; } + public boolean isUseGeneratedAnnotation() { + return useGeneratedAnnotation; + } + + public void setUseGeneratedAnnotation(boolean useGeneratedAnnotation) { + this.useGeneratedAnnotation = useGeneratedAnnotation; + } + + @Override public int hashCode() { final int prime = 31; @@ -149,6 +159,7 @@ public int hashCode() { result = prime * result + (restrictApiToFieldType ? 1231 : 1237); result = prime * result + (useAggressiveSubstitutions ? 1231 : 1237); result = prime * result + (useBooleanFieldErgonomics ? 1231 : 1237); + result = prime * result + (useGeneratedAnnotation ? 1231 : 1237); result = prime * result + (useHollowPrimitiveTypes ? 1231 : 1237); result = prime * result + (usePackageGrouping ? 1231 : 1237); result = prime * result + (useVerboseToString ? 1231 : 1237); @@ -184,6 +195,8 @@ public boolean equals(Object obj) { return false; if (useBooleanFieldErgonomics != other.useBooleanFieldErgonomics) return false; + if (useGeneratedAnnotation != other.useGeneratedAnnotation) + return false; if (useHollowPrimitiveTypes != other.useHollowPrimitiveTypes) return false; if (usePackageGrouping != other.usePackageGrouping) @@ -207,6 +220,8 @@ public String toString() { builder.append(classPostfix); builder.append(", getterPrefix="); builder.append(getterPrefix); + builder.append(", useGeneratedAnnotation="); + builder.append(useGeneratedAnnotation); builder.append(", usePackageGrouping="); builder.append(usePackageGrouping); builder.append(", useAggressiveSubstitutions="); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowAPIClassJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowAPIClassJavaGenerator.java index ef0af94a32..8721040e7f 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowAPIClassJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowAPIClassJavaGenerator.java @@ -100,6 +100,8 @@ public String generate() { builder.append("import ").append(SampleResult.class.getName()).append(";\n"); builder.append("import ").append(AllHollowRecordCollection.class.getName()).append(";\n"); + appendGeneratedAnnotation(builder); + builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class ").append(className).append(" extends HollowAPI "); Set primitiveTypes = HollowCodeGenerationUtils.getPrimitiveTypes(schemaList); // Implement Primitive Type Retriever(s) diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowAPIFactoryJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowAPIFactoryJavaGenerator.java index 675d8ff6c0..a0adf2d384 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowAPIFactoryJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowAPIFactoryJavaGenerator.java @@ -54,6 +54,7 @@ public String generate() { builder.append("import ").append(Collections.class.getName()).append(";\n"); builder.append("import ").append(Set.class.getName()).append(";\n"); + appendGeneratedAnnotation(builder); builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class ").append(className).append(" implements HollowAPIFactory {\n\n"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowConsumerJavaFileGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowConsumerJavaFileGenerator.java index cdb59786b8..0203a4bcd1 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowConsumerJavaFileGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowConsumerJavaFileGenerator.java @@ -15,6 +15,7 @@ */ package com.netflix.hollow.api.codegen; +import com.netflix.hollow.Generated; import com.netflix.hollow.api.codegen.objects.HollowCollectionsGenerator; import com.netflix.hollow.core.HollowDataset; import com.netflix.hollow.core.schema.HollowListSchema; @@ -129,6 +130,16 @@ protected void appendPackageAndCommonImports(StringBuilder builder, } } + /** + * Add @com.netflix.hollow.Generated annotation to the generated class + */ + protected void appendGeneratedAnnotation(StringBuilder builder) { + if(config.isUseGeneratedAnnotation()) { + builder.append("import ").append(Generated.class.getName()).append(";\n"); + builder.append("\n@Generated"); + } + } + private String createFullPackageName(String packageName, String subPackageName, boolean usePackageGrouping) { if (usePackageGrouping && !isEmpty(packageName) && !isEmpty(subPackageName)) { return packageName + "." + subPackageName; diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/api/HollowDataAccessorGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/api/HollowDataAccessorGenerator.java index e270a69081..a7ec051463 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/api/HollowDataAccessorGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/api/HollowDataAccessorGenerator.java @@ -66,7 +66,8 @@ public String generate() { builder.append("import " + HollowReadStateEngine.class.getName() + ";\n"); builder.append("\n"); - builder.append("@SuppressWarnings(\"all\")\n"); + appendGeneratedAnnotation(builder); + builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class " + className + " extends " + AbstractHollowDataAccessor.class.getSimpleName() + "<" + type +"> {\n\n"); builder.append(" public static final String TYPE = \"" + schema.getName() + "\";\n"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIListJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIListJavaGenerator.java index cf7bf40b46..e0ccaa3ed2 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIListJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIListJavaGenerator.java @@ -54,6 +54,7 @@ public String generate() { builder.append("import " + HollowListTypeDataAccess.class.getName() + ";\n"); builder.append("import " + HollowListLookupDelegate.class.getName() + ";\n"); + appendGeneratedAnnotation(builder); builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class ").append(className).append(" extends HollowListTypeAPI {\n\n"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIMapJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIMapJavaGenerator.java index 70e5fd7037..a1942a5e39 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIMapJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIMapJavaGenerator.java @@ -54,6 +54,8 @@ public String generate() { builder.append("import " + HollowMapTypeDataAccess.class.getName() + ";\n"); builder.append("import " + HollowMapLookupDelegate.class.getName() + ";\n"); + appendGeneratedAnnotation(builder); + builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class ").append(className).append(" extends HollowMapTypeAPI {\n\n"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIObjectJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIObjectJavaGenerator.java index 219060dfd3..6d7b43224f 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIObjectJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPIObjectJavaGenerator.java @@ -65,7 +65,8 @@ public TypeAPIObjectJavaGenerator(String apiClassname, String packageName, Hollo public String generate() { StringBuilder classBodyBuilder = new StringBuilder(); - classBodyBuilder.append("@SuppressWarnings(\"all\")\n"); + appendGeneratedAnnotation(classBodyBuilder); + classBodyBuilder.append("\n@SuppressWarnings(\"all\")\n"); classBodyBuilder.append("public class " + className + " extends HollowObjectTypeAPI {\n\n"); classBodyBuilder.append(" private final ").append(delegateLookupClassname(objectSchema)).append(" delegateLookupImpl;\n\n"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPISetJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPISetJavaGenerator.java index a2578c3632..58e099a906 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPISetJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/api/TypeAPISetJavaGenerator.java @@ -54,6 +54,7 @@ public String generate() { builder.append("import " + HollowSetTypeDataAccess.class.getName() + ";\n"); builder.append("import " + HollowSetLookupDelegate.class.getName() + ";\n"); + appendGeneratedAnnotation(builder); builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class ").append(className).append(" extends HollowSetTypeAPI {\n\n"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/delegate/HollowObjectDelegateCachedImplGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/delegate/HollowObjectDelegateCachedImplGenerator.java index 905d31f255..8cfca80bd2 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/delegate/HollowObjectDelegateCachedImplGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/delegate/HollowObjectDelegateCachedImplGenerator.java @@ -60,6 +60,7 @@ public String generate() { builder.append("import ").append(HollowTypeAPI.class.getName()).append(";\n"); builder.append("import ").append(HollowCachedDelegate.class.getName()).append(";\n"); + appendGeneratedAnnotation(builder); builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class ").append(className).append(" extends HollowObjectAbstractDelegate implements HollowCachedDelegate, ").append(delegateInterfaceName(schema.getName())).append(" {\n\n"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/delegate/HollowObjectDelegateLookupImplGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/delegate/HollowObjectDelegateLookupImplGenerator.java index 50747a3734..45523ab881 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/delegate/HollowObjectDelegateLookupImplGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/delegate/HollowObjectDelegateLookupImplGenerator.java @@ -54,6 +54,7 @@ public String generate() { builder.append("import ").append(HollowObjectTypeDataAccess.class.getName()).append(";\n"); builder.append("import ").append(HollowObjectSchema.class.getName()).append(";\n"); + appendGeneratedAnnotation(builder); builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class ").append(className).append(" extends HollowObjectAbstractDelegate implements ").append(delegateInterfaceName(schema.getName())).append(" {\n\n"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/indexes/HollowHashIndexGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/indexes/HollowHashIndexGenerator.java index 460da0a9e8..f28ebb3902 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/indexes/HollowHashIndexGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/indexes/HollowHashIndexGenerator.java @@ -63,12 +63,14 @@ public String generate() { builder.append("import " + AbstractHollowHashIndex.class.getName() + ";\n"); builder.append("import " + AbstractHollowOrdinalIterable.class.getName() + ";\n\n"); + appendGeneratedAnnotation(builder); + builder.append("\n"); builder.append("/**\n"); genDeprecatedJavaDoc(schemaList, builder); builder.append(" */\n"); builder.append("@Deprecated\n"); - builder.append("@SuppressWarnings(\"all\")\n"); + builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class " + className + " extends " + AbstractHollowHashIndex.class.getSimpleName() + "<" + apiClassname + "> {\n\n"); builder.append(" public " + className + "(HollowConsumer consumer, String queryType, String selectFieldPath, String... matchFieldPaths) {\n"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/indexes/HollowUniqueKeyIndexGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/indexes/HollowUniqueKeyIndexGenerator.java index 97d22d97de..671aac3eff 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/indexes/HollowUniqueKeyIndexGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/indexes/HollowUniqueKeyIndexGenerator.java @@ -65,11 +65,12 @@ public String generate() { if (isGenSimpleConstructor) builder.append("import " + HollowObjectSchema.class.getName() + ";\n"); + appendGeneratedAnnotation(builder); builder.append("\n/**\n"); genDeprecatedJavaDoc(builder); builder.append(" */\n"); builder.append("@Deprecated\n"); - builder.append("@SuppressWarnings(\"all\")\n"); + builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class " + className + " extends " + AbstractHollowUniqueKeyIndex.class.getSimpleName() + "<" + apiClassname + ", " + hollowImplClassname(type) + "> "); if (isImplementsUniqueKeyIndex) { builder.append("implements " + HollowUniqueKeyIndex.class.getSimpleName() + "<" + hollowImplClassname(type) + "> "); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowFactoryJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowFactoryJavaGenerator.java index a75b3b6e1a..faabb34dc2 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowFactoryJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowFactoryJavaGenerator.java @@ -72,6 +72,8 @@ public String generate() { if(schema instanceof HollowMapSchema) builder.append("import " + HollowMapCachedDelegate.class.getName() + ";\n"); + appendGeneratedAnnotation(builder); + builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class " + className + " extends HollowFactory {\n\n"); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowListJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowListJavaGenerator.java index fa5c79488c..c941bccd14 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowListJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowListJavaGenerator.java @@ -61,7 +61,9 @@ public String generate() { builder.append("import " + HollowListDelegate.class.getName() + ";\n"); builder.append("import " + GenericHollowRecordHelper.class.getName() + ";\n\n"); - builder.append("@SuppressWarnings(\"all\")\n"); + appendGeneratedAnnotation(builder); + + builder.append("\n@SuppressWarnings(\"all\")\n"); if(parameterize) builder.append("public class " + className + " extends HollowList {\n\n"); else diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowMapJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowMapJavaGenerator.java index aebc5af7b3..ad42668147 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowMapJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowMapJavaGenerator.java @@ -68,7 +68,8 @@ public String generate() { builder.append("import " + HollowMapDelegate.class.getName() + ";\n"); builder.append("import " + GenericHollowRecordHelper.class.getName() + ";\n\n"); - builder.append("@SuppressWarnings(\"all\")\n"); + appendGeneratedAnnotation(builder); + builder.append("\n@SuppressWarnings(\"all\")\n"); String keyGeneric = parameterizeKey ? "K" : keyClassName; String valueGeneric = parameterizeValue ? "V" : valueClassName; diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowObjectJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowObjectJavaGenerator.java index 627e006358..dc77c38d89 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowObjectJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowObjectJavaGenerator.java @@ -109,7 +109,8 @@ public String generate() { } classBuilder.append("\n"); - classBuilder.append("@SuppressWarnings(\"all\")\n"); + appendGeneratedAnnotation(classBuilder); + classBuilder.append("\n@SuppressWarnings(\"all\")\n"); if (requiresHollowTypeName) { classBuilder.append("@" + HollowTypeName.class.getSimpleName() + "(name=\"" + schema.getName() + "\")\n"); } diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowSetJavaGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowSetJavaGenerator.java index 7ea3173215..5bf11bf615 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowSetJavaGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/objects/HollowSetJavaGenerator.java @@ -62,7 +62,8 @@ public String generate() { builder.append("import " + HollowSetDelegate.class.getName() + ";\n"); builder.append("import " + GenericHollowRecordHelper.class.getName() + ";\n\n"); - builder.append("@SuppressWarnings(\"all\")\n"); + appendGeneratedAnnotation(builder); + builder.append("\n@SuppressWarnings(\"all\")\n"); if(parameterize) builder.append("public class " + className + " extends HollowSet {\n\n"); else diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/perfapi/HollowObjectTypePerfAPIClassGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/perfapi/HollowObjectTypePerfAPIClassGenerator.java index 8dd0d237b0..2e8933fe1b 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/perfapi/HollowObjectTypePerfAPIClassGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/perfapi/HollowObjectTypePerfAPIClassGenerator.java @@ -43,7 +43,7 @@ public String generate() { "import com.netflix.hollow.api.perfapi.Ref;\n" + "import com.netflix.hollow.core.read.dataaccess.HollowDataAccess;\n\n"); - builder.append("@SuppressWarnings(\"all\")\n"); + builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class " + schema.getName() + "PerfAPI extends HollowObjectTypePerfAPI {\n\n"); builder.append(" public static final String fieldNames[] = { "); diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/perfapi/HollowPerformanceAPIClassGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/perfapi/HollowPerformanceAPIClassGenerator.java index d8ca6a9271..aab4cfc8bb 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/perfapi/HollowPerformanceAPIClassGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/perfapi/HollowPerformanceAPIClassGenerator.java @@ -52,7 +52,7 @@ public String generate() { "import java.util.Set;\n" + "\n"); - builder.append("@SuppressWarnings(\"all\")\n"); + builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class " + apiClassName + " extends HollowPerformanceAPI {\n\n"); List schemas = new ArrayList<>(dataset.getSchemas()); diff --git a/hollow/src/test/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorTest.java b/hollow/src/test/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorTest.java index 3e4204c3f4..00ff8c82cf 100644 --- a/hollow/src/test/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorTest.java +++ b/hollow/src/test/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import com.netflix.hollow.Generated; import com.netflix.hollow.core.write.objectmapper.HollowTypeName; import java.io.File; import java.io.IOException; @@ -37,7 +38,7 @@ public class AbstractHollowAPIGeneratorTest { protected String clazzFolder = String.format("%s/classes", tmpFolder); private Path metaInfoPath = null; - void runGenerator(String apiClassName, String packageName, Class clazz, + protected void runGenerator(String apiClassName, String packageName, Class clazz, UnaryOperator generatorCustomizer) throws Exception { System.out.println(String.format("Folders (%s) : \n\tsource=%s \n\tclasses=%s", getClass().getSimpleName(), sourceFolder, clazzFolder)); @@ -71,6 +72,13 @@ void assertClassHasHollowTypeName(String clazz, String typeName) throws IOExcept assertEquals(typeName, ((HollowTypeName) annotation).name()); } + protected void assertClassHasGeneratedAnnotation(String clazz) throws IOException, ClassNotFoundException { + ClassLoader cl = new URLClassLoader(new URL[]{new File(clazzFolder).toURI().toURL()}); + Class cls = cl.loadClass(clazz); + Annotation annotation = cls.getAnnotation(Generated.class); + assertNotNull(annotation); + } + void assertFileDoesNotExist(String relativePath) { if (relativePath.startsWith("/")) { throw new IllegalArgumentException("Relative paths should not start with /"); diff --git a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowAPIGeneratorTest.java b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowAPIGeneratorTest.java index eeb0c34cb5..12399d06cd 100644 --- a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowAPIGeneratorTest.java +++ b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowAPIGeneratorTest.java @@ -81,6 +81,14 @@ public void testGenerateWithPostfixAndAggressiveSubstitutions() throws Exception assertFileDoesNotExist("codegen/api/StringGenerated.java"); } + @Test + public void testGenerateWithGeneratedAnnotation() throws Exception { + runGenerator("MyClassTestAPI", "codegen.api", MyClass.class, + builder -> builder.withClassPostfix("Generated").withGeneratedAnnotation()); + assertClassHasGeneratedAnnotation("codegen.api.MyClassTestAPI"); + } + + @SuppressWarnings("unused") @HollowPrimaryKey(fields = "id") private static class MyClass { diff --git a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowBooleanFieldErgonomicsAPIGeneratorTest.java b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowBooleanFieldErgonomicsAPIGeneratorTest.java index 260d40cabf..9bda05e632 100644 --- a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowBooleanFieldErgonomicsAPIGeneratorTest.java +++ b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowBooleanFieldErgonomicsAPIGeneratorTest.java @@ -24,7 +24,8 @@ public void test() throws Exception { String apiClassName = "BooleanErgoTestAPI"; String packageName = "codegen.booleanfield.ergo"; runGenerator(apiClassName, packageName, Movie.class, - builder -> builder.withBooleanFieldErgonomics(true)); + builder -> builder.withBooleanFieldErgonomics(true).withGeneratedAnnotation()); + assertClassHasGeneratedAnnotation(packageName + "." + apiClassName); } static class Movie { diff --git a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowDataAccessorAPIGeneratorTest.java b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowDataAccessorAPIGeneratorTest.java index 1703550542..9b000d5f24 100644 --- a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowDataAccessorAPIGeneratorTest.java +++ b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowDataAccessorAPIGeneratorTest.java @@ -34,6 +34,14 @@ public void testGenerateWithPostfix() throws Exception { builder -> builder.withClassPostfix("Generated").withPackageGrouping()); } + @Test + public void testGenerateWithGeneratedAnnotation() throws Exception { + runGenerator(API_CLASS_NAME, PACKAGE_NAME, Movie.class, + builder -> builder.withClassPostfix("Generated").withGeneratedAnnotation()); + assertClassHasGeneratedAnnotation(PACKAGE_NAME + "." + API_CLASS_NAME); + assertClassHasGeneratedAnnotation(PACKAGE_NAME + ".StringGenerated"); + } + @HollowPrimaryKey(fields="id") static class Movie { int id; diff --git a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowMapAPIGeneratorTest.java b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowMapAPIGeneratorTest.java index 166ecaf73f..ec3e868248 100644 --- a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowMapAPIGeneratorTest.java +++ b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowMapAPIGeneratorTest.java @@ -57,4 +57,11 @@ public void test_withPackageGrouping() throws Exception { public void test_withoutPackageGrouping() throws Exception { runGenerator(API_CLASS_NAME, PACKAGE_NAME, Gru.class, b -> b); } + + @Test + public void test_withGeneratedAnnotation() throws Exception { + runGenerator(API_CLASS_NAME, PACKAGE_NAME, Gru.class, + HollowAPIGenerator.Builder::withGeneratedAnnotation); + assertClassHasGeneratedAnnotation(PACKAGE_NAME + "." + API_CLASS_NAME); + } } diff --git a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPackageErgonomicsAPIGeneratorTest.java b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPackageErgonomicsAPIGeneratorTest.java index de61f4082f..d344ecaacf 100644 --- a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPackageErgonomicsAPIGeneratorTest.java +++ b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPackageErgonomicsAPIGeneratorTest.java @@ -38,6 +38,12 @@ public void testDefaultPackage() throws Exception { runGenerator(API_CLASS_NAME, "", Movie.class, b -> b); } + @Test + public void testWithGeneratedAnnotation() throws Exception { + runGenerator(API_CLASS_NAME, "codegen.subpackage.grouping", Movie.class, builder -> builder.withErgonomicShortcuts().withPackageGrouping().withGeneratedAnnotation()); + assertClassHasGeneratedAnnotation("codegen.subpackage.grouping." + API_CLASS_NAME); + } + @SuppressWarnings("unused") @HollowPrimaryKey(fields = { "id" }) static class Movie { diff --git a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPrimaryKeyAPIGeneratorTest.java b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPrimaryKeyAPIGeneratorTest.java index 767d5ba0b1..ebc64f526e 100644 --- a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPrimaryKeyAPIGeneratorTest.java +++ b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPrimaryKeyAPIGeneratorTest.java @@ -36,6 +36,15 @@ public void testWithPostfix() throws Exception { builder -> builder.withClassPostfix("Generated").withPackageGrouping()); } + @Test + public void testWithGeneratedAnnotation() throws Exception { + String apiClassName = "PrimaryKeyIndexTestAPI"; + String packageName = "codegen.primarykey"; + runGenerator(apiClassName, packageName, Movie.class, + builder -> builder.withClassPostfix("Generated").withPackageGrouping().withGeneratedAnnotation()); + assertClassHasGeneratedAnnotation(packageName + "." + apiClassName); + } + @HollowPrimaryKey(fields = {"id", "hasSubtitles", "actor", "role.id!", "role.rank"}) static class Movie { int id; diff --git a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPrimitiveTypesAPIGeneratorTest.java b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPrimitiveTypesAPIGeneratorTest.java index c6cb99d664..7a26106a90 100644 --- a/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPrimitiveTypesAPIGeneratorTest.java +++ b/hollow/src/test/java/com/netflix/hollow/api/codegen/HollowPrimitiveTypesAPIGeneratorTest.java @@ -28,7 +28,8 @@ public void test() throws Exception { String apiClassName = "PrimitiveTypeTestAPI"; String packageName = "codegen.primitive.types"; runGenerator(apiClassName, packageName, Movie.class, builder -> builder - .withErgonomicShortcuts().withPackageGrouping().withHollowPrimitiveTypes(true)); + .withErgonomicShortcuts().withPackageGrouping().withHollowPrimitiveTypes(true).withGeneratedAnnotation()); + assertClassHasGeneratedAnnotation(packageName + "." + apiClassName); } @SuppressWarnings("unused") diff --git a/hollow/src/test/java/com/netflix/hollow/api/codegen/ScalarFieldCodeGenTest.java b/hollow/src/test/java/com/netflix/hollow/api/codegen/ScalarFieldCodeGenTest.java index 0802cbf4c3..51df811ff2 100644 --- a/hollow/src/test/java/com/netflix/hollow/api/codegen/ScalarFieldCodeGenTest.java +++ b/hollow/src/test/java/com/netflix/hollow/api/codegen/ScalarFieldCodeGenTest.java @@ -29,7 +29,9 @@ public void test() throws Exception { String packageName = "codegen.scalar.types"; runGenerator(apiClassName, packageName, Movie.class, builder -> builder.withErgonomicShortcuts().withPackageGrouping() - .withHollowPrimitiveTypes(true).withRestrictApiToFieldType()); + .withHollowPrimitiveTypes(true) + .withRestrictApiToFieldType().withGeneratedAnnotation()); + assertClassHasGeneratedAnnotation(packageName + "." + apiClassName); } @SuppressWarnings("unused") From 3f9ac80c24f32737b82f2133b0560b130a22a210 Mon Sep 17 00:00:00 2001 From: Roberto Perez Alcolea Date: Thu, 6 Jun 2024 12:28:05 -0700 Subject: [PATCH 2/2] Rename Generated to HollowGenerated to avoid confusion for developers --- .../netflix/hollow/{Generated.java => HollowGenerated.java} | 2 +- .../hollow/api/codegen/HollowConsumerJavaFileGenerator.java | 6 +++--- .../hollow/api/codegen/AbstractHollowAPIGeneratorTest.java | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-) rename hollow/src/main/java/com/netflix/hollow/{Generated.java => HollowGenerated.java} (97%) diff --git a/hollow/src/main/java/com/netflix/hollow/Generated.java b/hollow/src/main/java/com/netflix/hollow/HollowGenerated.java similarity index 97% rename from hollow/src/main/java/com/netflix/hollow/Generated.java rename to hollow/src/main/java/com/netflix/hollow/HollowGenerated.java index 99b558eaa8..d7beffde34 100644 --- a/hollow/src/main/java/com/netflix/hollow/Generated.java +++ b/hollow/src/main/java/com/netflix/hollow/HollowGenerated.java @@ -32,4 +32,4 @@ @Retention(RetentionPolicy.RUNTIME) @Target(value = {CONSTRUCTOR, METHOD, TYPE}) @Documented -public @interface Generated {} +public @interface HollowGenerated {} diff --git a/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowConsumerJavaFileGenerator.java b/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowConsumerJavaFileGenerator.java index 0203a4bcd1..417e05d0f4 100644 --- a/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowConsumerJavaFileGenerator.java +++ b/hollow/src/main/java/com/netflix/hollow/api/codegen/HollowConsumerJavaFileGenerator.java @@ -15,7 +15,7 @@ */ package com.netflix.hollow.api.codegen; -import com.netflix.hollow.Generated; +import com.netflix.hollow.HollowGenerated; import com.netflix.hollow.api.codegen.objects.HollowCollectionsGenerator; import com.netflix.hollow.core.HollowDataset; import com.netflix.hollow.core.schema.HollowListSchema; @@ -135,8 +135,8 @@ protected void appendPackageAndCommonImports(StringBuilder builder, */ protected void appendGeneratedAnnotation(StringBuilder builder) { if(config.isUseGeneratedAnnotation()) { - builder.append("import ").append(Generated.class.getName()).append(";\n"); - builder.append("\n@Generated"); + builder.append("import ").append(HollowGenerated.class.getName()).append(";\n"); + builder.append("\n@HollowGenerated"); } } diff --git a/hollow/src/test/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorTest.java b/hollow/src/test/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorTest.java index 00ff8c82cf..2f7c30da05 100644 --- a/hollow/src/test/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorTest.java +++ b/hollow/src/test/java/com/netflix/hollow/api/codegen/AbstractHollowAPIGeneratorTest.java @@ -20,7 +20,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import com.netflix.hollow.Generated; +import com.netflix.hollow.HollowGenerated; import com.netflix.hollow.core.write.objectmapper.HollowTypeName; import java.io.File; import java.io.IOException; @@ -28,7 +28,6 @@ import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.function.UnaryOperator; import org.junit.After; @@ -75,7 +74,7 @@ void assertClassHasHollowTypeName(String clazz, String typeName) throws IOExcept protected void assertClassHasGeneratedAnnotation(String clazz) throws IOException, ClassNotFoundException { ClassLoader cl = new URLClassLoader(new URL[]{new File(clazzFolder).toURI().toURL()}); Class cls = cl.loadClass(clazz); - Annotation annotation = cls.getAnnotation(Generated.class); + Annotation annotation = cls.getAnnotation(HollowGenerated.class); assertNotNull(annotation); }