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

Collapse All | Expand All

(-)model/org/eclipse/jdt/internal/core/SourceMapper.java (-3 / +13 lines)
Lines 918-923 Link Here
918
	public char[] findSource(String fullName) {
918
	public char[] findSource(String fullName) {
919
		char[] source = null;
919
		char[] source = null;
920
		Object target = JavaModel.getTarget(this.sourcePath, true);
920
		Object target = JavaModel.getTarget(this.sourcePath, true);
921
		String charSet = null;
921
		if (target instanceof IContainer) {
922
		if (target instanceof IContainer) {
922
			IResource res = ((IContainer)target).findMember(fullName);
923
			IResource res = ((IContainer)target).findMember(fullName);
923
			if (res instanceof IFile) {
924
			if (res instanceof IFile) {
Lines 928-933 Link Here
928
				}
929
				}
929
			}
930
			}
930
		} else {
931
		} else {
932
			try {
933
				// https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511
934
				// For a resource inside the workspace, use the encoding set on the resource
935
				if (target instanceof IFile)
936
					charSet = ((IFile)target).getCharset();
937
			} catch (CoreException e) {
938
				// Ignore
939
			}
940
			
931
			// try to get the entry
941
			// try to get the entry
932
			ZipEntry entry = null;
942
			ZipEntry entry = null;
933
			ZipFile zip = null;
943
			ZipFile zip = null;
Lines 937-943 Link Here
937
				entry = zip.getEntry(fullName);
947
				entry = zip.getEntry(fullName);
938
				if (entry != null) {
948
				if (entry != null) {
939
					// now read the source code
949
					// now read the source code
940
					source = readSource(entry, zip);
950
					source = readSource(entry, zip, charSet);
941
				}
951
				}
942
			} catch (CoreException e) {
952
			} catch (CoreException e) {
943
				return null;
953
				return null;
Lines 1275-1285 Link Here
1275
			this.typeDepth = -1;
1285
			this.typeDepth = -1;
1276
		}
1286
		}
1277
	}
1287
	}
1278
	private char[] readSource(ZipEntry entry, ZipFile zip) {
1288
	private char[] readSource(ZipEntry entry, ZipFile zip, String charSet) {
1279
		try {
1289
		try {
1280
			byte[] bytes = Util.getZipEntryByteContent(entry, zip);
1290
			byte[] bytes = Util.getZipEntryByteContent(entry, zip);
1281
			if (bytes != null) {
1291
			if (bytes != null) {
1282
				return Util.bytesToChar(bytes, this.encoding);
1292
				return Util.bytesToChar(bytes, charSet == null ? this.encoding : charSet);
1283
			}
1293
			}
1284
		} catch (IOException e) {
1294
		} catch (IOException e) {
1285
			// ignore
1295
			// ignore
(-)src/org/eclipse/jdt/core/tests/model/EncodingTests.java (+219 lines)
Lines 24-32 Link Here
24
import org.eclipse.core.runtime.CoreException;
24
import org.eclipse.core.runtime.CoreException;
25
import org.eclipse.core.runtime.IProgressMonitor;
25
import org.eclipse.core.runtime.IProgressMonitor;
26
import org.eclipse.core.runtime.NullProgressMonitor;
26
import org.eclipse.core.runtime.NullProgressMonitor;
27
import org.eclipse.core.runtime.Path;
27
import org.eclipse.core.runtime.content.IContentDescription;
28
import org.eclipse.core.runtime.content.IContentDescription;
28
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
29
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
29
import org.eclipse.core.runtime.preferences.InstanceScope;
30
import org.eclipse.core.runtime.preferences.InstanceScope;
31
import org.eclipse.jdt.core.IClasspathEntry;
30
import org.eclipse.jdt.core.ICompilationUnit;
32
import org.eclipse.jdt.core.ICompilationUnit;
31
import org.eclipse.jdt.core.IJavaProject;
33
import org.eclipse.jdt.core.IJavaProject;
32
import org.eclipse.jdt.core.IOpenable;
34
import org.eclipse.jdt.core.IOpenable;
Lines 929-935 Link Here
929
			preferences.flush();
931
			preferences.flush();
930
		}
932
		}
931
	}
933
	}
