1 /*******************************************************************************
2  * Copyright (c) 2020 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *		IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.core.tests.rewrite.describing;
15 
16 import java.io.IOException;
17 import java.util.List;
18 
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jdt.core.ICompilationUnit;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.IPackageFragmentRoot;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.jdt.core.dom.AST;
25 import org.eclipse.jdt.core.dom.ASTParser;
26 import org.eclipse.jdt.core.dom.CompilationUnit;
27 import org.eclipse.jdt.core.dom.ITypeBinding;
28 import org.eclipse.jdt.core.dom.IVariableBinding;
29 import org.eclipse.jdt.core.dom.RecordDeclaration;
30 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
31 import org.eclipse.jdt.core.dom.Type;
32 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
33 import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
34 import org.eclipse.jdt.core.tests.model.AbstractJavaModelTests;
35 import org.eclipse.jface.text.BadLocationException;
36 import org.eclipse.jface.text.Document;
37 import org.eclipse.text.edits.MalformedTreeException;
38 import org.eclipse.text.edits.TextEdit;
39 import org.osgi.service.prefs.BackingStoreException;
40 
41 import junit.framework.Test;
42 
43 
44 @SuppressWarnings("rawtypes")
45 public class ImportRewrite14Test extends AbstractJavaModelTests {
46 
47 
48 	private static final Class THIS= ImportRewrite14Test.class;
49 	private static final String PROJECT = "ImportRewrite14TestProject";
50 
51 	protected IPackageFragmentRoot sourceFolder;
52 
53 
ImportRewrite14Test(String name)54 	public ImportRewrite14Test(String name) {
55 		super(name);
56 	}
57 
allTests()58 	public static Test allTests() {
59 		return new Suite(THIS);
60 	}
61 
suite()62 	public static Test suite() {
63 		return allTests();
64 	}
65 
66 	@Override
setUp()67 	protected void setUp() throws Exception {
68 		super.setUp();
69 
70 		IJavaProject proj= createJavaProject(PROJECT, new String[] {"src"}, new String[] {"JCL_LIB"}, "bin", "14");
71 		proj.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.SPACE);
72 		proj.setOption(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4");
73 		proj.setOption(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_14);
74 		proj.setOption(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR);
75 		proj.setOption(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_14);
76 		proj.setOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_14);
77 		proj.setOption(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, String.valueOf(99));
78 
79 		proj.setOption(DefaultCodeFormatterConstants.FORMATTER_BLANK_LINES_BETWEEN_IMPORT_GROUPS, String.valueOf(1));
80 		proj.setOption(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
81 		proj.setOption(JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES, JavaCore.IGNORE);
82 
83 
84 		this.sourceFolder = getPackageFragmentRoot(PROJECT, "src");
85 
86 		waitUntilIndexesReady();
87 	}
getJLS14()88 	protected static int getJLS14() {
89 		return AST.JLS14;
90 	}
91 	@Override
tearDown()92 	protected void tearDown() throws Exception {
93 		deleteProject(PROJECT);
94 		super.tearDown();
95 	}
96 
97 	/*
98 	 * typeBinding shows "int value" rather than "@MyAnnotation int value"
99 	 */
_test001()100 	public void _test001() throws Exception {
101 		String contents = "package pack1;\n" +
102 				"import pack2.MyAnnotation;\n" +
103 				"public record X(@MyAnnotation int value){\n" +
104 				"}\n";
105 		createFolder("/" + PROJECT + "/src/pack1");
106 		createFile("/" + PROJECT + "/src/pack1/X.java", contents);
107 		contents = "package pack2;\n" +
108 				"public @interface MyAnnotation{}\n";
109 		createFolder("/" + PROJECT + "/src/pack2");
110 		createFile("/" + PROJECT + "/src/pack2/MyAnnotation.java", contents);
111 
112 		ASTParser parser = ASTParser.newParser(getJLS14());
113 		parser.setSource(getCompilationUnit("/" + PROJECT + "/src/pack2/MyAnnotation.java"));
114 		parser.setResolveBindings(true);
115 		parser.setStatementsRecovery(true);
116 		CompilationUnit astRoot = (CompilationUnit) parser.createAST(null);
117 
118 		ICompilationUnit cu = getCompilationUnit("/" + PROJECT + "/src/pack1/X.java");
119 		parser.setSource(cu);
120 		parser.setResolveBindings(true);
121 		parser.setStatementsRecovery(true);
122 		astRoot = (CompilationUnit) parser.createAST(null);
123 		RecordDeclaration record= (RecordDeclaration) astRoot.types().get(0);
124 		List<SingleVariableDeclaration> recordComponents = record.recordComponents();
125 		SingleVariableDeclaration recordComponent = recordComponents.get(0);
126 		assertEquals("Record component type is int" , "int", recordComponent.getType().toString());
127 		IVariableBinding binding = recordComponent.resolveBinding();
128 
129 		ITypeBinding typeBinding = binding.getType();
130 		ImportRewrite rewrite = newImportsRewrite(cu, new String[0], 99, 99, true);
131 		cu = getCompilationUnit("/" + PROJECT + "/src/pack1/X.java");
132 		rewrite = newImportsRewrite(cu, new String[0], 99, 99, true);
133 		Type actualType = rewrite.addImport(typeBinding, astRoot.getAST());
134 		assertEquals("int", actualType.toString());
135 		assertTrue(actualType.isPrimitiveType());
136 		apply(rewrite);
137 		String contentsA = "package pack1;\n" +
138 				"\n" +
139 				"import pack2.MyAnnotation;\n" +
140 				"\n" +
141 				"public record X(@MyAnnotation int value){\n"+
142 				"}\n";
143 		assertEqualStringIgnoreDelim(cu.getSource(), contentsA);
144 	}
145 
assertEqualStringIgnoreDelim(String actual, String expected)146 	protected void assertEqualStringIgnoreDelim(String actual, String expected) throws IOException {
147 		StringAsserts.assertEqualStringIgnoreDelim(actual, expected);
148 	}
149 
newImportsRewrite(ICompilationUnit cu, String[] order, int normalThreshold, int staticThreshold, boolean restoreExistingImports)150 	protected ImportRewrite newImportsRewrite(ICompilationUnit cu, String[] order, int normalThreshold, int staticThreshold, boolean restoreExistingImports) throws CoreException, BackingStoreException {
151 		ImportRewrite rewrite= ImportRewrite.create(cu, restoreExistingImports);
152 		rewrite.setImportOrder(order);
153 		rewrite.setOnDemandImportThreshold(normalThreshold);
154 		rewrite.setStaticOnDemandImportThreshold(staticThreshold);
155 		return rewrite;
156 	}
157 
newImportsRewrite(CompilationUnit cu, String[] order, int normalThreshold, int staticThreshold, boolean restoreExistingImports)158 	protected ImportRewrite newImportsRewrite(CompilationUnit cu, String[] order, int normalThreshold, int staticThreshold, boolean restoreExistingImports) {
159 		ImportRewrite rewrite= ImportRewrite.create(cu, restoreExistingImports);
160 		rewrite.setImportOrder(order);
161 		rewrite.setOnDemandImportThreshold(normalThreshold);
162 		rewrite.setStaticOnDemandImportThreshold(staticThreshold);
163 		return rewrite;
164 	}
165 
apply(ImportRewrite rewrite)166 	protected void apply(ImportRewrite rewrite) throws CoreException, MalformedTreeException, BadLocationException {
167 		TextEdit edit= rewrite.rewriteImports(null);
168 
169 		// not the efficient way!
170 		ICompilationUnit compilationUnit= rewrite.getCompilationUnit();
171 		Document document= new Document(compilationUnit.getSource());
172 		edit.apply(document);
173 		compilationUnit.getBuffer().setContents(document.get());
174 	}
175 
176 }
177