Path: blob/master/test/jdk/java/lang/String/concat/ImplicitStringConcatAssignLHS.java
41153 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @summary "+=" applied to String operands can provoke side effects26* @bug 820432227*28* @compile ImplicitStringConcatAssignLHS.java29* @run main/othervm -Xverify:all ImplicitStringConcatAssignLHS30*31* @compile -XDstringConcat=inline ImplicitStringConcatAssignLHS.java32* @run main/othervm -Xverify:all ImplicitStringConcatAssignLHS33*34* @compile -XDstringConcat=indy ImplicitStringConcatAssignLHS.java35* @run main/othervm -Xverify:all ImplicitStringConcatAssignLHS36*37* @compile -XDstringConcat=indyWithConstants ImplicitStringConcatAssignLHS.java38* @run main/othervm -Xverify:all ImplicitStringConcatAssignLHS39*/40import java.lang.StringBuilder;4142public class ImplicitStringConcatAssignLHS {4344static final int ARR_SIZE = 10; // enough padding to capture ill offsets4546static int x;4748public static void main(String[] args) throws Exception {49{50x = 0;51Object[] arr = new Object[ARR_SIZE];52arr[x++] += "foo";53check(1, "plain-plain Object[]");54}5556{57x = 0;58getObjArray()[x++] += "foo";59check(2, "method-plain Object[]");60}6162{63x = 0;64getObjArray()[getIndex()] += "foo";65check(2, "method-method Object[]");66}6768{69x = 0;70String[] arr = new String[ARR_SIZE];71arr[x++] += "foo";72check(1, "plain-plain String[]");73}7475{76x = 0;77getStringArray()[x++] += "foo";78check(2, "method-plain String[]");79}8081{82x = 0;83getStringArray()[getIndex()] += "foo";84check(2, "method-method String[]");85}8687{88x = 0;89CharSequence[] arr = new CharSequence[ARR_SIZE];90arr[x++] += "foo";91check(1, "plain-plain CharSequence[]");92}9394{95x = 0;96getCharSequenceArray()[x++] += "foo";97check(2, "method-plain CharSequence[]");98}99100{101x = 0;102getCharSequenceArray()[getIndex()] += "foo";103check(2, "method-method CharSequence[]");104}105106{107x = 0;108new MyClass().s += "foo";109check(1, "MyClass::new (String)");110}111112{113x = 0;114getMyClass().s += "foo";115check(1, "method MyClass::new (String)");116}117118{119x = 0;120new MyClass().o += "foo";121check(1, "MyClass::new (object)");122}123124{125x = 0;126getMyClass().o += "foo";127check(1, "method MyClass::new (object)");128}129}130131public static void check(int expected, String label) {132if (x != expected) {133StringBuilder sb = new StringBuilder();134sb.append(label);135sb.append(": ");136sb.append("Expected = ");137sb.append(expected);138sb.append("actual = ");139sb.append(x);140throw new IllegalStateException(sb.toString());141}142}143144public static int getIndex() {145return x++;146}147148public static class MyClass {149Object o;150String s;151152public MyClass() {153x++;154}155}156157public static MyClass getMyClass() {158return new MyClass();159}160161public static Object[] getObjArray() {162x++;163return new Object[ARR_SIZE];164}165166public static String[] getStringArray() {167x++;168return new String[ARR_SIZE];169}170171public static CharSequence[] getCharSequenceArray() {172x++;173return new String[ARR_SIZE];174}175176}177178179180