aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/squareup
diff options
context:
space:
mode:
authorRon Shapiro <shapiro.rd@gmail.com>2018-08-21 21:12:33 -0400
committerJesse Wilson <jesse@swank.ca>2018-08-21 21:12:33 -0400
commitb879b58254804b953c6280d05d7882a89ec3b7c8 (patch)
treef76a13da6cae00ee55733c6c6497c1b5e10a7aa2 /src/test/java/com/squareup
parentdfb5bc6c4e0c33f0d40d02c9702089da8a848d2f (diff)
downloadjavapoet-b879b58254804b953c6280d05d7882a89ec3b7c8.tar.gz
Qualify types masked by type variables (#657)
* Use fully qualified names if a type variable masks a type name, even if it is in the same package * Add a makeshift multiset to handle https://github.com/square/javapoet/pull/657\#discussion_r205514292
Diffstat (limited to 'src/test/java/com/squareup')
-rw-r--r--src/test/java/com/squareup/javapoet/TypeSpecTest.java68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/test/java/com/squareup/javapoet/TypeSpecTest.java b/src/test/java/com/squareup/javapoet/TypeSpecTest.java
index 9cd22c2..733483b 100644
--- a/src/test/java/com/squareup/javapoet/TypeSpecTest.java
+++ b/src/test/java/com/squareup/javapoet/TypeSpecTest.java
@@ -985,6 +985,74 @@ public final class TypeSpecTest {
+ "}\n");
}
+ @Test public void simpleNameConflictsWithTypeVariable() {
+ ClassName inPackage = ClassName.get("com.squareup.tacos", "InPackage");
+ ClassName otherType = ClassName.get("com.other", "OtherType");
+ ClassName methodInPackage = ClassName.get("com.squareup.tacos", "MethodInPackage");
+ ClassName methodOtherType = ClassName.get("com.other", "MethodOtherType");
+ TypeSpec gen = TypeSpec.classBuilder("Gen")
+ .addTypeVariable(TypeVariableName.get("InPackage"))
+ .addTypeVariable(TypeVariableName.get("OtherType"))
+ .addField(FieldSpec.builder(inPackage, "inPackage").build())
+ .addField(FieldSpec.builder(otherType, "otherType").build())
+ .addMethod(MethodSpec.methodBuilder("withTypeVariables")
+ .addTypeVariable(TypeVariableName.get("MethodInPackage"))
+ .addTypeVariable(TypeVariableName.get("MethodOtherType"))
+ .addStatement("$T inPackage = null", methodInPackage)
+ .addStatement("$T otherType = null", methodOtherType)
+ .build())
+ .addMethod(MethodSpec.methodBuilder("withoutTypeVariables")
+ .addStatement("$T inPackage = null", methodInPackage)
+ .addStatement("$T otherType = null", methodOtherType)
+ .build())
+ .addMethod(MethodSpec.methodBuilder("againWithTypeVariables")
+ .addTypeVariable(TypeVariableName.get("MethodInPackage"))
+ .addTypeVariable(TypeVariableName.get("MethodOtherType"))
+ .addStatement("$T inPackage = null", methodInPackage)
+ .addStatement("$T otherType = null", methodOtherType)
+ .build())
+ // https://github.com/square/javapoet/pull/657#discussion_r205514292
+ .addMethod(MethodSpec.methodBuilder("masksEnclosingTypeVariable")
+ .addTypeVariable(TypeVariableName.get("InPackage"))
+ .build())
+ .addMethod(MethodSpec.methodBuilder("hasSimpleNameThatWasPreviouslyMasked")
+ .addStatement("$T inPackage = null", inPackage)
+ .build())
+ .build();
+ assertThat(toString(gen)).isEqualTo(""
+ + "package com.squareup.tacos;\n"
+ + "\n"
+ + "import com.other.MethodOtherType;\n"
+ + "\n"
+ + "class Gen<InPackage, OtherType> {\n"
+ + " com.squareup.tacos.InPackage inPackage;\n"
+ + "\n"
+ + " com.other.OtherType otherType;\n"
+ + "\n"
+ + " <MethodInPackage, MethodOtherType> void withTypeVariables() {\n"
+ + " com.squareup.tacos.MethodInPackage inPackage = null;\n"
+ + " com.other.MethodOtherType otherType = null;\n"
+ + " }\n"
+ + "\n"
+ + " void withoutTypeVariables() {\n"
+ + " MethodInPackage inPackage = null;\n"
+ + " MethodOtherType otherType = null;\n"
+ + " }\n"
+ + "\n"
+ + " <MethodInPackage, MethodOtherType> void againWithTypeVariables() {\n"
+ + " com.squareup.tacos.MethodInPackage inPackage = null;\n"
+ + " com.other.MethodOtherType otherType = null;\n"
+ + " }\n"
+ + "\n"
+ + " <InPackage> void masksEnclosingTypeVariable() {\n"
+ + " }\n"
+ + "\n"
+ + " void hasSimpleNameThatWasPreviouslyMasked() {\n"
+ + " com.squareup.tacos.InPackage inPackage = null;\n"
+ + " }\n"
+ + "}\n");
+ }
+
@Test public void originatingElementsIncludesThoseOfNestedTypes() {
Element outerElement = Mockito.mock(Element.class);
Element innerElement = Mockito.mock(Element.class);