diff options
author | Thomas Broyer <t.broyer@ltgt.net> | 2017-05-13 14:52:51 +0200 |
---|---|---|
committer | Jesse Wilson <jesse@swank.ca> | 2017-05-13 08:52:51 -0400 |
commit | 4cd975b9649ec87f9a71ce48916fba8d9d460e4e (patch) | |
tree | 171a12cbbec17a17c2505588e2a088f343e6e37a /src/main | |
parent | 40dfa0cf36d844ca733bef0d4a61e87c9339af2c (diff) | |
download | javapoet-4cd975b9649ec87f9a71ce48916fba8d9d460e4e.tar.gz |
Fix conflicts with types with same name as type being declared (#559)
* Add failing tests for types conflicting with type being declared
Also removes previous (flawed) test, which should have
been in JavaFileTest anyway, and add a separate test for
the "superclass references self" case (already handled
in TypeSpecTest#interfaceExtends btw, but this makes it
clearer what is tested and expected).
And add a separate test for classes in the same package
conflicting with a superclass, for completeness.
Reproduces #470
* Fix conflicts with types with same name as type being declared
Those types (annotations, or referenced in annotation values,
in type variable bounds, superclass, or superinterfaces)
were erroneously imported rather than being emitted with their
qualified name.
Fixes #470
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/squareup/javapoet/TypeSpec.java | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/main/java/com/squareup/javapoet/TypeSpec.java b/src/main/java/com/squareup/javapoet/TypeSpec.java index 85fa250..b493d0c 100644 --- a/src/main/java/com/squareup/javapoet/TypeSpec.java +++ b/src/main/java/com/squareup/javapoet/TypeSpec.java @@ -82,6 +82,30 @@ public final class TypeSpec { this.originatingElements = Util.immutableList(originatingElementsMutable); } + /** + * Creates a dummy type spec for type-resolution only (in CodeWriter) + * while emitting the type declaration but before entering the type body. + */ + private TypeSpec(TypeSpec type) { + assert type.anonymousTypeArguments == null; + this.kind = type.kind; + this.name = type.name; + this.anonymousTypeArguments = null; + this.javadoc = type.javadoc; + this.annotations = Collections.emptyList(); + this.modifiers = Collections.emptySet(); + this.typeVariables = Collections.emptyList(); + this.superclass = null; + this.superinterfaces = Collections.emptyList(); + this.enumConstants = Collections.emptyMap(); + this.fieldSpecs = Collections.emptyList(); + this.staticBlock = type.staticBlock; + this.initializerBlock = type.initializerBlock; + this.methodSpecs = Collections.emptyList(); + this.typeSpecs = Collections.emptyList(); + this.originatingElements = Collections.emptyList(); + } + public boolean hasModifier(Modifier modifier) { return modifiers.contains(modifier); } @@ -172,6 +196,9 @@ public final class TypeSpec { codeWriter.emit(anonymousTypeArguments); codeWriter.emit(") {\n"); } else { + // Push an empty type (specifically without nested types) for type-resolution. + codeWriter.pushType(new TypeSpec(this)); + codeWriter.emitJavadoc(javadoc); codeWriter.emitAnnotations(annotations, false); codeWriter.emitModifiers(modifiers, Util.union(implicitModifiers, kind.asMemberModifiers)); @@ -214,6 +241,8 @@ public final class TypeSpec { } } + codeWriter.popType(); + codeWriter.emit(" {\n"); } |