View | Details | Raw Unified | Return to bug 143684 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/model/AllJavaModelTests.java (+3 lines)
Lines 176-181 Link Here
176
		
176
		
177
		// Java-like extensions tests
177
		// Java-like extensions tests
178
		JavaLikeExtensionsTests.class,
178
		JavaLikeExtensionsTests.class,
179
		
180
		// Creation of imports
181
		CreateImportsTests.class,
179
	};
182
	};
180
	
183
	
181
	Class[] deprecatedClasses = getDeprecatedJDOMTestClasses();
184
	Class[] deprecatedClasses = getDeprecatedJDOMTestClasses();
(-)src/org/eclipse/jdt/core/tests/model/CopyMoveTests.java (+2 lines)
Lines 104-109 Link Here
104
				}
104
				}
105
			}
105
			}
106
		}
106
		}
107
		if (copy.getElementType() == IJavaElement.IMPORT_DECLARATION)
108
			container = ((ICompilationUnit) container).getImportContainer();
107
		IJavaElementDelta destDelta = getDeltaFor(container, true);
109
		IJavaElementDelta destDelta = getDeltaFor(container, true);
108
		assertTrue("Destination container not changed", destDelta != null && destDelta.getKind() == IJavaElementDelta.CHANGED);
110
		assertTrue("Destination container not changed", destDelta != null && destDelta.getKind() == IJavaElementDelta.CHANGED);
109
		IJavaElementDelta[] deltas = destDelta.getAddedChildren();
111
		IJavaElementDelta[] deltas = destDelta.getAddedChildren();
(-)src/org/eclipse/jdt/core/tests/model/CopyMoveElementsTests.java (-1 / +48 lines)
Lines 42-48 Link Here
42
public void setUp() throws Exception {
42
public void setUp() throws Exception {
43
	super.setUp();
43
	super.setUp();
44
	
44
	
45
	this.createJavaProject("P", new String[] {"src"}, new String[] {"/BinaryProject/bin"}, "bin");
45
	this.createJavaProject("P", new String[] {"src"}, new String[] {"/BinaryProject/bin"}, "bin", "1.5");
46
}
46
}
47
// Use this static initializer to specify subset for tests
47
// Use this static initializer to specify subset for tests
48
// All specified tests which do not belong to the class are skipped...
48
// All specified tests which do not belong to the class are skipped...
Lines 689-694 Link Here
689
		this.deleteProject("P2");
689
		this.deleteProject("P2");
690
	}
690
	}
691
}
691
}
692
/*
693
 * Ensures that an import can be copied to a different cu.
694
 */
695
public void testCopyImport() throws CoreException {
696
	this.createFile(
697
		"/P/src/X.java",
698
		"import java.util.*;\n" +
699
		"public class X {\n" +
700
		"}"
701
	);
702
	IImportDeclaration importSource = getCompilationUnit("/P/src/X.java").getImport("java.util.*");
703
704
	this.createFile(
705
		"/P/src/Y.java",
706
		"public class Y {\n" +
707
		"}"
708
	);
709
	ICompilationUnit cuDest = getCompilationUnit("/P/src/Y.java");
710
711
	copyPositive(importSource, cuDest, null, null, false);
712
}
713
/*
714
 * Ensures that a static import can be copied to a different cu.
715
 */
716
public void testCopyImportStatic() throws CoreException {
717
	this.createFile(
718
		"/P/src/X.java",
719
		"import static java.lang.Math;\n" +
720
		"public class X {\n" +
721
		"  int foo;\n" +
722
		"  {\n" +
723
		"    foo = 10;\n" +
724
		"  }\n" +
725
		"}"
726
	);
727
	IImportDeclaration importSource = getCompilationUnit("/P/src/X.java").getImport("java.lang.Math");
728
729
	this.createFile(
730
		"/P/src/Y.java",
731
		"public class Y {\n" +
732
		"}"
733
	);
734
	ICompilationUnit cuDest = getCompilationUnit("/P/src/Y.java");
735
736
	copyPositive(importSource, cuDest, null, null, false);
737
	assertEquals("Copied import should be static", Flags.AccStatic, cuDest.getImport("java.lang.Math").getFlags());
738
}
692
/**
739
/**
693
 * Ensures that a initializer can be copied to a different type.
740
 * Ensures that a initializer can be copied to a different type.
694
 */
