Skip to content

Commit b1f0450

Browse files
authored
Merge pull request #70 from openjavaformat/single-letter-type-name
Treat a single upper case letter as a type name in a dotted chain
2 parents 1760d93 + 010cef1 commit b1f0450

4 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎open-java-format/src/main/java/com/palantir/javaformat/java/TypeNameClassifier.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ static JavaCaseFormat from(String name) {
164164
hasLowercase |= Character.isLowerCase(c);
165165
}
166166
if (firstUppercase) {
167-
return hasLowercase ? UPPER_CAMEL : UPPERCASE;
167+
// A single upper case letter is a type name: Android's R, and generic type parameters such as T.
168+
return (hasLowercase || name.length() == 1) ? UPPER_CAMEL : UPPERCASE;
168169
} else {
169170
return hasUppercase ? LOWER_CAMEL : LOWERCASE;
170171
}

‎open-java-format/src/test/java/com/palantir/javaformat/java/TypeNameClassifierTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public void caseFormat() throws Exception {
4141
assertThat(JavaCaseFormat.from("a_$")).isEqualTo(JavaCaseFormat.LOWERCASE);
4242
assertThat(JavaCaseFormat.from("_")).isEqualTo(JavaCaseFormat.LOWERCASE);
4343
assertThat(JavaCaseFormat.from("_A")).isEqualTo(JavaCaseFormat.UPPERCASE);
44+
assertThat(JavaCaseFormat.from("A")).isEqualTo(JavaCaseFormat.UPPER_CAMEL);
4445
}
4546

4647
private static Optional<Integer> getPrefix(String qualifiedName) {
@@ -60,6 +61,7 @@ public void typePrefixLength() {
6061
assertThat(getPrefix("ClassName.CONST")).hasValue(1);
6162
assertThat(getPrefix("ClassName.varName")).hasValue(1);
6263
assertThat(getPrefix("ClassName.Inner.varName")).hasValue(2);
64+
assertThat(getPrefix("com.R.foo")).hasValue(2);
6365
}
6466

6567
@Test
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class B26306390 {
2+
int resourceId = com.some.extremely.verbose.pkg.name.R.string.some_extremely_long_resource_identifier_that_exceeds_the_column_limit;
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class B26306390 {
2+
int resourceId = com.some.extremely.verbose.pkg.name.R.string
3+
.some_extremely_long_resource_identifier_that_exceeds_the_column_limit;
4+
}

0 commit comments

Comments
 (0)