934
	
935
	/**
936
	 * Bug 303511: Allow to specify encoding for source attachments
937
	 * Test whether the source mapper picks the right encoding for the source attachment as a ZIP in workspace.
938
	 * The encoding could be explicitly set in the absense of which the inherited value from the project
939
	 * is taken.
940
	 *
941
	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511"
942
	 */
943
	public void testBug303511() throws JavaModelException, CoreException {
944
945
		// Set file encoding
946
		String encoding = "Shift-JIS";
947
		IFile zipFile = (IFile) this.encodingProject.findMember("testShiftJIS.zip"); //$NON-NLS-1$
948
		IFile sourceFile = (IFile) this.encodingProject.findMember("src/testShiftJIS/A.java");
949
		
950
		assertNotNull("Cannot find class file!", zipFile);
951
		zipFile.setCharset(encoding, null);
952
953
		// Get class file and compare source (should not be the same as modify charset on zip file has no effect...)
954
		IPackageFragmentRoot root = getPackageFragmentRoot("Encoding", "testShiftJIS.jar");
955
		ISourceReference sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class");
956
		assertNotNull(sourceRef);
957
		String source = sourceRef.getSource();
958
		assertNotNull(source);
959
		String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding));
960
		assertTrue("Sources should be decoded the same way", encodedContents.equals(source));
961
962
		// Reset zip file encoding
963
		zipFile.setCharset(null, null);
964
		String oldEncoding = this.encodingProject.getDefaultCharset();
965
		this.encodingProject.setDefaultCharset(encoding, null);
932
966
967
		root = getPackageFragmentRoot("Encoding", "testShiftJIS.jar");
968
		sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class");
969
		assertNotNull(sourceRef);
970
		source = sourceRef.getSource();
971
		assertNotNull(source);
972
		encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding));
973
		assertTrue("Sources should be decoded the same way", encodedContents.equals(source));
974
975
		// Reset zip file encoding
976
		zipFile.setCharset(null, null);
977
		this.encodingProject.setDefaultCharset(oldEncoding, null);
978
	}
979
980
	/**
981
	 * Bug 303511: Allow to specify encoding for source attachments
982
	 * Test whether the source mapper picks the right encoding for an external source attachment
983
	 * The attachment could be an external folder or external archive and have the encoding 
984
	 * explicitly set. In the absense of encoding for the source attachment resource, the default
985
	 * encoding from the workspace is applied.  
986
	 *
987
	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511"
988
	 */
989
	public void testBug303511a() throws JavaModelException, CoreException {
990
		// Set file encoding
991
		String encoding = "Shift-JIS";
992
		String externalPath = this.encodingProject.getLocation().toOSString() + File.separator + "testShiftJIS.zip";
993
		IFile sourceFile = (IFile) this.encodingProject.findMember("src/testShiftJIS/A.java");
994
		getWorkspaceRoot().setDefaultCharset(encoding, null);
995
		
996
		IClasspathEntry[] entries = this.encodingJavaProject.getRawClasspath();
997
		IClasspathEntry oldEntry = null;
998
		for (int index = 0; index < entries.length; index++) {
999
			IClasspathEntry entry = entries[index];
1000
			if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) {
1001
				oldEntry = entry;
1002
				IClasspathEntry newEntry = JavaCore.newLibraryEntry(entry.getPath(), new Path(externalPath), null);
1003
				entries[index] = newEntry; 
1004
			}
1005
		}
1006
		this.encodingJavaProject.setRawClasspath(entries, null);
1007
		this.encodingJavaProject.getResolvedClasspath(true);
1008
		
