sandbox/java.hints/spi.java.hints/test/unit/src/org/netbeans/spi/java/hints/matching/CopyFinderTest.java
1.1 --- a/sandbox/java.hints/spi.java.hints/test/unit/src/org/netbeans/spi/java/hints/matching/CopyFinderTest.java Mon Dec 19 11:37:36 2016 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,1612 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
1.8 - *
1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 - * Other names may be trademarks of their respective owners.
1.11 - *
1.12 - * The contents of this file are subject to the terms of either the GNU
1.13 - * General Public License Version 2 only ("GPL") or the Common
1.14 - * Development and Distribution License("CDDL") (collectively, the
1.15 - * "License"). You may not use this file except in compliance with the
1.16 - * License. You can obtain a copy of the License at
1.17 - * http://www.netbeans.org/cddl-gplv2.html
1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 - * specific language governing permissions and limitations under the
1.20 - * License. When distributing the software, include this License Header
1.21 - * Notice in each file and include the License file at
1.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 - * particular file as subject to the "Classpath" exception as provided
1.24 - * by Oracle in the GPL Version 2 section of the License file that
1.25 - * accompanied this code. If applicable, add the following below the
1.26 - * License Header, with the fields enclosed by brackets [] replaced by
1.27 - * your own identifying information:
1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
1.29 - *
1.30 - * Contributor(s):
1.31 - *
1.32 - * Portions Copyrighted 2007-2010 Sun Microsystems, Inc.
1.33 - */
1.34 -package org.netbeans.spi.java.hints.matching;
1.35 -
1.36 -import com.sun.source.tree.BlockTree;
1.37 -import com.sun.source.tree.StatementTree;
1.38 -import com.sun.source.tree.Tree;
1.39 -import com.sun.source.tree.VariableTree;
1.40 -import com.sun.source.util.SourcePositions;
1.41 -import com.sun.source.util.TreePath;
1.42 -import com.sun.source.util.TreePathScanner;
1.43 -import java.io.File;
1.44 -import java.util.Arrays;
1.45 -import java.util.Collection;
1.46 -import java.util.Collections;
1.47 -import java.util.EnumSet;
1.48 -import java.util.HashMap;
1.49 -import java.util.HashSet;
1.50 -import java.util.LinkedList;
1.51 -import java.util.List;
1.52 -import java.util.Map;
1.53 -import java.util.Map.Entry;
1.54 -import java.util.Set;
1.55 -import java.util.concurrent.atomic.AtomicBoolean;
1.56 -import javax.lang.model.element.VariableElement;
1.57 -import javax.lang.model.type.TypeMirror;
1.58 -import javax.swing.text.Document;
1.59 -import org.netbeans.api.java.lexer.JavaTokenId;
1.60 -import org.netbeans.api.java.source.CompilationInfo;
1.61 -import org.netbeans.api.java.source.JavaSource;
1.62 -import org.netbeans.api.java.source.JavaSource.Phase;
1.63 -import org.netbeans.api.java.source.SourceUtilsTestUtil;
1.64 -import org.netbeans.api.java.source.TestUtilities;
1.65 -import org.netbeans.api.java.source.TreePathHandle;
1.66 -import org.netbeans.api.java.source.matching.MatchingTestAccessor;
1.67 -import org.netbeans.api.java.source.matching.Pattern;
1.68 -import org.netbeans.api.lexer.Language;
1.69 -import org.netbeans.junit.NbTestCase;
1.70 -import org.netbeans.modules.java.hints.introduce.IntroduceMethodFix;
1.71 -import org.netbeans.modules.java.hints.spiimpl.pm.BulkSearch;
1.72 -import org.netbeans.modules.java.hints.spiimpl.pm.BulkSearch.BulkPattern;
1.73 -import org.netbeans.modules.java.hints.spiimpl.pm.PatternCompiler;
1.74 -import org.netbeans.modules.java.hints.spiimpl.pm.PatternCompilerUtilities;
1.75 -import org.netbeans.modules.java.source.matching.CopyFinder;
1.76 -import org.netbeans.modules.java.source.matching.CopyFinder.Cancel;
1.77 -import org.netbeans.modules.java.source.matching.CopyFinder.Options;
1.78 -import org.netbeans.modules.java.source.matching.CopyFinder.VariableAssignments;
1.79 -import org.openide.cookies.EditorCookie;
1.80 -import org.openide.filesystems.FileObject;
1.81 -import org.openide.filesystems.FileUtil;
1.82 -import org.openide.loaders.DataObject;
1.83 -
1.84 -/**
1.85 - *
1.86 - * @author Jan Lahoda
1.87 - */
1.88 -public class CopyFinderTest extends NbTestCase {
1.89 -
1.90 - public CopyFinderTest(String testName) {
1.91 - super(testName);
1.92 - }
1.93 -
1.94 -// public static TestSuite suite() {
1.95 -// NbTestSuite nb = new NbTestSuite();
1.96 -//
1.97 -// nb.addTest(new CopyFinderTest("testCorrectSite3"));
1.98 -//
1.99 -// return nb;
1.100 -// }
1.101 -
1.102 - @Override
1.103 - protected void setUp() throws Exception {
1.104 - SourceUtilsTestUtil.prepareTest(new String[0], new Object[0]);
1.105 - super.setUp();
1.106 - }
1.107 -
1.108 - public void testSimple1() throws Exception {
1.109 - performTest("package test; public class Test {public void test() {int i = 0; y = i + i; y = i + i;}}", 90 - 22, 95 - 22, 101 - 22, 106 - 22);
1.110 - }
1.111 -
1.112 -// public void testSimple2() throws Exception {
1.113 -// performTest("package test; public class Test {public void test() {int i = 0; y = i + i; y = i + i + i;}}", 90 - 22, 95 - 22, 101 - 22, 106 - 22);
1.114 -// }
1.115 -
1.116 - public void testSimple3() throws Exception {
1.117 - performTest("package test; public class Test {public void test() {int i = System.currentTimeMillis(); y = System.currentTimeMillis();}}", 83 - 22, 109 - 22, 115 - 22, 141 - 22);
1.118 - }
1.119 -
1.120 - public void testSimple4() throws Exception {
1.121 - performTest("package test; import java.util.ArrayList; public class Test {public void test() {Object o = new ArrayList<String>();o = new ArrayList<String>();}}", 114 - 22, 137- 22, 142 - 22, 165 - 22);
1.122 - }
1.123 -
1.124 - public void testSimple5() throws Exception {
1.125 - performTest("package test; public class Test {public void test() {Object o = null; String s = (String) o; s = (String) o; s = (String) null; o = (Object) o;}}", 103 - 22, 113 - 22, 119 - 22, 129 - 22);
1.126 - }
1.127 -
1.128 - public void testSimple6() throws Exception {
1.129 - performTest("package test; public class Test {public void test() {int i = 0; y = i + i; y = i + i;} public void test2() {int i = 0; y = i + i; y = i + i;}}", 90 - 22, 95 - 22, 101 - 22, 106 - 22);
1.130 - }
1.131 -
1.132 - public void testSimple7() throws Exception {
1.133 - performTest("package test; public class Test {public void test() {int i = 0; y = i != 0 ? i + i : i * i; y = i != 0 ? i + i : i * i; y = i != 1 ? i + i : i * i; y = i == 0 ? i + i : i * i; y = i != 0 ? i * i : i * i; y = i != 0 ? i + i : i + i; y = i != 0 ? i + i : i * 1;}}", 90 - 22, 112 - 22, 118 - 22, 140 - 22);
1.134 - }
1.135 -
1.136 - public void testSimple8() throws Exception {
1.137 - performTest("package test; public class Test {public void test() {int i = 0; int y = -i; y = -i; y = +i; y = +y;}}", 94 - 22, 96 - 22, 102 - 22, 104 - 22);
1.138 - }
1.139 -
1.140 - public void testSimple9() throws Exception {
1.141 - performTest("package test; public class Test {public void test() {int i = 0; int y = i *= 9; y = i *= 9; y = i /= 9; y = i *= 8; y = y *= 9;}}", 94 - 22, 100 - 22, 106 - 22, 112 - 22);
1.142 - }
1.143 -
1.144 - public void testSimple10() throws Exception {
1.145 - performTest("package test; public class Test {public void test() {int[] i = null; int y = i[1]; y = i[1]; y = i[y]; y = i[0];}}", 99 - 22, 103 - 22, 109 - 22, 113 - 22);
1.146 - }
1.147 -
1.148 - public void testSimple11() throws Exception {
1.149 - performTest("package test; public class Test {public void test() {int[] i = new int[0]; i = new int[0]; i = new int[1];}}", 85 - 22, 95 - 22, 101 - 22, 111 - 22);
1.150 - }
1.151 -
1.152 - public void testSimple12() throws Exception {
1.153 - performTest("package test; public class Test {public void test() {int[] i = new int[1]; i = new int[1]; i = new int[0];}}", 85 - 22, 95 - 22, 101 - 22, 111 - 22);
1.154 - }
1.155 -
1.156 - public void testSimple13() throws Exception {
1.157 - performTest("package test; public class Test {public void test() {int i = 0; int y = (i); y = (i); y = i;}}", 94 - 22, 97 - 22, 103 - 22, 106 - 22);
1.158 - }
1.159 -
1.160 - public void testSimple14() throws Exception {
1.161 - performTest("package test; public class Test {public void test() {Object o = null; boolean b = o instanceof String; b = o instanceof String; b = o instanceof Object;}}", 104 - 22, 123 - 22, 129 - 22, 148 - 22);
1.162 - }
1.163 -
1.164 - public void testSimple15() throws Exception {
1.165 - performTest("package test; public class Test {private int x = 1; private int y = 1; public void test() {int x = 1; int y = 1;}}", 90 - 22, 91 - 22, 71 - 22, 72 - 22, 121 - 22, 122 - 22, 132 - 22, 133 - 22);
1.166 - }
1.167 -
1.168 - public void testSimple16() throws Exception {
1.169 - performTest("package test; public class Test {public void test(int i) {int y = \"\".length(); test(\"\".length());} }", 88 - 22, 99 - 22, 106 - 22, 117 - 22);
1.170 - }
1.171 -
1.172 - public void testSimple17() throws Exception {
1.173 - performTest("package test; public class Test {public void test2() {int a = test(test(test(1))); a = test(test(test(1))); a = test(test(test(1)));} public int test(int i) {return 0;} }", 94 - 22, 101 - 22, 119 - 22, 126 - 22, 144 - 22, 151 - 22);
1.174 - }
1.175 -
1.176 - public void testMemberSelectAndIdentifierAreSame() throws Exception {
1.177 - performTest("package test; import static java.lang.String.*; public class Test {public void test1() {|String.valueOf(2)|; |valueOf(2)|;} }");
1.178 - }
1.179 -
1.180 - public void testVariables1() throws Exception {
1.181 - performVariablesTest("package test; import static java.lang.String.*; public class Test {public void test1() {String.valueOf(2+4);} }",
1.182 - "java.lang.String.valueOf($1)",
1.183 - new Pair[] {new Pair<String, int[]>("$1", new int[] {134 - 31, 137 - 31})},
1.184 - new Pair[0]);
1.185 - }
1.186 -
1.187 - public void testAssert1() throws Exception {
1.188 - performTest("package test; public class Test {public void test() {int i = 0; |assert i == 1;| |assert i == 1;|}}");
1.189 - }
1.190 -
1.191 - public void testReturn1() throws Exception {
1.192 - performTest("package test; public class Test {public int test1() {|return 1;|} public int test2() {|return 1;|}}");
1.193 - }
1.194 -
1.195 - public void testIf1() throws Exception {
1.196 - performTest("package test; public class Test {public void test() { int i = 0; int j; |if (i == 0) {j = 1;} else {j = 2;}| |if (i == 0) {j = 1;} else {j = 2;}| } }");
1.197 - }
1.198 -
1.199 - public void testExpressionStatement1() throws Exception {
1.200 - performTest("package test; public class Test {public void test() { int i = 0; |i = 1;| |i = 1;| } }");
1.201 - }
1.202 -
1.203 - public void testBlock1() throws Exception {
1.204 - performTest("package test; public class Test {public void test() { int i = 0; |{i = 1;}| |{i = 1;}| } }");
1.205 - }
1.206 -
1.207 - public void testSynchronized1() throws Exception {
1.208 - performTest("package test; public class Test {public void test() { Object o = null; int i = 0; |synchronized (o) {i = 1;}| |synchronized (o) {i = 1;}| } }");
1.209 - }
1.210 -
1.211 -// public void testEnhancedForLoop() throws Exception {
1.212 -// performTest("package test; public class Test {public void test(Iterable<String> i) { |for (String s : i) { System.err.println(); }| |for (String s : i) { System.err.println(); }| }");
1.213 -// }
1.214 -
1.215 -// public void testConstants() throws Exception {
1.216 -// performTest("package test; public class Test {public static final int A = 3; public void test() { int i = |3|; i = |test.Test.A|; } }");
1.217 -// }
1.218 -
1.219 - public void testOverridingImplementing1() throws Exception {
1.220 - performVariablesTest("package test; public class Test implements Runnable { { this.run(); } public void run() { } } }",
1.221 - "$0{java.lang.Runnable}.run()",
1.222 - new Pair[] {new Pair<String, int[]>("$0", new int[] {56, 60})},
1.223 - new Pair[0]);
1.224 - }
1.225 -
1.226 - public void testMemberSelectCCE() throws Exception {
1.227 - //should not throw a CCE
1.228 - //(selected regions are not duplicates)
1.229 - performTest("package test; public class Test {public static class T extends Test { public void test() { |Test.test|(); |System.err.println|(); } } }", false);
1.230 - }
1.231 -
1.232 - public void testLocalVariable() throws Exception {
1.233 - performVariablesTest("package test; public class Test {public void test1() { { int y; y = 1; } int z; { int y; z = 1; } } }",
1.234 - "{ int $1; $1 = 1; }",
1.235 - new Pair[0],
1.236 - new Pair[] {new Pair<String, String>("$1", "y")});
1.237 - }
1.238 -
1.239 - public void testStatementAndSingleBlockStatementAreSame1() throws Exception {
1.240 - performVariablesTest("package test; public class Test {public void test1() { { int x; { x = 1; } } } }",
1.241 - "{ int $1; $1 = 1; }",
1.242 - new Pair[0],
1.243 - new Pair[] {new Pair<String, String>("$1", "x")});
1.244 - }
1.245 -
1.246 - public void testStatementAndSingleBlockStatementAreSame2() throws Exception {
1.247 - performVariablesTest("package test; public class Test {public void test1() { { int x; x = 1; } } }",
1.248 - "{ int $1; { $1 = 1; } }",
1.249 - new Pair[0],
1.250 - new Pair[] {new Pair<String, String>("$1", "x")});
1.251 - }
1.252 -
1.253 - public void testStatementVariables() throws Exception {
1.254 - performVariablesTest("package test; public class Test {public int test1() { if (true) return 1; else return 2; } }",
1.255 - "if ($1) $2; else $3;",
1.256 - new Pair[] {
1.257 - new Pair<String, int[]>("$1", new int[] {89 - 31, 93 - 31}),
1.258 - new Pair<String, int[]>("$2", new int[] {95 - 31, 104 - 31}),
1.259 - new Pair<String, int[]>("$3", new int[] {110 - 31, 119 - 31})
1.260 - },
1.261 - new Pair[0]);
1.262 - }
1.263 -
1.264 - public void testThrowStatement() throws Exception {
1.265 - performVariablesTest("package test; public class Test {public void test() { throw new NullPointerException(); throw new IllegalStateException();} }",
1.266 - "throw new NullPointerException()",
1.267 - new Pair[0],
1.268 - new Pair[0]);
1.269 - }
1.270 -
1.271 - public void testMultiStatementVariables1() throws Exception {
1.272 - performVariablesTest("package test; public class Test { public int test1() { System.err.println(); System.err.println(); int i = 3; System.err.println(i); System.err.println(i); return i; } }",
1.273 - "{ $s1$; int $i = 3; $s2$; return $i; }",
1.274 - new Pair[0],
1.275 - new Pair[] {
1.276 - new Pair<String, int[]>("$s1$", new int[] {55, 76, 77, 98}),
1.277 - new Pair<String, int[]>("$s2$", new int[] {110, 132, 133, 155})
1.278 - },
1.279 - new Pair[] {new Pair<String, String>("$i", "i")});
1.280 - }
1.281 -
1.282 - public void testMultiStatementVariables2() throws Exception {
1.283 - performVariablesTest("package test; public class Test { public int test1() { int i = 3; return i; } }",
1.284 - "{ $s1$; int $i = 3; $s2$; return $i; }",
1.285 - new Pair[0],
1.286 - new Pair[] {
1.287 - new Pair<String, int[]>("$s1$", new int[] {}),
1.288 - new Pair<String, int[]>("$s2$", new int[] {}),
1.289 - },
1.290 - new Pair[] {new Pair<String, String>("$i", "i")});
1.291 - }
1.292 -
1.293 - public void testMultiStatementVariablesAndBlocks1() throws Exception {
1.294 - performVariablesTest("package test; public class Test { public void test1() { if (true) System.err.println(); } }",
1.295 - "if ($c) {$s1$; System.err.println(); $s2$; }",
1.296 - new Pair[] {new Pair<String, int[]>("$c", new int[] {60, 64})},
1.297 - new Pair[] {
1.298 - new Pair<String, int[]>("$s1$", new int[] {}),
1.299 - new Pair<String, int[]>("$s2$", new int[] {}),
1.300 - },
1.301 - new Pair[0]);
1.302 - }
1.303 -
1.304 - public void testMultiStatementVariablesAndBlocks2() throws Exception {
1.305 - performVariablesTest("package test; public class Test { public void test1() { if (true) System.err.println(); } }",
1.306 - "if ($c) {$s1$; System.err.println(); }",
1.307 - new Pair[] {new Pair<String, int[]>("$c", new int[] {60, 64})},
1.308 - new Pair[] {
1.309 - new Pair<String, int[]>("$s1$", new int[] {}),
1.310 - },
1.311 - new Pair[0]);
1.312 - }
1.313 -
1.314 - public void testMultiStatementVariablesAndBlocks3() throws Exception {
1.315 - performVariablesTest("package test; public class Test { public void test1() { if (true) System.err.println(); } }",
1.316 - "if ($c) {System.err.println(); $s2$; }",
1.317 - new Pair[] {new Pair<String, int[]>("$c", new int[] {60, 64})},
1.318 - new Pair[] {
1.319 - new Pair<String, int[]>("$s2$", new int[] {}),
1.320 - },
1.321 - new Pair[0]);
1.322 - }
1.323 -
1.324 - public void testMultiStatementVariablesAndBlocks4() throws Exception {
1.325 - performVariablesTest("package test; public class Test { public void test1() { if (true) System.err.println(); } }",
1.326 - "if ($c) { $s$; }",
1.327 - new Pair[] {new Pair<String, int[]>("$c", new int[] {60, 64})},
1.328 - new Pair[] {
1.329 - new Pair<String, int[]>("$s$", new int[] {66, 87}),
1.330 - },
1.331 - new Pair[0]);
1.332 - }
1.333 -
1.334 - public void testVariableVerification() throws Exception {
1.335 - performVariablesTest("package test; public class Test { public void test1(String[] a, String[] b) { for (int c = 0; c < a.length; c++) { String s = b[c]; System.err.println(s); } } }",
1.336 - "for(int $i = 0; $i < $array.length; $i++) { $T $var = $array[$i]; $stmts$; }",
1.337 - new Pair[0],
1.338 - new Pair[0],
1.339 - new Pair[0],
1.340 - true);
1.341 - }
1.342 -
1.343 - public void testFor() throws Exception {
1.344 - performVariablesTest("package test; public class Test { public void test1(String[] a) { for (int c = 0; c < a.length; c++) { String s = a[c]; System.err.println(s); } } }",
1.345 - "for(int $i = 0; $i < $array.length; $i++) { $T $var = $array[$i]; $stmts$; }",
1.346 - new Pair[] {
1.347 - new Pair<String, int[]>("$array", new int[] {117 - 31, 118 - 31}),
1.348 - new Pair<String, int[]>("$T", new int[] {134 - 31, 140 - 31}),
1.349 - },
1.350 - new Pair[] {
1.351 - new Pair<String, int[]>("$stmts$", new int[] {151 - 31, 173 - 31}),
1.352 - },
1.353 - new Pair[] {
1.354 - new Pair<String, String>("$i", "c"),
1.355 - new Pair<String, String>("$var", "s"),
1.356 - });
1.357 - }
1.358 -
1.359 - public void testEnhancedFor() throws Exception {
1.360 - performVariablesTest("package test; public class Test { public void test1(String[] a) { for (String s : a) { System.err.println(s); } } }",
1.361 - "for($T $var : $array) { $stmts$; }",
1.362 - new Pair[] {
1.363 - new Pair<String, int[]>("$array", new int[] {113 - 31, 114 - 31}),
1.364 - new Pair<String, int[]>("$T", new int[] {102 - 31, 108 - 31}),
1.365 - },
1.366 - new Pair[] {
1.367 - new Pair<String, int[]>("$stmts$", new int[] {118 - 31, 140 - 31}),
1.368 - },
1.369 - new Pair[] {
1.370 - new Pair<String, String>("$var", "s"),
1.371 - });
1.372 - }
1.373 -
1.374 - public void testWhile() throws Exception {
1.375 - performVariablesTest("package test; public class Test { public void test1(String[] a) { int c = 0; while (c < a.length) { String s = a[c]; System.err.println(s); c++; } } }",
1.376 - "while ($i < $array.length) { $T $var = $array[$i]; $stmts$; $i++; }",
1.377 - new Pair[] {
1.378 - new Pair<String, int[]>("$array", new int[] {120 - 31, 121 - 31}),
1.379 - new Pair<String, int[]>("$T", new int[] {132 - 31, 138 - 31}),
1.380 - new Pair<String, int[]>("$i", new int[] {116 - 31, 117 - 31}),
1.381 - },
1.382 - new Pair[] {
1.383 - new Pair<String, int[]>("$stmts$", new int[] {149 - 31, 171 - 31}),
1.384 - },
1.385 - new Pair[] {
1.386 - new Pair<String, String>("$var", "s"),
1.387 - });
1.388 - }
1.389 -
1.390 - public void testDoWhile() throws Exception {
1.391 - performVariablesTest("package test; public class Test { public void test1(String[] a) { int c = 0; do { String s = a[c]; System.err.println(s); c++; } while (c < a.length); } }",
1.392 - "do { $T $var = $array[$i]; $stmts$; $i++; } while ($i < $array.length);",
1.393 - new Pair[] {
1.394 - new Pair<String, int[]>("$array", new int[] {124 - 31, 125 - 31}),
1.395 - new Pair<String, int[]>("$T", new int[] {113 - 31, 119 - 31}),
1.396 - new Pair<String, int[]>("$i", new int[] {126 - 31, 127 - 31}),
1.397 - },
1.398 - new Pair[] {
1.399 - new Pair<String, int[]>("$stmts$", new int[] {130 - 31, 152 - 31}),
1.400 - },
1.401 - new Pair[] {
1.402 - new Pair<String, String>("$var", "s"),
1.403 - });
1.404 - }
1.405 -
1.406 - public void testArrayType() throws Exception {
1.407 - performVariablesTest("package test; public class Test { public void test1() { int[][] a; } }",
1.408 - "$T[]",
1.409 - new Pair[] {
1.410 - new Pair<String, int[]>("$T", new int[] {87 - 31, /*92*//*XXX:*/94 - 31}),
1.411 - },
1.412 - new Pair[0],
1.413 - new Pair[0]);
1.414 - }
1.415 -
1.416 - public void testSemiMatchPackage() throws Exception {
1.417 - performVariablesTest("package test; import javax.lang.model.type.TypeMirror; public class Test { }",
1.418 - "$T{java.lang.Object}.type",
1.419 - new Pair[0],
1.420 - new Pair[0],
1.421 - new Pair[0],
1.422 - true);
1.423 - }
1.424 -
1.425 - public void testNullType() throws Exception {
1.426 - performVariablesTest("package javax.lang.model.type; public class Test { }",
1.427 - "$T{java.lang.Object}.type",
1.428 - new Pair[0],
1.429 - new Pair[0],
1.430 - new Pair[0],
1.431 - true);
1.432 - }
1.433 -
1.434 - public void testTryCatch() throws Exception {
1.435 - performVariablesTest("package test; import java.io.*; public class Test { public void test() { InputStream ins = null; try { ins = new FileInputStream(\"\"); } catch (IOException e) { e.printStackTrace(); } finally {ins.close();} } }",
1.436 - "try {$stmts$;} catch (java.io.IOException $e) {$e.printStackTrace();} finally {$finally$;}",
1.437 - new Pair[] {
1.438 - new Pair<String, int[]>("$e", new int[] {176 - 31 - 2, 189 - 31 - 2}),
1.439 - },
1.440 - new Pair[] {
1.441 - new Pair<String, int[]>("$stmts$", new int[] {134 - 31, 166 - 31 - 2}),
1.442 - new Pair<String, int[]>("$finally$", new int[] {225 - 31 - 2, 237 - 31 - 2}),
1.443 - },
1.444 - new Pair[] {
1.445 - new Pair<String, String>("$e", "e"),
1.446 - });
1.447 - }
1.448 -
1.449 - public void testMultiParameters1() throws Exception {
1.450 - performVariablesTest("package test; public class Test { { java.util.Arrays.asList(\"a\", \"b\", \"c\"); }",
1.451 - "java.util.Arrays.asList($1$)",
1.452 - new Pair[] {
1.453 - },
1.454 - new Pair[] {
1.455 - new Pair<String, int[]>("$1$", new int[] {60, 63, 65, 68, 70, 73}),
1.456 - },
1.457 - new Pair[] {
1.458 - });
1.459 - }
1.460 -
1.461 - public void testMultiParameters2() throws Exception {
1.462 - performVariablesTest("package test; public class Test { { java.util.Arrays.asList(new String(\"a\"), \"b\", \"c\"); }",
1.463 - "java.util.Arrays.asList(new String(\"a\"), $1$)",
1.464 - new Pair[] {
1.465 - },
1.466 - new Pair[] {
1.467 - new Pair<String, int[]>("$1$", new int[] {77, 80, 82, 85}),
1.468 - },
1.469 - new Pair[] {
1.470 - });
1.471 - }
1.472 -
1.473 - public void testMultiParameters3() throws Exception {
1.474 - performVariablesTest("package test; public class Test { { java.util.Arrays.asList(); }",
1.475 - "java.util.Arrays.asList($1$)",
1.476 - new Pair[] {
1.477 - },
1.478 - new Pair[] {
1.479 - new Pair<String, int[]>("$1$", new int[] {}),
1.480 - },
1.481 - new Pair[] {
1.482 - });
1.483 - }
1.484 -
1.485 - public void testTypeParameters() throws Exception {
1.486 - performVariablesTest("package test; public class Test { { java.util.Arrays.<String>asList(\"a\", \"b\"); }",
1.487 - "java.util.Arrays.<$1>asList($1$)",
1.488 - new Pair[] {
1.489 - new Pair<String, int[]>("$1", new int[] {85 - 31, 91 - 31}),
1.490 - },
1.491 - new Pair[] {
1.492 - },
1.493 - new Pair[] {
1.494 - });
1.495 - }
1.496 -
1.497 - public void testModifiers() throws Exception {
1.498 - performVariablesTest("package test; public class Test { private String s; }",
1.499 - "$mods$ java.lang.String $name;",
1.500 - new Pair[] {
1.501 - new Pair<String, int[]>("$name", new int[] {65 - 31, 82 - 31}),
1.502 - new Pair<String, int[]>("$mods$", new int[] {65 - 31, 72 - 31}), //XXX: shouldn't this be a multi-variable?
1.503 - },
1.504 - new Pair[] {
1.505 - },
1.506 - new Pair[] {
1.507 - new Pair<String, String>("$name", "s"),
1.508 - });
1.509 - }
1.510 -
1.511 - public void testVariableIsFullPattern1() throws Exception {
1.512 - performVariablesTest("package test; public class Test { private int a; {System.err.println(a);} }",
1.513 - "$0{int}",
1.514 - new Pair[] {
1.515 - new Pair<String, int[]>("$0", new int[] {100 - 31, 101 - 31}),
1.516 - },
1.517 - new Pair[] {
1.518 - },
1.519 - new Pair[] {
1.520 - });
1.521 - }
1.522 -
1.523 - public void testVariableIsFullPattern2() throws Exception {
1.524 - performVariablesTest("package test; public class Test { private int a; {System.err.println(a);} }",
1.525 - "$0{int}",
1.526 - new Pair[] {
1.527 - new Pair<String, int[]>("$0", new int[] {100 - 31, 101 - 31}),
1.528 - },
1.529 - new Pair[] {
1.530 - },
1.531 - new Pair[] {
1.532 - },
1.533 - false,
1.534 - true);
1.535 - }
1.536 -
1.537 - public void testNoCCEForVariableName() throws Exception {
1.538 - performVariablesTest("package test; public class Test { { int[] arr = null; int a; arr[a] = 0;} }",
1.539 - "int $a; $a = 0;",
1.540 - new Pair[] {
1.541 - },
1.542 - new Pair[] {
1.543 - },
1.544 - new Pair[] {
1.545 - },
1.546 - true,
1.547 - true);
1.548 - }
1.549 -
1.550 - public void testVerifySameTrees1() throws Exception {
1.551 - performVariablesTest("package test; public class Test { { if (true) { System.err.println(); } else { System.err.println(); System.err.println(); } } }",
1.552 - "if ($c) $s; else $s;",
1.553 - new Pair[] {
1.554 - },
1.555 - new Pair[] {
1.556 - },
1.557 - new Pair[] {
1.558 - },
1.559 - true,
1.560 - true);
1.561 - }
1.562 -
1.563 - public void testVerifySameTreesMultiVariables1() throws Exception {
1.564 - performVariablesTest("package test; public class Test { { if (true) { System.err.println(); System.err.println(); } else { System.err.println(); System.err.println(); System.err.println(); } } }",
1.565 - "if ($c) { $s$;} else { $s$; }",
1.566 - new Pair[] {
1.567 - },
1.568 - new Pair[] {
1.569 - },
1.570 - new Pair[] {
1.571 - },
1.572 - true,
1.573 - true);
1.574 - }
1.575 -
1.576 - public void testVerifySameTreesMultiVariables2() throws Exception {
1.577 - performVariablesTest("package test; public class Test { { if (true) { System.err.println(1); System.err.println(); } else System.err.println(1); } }",
1.578 - "if ($c) { System.err.println(1); $s2$; } else { System.err.println(1); $s2$; }",
1.579 - new Pair[] {
1.580 - },
1.581 - new Pair[] {
1.582 - },
1.583 - new Pair[] {
1.584 - },
1.585 - true,
1.586 - true);
1.587 - }
1.588 -
1.589 - public void testVerifySameTreesMultiVariables3() throws Exception {
1.590 - performVariablesTest("package test; public class Test { { if (true) { System.err.println(); System.err.println(1); } else System.err.println(1); } }",
1.591 - "if ($c) { $s1$; System.err.println(1); } else { $s1$; System.err.println(1); }",
1.592 - new Pair[] {
1.593 - },
1.594 - new Pair[] {
1.595 - },
1.596 - new Pair[] {
1.597 - },
1.598 - true,
1.599 - true);
1.600 - }
1.601 -
1.602 - public void XtestVerifySameTreesMultiVariables4() throws Exception {
1.603 - performVariablesTest("package test; public class Test { { if (true) { System.err.println(); System.err.println(1); System.err.println(); } else System.err.println(1); } }",
1.604 - "if ($c) { $s1$; System.err.println(1); $s2$; } else { $s1$; System.err.println(1); $s2$; }",
1.605 - new Pair[] {
1.606 - },
1.607 - new Pair[] {
1.608 - },
1.609 - new Pair[] {
1.610 - },
1.611 - true,
1.612 - true);
1.613 - }
1.614 -
1.615 - public void testVerifySameTreesMultiVariables5() throws Exception {
1.616 - performVariablesTest("package test; public class Test { { if (true) { System.err.println(1); } else System.err.println(2); } }",
1.617 - "if ($c) { $s$; } else { $s$; }",
1.618 - new Pair[] {
1.619 - },
1.620 - new Pair[] {
1.621 - },
1.622 - new Pair[] {
1.623 - },
1.624 - true,
1.625 - true);
1.626 - }
1.627 -
1.628 - public void testSimpleRemapping1() throws Exception {
1.629 - performRemappingTest("package test;\n" +
1.630 - "public class Test {\n" +
1.631 - " void t1() {\n" +
1.632 - " int i = 0;\n" +
1.633 - " |System.err.println(i);|\n" +
1.634 - " }\n" +
1.635 - " void t2() {\n" +
1.636 - " int a = 0;\n" +
1.637 - " |System.err.println(a);|\n" +
1.638 - " }\n" +
1.639 - "}\n",
1.640 - "i",
1.641 - Options.ALLOW_REMAP_VARIABLE_TO_EXPRESSION);
1.642 - }
1.643 -
1.644 - public void testSimpleRemapping2() throws Exception {
1.645 - performRemappingTest("package test;\n" +
1.646 - "public class Test {\n" +
1.647 - " void t1() {\n" +
1.648 - " int i = 0;\n" +
1.649 - " |System.err.println(i);\n" +
1.650 - " int i2 = 0;\n" +
1.651 - " System.err.println(i2);|\n" +
1.652 - " }\n" +
1.653 - " void t2() {\n" +
1.654 - " int a = 0;\n" +
1.655 - " |System.err.println(a);\n" +
1.656 - " int a2 = 0;\n" +
1.657 - " System.err.println(a2);|\n" +
1.658 - " }\n" +
1.659 - "}\n",
1.660 - "i",
1.661 - Options.ALLOW_REMAP_VARIABLE_TO_EXPRESSION);
1.662 - }
1.663 -
1.664 - public void testSimpleRemapping3() throws Exception {
1.665 - performRemappingTest("package test;\n" +
1.666 - "public class Test {\n" +
1.667 - " void t1() {\n" +
1.668 - " |int i = 0;\n" +
1.669 - " System.err.println(i);\n" +
1.670 - " int i2 = 0;\n" +
1.671 - " System.err.println(i2);|\n" +
1.672 - " }\n" +
1.673 - " void t2() {\n" +
1.674 - " |int a = 0;\n" +
1.675 - " System.err.println(a);\n" +
1.676 - " int a2 = 0;\n" +
1.677 - " System.err.println(a2);|\n" +
1.678 - " }\n" +
1.679 - "}\n",
1.680 - "",
1.681 - Options.ALLOW_REMAP_VARIABLE_TO_EXPRESSION);
1.682 - }
1.683 -
1.684 - public void testSimpleRemapping4() throws Exception {
1.685 - performRemappingTest("package test;\n" +
1.686 - "public class Test {\n" +
1.687 - " void t1() {\n" +
1.688 - " int i = 0;\n" +
1.689 - " |System.err.println(i);|\n" +
1.690 - " }\n" +
1.691 - " void t2() {\n" +
1.692 - " int[] a = {0};\n" +
1.693 - " |System.err.println(a[0]);|\n" +
1.694 - " }\n" +
1.695 - "}\n",
1.696 - "i",
1.697 - Options.ALLOW_REMAP_VARIABLE_TO_EXPRESSION);
1.698 - }
1.699 -
1.700 - public void testPreventRemapOnExpressions1() throws Exception {
1.701 - performRemappingTest("package test;\n" +
1.702 - "public class Test {\n" +
1.703 - " void t1() {\n" +
1.704 - " Throwable t = null;\n" +
1.705 - " |System.err.println(t);|\n" +
1.706 - " }\n" +
1.707 - " void t2() {\n" +
1.708 - " Throwable t = null;\n" +
1.709 - " |System.err.println(t.getCause());|\n" +
1.710 - " }\n" +
1.711 - "}\n",
1.712 - "t",
1.713 - Options.ALLOW_REMAP_VARIABLE_TO_EXPRESSION);
1.714 - }
1.715 -
1.716 - public void testPreventRemapOnExpressions2() throws Exception {
1.717 - performRemappingTest("package test;\n" +
1.718 - "public class Test {\n" +
1.719 - " void t1() {\n" +
1.720 - " Throwable t = null;\n" +
1.721 - " |System.err.println(t);|\n" +
1.722 - " }\n" +
1.723 - " void t2() {\n" +
1.724 - " Throwable t = null;\n" +
1.725 - " System.err.println(t.getCause());\n" +
1.726 - " }\n" +
1.727 - "}\n",
1.728 - "t");
1.729 - }
1.730 -
1.731 - public void testVariableMemberSelect() throws Exception {
1.732 - performVariablesTest("package test; public class Test {public void test(String str) { str.length(); str.length(); } public void test1(String str) { str.length(); str.isEmpty(); } }",
1.733 - "{ $str.$method(); $str.$method(); }",
1.734 - new Pair[0],
1.735 - new Pair[] {new Pair<String, String>("$method", "length")});
1.736 - }
1.737 -
1.738 - public void testCorrectSite1() throws Exception {
1.739 - performVariablesTest("package test; public class Test { public void test(Object o) { o.wait(); } }",
1.740 - "$s{java.util.concurrent.locks.Condition}.wait()",
1.741 - new Pair[0],
1.742 - new Pair[0],
1.743 - new Pair[0],
1.744 - true);
1.745 - }
1.746 -
1.747 - public void testCorrectSite2() throws Exception {
1.748 - performVariablesTest("package test; public class Test { public void test(Object o) { wait(); } }",
1.749 - "$s{java.util.concurrent.locks.Condition}.wait()",
1.750 - new Pair[0],
1.751 - new Pair[0],
1.752 - new Pair[0],
1.753 - true);
1.754 - }
1.755 -
1.756 - public void testCorrectSite3() throws Exception {
1.757 - performVariablesTest("package test; public abstract class Test implements java.util.concurrent.locks.Condition { public void test() { new Runnable() { public void run() { wait(); } } } }",
1.758 - "$0{java.util.concurrent.locks.Condition}.wait()",
1.759 - new Pair[0],// {new Pair<String, int[]>("$s", new int[] {-1, -1})},
1.760 - new Pair[0],
1.761 - new Pair[0]);
1.762 - }
1.763 -
1.764 - public void testCorrectSite4() throws Exception {
1.765 - performVariablesTest("package test; public class Test { public void test() { foo.stop(); } }",
1.766 - "$0{java.lang.Thread}.stop()",
1.767 - new Pair[0],
1.768 - new Pair[0],
1.769 - new Pair[0],
1.770 - true);
1.771 - }
1.772 -
1.773 - public void testDotClassForSameClass() throws Exception {
1.774 - performTest("package test; public class Test { {Class c = |Test.class|; c = |Test.class|; c = String.class; } }");
1.775 - }
1.776 -
1.777 - public void testTryCatchVariable() throws Exception {
1.778 - performVariablesTest("package test; public class Test { { try { throw new java.io.IOException(); } catch (java.io.IOException ex) { } } }",
1.779 - "try { $stmts$; } catch $catches$",
1.780 - new Pair[] {
1.781 - },
1.782 - new Pair[] {
1.783 - new Pair<String, int[]>("$stmts$", new int[] {42, 74}),
1.784 - new Pair<String, int[]>("$catches$", new int[] {77, 111}),
1.785 - },
1.786 - new Pair[] {
1.787 - },
1.788 - false,
1.789 - true);
1.790 - }
1.791 -
1.792 - public void testMatchInterfaceNoFQN() throws Exception {
1.793 - performTest("package test; import java.util.*; public class Test { public void test() { |List| l1; |java.util.List| l2;} }");
1.794 - }
1.795 -
1.796 - public void testUnresolvableNonMatchingConstraint() throws Exception {
1.797 - performVariablesTest("package test; public class Test { private Object a; {System.err.println(a);} }",
1.798 - "System.err.println($v{does.not.Exist}",
1.799 - new Pair[0],
1.800 - new Pair[0],
1.801 - new Pair[0],
1.802 - true);
1.803 - }
1.804 -
1.805 - public void testIndexOutOfBoundsInMultiList() throws Exception {
1.806 - performVariablesTest("package test;" +
1.807 - "public class Test {" +
1.808 - " public void test() {" +
1.809 - " int i = 0;" +
1.810 - " int j = 0;" +
1.811 - " i++;" +
1.812 - " j++;" +
1.813 - " }" +
1.814 - "}",
1.815 - "{$type $i = $init; $stms$; $i++;}",
1.816 - new Pair[0],
1.817 - new Pair[0],
1.818 - new Pair[0],
1.819 - true,
1.820 - false);
1.821 - }
1.822 -
1.823 - public void testCorrectSite192812() throws Exception {
1.824 - performVariablesTest("package test; public class Test { private int i; public void test(Test t) { t.i = i - 10; } }",
1.825 - "$t = $t - $v",
1.826 - new Pair[0],
1.827 - new Pair[0],
1.828 - new Pair[0],
1.829 - true,
1.830 - true);
1.831 - }
1.832 -
1.833 - public void testCorrectSite183367() throws Exception {
1.834 - performVariablesTest("package test; public class Test { public void test(java.util.List l) { l.subList(0, 0).remove(0); } }",
1.835 - "$l{java.util.Collection}.remove($o{java.lang.Object})",
1.836 - new Pair[0],
1.837 - new Pair[0],
1.838 - new Pair[0],
1.839 - true,
1.840 - true);
1.841 - }
1.842 -
1.843 - public void testDisableVariablesWhenVerifyingDuplicates1() throws Exception {
1.844 - performVariablesTest("package test; public class Test { public void test() { int $i = 1, $j = 2; int k = $i + $i; } }",
1.845 - "$i + $i",
1.846 - new Pair[] {new Pair<String, int[]>("$i", new int[] {83, 85})},
1.847 - new Pair[0],
1.848 - new Pair[0],
1.849 - false,
1.850 - true);
1.851 - }
1.852 -
1.853 - public void testDisableVariablesWhenVerifyingDuplicates2() throws Exception {
1.854 - performVariablesTest("package test; public class Test { public void test() { int $i = 1, $j = 2; int k = $i + $i; } }",
1.855 - "$i + $i",
1.856 - new Pair[] {new Pair<String, int[]>("$i", new int[] {83, 85})},
1.857 - new Pair[0],
1.858 - new Pair[0],
1.859 - false,
1.860 - false);
1.861 - }
1.862 -
1.863 - public void testMethodMatchingMoreParams() throws Exception {
1.864 - performVariablesTest("package test; public class Test {public void test(String s1, String s2) { } }",
1.865 - "public void test($params$) { }",
1.866 - new Pair[0],
1.867 - new Pair[] {new Pair<String, int[]>("$params$", new int[] {50, 59, 61, 70})},
1.868 - new Pair[0],
1.869 - false,
1.870 - true);
1.871 - }
1.872 -
1.873 - public void testLambdaInput1() throws Exception {
1.874 - performVariablesTest("package test; public class Test {public void test() { new java.io.FilenameFilter() { public boolean accept(File dir, String name) { } }; } }",
1.875 - "new $type() {public $retType $name($params$) { $body$; } }",
1.876 - new Pair[0],
1.877 - new Pair[] {new Pair<String, int[]>("$params$", new int[] { 107, 115, 117, 128 })},
1.878 - new Pair[] {new Pair<String, String>("$name", "accept")},
1.879 - false,
1.880 - false);
1.881 - }
1.882 -
1.883 - public void testLambdaInput2() throws Exception {
1.884 - performVariablesTest("package test; public class Test {public void test() { new java.io.FilenameFilter() { public boolean accept(File dir, String name) { } }; } }",
1.885 - "new $type() { $mods$ $retType $name($params$) { $body$; } }",
1.886 - new Pair[0],
1.887 - new Pair[] {new Pair<String, int[]>("$params$", new int[] { 107, 115, 117, 128 })},
1.888 - new Pair[] {new Pair<String, String>("$name", "accept")},
1.889 - false,
1.890 - true);
1.891 - }
1.892 -
1.893 - public void testSwitch1() throws Exception {
1.894 - performVariablesTest("package test;\n" +
1.895 - "public class Test {\n" +
1.896 - " {\n" +
1.897 - " E e = null;\n" +
1.898 - " switch (e) {\n" +
1.899 - " case A: System.err.println(1); break;\n" +
1.900 - " case D: System.err.println(2); break;\n" +
1.901 - " case E: System.err.println(3); break;\n" +
1.902 - " }\n" +
1.903 - " }\n" +
1.904 - " public enum E {A, B, C, D, E, F;}\n" +
1.905 - "}\n",
1.906 - "switch ($0{test.Test.E}) { case $c1$ case D: $stmts$; case $c2$ }",
1.907 - new Pair[] {new Pair<String, int[]>("$0", new int[] {79, 80})},
1.908 - new Pair[] {
1.909 - new Pair<String, int[]>("$stmts$", new int[] { 156, 178, 179, 185 }),
1.910 - new Pair<String, int[]>("$c1$", new int[] { 97, 134 }),
1.911 - new Pair<String, int[]>("$c2$", new int[] { 199, 236 }),
1.912 - },
1.913 - new Pair[0],
1.914 - false,
1.915 - false);
1.916 - }
1.917 -
1.918 - public void testWildcard1() throws Exception {
1.919 - performTest("package test; import java.util.*; public class Test { public void test() { |List<?>| l1; |List<?>| l2;} }");
1.920 - }
1.921 -
1.922 - public void testWildcard2() throws Exception {
1.923 - performTest("package test; import java.util.*; public class Test { public void test() { |List<? extends String>| l1; |List<? extends String>| l2;} }");
1.924 - }
1.925 -
1.926 - public void testWildcard3() throws Exception {
1.927 - performTest("package test; import java.util.*; public class Test { public void test() { |List<? super String>| l1; |List<? super String>| l2;} }");
1.928 - }
1.929 -
1.930 - public void testSingleVariableStrict() throws Exception {
1.931 - performVariablesTest("package test; public class Test { public void test() { if (true) System.err.println(1); } }",
1.932 - "if ($c) $then; else $else;",
1.933 - new Pair[0],
1.934 - new Pair[0],
1.935 - new Pair[0],
1.936 - true,
1.937 - true);
1.938 - }
1.939 -
1.940 - public void testMultiVariableZeroOrOne1() throws Exception {
1.941 - performVariablesTest("package test; public class Test { public void test() { if (true) System.err.println(1); } }",
1.942 - "if ($c) $then; else $else$;",
1.943 - new Pair[] {new Pair<String, int[]>("$c", new int[] {59, 63}),
1.944 - new Pair<String, int[]>("$then", new int[] {65, 87})},
1.945 - new Pair[0],
1.946 - new Pair[0],
1.947 - false,
1.948 - true);
1.949 - }
1.950 -
1.951 - public void testMultiVariableZeroOrOne2() throws Exception {
1.952 - performVariablesTest("package test; public class Test { public void test() { if (true) System.err.println(1); else System.err.println(2); } }",
1.953 - "if ($c) $then; else $else$;",
1.954 - new Pair[] {new Pair<String, int[]>("$c", new int[] {59, 63}),
1.955 - new Pair<String, int[]>("$then", new int[] {65, 87}),
1.956 - new Pair<String, int[]>("$else$", new int[] {93, 115})},
1.957 - new Pair[0],
1.958 - new Pair[0],
1.959 - false,
1.960 - true);
1.961 - }
1.962 -
1.963 - public void testNonResolvableType() throws Exception {
1.964 - performVariablesTest("package test; public class Test { { java.io.File f = null; boolean b = f.isDirectory(); } }",
1.965 - "$1{can.not.Resolve}.$m($args$)",
1.966 - new Pair[0],
1.967 - new Pair[0],
1.968 - new Pair[0],
1.969 - true,
1.970 - true);
1.971 - }
1.972 -
1.973 - public void testTryWithResources() throws Exception {
1.974 - performVariablesTest("package test; public class Test { { try (java.io.InputStream in = null) { System.err.println(1); } } }",
1.975 - "try ($resources$) {$body$;}",
1.976 - new Pair[] {
1.977 - },
1.978 - new Pair[] {
1.979 - new Pair<String, int[]>("$resources$", new int[] {41, 70}),
1.980 - new Pair<String, int[]>("$body$", new int[] {74, 96}),
1.981 - },
1.982 - new Pair[] {
1.983 - },
1.984 - false,
1.985 - true);
1.986 - }
1.987 -
1.988 - public void testIgnoreOtherKind() throws Exception {
1.989 - performVariablesTest("package test; public class Test { private java.util.Collection<String> x() { return java.util.Collections.emptySet(); } } }",
1.990 - "$i{java.lang.Class}",
1.991 - new Pair[] {
1.992 - },
1.993 - new Pair[] {
1.994 - },
1.995 - new Pair[] {
1.996 - },
1.997 - true,
1.998 - true);
1.999 - }
1.1000 -
1.1001 - public void testSearchPackageClause() throws Exception {
1.1002 - performVariablesTest("package test.a; public class Test { }",
1.1003 - "test.$1",
1.1004 - new Pair[] {
1.1005 - },
1.1006 - new Pair[] {
1.1007 - },
1.1008 - new Pair[] {
1.1009 - new Pair<String, String>("$1", "a"),
1.1010 - },
1.1011 - false,
1.1012 - true);
1.1013 - }
1.1014 -
1.1015 - public void testPackageImport() throws Exception {
1.1016 - performVariablesTest("package test; import java.util.*; public class Test { }",
1.1017 - "java.$1",
1.1018 - new Pair[] {
1.1019 - },
1.1020 - new Pair[] {
1.1021 - },
1.1022 - new Pair[] {
1.1023 - new Pair<String, String>("$1", "util"),
1.1024 - },
1.1025 - false,
1.1026 - true);
1.1027 - }
1.1028 -
1.1029 - public void testSubclassMatching() throws Exception {
1.1030 - performVariablesTest("package test; import java.util.*; public abstract class Test { Map.Entry e; }",
1.1031 - "java.util.Map.$1",
1.1032 - new Pair[] {
1.1033 - },
1.1034 - new Pair[] {
1.1035 - },
1.1036 - new Pair[] {
1.1037 - new Pair<String, String>("$1", "Entry"),
1.1038 - },
1.1039 - false,
1.1040 - true);
1.1041 - }
1.1042 -
1.1043 - public void testMethodTypeParameters1() throws Exception {
1.1044 - performVariablesTest("package test; public class Test { private void t() { } }",
1.1045 - "$mods$ <$tp$> $ret $name($args$) { $body$; }",
1.1046 - new Pair[] {
1.1047 - new Pair<String, int[]>("$ret", new int[] {42, 46}),
1.1048 - new Pair<String, int[]>("$mods$", new int[] {34, 41}),
1.1049 - },
1.1050 - new Pair[] {
1.1051 - new Pair<String, int[]>("$tp$", new int[] {}),
1.1052 - new Pair<String, int[]>("$args$", new int[] {}),
1.1053 - new Pair<String, int[]>("$body$", new int[] {}),
1.1054 - },
1.1055 - new Pair[] {
1.1056 - new Pair<String, String>("$name", "t")
1.1057 - },
1.1058 - false,
1.1059 - false);
1.1060 - }
1.1061 -
1.1062 - public void testMethodTypeParameters2() throws Exception {
1.1063 - performVariablesTest("package test; public class Test { private <A, B> String aa(int a, int b) { a = b; b = a;} }",
1.1064 - "$mods$ <$tp$> $ret $name($args$) { $body$; }",
1.1065 - new Pair[] {
1.1066 - new Pair<String, int[]>("$ret", new int[] {49, 55}),
1.1067 - new Pair<String, int[]>("$mods$", new int[] {34, 41}),
1.1068 - },
1.1069 - new Pair[] {
1.1070 - new Pair<String, int[]>("$tp$", new int[] {43, 44, 46, 47}),
1.1071 - new Pair<String, int[]>("$args$", new int[] {59, 64, 66, 71}),
1.1072 - new Pair<String, int[]>("$body$", new int[] {75, 81, 82, 88}),
1.1073 - },
1.1074 - new Pair[] {
1.1075 - new Pair<String, String>("$name", "aa")
1.1076 - },
1.1077 - false,
1.1078 - true);
1.1079 - }
1.1080 -
1.1081 - public void testMethodTypeParameters3() throws Exception {
1.1082 - performVariablesTest("package test; public class Test { private <A> String aa(int a, int b) { a = b; b = a;} }",
1.1083 - "$mods$ <$tp> $ret $name($args$) { $body$; }",
1.1084 - new Pair[] {
1.1085 - new Pair<String, int[]>("$ret", new int[] {46, 52}),
1.1086 - new Pair<String, int[]>("$mods$", new int[] {34, 41}),
1.1087 - new Pair<String, int[]>("$tp", new int[] {43, 44}),
1.1088 - },
1.1089 - new Pair[] {
1.1090 - new Pair<String, int[]>("$args$", new int[] {56, 61, 63, 68}),
1.1091 - new Pair<String, int[]>("$body$", new int[] {72, 78, 79, 85}),
1.1092 - },
1.1093 - new Pair[] {
1.1094 - new Pair<String, String>("$name", "aa")
1.1095 - },
1.1096 - false,
1.1097 - true);
1.1098 - }
1.1099 -
1.1100 - public void testTypeParameters1() throws Exception {
1.1101 - performVariablesTest("package test; public class Test { private <A extends String> void aa() { } }",
1.1102 - "$mods$ <$tp extends $bound&$obounds$> $ret $name($args$) { $body$; }",
1.1103 - new Pair[] {
1.1104 - new Pair<String, int[]>("$ret", new int[] {61, 65}),
1.1105 - new Pair<String, int[]>("$mods$", new int[] {34, 41}),
1.1106 - new Pair<String, int[]>("$tp", new int[] {43, 59}),
1.1107 - new Pair<String, int[]>("$bound", new int[] {53, 59}),
1.1108 - },
1.1109 - new Pair[] {
1.1110 - new Pair<String, int[]>("$obounds$", new int[] {}),
1.1111 - },
1.1112 - new Pair[] {
1.1113 - new Pair<String, String>("$name", "aa"),
1.1114 - new Pair<String, String>("$tp", "A")
1.1115 - },
1.1116 - false,
1.1117 - true);
1.1118 - }
1.1119 -
1.1120 - public void testPartialModifiers1() throws Exception {
1.1121 - performVariablesTest("package test; public class Test { @Deprecated @Override private void aa() { } }",
1.1122 - "$mods$ @Deprecated private $ret $name() { $body$; }",
1.1123 - new Pair[] {
1.1124 - new Pair<String, int[]>("$ret", new int[] {64, 68}),
1.1125 - new Pair<String, int[]>("$mods$", new int[] {34, 63}),
1.1126 - },
1.1127 - new Pair[] {
1.1128 - },
1.1129 - new Pair[] {
1.1130 - new Pair<String, String>("$name", "aa"),
1.1131 - },
1.1132 - false,
1.1133 - true);
1.1134 - }
1.1135 -
1.1136 - public void testPartialModifiers2() throws Exception {
1.1137 - performVariablesTest("package test; public class Test { @Override private void aa() { } }",
1.1138 - "$mods$ @Deprecated private $ret $name() { $body$; }",
1.1139 - new Pair[0],
1.1140 - new Pair[0],
1.1141 - new Pair[0],
1.1142 - true,
1.1143 - true);
1.1144 - }
1.1145 -
1.1146 - public void testNonStaticInnerClassesMatch() throws Exception {
1.1147 - performVariablesTest("package test; import test.Test.Inner; public class Test { public class Inner { } } class Other { { Inner i = null; } }",
1.1148 - "test.Test.Inner $i = $init$;",
1.1149 - new Pair[] {
1.1150 - new Pair<String, int[]>("$i", new int[] {99, 114}),
1.1151 - new Pair<String, int[]>("$init$", new int[] {109, 113})
1.1152 - },
1.1153 - new Pair[0],
1.1154 - new Pair[] {
1.1155 - new Pair<String, String>("$i", "i")
1.1156 - },
1.1157 - false,
1.1158 - true);
1.1159 - }
1.1160 -
1.1161 - public void testNewClassTypeParams222066a() throws Exception {
1.1162 - performVariablesTest("package test; public class Test { private Object aa() { return new java.util.ArrayList(1); } }",
1.1163 - "new java.util.ArrayList<$whatever$>($param)",
1.1164 - new Pair[] {
1.1165 - new Pair<String, int[]>("$param", new int[] {87, 88})
1.1166 - },
1.1167 - new Pair[] {
1.1168 - new Pair<String, int[]>("$whatever$", new int[0])
1.1169 - },
1.1170 - new Pair[0],
1.1171 - false,
1.1172 - false);
1.1173 - }
1.1174 -
1.1175 - public void testNewClassTypeParams222066b() throws Exception {
1.1176 - performVariablesTest("package test; import java.util.ArrayList; public class Test { private Object aa() { return new ArrayList(1); } }",
1.1177 - "new java.util.ArrayList<$whatever$>($param)",
1.1178 - new Pair[] {
1.1179 - new Pair<String, int[]>("$param", new int[] {105, 106})
1.1180 - },
1.1181 - new Pair[] {
1.1182 - new Pair<String, int[]>("$whatever$", new int[0])
1.1183 - },
1.1184 - new Pair[0],
1.1185 - false,
1.1186 - false);
1.1187 - }
1.1188 -
1.1189 - public void testFindLambda() throws Exception {
1.1190 - performVariablesTest("package test; import java.util.Comparator; public class Test { private void aa() { Comparator<String> c = (l, r) -> l.compareTo(r); } }",
1.1191 - "($args$) -> $expression",
1.1192 - new Pair[] {
1.1193 - new Pair<String, int[]>("$expression", new int[] {116, 130})
1.1194 - },
1.1195 - new Pair[] {
1.1196 - new Pair<String, int[]>("$args$", new int[] {107, 108, 110, 111})
1.1197 - },
1.1198 - new Pair[0],
1.1199 - false,
1.1200 - false);
1.1201 - }
1.1202 -
1.1203 - public void testAnnotation1() throws Exception {
1.1204 - performVariablesTest("package test; import test.Test.A; @A(i=1) public class Test { @interface A { public int i(); } }",
1.1205 - "@$annotation($args$)",
1.1206 - new Pair[] {
1.1207 - new Pair<String, int[]>("$annotation", new int[] {35, 36})
1.1208 - },
1.1209 - new Pair[] {
1.1210 - new Pair<String, int[]>("$args$", new int[] {37, 40})
1.1211 - },
1.1212 - new Pair[0],
1.1213 - false,
1.1214 - true);
1.1215 - }
1.1216 -
1.1217 - public void testAnnotation2() throws Exception {
1.1218 - performVariablesTest("package test; import test.Test.A; @A(i=1,b=true,l=2) public class Test { @interface A { public int i(); public boolean b(); public long l(); } }",
1.1219 - "@test.Test.A($prefix$, b=$value, $suffix$)",
1.1220 - new Pair[] {
1.1221 - new Pair<String, int[]>("$value", new int[] {43, 47})
1.1222 - },
1.1223 - new Pair[] {
1.1224 - new Pair<String, int[]>("$prefix$", new int[] {37, 40}),
1.1225 - new Pair<String, int[]>("$suffix$", new int[] {48, 51})
1.1226 - },
1.1227 - new Pair[0],
1.1228 - false,
1.1229 - false);
1.1230 - }
1.1231 -
1.1232 - protected void prepareTest(String code) throws Exception {
1.1233 - prepareTest(code, -1);
1.1234 - }
1.1235 -
1.1236 - protected void prepareTest(String code, int testIndex) throws Exception {
1.1237 - File workDirWithIndexFile = testIndex != (-1) ? new File(getWorkDir(), Integer.toString(testIndex)) : getWorkDir();
1.1238 - FileObject workDirWithIndex = FileUtil.toFileObject(workDirWithIndexFile);
1.1239 -
1.1240 - if (workDirWithIndex != null) {
1.1241 - workDirWithIndex.delete();
1.1242 - }
1.1243 -
1.1244 - workDirWithIndex = FileUtil.createFolder(workDirWithIndexFile);
1.1245 -
1.1246 - assertNotNull(workDirWithIndexFile);
1.1247 -
1.1248 - FileObject sourceRoot = workDirWithIndex.createFolder("src");
1.1249 - FileObject buildRoot = workDirWithIndex.createFolder("build");
1.1250 - FileObject cache = workDirWithIndex.createFolder("cache");
1.1251 -
1.1252 - FileObject data = FileUtil.createData(sourceRoot, "test/Test.java");
1.1253 -
1.1254 - TestUtilities.copyStringToFile(data, code);
1.1255 -
1.1256 - data.refresh();
1.1257 -
1.1258 - SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);
1.1259 -
1.1260 - DataObject od = DataObject.find(data);
1.1261 - EditorCookie ec = od.getLookup().lookup(EditorCookie.class);
1.1262 -
1.1263 - assertNotNull(ec);
1.1264 -
1.1265 - doc = ec.openDocument();
1.1266 -
1.1267 - doc.putProperty(Language.class, JavaTokenId.language());
1.1268 - doc.putProperty("mimeType", "text/x-java");
1.1269 -
1.1270 - JavaSource js = JavaSource.forFileObject(data);
1.1271 -
1.1272 - assertNotNull(js);
1.1273 -
1.1274 - info = SourceUtilsTestUtil.getCompilationInfo(js, Phase.RESOLVED);
1.1275 -
1.1276 - assertNotNull(info);
1.1277 - }
1.1278 -
1.1279 - private static String findRegions(String code, List<int[]> regions) {
1.1280 - String[] split = code.split("\\|");
1.1281 - StringBuilder filtered = new StringBuilder();
1.1282 -
1.1283 - filtered.append(split[0]);
1.1284 -
1.1285 - int offset = split[0].length();
1.1286 -
1.1287 - for (int cntr = 1; cntr < split.length; cntr += 2) {
1.1288 - int[] i = new int[] {
1.1289 - offset,
1.1290 - offset + split[cntr].length()
1.1291 - };
1.1292 -
1.1293 - regions.add(i);
1.1294 -
1.1295 - filtered.append(split[cntr]);
1.1296 - filtered.append(split[cntr + 1]);
1.1297 -
1.1298 - offset += split[cntr].length();
1.1299 - offset += split[cntr + 1].length();
1.1300 - }
1.1301 -
1.1302 - return filtered.toString();
1.1303 - }
1.1304 -
1.1305 - protected CompilationInfo info;
1.1306 - private Document doc;
1.1307 -
1.1308 - private void performTest(String code) throws Exception {
1.1309 - performTest(code, true);
1.1310 - }
1.1311 -
1.1312 - private void performTest(String code, boolean verify) throws Exception {
1.1313 - List<int[]> result = new LinkedList<int[]>();
1.1314 -
1.1315 - code = findRegions(code, result);
1.1316 -
1.1317 - int testIndex = 0;
1.1318 -
1.1319 - for (int[] i : result) {
1.1320 - int[] duplicates = new int[2 * (result.size() - 1)];
1.1321 - int cntr = 0;
1.1322 - List<int[]> l = new LinkedList<int[]>(result);
1.1323 -
1.1324 - l.remove(i);
1.1325 -
1.1326 - for (int[] span : l) {
1.1327 - duplicates[cntr++] = span[0];
1.1328 - duplicates[cntr++] = span[1];
1.1329 - }
1.1330 -
1.1331 - doPerformTest(code, i[0], i[1], testIndex++, verify, duplicates);
1.1332 - }
1.1333 - }
1.1334 -
1.1335 - protected void performTest(String code, int start, int end, int... duplicates) throws Exception {
1.1336 - doPerformTest(code, start, end, -1, true, duplicates);
1.1337 - }
1.1338 -
1.1339 - protected void doPerformTest(String code, int start, int end, int testIndex, int... duplicates) throws Exception {
1.1340 - doPerformTest(code, start, end, testIndex, true, duplicates);
1.1341 - }
1.1342 -
1.1343 - protected void doPerformTest(String code, int start, int end, int testIndex, boolean verify, int... duplicates) throws Exception {
1.1344 - prepareTest(code, testIndex);
1.1345 -
1.1346 - TreePath path = info.getTreeUtilities().pathFor((start + end) / 2 + 1);
1.1347 -
1.1348 - while (path != null) {
1.1349 - Tree t = path.getLeaf();
1.1350 - SourcePositions sp = info.getTrees().getSourcePositions();
1.1351 -
1.1352 - if ( start == sp.getStartPosition(info.getCompilationUnit(), t)
1.1353 - && end == sp.getEndPosition(info.getCompilationUnit(), t)) {
1.1354 - break;
1.1355 - }
1.1356 -
1.1357 - path = path.getParentPath();
1.1358 - }
1.1359 -
1.1360 - assertNotNull(path);
1.1361 -
1.1362 - Collection<TreePath> result = computeDuplicates(path);
1.1363 -
1.1364 - // assertEquals(f.result.toString(), duplicates.length / 2, f.result.size());
1.1365 -
1.1366 - if (verify) {
1.1367 - int[] dupes = new int[result.size() * 2];
1.1368 - int index = 0;
1.1369 -
1.1370 - for (TreePath tp : result) {
1.1371 - dupes[index++] = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), tp.getLeaf());
1.1372 - dupes[index++] = (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), tp.getLeaf());
1.1373 - }
1.1374 -
1.1375 - assertTrue("Was: " + Arrays.toString(dupes) + " should have been: " + Arrays.toString(duplicates), Arrays.equals(duplicates, dupes));
1.1376 - }
1.1377 - }
1.1378 -
1.1379 - protected void performVariablesTest(String code, String pattern, Pair<String, int[]>[] duplicatesPos, Pair<String, String>[] duplicatesNames) throws Exception {
1.1380 - performVariablesTest(code, pattern, duplicatesPos, new Pair[0], duplicatesNames);
1.1381 - }
1.1382 -
1.1383 - protected void performVariablesTest(String code, String pattern, Pair<String, int[]>[] duplicatesPos, Pair<String, int[]>[] multiStatementPos, Pair<String, String>[] duplicatesNames) throws Exception {
1.1384 - performVariablesTest(code, pattern, duplicatesPos, multiStatementPos, duplicatesNames, false);
1.1385 - }
1.1386 -
1.1387 - protected void performVariablesTest(String code, String pattern, Pair<String, int[]>[] duplicatesPos, Pair<String, int[]>[] multiStatementPos, Pair<String, String>[] duplicatesNames, boolean noOccurrences) throws Exception {
1.1388 - performVariablesTest(code, pattern, duplicatesPos, multiStatementPos, duplicatesNames, noOccurrences, false);
1.1389 - }
1.1390 -
1.1391 - protected void performVariablesTest(String code, String pattern, Pair<String, int[]>[] duplicatesPos, Pair<String, int[]>[] multiStatementPos, Pair<String, String>[] duplicatesNames, boolean noOccurrences, boolean useBulkSearch) throws Exception {
1.1392 - prepareTest(code, -1);
1.1393 -
1.1394 - Map<String, TypeMirror> constraints = new HashMap<String, TypeMirror>();
1.1395 - String patternCode = PatternCompilerUtilities.parseOutTypesFromPattern(info, pattern, constraints);
1.1396 -
1.1397 - Pattern patternObj = PatternCompiler.compile(info, patternCode, constraints, Collections.<String>emptyList());
1.1398 - TreePath patternPath = MatchingTestAccessor.getPattern(patternObj).iterator().next();
1.1399 - Map<TreePath, VariableAssignments> result;
1.1400 -
1.1401 - if (useBulkSearch) {
1.1402 - result = new HashMap<TreePath, VariableAssignments>();
1.1403 -
1.1404 - BulkPattern bulkPattern = BulkSearch.getDefault().create(info, new AtomicBoolean(), patternCode);
1.1405 -
1.1406 - for (Entry<String, Collection<TreePath>> e : BulkSearch.getDefault().match(info, new AtomicBoolean(), new TreePath(info.getCompilationUnit()), bulkPattern).entrySet()) {
1.1407 - for (TreePath tp : e.getValue()) {
1.1408 - VariableAssignments vars = computeVariables(info, patternPath, tp, new AtomicBoolean(), MatchingTestAccessor.getVariable2Type(patternObj));
1.1409 -
1.1410 - if (vars != null) {
1.1411 - result.put(tp, vars);
1.1412 - }
1.1413 - }
1.1414 - }
1.1415 - } else {
1.1416 - result = computeDuplicates(info, patternPath, new TreePath( info.getCompilationUnit()), new AtomicBoolean(), MatchingTestAccessor.getVariable2Type(patternObj));
1.1417 - }
1.1418 -
1.1419 - if (noOccurrences) {
1.1420 - assertEquals(0, result.size());
1.1421 - return ;
1.1422 - }
1.1423 -
1.1424 - assertSame(1, result.size());
1.1425 -
1.1426 - Map<String, int[]> actual = new HashMap<String, int[]>();
1.1427 -
1.1428 - for (Entry<String, TreePath> e : result.values().iterator().next().variables.entrySet()) {
1.1429 - int[] span = new int[] {
1.1430 - (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), e.getValue().getLeaf()),
1.1431 - (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), e.getValue().getLeaf())
1.1432 - };
1.1433 -
1.1434 - actual.put(e.getKey(), span);
1.1435 - }
1.1436 -
1.1437 - for (Pair<String, int[]> dup : duplicatesPos) {
1.1438 - int[] span = actual.remove(dup.getA());
1.1439 -
1.1440 - if (span == null) {
1.1441 - fail(dup.getA());
1.1442 - }
1.1443 - assertTrue(dup.getA() + ":" + Arrays.toString(span), Arrays.equals(span, dup.getB()));
1.1444 - }
1.1445 -
1.1446 - Map<String, int[]> actualMulti = new HashMap<String, int[]>();
1.1447 -
1.1448 - for (Entry<String, Collection<? extends TreePath>> e : result.values().iterator().next().multiVariables.entrySet()) {
1.1449 - int[] span = new int[2 * e.getValue().size()];
1.1450 - int i = 0;
1.1451 -
1.1452 - for (TreePath tp : e.getValue()) {
1.1453 - span[i++] = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), tp.getLeaf());
1.1454 - span[i++] = (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), tp.getLeaf());
1.1455 - }
1.1456 -
1.1457 - actualMulti.put(e.getKey(), span);
1.1458 - }
1.1459 -
1.1460 - for (Pair<String, int[]> dup : multiStatementPos) {
1.1461 - int[] span = actualMulti.remove(dup.getA());
1.1462 -
1.1463 - if (span == null) {
1.1464 - fail(dup.getA());
1.1465 - }
1.1466 - assertTrue(dup.getA() + ":" + Arrays.toString(span), Arrays.equals(span, dup.getB()));
1.1467 - }
1.1468 -
1.1469 - Map<String, String> golden = new HashMap<String, String>();
1.1470 -
1.1471 - for ( Pair<String, String> e : duplicatesNames) {
1.1472 - golden.put(e.getA(), e.getB());
1.1473 - }
1.1474 -
1.1475 - assertEquals(golden, result.values().iterator().next().variables2Names);
1.1476 - }
1.1477 -
1.1478 - protected VariableAssignments computeVariables(CompilationInfo info, TreePath searchingFor, TreePath scope, AtomicBoolean cancel, Map<String, TypeMirror> designedTypeHack) {
1.1479 - Collection<VariableAssignments> values = CopyFinder.internalComputeDuplicates(info, Collections.singletonList(searchingFor), scope, null, null, new AtomicBooleanCancel(cancel), designedTypeHack, Options.ALLOW_VARIABLES_IN_PATTERN).values();
1.1480 -
1.1481 - if (values.iterator().hasNext()) {
1.1482 - return values.iterator().next();
1.1483 - } else {
1.1484 - return null;
1.1485 - }
1.1486 - }
1.1487 -
1.1488 - protected Map<TreePath, VariableAssignments> computeDuplicates(CompilationInfo info, TreePath searchingFor, TreePath scope, AtomicBoolean cancel, Map<String, TypeMirror> designedTypeHack) {
1.1489 - return CopyFinder.internalComputeDuplicates(info, Collections.singletonList(searchingFor), scope, null, null, new AtomicBooleanCancel(cancel), designedTypeHack, Options.ALLOW_VARIABLES_IN_PATTERN, Options.ALLOW_GO_DEEPER);
1.1490 - }
1.1491 -
1.1492 - private void performRemappingTest(String code, String remappableVariables, Options... options) throws Exception {
1.1493 - List<int[]> regions = new LinkedList<int[]>();
1.1494 -
1.1495 - code = findRegions(code, regions);
1.1496 -
1.1497 - prepareTest(code, -1);
1.1498 -
1.1499 - int[] statements = new int[2];
1.1500 -
1.1501 - int[] currentRegion = regions.get(0);
1.1502 - TreePathHandle tph = IntroduceMethodFix.validateSelectionForIntroduceMethod(info, currentRegion[0], currentRegion[1], statements);
1.1503 -
1.1504 - assertNotNull(tph);
1.1505 -
1.1506 - TreePath tp = tph.resolve(info);
1.1507 -
1.1508 - assertNotNull(tp);
1.1509 -
1.1510 - BlockTree bt = (BlockTree) tp.getParentPath().getLeaf();
1.1511 - List<TreePath> searchFor = new LinkedList<TreePath>();
1.1512 -
1.1513 - for (StatementTree t : bt.getStatements().subList(statements[0], statements[1] + 1)) {
1.1514 - searchFor.add(new TreePath(tp, t));
1.1515 - }
1.1516 -
1.1517 - final Set<VariableElement> vars = new HashSet<VariableElement>();
1.1518 -
1.1519 - for (final String name : remappableVariables.split(",")) {
1.1520 - if (name.isEmpty()) continue;
1.1521 - new TreePathScanner<Object, Object>() {
1.1522 - @Override
1.1523 - public Object visitVariable(VariableTree node, Object p) {
1.1524 - if (node.getName().contentEquals(name)) {
1.1525 - vars.add((VariableElement) info.getTrees().getElement(getCurrentPath()));
1.1526 - }
1.1527 -
1.1528 - return super.visitVariable(node, p);
1.1529 - }
1.1530 - }.scan(info.getCompilationUnit(), null);
1.1531 - }
1.1532 -
1.1533 - Set<Options> opts = EnumSet.of(Options.ALLOW_GO_DEEPER);
1.1534 -
1.1535 - opts.addAll(Arrays.asList(options));
1.1536 -
1.1537 - Map<TreePath, VariableAssignments> result = CopyFinder.internalComputeDuplicates(info, searchFor, new TreePath(info.getCompilationUnit()), null, vars, new AtomicBooleanCancel(), Collections.<String, TypeMirror>emptyMap(), opts.toArray(new Options[0]));
1.1538 - Set<List<Integer>> realSpans = new HashSet<List<Integer>>();
1.1539 -
1.1540 - for (Entry<TreePath, VariableAssignments> e : result.entrySet()) {
1.1541 - List<? extends StatementTree> parentStatements = CopyFinder.getStatements(e.getKey());
1.1542 - int dupeStart = parentStatements.indexOf(e.getKey().getLeaf());
1.1543 - int startPos = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), parentStatements.get(dupeStart));
1.1544 - int endPos = (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), parentStatements.get(dupeStart + searchFor.size() - 1));
1.1545 -
1.1546 - realSpans.add(Arrays.asList(startPos, endPos));
1.1547 - }
1.1548 -
1.1549 - Set<List<Integer>> goldenSpans = new HashSet<List<Integer>>();
1.1550 -
1.1551 - for (int[] region : regions) {
1.1552 - if (region == currentRegion) continue;
1.1553 -
1.1554 - int[] stmts = new int[2];
1.1555 - TreePathHandle gtph = IntroduceMethodFix.validateSelectionForIntroduceMethod(info, region[0], region[1], stmts);
1.1556 -
1.1557 - assertNotNull(gtph);
1.1558 -
1.1559 - TreePath gtp = gtph.resolve(info);
1.1560 -
1.1561 - assertNotNull(gtp);
1.1562 -
1.1563 - BlockTree b = (BlockTree) gtp.getParentPath().getLeaf();
1.1564 -
1.1565 - int startPos = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), b.getStatements().get(stmts[0]));
1.1566 - int endPos = (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), b.getStatements().get(stmts[1]));
1.1567 -
1.1568 - goldenSpans.add(Arrays.asList(startPos, endPos));
1.1569 - }
1.1570 -
1.1571 - assertEquals(goldenSpans, realSpans);
1.1572 - }
1.1573 -
1.1574 - protected Collection<TreePath> computeDuplicates(TreePath path) {
1.1575 - return CopyFinder.internalComputeDuplicates(info, Collections.singletonList(path), new TreePath(info.getCompilationUnit()), null, null, new AtomicBooleanCancel(), null, Options.ALLOW_GO_DEEPER).keySet();
1.1576 - }
1.1577 -
1.1578 - public static final class Pair<A, B> {
1.1579 - private final A a;
1.1580 - private final B b;
1.1581 -
1.1582 - public Pair(A a, B b) {
1.1583 - this.a = a;
1.1584 - this.b = b;
1.1585 - }
1.1586 -
1.1587 - public A getA() {
1.1588 - return a;
1.1589 - }
1.1590 -
1.1591 - public B getB() {
1.1592 - return b;
1.1593 - }
1.1594 -
1.1595 - }
1.1596 -
1.1597 - private static final class AtomicBooleanCancel implements Cancel {
1.1598 -
1.1599 - private final AtomicBoolean cancel;
1.1600 -
1.1601 - public AtomicBooleanCancel() {
1.1602 - this(new AtomicBoolean());
1.1603 - }
1.1604 -
1.1605 - public AtomicBooleanCancel(AtomicBoolean cancel) {
1.1606 - this.cancel = cancel;
1.1607 - }
1.1608 -
1.1609 - @Override
1.1610 - public boolean isCancelled() {
1.1611 - return cancel.get();
1.1612 - }
1.1613 -
1.1614 - }
1.1615 -}