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

Collapse All | Expand All

(-)dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java (-5 / +18 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 12-18 Link Here
12
package org.eclipse.jdt.core.dom.rewrite;
12
package org.eclipse.jdt.core.dom.rewrite;
13
13
14
import java.util.ArrayList;
14
import java.util.ArrayList;
15
import java.util.HashMap;
15
import java.util.List;
16
import java.util.List;
17
import java.util.Map;
16
18
17
import org.eclipse.core.runtime.CoreException;
19
import org.eclipse.core.runtime.CoreException;
18
import org.eclipse.core.runtime.IProgressMonitor;
20
import org.eclipse.core.runtime.IProgressMonitor;
Lines 122-127 Link Here
122
124
123
	private final boolean restoreExistingImports;
125
	private final boolean restoreExistingImports;
124
	private final List existingImports;
126
	private final List existingImports;
127
	private final Map importsKindMap;
125
128
126
	private String[] importOrder;
129
	private String[] importOrder;
127
	private int importOnDemandThreshold;
130
	private int importOnDemandThreshold;
Lines 232-237 Link Here
232
		this.importOrder= CharOperation.NO_STRINGS;
235
		this.importOrder= CharOperation.NO_STRINGS;
233
		this.importOnDemandThreshold= 99;
236
		this.importOnDemandThreshold= 99;
234
		this.staticImportOnDemandThreshold= 99;
237
		this.staticImportOnDemandThreshold= 99;
238
		
239
		this.importsKindMap = new HashMap();
235
	}
240
	}
236
241
237
242
Lines 345-351 Link Here
345
			int res= compareImport(prefix, qualifier, name, curr);
350
			int res= compareImport(prefix, qualifier, name, curr);
346
			if (res != ImportRewriteContext.RES_NAME_UNKNOWN) {
351
			if (res != ImportRewriteContext.RES_NAME_UNKNOWN) {
347
				if (!allowAmbiguity || res == ImportRewriteContext.RES_NAME_FOUND) {
352
				if (!allowAmbiguity || res == ImportRewriteContext.RES_NAME_FOUND) {
348
					return res;
353
					if (prefix != STATIC_PREFIX) {
354
						return res;
355
					}
356
					Object currKind = this.importsKindMap.get(curr.substring(1));
357
					if (currKind != null && currKind.equals(this.importsKindMap.get(qualifier + '.' + name))) {
358
						return res;
359
					}
349
				}
360
				}
350
			}
361
			}
351
		}
362
		}
Lines 832-850 Link Here
832
	 * an import conflict prevented the import.
843
	 * an import conflict prevented the import.
833
	 */
844
	 */
834
	public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
845
	public String addStaticImport(String declaringTypeName, String simpleName, boolean isField, ImportRewriteContext context) {
846
		String key = declaringTypeName + '.' + simpleName;
835
		if (declaringTypeName.indexOf('.') == -1) {
847
		if (declaringTypeName.indexOf('.') == -1) {
836
			return declaringTypeName + '.' + simpleName;
848
			return key;
837
		}
849
		}
838
		if (context == null) {
850
		if (context == null) {
839
			context= this.defaultContext;
851
			context= this.defaultContext;
840
		}
852
		}
841
		int kind= isField ? ImportRewriteContext.KIND_STATIC_FIELD : ImportRewriteContext.KIND_STATIC_METHOD;
853
		int kind= isField ? ImportRewriteContext.KIND_STATIC_FIELD : ImportRewriteContext.KIND_STATIC_METHOD;
854
		this.importsKindMap.put(key, new Integer(kind));
842
		int res= context.findInContext(declaringTypeName, simpleName, kind);
855
		int res= context.findInContext(declaringTypeName, simpleName, kind);
843
		if (res == ImportRewriteContext.RES_NAME_CONFLICT) {
856
		if (res == ImportRewriteContext.RES_NAME_CONFLICT) {
844
			return declaringTypeName + '.' + simpleName;
857
			return key;
845
		}
858
		}
846
		if (res == ImportRewriteContext.RES_NAME_UNKNOWN) {
859
		if (res == ImportRewriteContext.RES_NAME_UNKNOWN) {
847
			addEntry(STATIC_PREFIX + declaringTypeName + '.' + simpleName);
860
			addEntry(STATIC_PREFIX + key);
848
		}
861
		}
849
		return simpleName;
862
		return simpleName;
850
	}
