From 882790ea9749dc27a1a9f6dc52786351bf479360 Mon Sep 17 00:00:00 2001 From: Gene Date: Tue, 25 Oct 2016 22:30:56 -0700 Subject: [PATCH] #476 class comments replication when implementation is primary --- .../value/processor/Immutables.generator | 18 +++++++---- .../value/processor/meta/ValueAttribute.java | 14 ++------- .../value/processor/meta/ValueType.java | 30 +++++++++++++++++++ 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/value-processor/src/org/immutables/value/processor/Immutables.generator b/value-processor/src/org/immutables/value/processor/Immutables.generator index b6d8c2198..08adeb062 100644 --- a/value-processor/src/org/immutables/value/processor/Immutables.generator +++ b/value-processor/src/org/immutables/value/processor/Immutables.generator @@ -116,20 +116,26 @@ import static [routine].immutableCopyOf; [for setters = type.settableAttributes] /** + [if type.docComment] +[for d in type.docComment] + *[d][-- no space before doc line!] +[/for] + [else] * Immutable implementation of {@link [type.typeAbstract.relativeRaw]}. *

- [if type.useBuilder] +[if type.useBuilder] * Use the builder to create immutable instances: * {@code [type.factoryBuilder.relative]()}. - [/if] - [if type.useConstructor] +[/if] +[if type.useConstructor] * Use the static factory method to create immutable instances: * {@code [type.factoryOf.relative]()}. - [/if] - [if type.useSingleton] +[/if] +[if type.useSingleton] * Use the static factory method to get the default singleton instance: * {@code [type.factoryInstance.relative]()}. - [/if] +[/if] + [/if] */ [annotationsWhenTopLevel type topLevel] [if classpath.available 'javax.annotation.concurrent.Immutable'] diff --git a/value-processor/src/org/immutables/value/processor/meta/ValueAttribute.java b/value-processor/src/org/immutables/value/processor/meta/ValueAttribute.java index be205f011..7ed1488d8 100644 --- a/value-processor/src/org/immutables/value/processor/meta/ValueAttribute.java +++ b/value-processor/src/org/immutables/value/processor/meta/ValueAttribute.java @@ -65,7 +65,6 @@ public final class ValueAttribute extends TypeIntrospectionBase { private static final String GUAVA_IMMUTABLE_PREFIX = UnshadeGuava.typeString("collect.Immutable"); private static final String VALUE_ATTRIBUTE_NAME = "value"; private static final String ID_ATTRIBUTE_NAME = "_id"; - private static final Splitter DOC_COMMENT_LINE_SPLITTER = Splitter.on('\n').omitEmptyStrings(); private static final String[] EMPTY_SERIALIZED_NAMES = {}; public AttributeNames names; @@ -1287,18 +1286,11 @@ public boolean isDeferCollectionAllocation() { } private void initMiscellaneous() { - Elements elements = protoclass() + this.deprecated = protoclass() .processing() - .getElementUtils(); + .getElementUtils().isDeprecated(element); - this.deprecated = elements.isDeprecated(element); - - if (containingType.constitution.implementationVisibility().isPublic()) { - @Nullable String docComment = elements.getDocComment(element); - if (docComment != null) { - this.docComment = ImmutableList.copyOf(DOC_COMMENT_LINE_SPLITTER.split(docComment)); - } - } + this.docComment = containingType.extractDocComment(element); } private void initBuilderParamsIfApplicable() { diff --git a/value-processor/src/org/immutables/value/processor/meta/ValueType.java b/value-processor/src/org/immutables/value/processor/meta/ValueType.java index be16b5938..39bfd3cb7 100644 --- a/value-processor/src/org/immutables/value/processor/meta/ValueType.java +++ b/value-processor/src/org/immutables/value/processor/meta/ValueType.java @@ -15,6 +15,7 @@ */ package org.immutables.value.processor.meta; +import com.google.common.base.Splitter; import com.google.common.base.CaseFormat; import com.google.common.base.Function; import com.google.common.base.Optional; @@ -1269,6 +1270,33 @@ public boolean isUseCompactBuilder() { && getThrowForInvalidImmutableState().equals(IllegalStateException.class.getName()); } + public ImmutableList extractDocComment(Element element) { + // Only extract for generated type which is public + if (constitution.implementationVisibility().isPublic()) { + @Nullable String docComment = constitution + .protoclass() + .processing() + .getElementUtils() + .getDocComment(CachingElements.getDelegate(element)); + + if (docComment != null) { + return ImmutableList.copyOf(DOC_COMMENT_LINE_SPLITTER.split(docComment)); + } + } + return ImmutableList.of(); + } + + private ImmutableList docComment; + + public ImmutableList getDocComment() { + if (docComment == null) { + this.docComment = constitution.isImplementationPrimary() + ? extractDocComment(element) + : ImmutableList.of(); + } + return docComment; + } + DeclaringType inferDeclaringType(Element element) { return constitution.protoclass().environment().round().inferDeclaringTypeFor(element); } @@ -1476,4 +1504,6 @@ public int hashCode() { public String toString() { return "Type[" + name() + "]"; } + + private static final Splitter DOC_COMMENT_LINE_SPLITTER = Splitter.on('\n').omitEmptyStrings(); }