1009
		// Get class file and compare source (should not be the same as modify charset on zip file has no effect...)
1010
		IPackageFragmentRoot root = getPackageFragmentRoot("Encoding", "testShiftJIS.jar");
1011
		ISourceReference sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class");
1012
		assertNotNull(sourceRef);
1013
		String source = sourceRef.getSource();
1014
		assertNotNull(source);
1015
		String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding));
1016
		assertTrue("Sources should be decoded the same way", encodedContents.equals(source));
1017
1018
		entries = this.encodingJavaProject.getRawClasspath();
1019
		String sourcePath = this.encodingProject.getLocation().toOSString() + File.separator + "src";
1020
		for (int index = 0; index < entries.length; index++) {
1021
			IClasspathEntry entry = entries[index];
1022
			if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) {
1023
				IClasspathEntry newEntry = JavaCore.newLibraryEntry(entry.getPath(), new Path(sourcePath), null);
1024
				entries[index] = newEntry; 
1025
			}
1026
		}
1027
		this.encodingJavaProject.setRawClasspath(entries, null);
1028
		this.encodingJavaProject.getResolvedClasspath(true);
1029
		
1030
		entries = this.encodingJavaProject.getRawClasspath();
1031
		for (int index = 0; index < entries.length; index++) {
1032
			IClasspathEntry entry = entries[index];
1033
			if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) {
1034
				entries[index] = oldEntry;
1035
			}
1036
		}
1037
		this.encodingJavaProject.setRawClasspath(entries, null);
1038
		this.encodingJavaProject.getResolvedClasspath(true);
1039
		getWorkspaceRoot().setDefaultCharset(wkspEncoding, null);
1040
	}
1041
1042
	/**
1043
	 * Bug 303511: Allow to specify encoding for source attachments
1044
	 * Test that, for a source attachment in form of archives from another project (in the same workspace), the
1045
	 * encoding of the archive (IResource), if set, is used. In the absense of explicit encoding, the encoding 
1046
	 * of the project that contains this archive is used.
1047
	 *
1048
	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511"
1049
	 */
1050
	public void testBug303511b() throws Exception{
1051
		try{
1052
			IJavaProject project = this.createJavaProject("Encoding2", new String[] {""}, "");
1053
			String oldEncoding = this.encodingProject.getDefaultCharset();
1054
			String encoding = "Shift-JIS";
1055
			IFile zipFile = (IFile) this.encodingProject.findMember("testShiftJIS.zip"); //$NON-NLS-1$
1056
			IFile sourceFile = (IFile) this.encodingProject.findMember("src/testShiftJIS/A.java");
1057
			assertNotNull("Cannot find class file!", zipFile);
1058
1059
			zipFile.setCharset(encoding, null);
1060
1061
			IClasspathEntry[] entries = this.encodingJavaProject.getRawClasspath();
1062
			IClasspathEntry newEntry = null;
1063
			for (int index = 0; index < entries.length; index++) {
1064
				IClasspathEntry entry = entries[index];
1065
				if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) {
1066
					newEntry = entries[index]; 
1067
				}
1068
			}
1069
			project.setRawClasspath(new IClasspathEntry[]{JavaCore.newLibraryEntry(newEntry.getPath(), new Path("/Encoding/testShiftJIS.zip"), null)}, null);
1070
1071
			IPackageFragmentRoot root = getPackageFragmentRoot("Encoding2", "testShiftJIS.jar");
1072
			ISourceReference sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class");
1073
			assertNotNull(sourceRef);
1074
			String source = sourceRef.getSource();
1075
			assertNotNull(source);
1076
			String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding));
1077
			assertTrue("Sources should be decoded the same way", encodedContents.equals(source));
1078
1079
			zipFile.setCharset(null, null);
1080
			this.encodingProject.setDefaultCharset(encoding, null);
1081
1082
			root = getPackageFragmentRoot("Encoding2", "testShiftJIS.jar");
