Skip to content

Commit f878642

Browse files
buchgrcgdecker
authored andcommitted
Remove dependency on java.beans.Introspector
This eliminates the need to include the giant java.desktop module on Java 9+. Based on #647 by Jakob Buchgraber. RELNOTES=Remove the need for the java.desktop module on Java 9+. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=202499735
1 parent 9efb4f5 commit f878642

File tree

4 files changed

+118
-12
lines changed

4 files changed

+118
-12
lines changed

value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.google.common.collect.ImmutableListMultimap;
3737
import com.google.common.collect.ImmutableMap;
3838
import com.google.common.collect.ImmutableSet;
39-
import java.beans.Introspector;
4039
import java.io.IOException;
4140
import java.io.Serializable;
4241
import java.io.Writer;
@@ -558,7 +557,7 @@ private static String nameWithoutPrefix(String name) {
558557
assert name.startsWith("is");
559558
name = name.substring(2);
560559
}
561-
return Introspector.decapitalize(name);
560+
return PropertyNames.decapitalizeLikeJavaBeans(name);
562561
}
563562

564563
/**

value/src/main/java/com/google/auto/value/processor/BuilderMethodClassifier.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.google.common.collect.Iterables;
3030
import com.google.common.collect.LinkedListMultimap;
3131
import com.google.common.collect.Multimap;
32-
import java.beans.Introspector;
3332
import java.util.LinkedHashMap;
3433
import java.util.LinkedHashSet;
3534
import java.util.Map;
@@ -345,7 +344,7 @@ private boolean classifyMethodOneArg(ExecutableElement method) {
345344
propertyName = methodName;
346345
} else if (valueGetter == null && methodName.startsWith("set") && methodName.length() > 3) {
347346
propertyNameToSetters = propertyNameToPrefixedSetters;
348-
propertyName = Introspector.decapitalize(methodName.substring(3));
347+
propertyName = PropertyNames.decapitalizeLikeJavaBeans(methodName.substring(3));
349348
valueGetter = propertyNameToGetter.get(propertyName);
350349
if (valueGetter == null) {
351350
// If our property is defined by a getter called getOAuth() then it is called "OAuth"
@@ -354,7 +353,7 @@ private boolean classifyMethodOneArg(ExecutableElement method) {
354353
// is defined by a getter called oAuth() then it is called "oAuth", but you would still
355354
// expect to be able to set it using setOAuth(x). Hence the second try using a decapitalize
356355
// method without the quirky two-leading-capitals rule.
357-
propertyName = decapitalize(methodName.substring(3));
356+
propertyName = PropertyNames.decapitalizeNormally(methodName.substring(3));
358357
valueGetter = propertyNameToGetter.get(propertyName);
359358
}
360359
}
@@ -550,13 +549,6 @@ private static String prefixWithSet(String propertyName) {
550549
return "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
551550
}
552551

553-
// Equivalent to Introspector.decapitalize but without the quirky exception whereby
554-
// Introspector.decapitalize("OAuth").equals("OAuth"). (If the first two letters are capitals
555-
// then Introspector.decapitalize does nothing.)
556-
private static String decapitalize(String propertyName) {
557-
return Character.toLowerCase(propertyName.charAt(0)) + propertyName.substring(1);
558-
}
559-
560552
private String typeParamsString() {
561553
return TypeSimplifier.actualTypeParametersString(autoValueClass);
562554
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2018 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.auto.value.processor;
17+
18+
import com.google.common.base.Strings;
19+
20+
/** Helper methods to create property names. */
21+
class PropertyNames {
22+
/**
23+
* Returns the {@code propertyName} with its first character in lower case, but leaves it intact
24+
* if it starts with two capitals.
25+
*
26+
* <p>For consistency with JavaBeans, a getter called {@code getHTMLPage()} defines a property
27+
* called {@code HTMLPage}. The <a
28+
* href="https://docs.oracle.com/javase/8/docs/api/java/beans/Introspector.html#decapitalize-java.lang.String-">
29+
* rule</a> is: the name of the property is the part after {@code get} or {@code
30+
* is}, with the first letter lowercased <i>unless</i> the first two letters are uppercase. This
31+
* works well for the {@code HTMLPage} example, but in these more enlightened times we use {@code
32+
* HtmlPage} anyway, so the special behaviour is not useful, and of course it behaves poorly with
33+
* examples like {@code OAuth}. Nevertheless, we preserve it for compatibility.
34+
*/
35+
static String decapitalizeLikeJavaBeans(String propertyName) {
36+
if (propertyName != null
37+
&& propertyName.length() >= 2
38+
&& Character.isUpperCase(propertyName.charAt(0))
39+
&& Character.isUpperCase(propertyName.charAt(1))) {
40+
return propertyName;
41+
}
42+
return decapitalizeNormally(propertyName);
43+
}
44+
45+
/**
46+
* Returns the {@code propertyName} with its first character in lower case.
47+
*/
48+
static String decapitalizeNormally(String propertyName) {
49+
if (Strings.isNullOrEmpty(propertyName)) {
50+
return propertyName;
51+
}
52+
return Character.toLowerCase(propertyName.charAt(0)) + propertyName.substring(1);
53+
}
54+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) 2018 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.auto.value.processor;
17+
18+
import com.google.common.collect.ImmutableMap;
19+
import com.google.common.truth.Expect;
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.junit.runners.JUnit4;
24+
25+
@RunWith(JUnit4.class)
26+
public class PropertyNamesTest {
27+
@Rule public Expect expect = Expect.create();
28+
29+
private static final ImmutableMap<String, String> NORMAL_CASES =
30+
ImmutableMap.<String, String>builder()
31+
.put("Foo", "foo")
32+
.put("foo", "foo")
33+
.put("X", "x")
34+
.put("x", "x")
35+
.put("", "")
36+
.build();
37+
38+
@Test
39+
public void decapitalizeLikeJavaBeans() {
40+
NORMAL_CASES
41+
.forEach(
42+
(input, output) -> {
43+
expect.that(PropertyNames.decapitalizeLikeJavaBeans(input)).isEqualTo(output);
44+
});
45+
expect.that(PropertyNames.decapitalizeLikeJavaBeans(null)).isNull();
46+
expect.that(PropertyNames.decapitalizeLikeJavaBeans("HTMLPage")).isEqualTo("HTMLPage");
47+
expect.that(PropertyNames.decapitalizeLikeJavaBeans("OAuth")).isEqualTo("OAuth");
48+
}
49+
50+
@Test
51+
public void decapitalizeNormally() {
52+
NORMAL_CASES
53+
.forEach(
54+
(input, output) -> {
55+
expect.that(PropertyNames.decapitalizeNormally(input)).isEqualTo(output);
56+
});
57+
expect.that(PropertyNames.decapitalizeNormally(null)).isNull();
58+
expect.that(PropertyNames.decapitalizeNormally("HTMLPage")).isEqualTo("hTMLPage");
59+
expect.that(PropertyNames.decapitalizeNormally("OAuth")).isEqualTo("oAuth");
60+
}
61+
}

0 commit comments

Comments
 (0)