863
	}
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ImportRewriteTest.java (-1 / +58 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 922-927 Link Here
922
		assertEqualString(cu.getSource(), buf.toString());
922
		assertEqualString(cu.getSource(), buf.toString());
923
	}
923
	}
924
924
925
	public void testBug252379() throws CoreException, BackingStoreException,
926
			MalformedTreeException, BadLocationException {
927
		
928
929
		ICompilationUnit[] units = new ICompilationUnit[3];
930
		
931
		IPackageFragment pack1 = this.sourceFolder.createPackageFragment(
932
				"bug", false, null);
933
934
		StringBuffer buf = new StringBuffer();
935
		buf.append("package bug;\n");
936
		buf.append("\n");
937
		buf.append("enum CaseType {\n");
938
		buf.append("\tone;\n");
939
		buf.append("\tstatic CaseType[] all(){return null;}\n");
940
		buf.append("}\n");
941
		
942
		units[0] = pack1.createCompilationUnit("CaseType.java", buf.toString(), false, null);
943
		
944
		buf = new StringBuffer();
945
		buf.append("package bug;\n");
946
		buf.append("enum ShareLevel{all})\n");
947
		
948
		units[1] = pack1.createCompilationUnit("ShareLevel.java", buf.toString(), false, null);
949
		
950
		buf = new StringBuffer();
951
		buf.append("package bug;\n");
952
		buf.append("class Bug {\n");
953
		buf.append("public ShareLevel createControl() {\n");
954
		buf.append("for (CaseType cat : all())\n");
955
		buf.append("cat.hashCode();\n");
956
		buf.append("ShareLevel temp = all;\n");
957
		buf.append("return temp;\n");
958
		buf.append("};\n");
959
		buf.append("}\n");
960
		units[2] = pack1.createCompilationUnit("Bug.java", buf.toString(), false, null);
961
962
		ImportRewrite imports = newImportsRewrite(units[2], new String[] {}, 99, 99, false);
963
		imports.addStaticImport("bug.CaseType", "all", false);
964
		imports.addStaticImport("bug.ShareLevel", "all", true);
965
966
		apply(imports);
967
968
		buf = new StringBuffer();
969
		buf.append("package bug;\n\n");
970
		buf.append("import static bug.CaseType.all;\n");
971
		buf.append("import static bug.ShareLevel.all;\n\n");
972
		buf.append("class Bug {\n");
973
		buf.append("public ShareLevel createControl() {\n");
974
		buf.append("for (CaseType cat : all())\n");
975
		buf.append("cat.hashCode();\n");
976
		buf.append("ShareLevel temp = all;\n");
977
		buf.append("return temp;\n");
978
		buf.append("};\n");
979
		buf.append("}\n");
980
		assertEqualString(units[2].getSource(), buf.toString());
981
	}
925
982
926
	private void assertAddedAndRemoved(ImportRewrite imports, String[] expectedAdded, String[] expectedRemoved, String[] expectedAddedStatic, String[] expectedRemovedStatic) {
983
	private void assertAddedAndRemoved(ImportRewrite imports, String[] expectedAdded, String[] expectedRemoved, String[] expectedAddedStatic, String[] expectedRemovedStatic) {
927
		assertEqualStringsIgnoreOrder(imports.getAddedImports(), expectedAdded);
984
		assertEqualStringsIgnoreOrder(imports.getAddedImports(), expectedAdded);

Return to bug 252379