Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/String/concat/ImplicitStringConcat.java
41153 views
1
/*
2
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @summary test implicit String concatenations
27
*
28
* @compile ImplicitStringConcat.java
29
* @run main/othervm -Xverify:all ImplicitStringConcat
30
*
31
* @compile -XDstringConcat=inline ImplicitStringConcat.java
32
* @run main/othervm -Xverify:all ImplicitStringConcat
33
*
34
* @compile -XDstringConcat=indy ImplicitStringConcat.java
35
* @run main/othervm -Xverify:all ImplicitStringConcat
36
*
37
* @compile -XDstringConcat=indyWithConstants ImplicitStringConcat.java
38
* @run main/othervm -Xverify:all ImplicitStringConcat
39
*/
40
import java.lang.StringBuilder;
41
42
public class ImplicitStringConcat {
43
44
static boolean b = true;
45
static byte by = 42;
46
static short sh = 42;
47
static char ch = 'a';
48
static int i = 42;
49
static float fl = 42.0f;
50
static long l = 42;
51
static double d = 42.0d;
52
static String s = "foo";
53
static String sNull = null;
54
static Object o = "bar";
55
static Object oNull = null;
56
static CharSequence cs = "bar";
57
static char[] chars = new char[] {'a'};
58
59
static MyClass myCl = new MyClass();
60
static MyClassNull myClNull = new MyClassNull();
61
static Object myCl2 = new MyClass();
62
static Object[] myArr = new Object[] { myCl };
63
static final Object[] s_myArr = new Object[] { myCl };
64
65
static StringBuffer sb = new StringBuffer("a");
66
67
public static void main(String[] args) throws Exception {
68
69
test("footrue", s + b);
70
test("foo42", s + by);
71
test("foo42", s + sh);
72
test("fooa", s + ch);
73
test("foo42", s + i);
74
test("foo42", s + l);
75
test("foo42.0", s + fl);
76
test("foo42.0", s + d);
77
test("foofoo", s + s);
78
test("foonull", s + sNull);
79
test("foobar", s + o);
80
test("foonull", s + oNull);
81
test("foobar", s + cs);
82
83
{
84
StringBuilder sb = new StringBuilder();
85
sb.append("foo");
86
sb.append(myArr.toString());
87
test(sb.toString(), s + myArr);
88
}
89
90
{
91
StringBuilder sb = new StringBuilder();
92
sb.append("foo");
93
sb.append(s_myArr.toString());
94
test(sb.toString(), s + s_myArr);
95
}
96
97
{
98
StringBuilder sb = new StringBuilder();
99
sb.append("foo[C@");
100
sb.append(Integer.toHexString(System.identityHashCode(chars)));
101
test(sb.toString(), s + chars);
102
}
103
104
test("fooa", s + ImplicitStringConcat.sb);
105
test("foonull", s + null);
106
test("fooMyClass", s + myCl);
107
test("foonull", s + myClNull);
108
test("fooMyClass", s + myCl2);
109
110
s = "foo"; s += b; test("footrue", s);
111
s = "foo"; s += by; test("foo42", s);
112
s = "foo"; s += sh; test("foo42", s);
113
s = "foo"; s += ch; test("fooa", s);
114
s = "foo"; s += i; test("foo42", s);
115
s = "foo"; s += l; test("foo42", s);
116
s = "foo"; s += fl; test("foo42.0", s);
117
s = "foo"; s += d; test("foo42.0", s);
118
s = "foo"; s += s; test("foofoo", s);
119
s = "foo"; s += sNull; test("foonull", s);
120
s = "foo"; s += o; test("foobar", s);
121
s = "foo"; s += oNull; test("foonull", s);
122
s = "foo"; s += cs; test("foobar", s);
123
124
{
125
StringBuilder sb = new StringBuilder();
126
sb.append("foo[C@");
127
sb.append(Integer.toHexString(System.identityHashCode(chars)));
128
s = "foo";
129
s += chars;
130
test(sb.toString(), s);
131
}
132
133
s = "foo"; s += ImplicitStringConcat.sb; test("fooa", s);
134
s = "foo"; s += null; test("foonull", s);
135
s = "foo"; s += myCl; test("fooMyClass", s);
136
s = "foo"; s += myCl2; test("fooMyClass", s);
137
}
138
139
public static void test(String expected, String actual) {
140
// Fingers crossed: String concat should work.
141
if (!expected.equals(actual)) {
142
StringBuilder sb = new StringBuilder();
143
sb.append("Expected = ");
144
sb.append(expected);
145
sb.append(", actual = ");
146
sb.append(actual);
147
throw new IllegalStateException(sb.toString());
148
}
149
}
150
151
static class MyClass {
152
public String toString() {
153
return "MyClass";
154
}
155
}
156
157
static class MyClassNull {
158
public String toString() {
159
return null;
160
}
161
}
162
}
163
164