Skip to content
This repository was archived by the owner on Jul 4, 2023. It is now read-only.

Commit f78ba14

Browse files
cpovirkcgdecker
authored andcommitted
Use features from GWT 2.6.0:
- Use Class.getSimpleName() instead of our custom implementations. - Mark test that uses StringBuilder.appendCodePoint() as working under GWT. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=79579791
1 parent 39d4b21 commit f78ba14

File tree

5 files changed

+44
-28
lines changed

5 files changed

+44
-28
lines changed

guava-gwt/test-super/com/google/common/base/super/com/google/common/base/Utf8Test.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
import junit.framework.TestCase;
2222

23+
import java.util.HashMap;
24+
import java.util.Random;
25+
2326
/**
2427
* Unit tests for {@link Utf8}.
2528
*
@@ -44,6 +47,38 @@ public void testEncodedLength_validStrings() {
4447
newString(Character.MIN_HIGH_SURROGATE, Character.MIN_LOW_SURROGATE)));
4548
}
4649

50+
public void testEncodedLength_validStrings2() {
51+
HashMap<Integer, Integer> utf8Lengths = new HashMap<Integer, Integer>();
52+
utf8Lengths.put(0x00, 1);
53+
utf8Lengths.put(0x7f, 1);
54+
utf8Lengths.put(0x80, 2);
55+
utf8Lengths.put(0x7ff, 2);
56+
utf8Lengths.put(0x800, 3);
57+
utf8Lengths.put(Character.MIN_SUPPLEMENTARY_CODE_POINT - 1, 3);
58+
utf8Lengths.put(Character.MIN_SUPPLEMENTARY_CODE_POINT, 4);
59+
utf8Lengths.put(Character.MAX_CODE_POINT, 4);
60+
61+
Integer[] codePoints = utf8Lengths.keySet().toArray(new Integer[]{});
62+
StringBuilder sb = new StringBuilder();
63+
Random rnd = new Random();
64+
for (int trial = 0; trial < 100; trial++) {
65+
sb.setLength(0);
66+
int utf8Length = 0;
67+
for (int i = 0; i < 6; i++) {
68+
Integer randomCodePoint = codePoints[rnd.nextInt(codePoints.length)];
69+
sb.appendCodePoint(randomCodePoint);
70+
utf8Length += utf8Lengths.get(randomCodePoint);
71+
if (utf8Length != Utf8.encodedLength(sb)) {
72+
StringBuilder repro = new StringBuilder();
73+
for (int j = 0; j < sb.length(); j++) {
74+
repro.append(" " + (int) sb.charAt(j)); // GWT compatible
75+
}
76+
assertEquals(repro.toString(), utf8Length, Utf8.encodedLength(sb));
77+
}
78+
}
79+
}
80+
}
81+
4782
public void testEncodedLength_invalidStrings() {
4883
testEncodedLengthFails(newString(Character.MIN_HIGH_SURROGATE), 0);
4984
testEncodedLengthFails("foobar" + newString(Character.MIN_HIGH_SURROGATE), 6);

guava-gwt/test/com/google/common/base/Utf8Test_gwt.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public void testEncodedLength_validStrings() throws Exception {
2828
testCase.testEncodedLength_validStrings();
2929
}
3030

31+
public void testEncodedLength_validStrings2() throws Exception {
32+
com.google.common.base.Utf8Test testCase = new com.google.common.base.Utf8Test();
33+
testCase.testEncodedLength_validStrings2();
34+
}
35+
3136
public void testIsWellFormed_4BytesSamples() throws Exception {
3237
com.google.common.base.Utf8Test testCase = new com.google.common.base.Utf8Test();
3338
testCase.testIsWellFormed_4BytesSamples();

guava-tests/test/com/google/common/base/Utf8Test.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public void testEncodedLength_validStrings() {
4949
newString(Character.MIN_HIGH_SURROGATE, Character.MIN_LOW_SURROGATE)));
5050
}
5151

52-
@GwtIncompatible("StringBuilder.appendCodePoint()")
5352
public void testEncodedLength_validStrings2() {
5453
HashMap<Integer, Integer> utf8Lengths = new HashMap<Integer, Integer>();
5554
utf8Lengths.put(0x00, 1);

guava/src/com/google/common/base/MoreObjects.java

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static <T> T firstNonNull(@Nullable T first, @Nullable T second) {
9292
* @since 18.0 (since 2.0 as {@code Objects.toStringHelper()}).
9393
*/
9494
public static ToStringHelper toStringHelper(Object self) {
95-
return new ToStringHelper(simpleName(self.getClass()));
95+
return new ToStringHelper(self.getClass().getSimpleName());
9696
}
9797

9898
/**
@@ -106,7 +106,7 @@ public static ToStringHelper toStringHelper(Object self) {
106106
* @since 18.0 (since 7.0 as {@code Objects.toStringHelper()}).
107107
*/
108108
public static ToStringHelper toStringHelper(Class<?> clazz) {
109-
return new ToStringHelper(simpleName(clazz));
109+
return new ToStringHelper(clazz.getSimpleName());
110110
}
111111

112112
/**
@@ -121,29 +121,6 @@ public static ToStringHelper toStringHelper(String className) {
121121
return new ToStringHelper(className);
122122
}
123123

124-
/**
125-
* {@link Class#getSimpleName()} is not GWT compatible yet, so we
126-
* provide our own implementation.
127-
*/
128-
// Package-private so Objects can call it.
129-
static String simpleName(Class<?> clazz) {
130-
String name = clazz.getName();
131-
132-
// the nth anonymous class has a class name ending in "Outer$n"
133-
// and local inner classes have names ending in "Outer.$1Inner"
134-
name = name.replaceAll("\\$[0-9]+", "\\$");
135-
136-
// we want the name of the inner class all by its lonesome
137-
int start = name.lastIndexOf('$');
138-
139-
// if this isn't an inner class, just find the start of the
140-
// top level class name.
141-
if (start == -1) {
142-
start = name.lastIndexOf('.');
143-
}
144-
return name.substring(start + 1);
145-
}
146-
147124
/**
148125
* Support class for {@link MoreObjects#toStringHelper}.
149126
*

guava/src/com/google/common/base/Objects.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static int hashCode(@Nullable Object... objects) {
127127
*/
128128
@Deprecated
129129
public static ToStringHelper toStringHelper(Object self) {
130-
return new ToStringHelper(MoreObjects.simpleName(self.getClass()));
130+
return new ToStringHelper(self.getClass().getSimpleName());
131131
}
132132

133133
/**
@@ -144,7 +144,7 @@ public static ToStringHelper toStringHelper(Object self) {
144144
*/
145145
@Deprecated
146146
public static ToStringHelper toStringHelper(Class<?> clazz) {
147-
return new ToStringHelper(MoreObjects.simpleName(clazz));
147+
return new ToStringHelper(clazz.getSimpleName());
148148
}
149149

150150
/**

0 commit comments

Comments
 (0)