Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/String/concat/ImplicitStringConcatAssignLHS.java
41153 views
1
/*
2
* Copyright (c) 2018, 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 "+=" applied to String operands can provoke side effects
27
* @bug 8204322
28
*
29
* @compile ImplicitStringConcatAssignLHS.java
30
* @run main/othervm -Xverify:all ImplicitStringConcatAssignLHS
31
*
32
* @compile -XDstringConcat=inline ImplicitStringConcatAssignLHS.java
33
* @run main/othervm -Xverify:all ImplicitStringConcatAssignLHS
34
*
35
* @compile -XDstringConcat=indy ImplicitStringConcatAssignLHS.java
36
* @run main/othervm -Xverify:all ImplicitStringConcatAssignLHS
37
*
38
* @compile -XDstringConcat=indyWithConstants ImplicitStringConcatAssignLHS.java
39
* @run main/othervm -Xverify:all ImplicitStringConcatAssignLHS
40
*/
41
import java.lang.StringBuilder;
42
43
public class ImplicitStringConcatAssignLHS {
44
45
static final int ARR_SIZE = 10; // enough padding to capture ill offsets
46
47
static int x;
48
49
public static void main(String[] args) throws Exception {
50
{
51
x = 0;
52
Object[] arr = new Object[ARR_SIZE];
53
arr[x++] += "foo";
54
check(1, "plain-plain Object[]");
55
}
56
57
{
58
x = 0;
59
getObjArray()[x++] += "foo";
60
check(2, "method-plain Object[]");
61
}
62
63
{
64
x = 0;
65
getObjArray()[getIndex()] += "foo";
66
check(2, "method-method Object[]");
67
}
68
69
{
70
x = 0;
71
String[] arr = new String[ARR_SIZE];
72
arr[x++] += "foo";
73
check(1, "plain-plain String[]");
74
}
75
76
{
77
x = 0;
78
getStringArray()[x++] += "foo";
79
check(2, "method-plain String[]");
80
}
81
82
{
83
x = 0;
84
getStringArray()[getIndex()] += "foo";
85
check(2, "method-method String[]");
86
}
87
88
{
89
x = 0;
90
CharSequence[] arr = new CharSequence[ARR_SIZE];
91
arr[x++] += "foo";
92
check(1, "plain-plain CharSequence[]");
93
}
94
95
{
96
x = 0;
97
getCharSequenceArray()[x++] += "foo";
98
check(2, "method-plain CharSequence[]");
99
}
100
101
{
102
x = 0;
103
getCharSequenceArray()[getIndex()] += "foo";
104
check(2, "method-method CharSequence[]");
105
}
106
107
{
108
x = 0;
109
new MyClass().s += "foo";
110
check(1, "MyClass::new (String)");
111
}
112
113
{
114
x = 0;
115
getMyClass().s += "foo";
116
check(1, "method MyClass::new (String)");
117
}
118
119
{
120
x = 0;
121
new MyClass().o += "foo";
122
check(1, "MyClass::new (object)");
123
}
124
125
{
126
x = 0;
127
getMyClass().o += "foo";
128
check(1, "method MyClass::new (object)");
129
}
130
}
131
132
public static void check(int expected, String label) {
133
if (x != expected) {
134
StringBuilder sb = new StringBuilder();
135
sb.append(label);
136
sb.append(": ");
137
sb.append("Expected = ");
138
sb.append(expected);
139
sb.append("actual = ");
140
sb.append(x);
141
throw new IllegalStateException(sb.toString());
142
}
143
}
144
145
public static int getIndex() {
146
return x++;
147
}
148
149
public static class MyClass {
150
Object o;
151
String s;
152
153
public MyClass() {
154
x++;
155
}
156
}
157
158
public static MyClass getMyClass() {
159
return new MyClass();
160
}
161
162
public static Object[] getObjArray() {
163
x++;
164
return new Object[ARR_SIZE];
165
}
166
167
public static String[] getStringArray() {
168
x++;
169
return new String[ARR_SIZE];
170
}
171
172
public static CharSequence[] getCharSequenceArray() {
173
x++;
174
return new String[ARR_SIZE];
175
}
176
177
}
178
179
180