1083
			sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class");
1084
			assertNotNull(sourceRef);
1085
			source = sourceRef.getSource();
1086
			assertNotNull(source);
1087
			encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding));
1088
			assertTrue("Sources should be decoded the same way", encodedContents.equals(source));
1089
			
1090
			this.encodingProject.setDefaultCharset(oldEncoding, null);
1091
		}
1092
		finally {
1093
			deleteProject("Encoding2");
1094
		}
1095
	}
1096
	/**
1097
	 * Bug 303511: Allow to specify encoding for source attachments
1098
	 * Test that, for a source attachment in form of folder from another project (in the same workspace), the
1099
	 * encoding of the folder (IResource), if set, is used. In the absense of explicit encoding, the encoding 
1100
	 * of the project which contains this folder is used.
1101
	 *
1102
	 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=303511"
1103
	 */
1104
	public void testBug303511c() throws Exception{
1105
		try{
1106
			IJavaProject project = this.createJavaProject("Encoding2", new String[] {""}, "");
1107
			String oldEncoding = this.encodingProject.getDefaultCharset();
1108
			String encoding = "Shift-JIS";
1109
			IFile zipFile = (IFile) this.encodingProject.findMember("testShiftJIS.zip"); //$NON-NLS-1$
1110
			IFile sourceFile = (IFile) this.encodingProject.findMember("src/testShiftJIS/A.java");
1111
			assertNotNull("Cannot find class file!", zipFile);
1112
			
1113
			IClasspathEntry[] entries = this.encodingJavaProject.getRawClasspath();
1114
			IClasspathEntry newEntry = null;
1115
			for (int index = 0; index < entries.length; index++) {
1116
				IClasspathEntry entry = entries[index];
1117
				if (entry.getPath().toOSString().endsWith("testShiftJIS.jar")) {
1118
					newEntry = entries[index]; 
1119
				}
1120
			}
1121
1122
			project.setRawClasspath(new IClasspathEntry[]{JavaCore.newLibraryEntry(newEntry.getPath(), new Path("/Encoding/src"), null)}, null);
1123
			this.encodingProject.setDefaultCharset(encoding, null);
1124
			sourceFile.setCharset(null, null);
1125
			
1126
			IPackageFragmentRoot root = getPackageFragmentRoot("Encoding2", "testShiftJIS.jar");
1127
			ISourceReference sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class");
1128
			assertNotNull(sourceRef);
1129
			String source = sourceRef.getSource();
1130
			assertNotNull(source);
1131
			String encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding));
1132
			assertTrue("Sources should be decoded the same way", encodedContents.equals(source));
1133
			
1134
			this.encodingProject.setDefaultCharset(oldEncoding, null);
1135
			sourceFile.setCharset(encoding, null);
1136
1137
			root = getPackageFragmentRoot("Encoding2", "testShiftJIS.jar");
1138
			sourceRef = root.getPackageFragment("testShiftJIS").getClassFile("A.class");
1139
			assertNotNull(sourceRef);
1140
			source = sourceRef.getSource();
1141
			assertNotNull(source);
1142
			encodedContents = new String (Util.getResourceContentsAsCharArray(sourceFile, encoding));
1143
			assertTrue("Sources should be decoded the same way", encodedContents.equals(source));	
1144
			
1145
			sourceFile.setCharset(null, null);
1146
		}
1147
		finally {
1148
			deleteProject("Encoding2");
1149
		}
1150
	}
1151
	
933
	private void verifyUtf8BOM(IFile file) throws CoreException {
1152
	private void verifyUtf8BOM(IFile file) throws CoreException {
934
		assertNull("File should not have any explicit charset", file.getCharset(false));
1153
		assertNull("File should not have any explicit charset", file.getCharset(false));
935
		IContentDescription contentDescription = file.getContentDescription();
1154
		IContentDescription contentDescription = file.getContentDescription();

Return to bug 303511