741
 */
(-)src/org/eclipse/jdt/core/tests/model/CreateImportsTests.java (+289 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2006 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jdt.core.tests.model;
12
13
import java.util.Map;
14
15
import junit.framework.Test;
16
17
import org.eclipse.jdt.core.Flags;
18
import org.eclipse.jdt.core.ICompilationUnit;
19
import org.eclipse.jdt.core.IJavaElement;
20
import org.eclipse.jdt.core.JavaModelException;
21
import org.eclipse.jdt.core.ToolFactory;
22
import org.eclipse.jdt.core.formatter.CodeFormatter;
23
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
24
import org.eclipse.jface.text.BadLocationException;
25
import org.eclipse.jface.text.Document;
26
import org.eclipse.text.edits.MalformedTreeException;
27
import org.eclipse.text.edits.TextEdit;
28
29
public class CreateImportsTests extends AbstractJavaModelTests {
30
31
	public CreateImportsTests(String name) {
32
		super(name);
33
	}
34
	public static Test suite() {
35
		return buildModelTestSuite(CreateImportsTests.class);
36
	}
37
	protected void setUp() throws Exception {
38
		super.setUp();
39
		ICompilationUnit workingCopy = getCompilationUnit("P/X.java");
40
		workingCopy.becomeWorkingCopy(null/*no problem requested*/, null/*no progress*/);
41
		this.workingCopies = new ICompilationUnit[] {workingCopy};
42
		setContents(
43
			"public class X {\n" +
44
			"}"
45
		);
46
	}
47
	public void setUpSuite() throws Exception {
48
		super.setUpSuite();
49
		createJavaProject("P", new String[] {""}, new String[] {"JCL15_LIB"}, "", "1.5");
50
	}
51
	public void tearDownSuite() throws Exception {
52
		deleteProject("P");
53
		super.tearDownSuite();
54
	}
55
56
	private String createImport(String importName, int flags) throws JavaModelException {
57
		return createImport(importName, flags, null/*no sibling*/);
58
	}
59
	private String createImport(String importName, int flags, IJavaElement sibling) throws JavaModelException {
60
		ICompilationUnit workingCopy = this.workingCopies[0];
61
		workingCopy.createImport(importName, sibling, flags, null/*no progress*/);
62
		Map options = getJavaProject("P").getOptions(true);
63
		options.put(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, "0");
64
		CodeFormatter formatter = ToolFactory.createCodeFormatter(options);
65
		String source = workingCopy.getSource();
66
		Document document = new Document(source);
67
		TextEdit edit = formatter.format(CodeFormatter.K_COMPILATION_UNIT, source, 0, source.length() - 1, 0, "\n");
68
		try {
69
			edit.apply(document, TextEdit.NONE);
70
		} catch(MalformedTreeException e) {
71
			// ignore
72
		} catch(BadLocationException e) {
73
			// ignore
74
		}
75
		return document.get();
76
	}
77
	
78
	private void setContents(String contents) throws JavaModelException {
79
		this.workingCopies[0].getBuffer().setContents(contents);
80
		this.workingCopies[0].makeConsistent(null/*no progress*/);
81
	}
82
	
83
	/*
84
	 * Ensures that adding a static import is reflected in the source.
85
	 */
86
	public void test001() throws JavaModelException {
87
		String actualSource = createImport("java.lang.Math.*", Flags.AccStatic);
88
		assertSourceEquals(
89
			"Unexpected source", 
90
			"import static java.lang.Math.*;\n" + 
91
			"\n" + 
92
			"public class X {\n" + 
93
			"}", 
94
			actualSource);
95
	}
96
	
97
	/*
98
	 * Ensures that adding a non-static import is reflected in the source.
99
	 */
100
	public void test002() throws JavaModelException {
101
		String actualSource = createImport("java.util.ZipFile", Flags.AccDefault);
102
		assertSourceEquals(
103
			"Unexpected source", 
104
			"import java.util.ZipFile;\n" + 
105
			"\n" + 
106
			"public class X {\n" + 
107
			"}", 
108
			actualSource);
109
	}
110
	
111
	/*
112
	 * Ensures that adding the same static import doesn't change the source.
113
	 */
114
	public void test003() throws JavaModelException {
115
		setContents(
116
			"import static java.lang.Math.*;\n" + 
117
			"\n" + 
118
			"public class X {\n" + 
119
			"}"
120
		);
121
		String actualSource = createImport("java.lang.Math.*", Flags.AccStatic);
122
		assertSourceEquals(
123
			"Unexpected source", 
124
			"import static java.lang.Math.*;\n" + 
125
			"\n" + 
126
			"public class X {\n" + 
127
			"}", 
128
			actualSource);
129
	}
130
	
131
	/*
132
	 * Ensures that adding the same non-static import doesn't change the source.
133
	 */
134
	public void test004() throws JavaModelException {
135
		setContents(
136
			"import java.util.ZipFile;\n" + 
137
			"\n" + 
138
			"public class X {\n" + 
139
			"}"
140
		);
141
		String actualSource = createImport("java.util.ZipFile", Flags.AccDefault);
142
		assertSourceEquals(
143
			"Unexpected source", 
144
			"import java.util.ZipFile;\n" + 
145
			"\n" + 
146
			"public class X {\n" + 
147
			"}",
148
			actualSource);
149
	}
150
	
151
	/*
152
	 * Ensures that adding an onDemand static import starting with an existing non-onDemand import
153
	 * is reflected in the source.
154
	 */
155
	public void test005() throws JavaModelException {
156
		setContents(
157
			"import java.util.ZipFile;\n" + 
158
			"\n" + 
159
			"public class X {\n" + 
160
			"}"
161
		);
162
		String actualSource = createImport("java.util.ZipFile.*", Flags.AccStatic);
163
		assertSourceEquals(
164
			"Unexpected source", 
165
			"import java.util.ZipFile;\n" + 
166
			"import static java.util.ZipFile.*;\n" + 
167
			"\n" + 
168
			"public class X {\n" + 
169
			"}",
170
			actualSource);
171
	}
172
	
173
	/*
174
	 * Ensures that adding an onDemand non-static import starting with an existing non-onDemand import
175
	 * is reflected in the source.
176
	 */
177
	public void test006() throws JavaModelException {
178
		setContents(
179
			"import java.util.ZipFile;\n" + 
180
			"import static java.util.ZipFile.*;\n" + 
181
			"\n" + 
182
			"public class X {\n" + 
183
			"}"
184
		);
185
		String actualSource = createImport("java.util.ZipFile.*", Flags.AccDefault);
186
		assertSourceEquals(
187
			"Unexpected source", 
188
			"import java.util.ZipFile;\n" + 
189
			"import static java.util.ZipFile.*;\n" + 
190
			"import java.util.ZipFile.*;\n" + 
191
			"\n" + 
192
			"public class X {\n" + 
193
			"}",
194
			actualSource);
195
	}
196
197
	/*
198
	 * Ensures that adding an import triggers the correct delta.
199
	 */
200
	public void test007() throws JavaModelException {
201
		try {
202
			startDeltas();
203
			createImport("java.util.ZipFile", Flags.AccDefault);
204
			assertDeltas(
205
				"Unexpected delta",
206
				"<import container>[+]: {}"
207
			);
208
		} finally {
209
			stopDeltas();
210
		}
211
	}
212
213
	/*
214
	 * Ensures that adding an import triggers the correct delta.
215
	 */
216
	public void test008() throws JavaModelException {
217
		setContents(
218
			"import static java.lang.Math.*;\n" + 
219
			"\n" + 
220
			"public class X {\n" + 
221
			"}"
222
		);
223
		try {
224
			startDeltas();
225
			createImport("java.util.*", Flags.AccDefault);
226
			assertDeltas(
227
				"Unexpected delta",
228
				"<import container>[*]: {CHILDREN | FINE GRAINED}\n" + 
229
				"	import java.util.*[+]: {}"
230
			);
231
		} finally {
232
			stopDeltas();
233
		}
234
	}
235
	
236
	/*
237
	 * Ensures that adding an import before a sibling is correctly reflected in the source.
238
	 */
239
	public void test009() throws JavaModelException {
240
		setContents(
241
			"import static java.lang.Math.*;\n" + 
242
			"\n" + 
243
			"public class X {\n" + 
244
			"}"
245
		);
246
		IJavaElement sibling = this.workingCopies[0].getImport("java.lang.Math.*");
247
		String actualSource = createImport("java.util.ZipFile", Flags.AccDefault, sibling);
248
		assertSourceEquals(
249
			"Unexpected source", 
250
			"import java.util.ZipFile;\n" +
251
			"import static java.lang.Math.*;\n" + 
252
			"\n" + 
253
			"public class X {\n" + 
254
			"}", 
255
			actualSource);
256
	}
257
	
258
	/*
259
	 * Ensures that adding a null import throws the correct exception.
260
	 */
261
	public void test010() throws JavaModelException {
262
		JavaModelException exception = null;
263
		try {
264
			createImport(null, Flags.AccStatic);
265
		} catch (JavaModelException e) {
266
			exception = e;
267
		}
268
		assertExceptionEquals(
269
			"Unexpected exception", 
270
			"Invalid name specified: null",
271
			exception);
272
	}
273
	
274
	/*
275
	 * Ensures that adding an import with an invalid name throws the correct exception.
276
	 */
277
	public void test011() throws JavaModelException {
278
		JavaModelException exception = null;
279
		try {
280
			createImport("java.,.", Flags.AccStatic);
281
		} catch (JavaModelException e) {
282
			exception = e;
283
		}
284
		assertExceptionEquals(
285
			"Unexpected exception", 
286
			"Invalid name specified: java.,.",
287
			exception);
288
	}
289
}
(-)model/org/eclipse/jdt/internal/core/CreateImportOperation.java (-6 / +20 lines)
Lines 13-18 Link Here
13
import java.util.Iterator;
13
import java.util.Iterator;
14
14
15
import org.eclipse.core.runtime.IStatus;
15
import org.eclipse.core.runtime.IStatus;
16
import org.eclipse.jdt.core.Flags;
16
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.IImportDeclaration;
18
import org.eclipse.jdt.core.IImportDeclaration;
18
import org.eclipse.jdt.core.IJavaElement;
19
import org.eclipse.jdt.core.IJavaElement;
Lines 52-67 Link Here
52
 */
53
 */
53
public class CreateImportOperation extends CreateElementInCUOperation {
54
public class CreateImportOperation extends CreateElementInCUOperation {
54
55
55
	/**
56
	/*
56
	 * The name of the import to be created.
57
	 * The name of the import to be created.
57
	 */
58
	 */
58
	protected String importName;
59
	protected String importName;
60
	
61
	/*
62
	 * The flags of the import to be created (either Flags#AccDefault or Flags#AccStatic)
63
	 */
64
	protected int flags;
65
59
/**
66
/**
60
 * When executed, this operation will add an import to the given compilation unit.
67
 * When executed, this operation will add an import to the given compilation unit.
61
 */
68
 */
62
public CreateImportOperation(String importName, ICompilationUnit parentElement) {
69
public CreateImportOperation(String importName, ICompilationUnit parentElement, int flags) {
63
	super(parentElement);
70
	super(parentElement);
64
	this.importName = importName;
71
	this.importName = importName;
72
	this.flags = flags;
65
}
73
}
66
protected StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent) {
74
protected StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent) {
67
	return CompilationUnit.IMPORTS_PROPERTY;
75
	return CompilationUnit.IMPORTS_PROPERTY;
Lines 69-78 Link Here
69
protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
77
protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
70
	// ensure no duplicate
78
	// ensure no duplicate
71
	Iterator imports = this.cuAST.imports().iterator();
79
	Iterator imports = this.cuAST.imports().iterator();
80
	boolean onDemand = this.importName.endsWith(".*"); //$NON-NLS-1$
81
	String importActualName = this.importName;
82
	if (onDemand) {
83
		importActualName = this.importName.substring(0, this.importName.length() - 2);
84
	}
72
	while (imports.hasNext()) {
85
	while (imports.hasNext()) {
73
		ImportDeclaration importDeclaration = (ImportDeclaration) imports.next();
86
		ImportDeclaration importDeclaration = (ImportDeclaration) imports.next();
74
		if (this.importName.equals(importDeclaration.getName().getFullyQualifiedName())) {
87
		if (importActualName.equals(importDeclaration.getName().getFullyQualifiedName())
75
			//no new import was generated
88
				&& (onDemand == importDeclaration.isOnDemand())
89
				&& (Flags.isStatic(this.flags) == importDeclaration.isStatic())) {
76
			this.creationOccurred = false;
90
			this.creationOccurred = false;
77
			return null;
91
			return null;
78
		}
92
		}
Lines 80-88 Link Here
80
	
94
	
81
	AST ast = this.cuAST.getAST();
95
	AST ast = this.cuAST.getAST();
82
	ImportDeclaration importDeclaration = ast.newImportDeclaration();
96
	ImportDeclaration importDeclaration = ast.newImportDeclaration();
97
	importDeclaration.setStatic(Flags.isStatic(this.flags));
83
	// split import name into individual fragments, checking for on demand imports
98
	// split import name into individual fragments, checking for on demand imports
84
	boolean onDemand = this.importName.endsWith("*"); //$NON-NLS-1$
99
	char[][] charFragments = CharOperation.splitOn('.', importActualName.toCharArray(), 0, importActualName.length());
85
	char[][] charFragments = CharOperation.splitOn('.', this.importName.toCharArray(), 0, onDemand ? this.importName.length()-2 : this.importName.length());
86
	int length = charFragments.length;
100
	int length = charFragments.length;
87
	String[] strFragments = new String[length];
101
	String[] strFragments = new String[length];
88
	for (int i = 0; i < length; i++) {
102
	for (int i = 0; i < length; i++) {
(-)model/org/eclipse/jdt/internal/core/CompilationUnit.java (-2 / +1 lines)
Lines 372-379 Link Here
372
 * @since 3.0
372
 * @since 3.0
373
 */
373
 */
374
public IImportDeclaration createImport(String importName, IJavaElement sibling, int flags, IProgressMonitor monitor) throws JavaModelException {
374
public IImportDeclaration createImport(String importName, IJavaElement sibling, int flags, IProgressMonitor monitor) throws JavaModelException {
375
	// TODO (jerome) - consult flags to create static imports
375
	CreateImportOperation op = new CreateImportOperation(importName, this, flags);
376
	CreateImportOperation op = new CreateImportOperation(importName, this);
377
	if (sibling != null) {
376
	if (sibling != null) {
378
		op.createBefore(sibling);
377
		op.createBefore(sibling);
379
	}
378
	}
(-)model/org/eclipse/jdt/internal/core/CopyElementsOperation.java (-1 / +3 lines)
Lines 15-20 Link Here
15
15
16
import org.eclipse.core.runtime.IPath;
16
import org.eclipse.core.runtime.IPath;
17
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.ICompilationUnit;
18
import org.eclipse.jdt.core.IImportDeclaration;
18
import org.eclipse.jdt.core.IJavaElement;
19
import org.eclipse.jdt.core.IJavaElement;
19
import org.eclipse.jdt.core.IJavaModelStatus;
20
import org.eclipse.jdt.core.IJavaModelStatus;
20
import org.eclipse.jdt.core.IJavaModelStatusConstants;
21
import org.eclipse.jdt.core.IJavaModelStatusConstants;
Lines 95-101 Link Here
95
			case IJavaElement.PACKAGE_DECLARATION :
96
			case IJavaElement.PACKAGE_DECLARATION :
96
				return new CreatePackageDeclarationOperation(element.getElementName(), (ICompilationUnit) dest);
97
				return new CreatePackageDeclarationOperation(element.getElementName(), (ICompilationUnit) dest);
97
			case IJavaElement.IMPORT_DECLARATION :
98
			case IJavaElement.IMPORT_DECLARATION :
98
				return new CreateImportOperation(element.getElementName(), (ICompilationUnit) dest);
99
				IImportDeclaration importDeclaration = (IImportDeclaration) element;
100
				return new CreateImportOperation(element.getElementName(), (ICompilationUnit) dest, importDeclaration.getFlags());
99
			case IJavaElement.TYPE :
101
			case IJavaElement.TYPE :
100
				if (isRenamingMainType(element, dest)) {
102
				if (isRenamingMainType(element, dest)) {
101
					IPath path = element.getPath();
103
					IPath path = element.getPath();

Return to bug 143684