|
1 | 1 | /******************************************************************************* |
2 | | - * Copyright (c) 2021, 2022 Carsten Hammer. |
| 2 | + * Copyright (c) 2021, 2024 Carsten Hammer. |
3 | 3 | * |
4 | 4 | * This program and the accompanying materials |
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0 |
|
28 | 28 | import org.junit.jupiter.api.BeforeAll; |
29 | 29 | import org.junit.jupiter.api.Test; |
30 | 30 |
|
31 | | -import org.eclipse.jdt.core.JavaCore; |
32 | | -import org.eclipse.jdt.core.dom.AST; |
| 31 | +import org.eclipse.jdt.testplugin.JavaProjectHelper; |
| 32 | + |
| 33 | +import org.eclipse.core.runtime.CoreException; |
| 34 | + |
| 35 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 36 | +import org.eclipse.jdt.core.IJavaProject; |
| 37 | +import org.eclipse.jdt.core.IPackageFragment; |
| 38 | +import org.eclipse.jdt.core.IPackageFragmentRoot; |
33 | 39 | import org.eclipse.jdt.core.dom.ASTNode; |
34 | 40 | import org.eclipse.jdt.core.dom.ASTParser; |
35 | 41 | import org.eclipse.jdt.core.dom.CompilationUnit; |
|
44 | 50 | import org.eclipse.jdt.core.dom.WhileStatement; |
45 | 51 |
|
46 | 52 | import org.eclipse.jdt.internal.corext.dom.ASTNodes; |
| 53 | +import org.eclipse.jdt.internal.corext.dom.IASTSharedValues; |
47 | 54 | import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer; |
48 | 55 |
|
| 56 | +import org.eclipse.jdt.ui.tests.core.rules.Java11ProjectTestSetup; |
| 57 | + |
49 | 58 | public class VisitorTest { |
50 | 59 |
|
51 | 60 | private static CompilationUnit result; |
52 | 61 | private static CompilationUnit result2; |
53 | 62 |
|
| 63 | + private static class VisitorProjectTestSetup extends Java11ProjectTestSetup { |
| 64 | + public void initialize() throws CoreException { |
| 65 | + createAndInitializeProject(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
54 | 69 | @BeforeAll |
55 | 70 | public static void init() { |
56 | | - ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); |
57 | | - String code =""" |
58 | | - package test; |
59 | | - import java.util.Collection; |
60 | | - |
61 | | - public class E { |
62 | | - public void hui(Collection<String> arr) { |
63 | | - Collection coll = null; |
64 | | - for (String var : arr) { |
65 | | - coll.add(var); |
66 | | - System.out.println(var); |
67 | | - System.err.println(var); |
| 71 | + VisitorProjectTestSetup projectSetup= new VisitorProjectTestSetup(); |
| 72 | + try { |
| 73 | + projectSetup.initialize(); |
| 74 | + IJavaProject project= projectSetup.getProject(); |
| 75 | + IPackageFragmentRoot fSourceFolder= JavaProjectHelper.addSourceContainer(project, "src"); |
| 76 | + IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null); |
| 77 | + |
| 78 | + String code =""" |
| 79 | + package test; |
| 80 | + import java.util.Collection; |
| 81 | +
|
| 82 | + public class E { |
| 83 | + public void hui(Collection<String> arr) { |
| 84 | + Collection coll = null; |
| 85 | + for (String var : arr) { |
| 86 | + coll.add(var); |
| 87 | + System.out.println(var); |
| 88 | + System.err.println(var); |
| 89 | + } |
| 90 | + System.out.println(arr); |
| 91 | + } |
| 92 | + }"""; |
| 93 | + |
| 94 | + ICompilationUnit cu1= pack1.createCompilationUnit("E.java", code, false, null); |
| 95 | + ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL); |
| 96 | + parser.setSource(cu1); |
| 97 | + parser.setResolveBindings(true); |
| 98 | + |
| 99 | + result = (CompilationUnit) parser.createAST(null); |
| 100 | + |
| 101 | + String code2=""" |
| 102 | + package test; |
| 103 | + import java.util.*; |
| 104 | + public class Test { |
| 105 | + void m(List<String> strings,List<String> strings2) { |
| 106 | + Collections.reverse(strings); |
| 107 | + Iterator it = strings.iterator(); |
| 108 | + while (it.hasNext()) { |
| 109 | + Iterator it2 = strings2.iterator(); |
| 110 | + while (it2.hasNext()) { |
| 111 | + String s2 = (String) it2.next(); |
| 112 | + System.out.println(s2); |
| 113 | + } |
| 114 | + // OK |
| 115 | + System.out.println(it.next()); |
| 116 | + } |
| 117 | + System.out.println(); |
| 118 | + } |
68 | 119 | } |
69 | | - System.out.println(arr); |
70 | | - } |
71 | | - }"""; |
72 | | - parser.setKind(ASTParser.K_COMPILATION_UNIT); |
73 | | - parser.setEnvironment(new String[]{}, new String[]{}, null, true); |
74 | | - parser.setBindingsRecovery(true); |
75 | | - parser.setResolveBindings(true); |
76 | | - Map<String, String> options = JavaCore.getOptions(); |
77 | | - JavaCore.setComplianceOptions(JavaCore.VERSION_11, options); |
78 | | - parser.setCompilerOptions(options); |
79 | | - parser.setUnitName("E"); |
80 | | - parser.setSource(code.toCharArray()); |
81 | | - result = (CompilationUnit) parser.createAST(null); |
82 | | - |
83 | | - |
84 | | - String code2=""" |
85 | | - package test; |
86 | | - import java.util.*; |
87 | | - public class Test { |
88 | | - void m(List<String> strings,List<String> strings2) { |
89 | | - Collections.reverse(strings); |
90 | | - Iterator it = strings.iterator(); |
91 | | - while (it.hasNext()) { |
92 | | - Iterator it2 = strings2.iterator(); |
93 | | - while (it2.hasNext()) { |
94 | | - String s2 = (String) it2.next(); |
95 | | - System.out.println(s2); |
96 | | - } |
97 | | - // OK |
98 | | - System.out.println(it.next()); |
99 | | - } |
100 | | - System.out.println(); |
101 | | - } |
102 | | - } |
103 | | - """; |
104 | | - parser.setEnvironment(new String[]{}, new String[]{}, null, true); |
105 | | - parser.setBindingsRecovery(true); |
106 | | - parser.setResolveBindings(true); |
107 | | - parser.setUnitName("Test"); |
108 | | - parser.setSource(code2.toCharArray()); |
109 | | - result2 = (CompilationUnit) parser.createAST(null); |
110 | | -// System.out.println(result.toString()); |
| 120 | + """; |
| 121 | + ICompilationUnit cu2= pack1.createCompilationUnit("Test.java", code2, false, null); |
| 122 | + parser.setSource(cu2); |
| 123 | + parser.setResolveBindings(true); |
| 124 | + result2 = (CompilationUnit) parser.createAST(null); |
| 125 | + } catch (CoreException e) { |
| 126 | + // TODO Auto-generated catch block |
| 127 | + e.printStackTrace(); |
| 128 | + } |
111 | 129 | } |
112 | 130 |
|
113 | 131 | private void astnodeprocessorend(ASTNode node, @SuppressWarnings("unused") ReferenceHolder<String,NodeFound> holder) { |
|
0 commit comments