-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
8035787: SourcePositions are wrong for Strings concatenated with '+' …
…operator Backport-of: 25642dd
- Loading branch information
Showing
1 changed file
with
121 additions
and
0 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
test/langtools/tools/javac/parser/StringFoldingPosTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright (c) 2020, Google LLC. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
/* | ||
* @test | ||
* @bug 8134007 8035787 | ||
* @summary folded string literals should have correct start and end positions | ||
*/ | ||
|
||
import com.sun.source.tree.CompilationUnitTree; | ||
import com.sun.source.tree.LiteralTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.util.JavacTask; | ||
import com.sun.source.util.SourcePositions; | ||
import com.sun.source.util.TreeScanner; | ||
import com.sun.source.util.Trees; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.Arrays; | ||
|
||
import javax.tools.JavaCompiler; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.SimpleJavaFileObject; | ||
import javax.tools.ToolProvider; | ||
|
||
public class StringFoldingPosTest { | ||
private final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); | ||
|
||
public static void main(String[] args) throws IOException { | ||
StringFoldingPosTest t = new StringFoldingPosTest(); | ||
JavaFileObject source = | ||
t.makeSource( | ||
"C", "class C {String X=\"F\" + \"O\" + \"L\" + \"D\" + \"E\" + \"D\";}"); | ||
t.run(source, "FOLDED", 18, 51); | ||
source = | ||
t.makeSource( | ||
"C", | ||
"class C {String X=(\"F\" + \"O\") + (\"L\" + \"D\") + (\"E\" + \"D\");}"); | ||
t.run(source, "FO", 19, 28); | ||
t.run(source, "LD", 33, 42); | ||
t.run(source, "ED", 47, 56); | ||
} | ||
|
||
private static JavaFileObject makeSource(String name, String code) { | ||
return new SimpleJavaFileObject( | ||
URI.create( | ||
"file:/" + name.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension), | ||
JavaFileObject.Kind.SOURCE) { | ||
@Override | ||
public CharSequence getCharContent(boolean ignoreEncodingErrors) { | ||
return code; | ||
} | ||
}; | ||
} | ||
|
||
private void run( | ||
JavaFileObject source, | ||
String expectedLiteral, | ||
long expectedStartPos, | ||
long expectedEndPos) | ||
throws IOException { | ||
JavacTask ct = | ||
(JavacTask) compiler.getTask(null, null, null, null, null, Arrays.asList(source)); | ||
SourcePositions positions = Trees.instance(ct).getSourcePositions(); | ||
Iterable<? extends CompilationUnitTree> trees = ct.parse(); | ||
boolean[] found = {false}; | ||
for (CompilationUnitTree tree : trees) { | ||
new TreeScanner<Void, Void>() { | ||
@Override | ||
public Void visitLiteral(LiteralTree literal, Void v) { | ||
if (literal.getKind() == Tree.Kind.STRING_LITERAL | ||
&& literal.getValue().equals(expectedLiteral)) { | ||
long startPos = positions.getStartPosition(tree, literal); | ||
long endPos = positions.getEndPosition(tree, literal); | ||
if (startPos != expectedStartPos) { | ||
throw new AssertionError( | ||
"Expected start position " | ||
+ expectedStartPos | ||
+ ", but was " | ||
+ startPos); | ||
} | ||
if (endPos != expectedEndPos) { | ||
throw new AssertionError( | ||
"Expected end position " | ||
+ expectedEndPos | ||
+ ", but was " | ||
+ endPos); | ||
} | ||
found[0] = true; | ||
} | ||
return null; | ||
} | ||
}.scan(trees, null); | ||
} | ||
if (found[0]) { | ||
return; | ||
} | ||
throw new AssertionError("Expected string literal " + expectedLiteral + " not found"); | ||
} | ||
} |
131084c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
Issues