View | Details | Raw Unified | Return to bug 308256
Collapse All | Expand All

(-)src/org/eclipse/jdt/internal/compiler/tool/EclipseCompilerImpl.java (-18 / +6 lines)
Lines 206-220 Link Here
206
						}
206
						}
207
						@Override
207
						@Override
208
						public JavaFileObject getSource() {
208
						public JavaFileObject getSource() {
209
							try {
209
							File f = new File(new String(originatingFileName));
210
								if (EclipseCompilerImpl.this.fileManager.hasLocation(StandardLocation.SOURCE_PATH)) {
210
							if (f.exists()) {
211
									return EclipseCompilerImpl.this.fileManager.getJavaFileForInput(
211
								return new EclipseFileObject(null, f.toURI(), JavaFileObject.Kind.SOURCE, null);
212
											StandardLocation.SOURCE_PATH,
213
											new String(originatingFileName),
214
											JavaFileObject.Kind.SOURCE);
215
								}
216
							} catch (IOException e) {
217
								// ignore
218
							}
212
							}
219
							return null;
213
							return null;
220
						}
214
						}
Lines 282-296 Link Here
282
						}
276
						}
283
						@Override
277
						@Override
284
						public JavaFileObject getSource() {
278
						public JavaFileObject getSource() {
285
							try {
279
							File f = new File(new String(originatingFileName));
286
								if (EclipseCompilerImpl.this.fileManager.hasLocation(StandardLocation.SOURCE_PATH)) {
280
							if (f.exists()) {
287
									return EclipseCompilerImpl.this.fileManager.getJavaFileForInput(
281
								return new EclipseFileObject(null, f.toURI(), JavaFileObject.Kind.SOURCE, null);
288
											StandardLocation.SOURCE_PATH,
289
											new String(originatingFileName),
290
											JavaFileObject.Kind.SOURCE);
291
								}
292
							} catch (IOException e) {
293
								// ignore
294
							}
282
							}
295
							return null;
283
							return null;
296
						}
284
						}
(-)src/org/eclipse/jdt/compiler/tool/tests/CompilerToolTests.java (+122 lines)
Lines 26-31 Link Here
26
import java.util.ServiceLoader;
26
import java.util.ServiceLoader;
27
import java.util.Set;
27
import java.util.Set;
28
28
29
import javax.tools.Diagnostic;
29
import javax.tools.FileObject;
30
import javax.tools.FileObject;
30
import javax.tools.ForwardingJavaFileManager;
31
import javax.tools.ForwardingJavaFileManager;
31
import javax.tools.JavaCompiler;
32
import javax.tools.JavaCompiler;
Lines 65-70 Link Here
65
		suite.addTest(new CompilerToolTests("testCompilerOneClassWithEclipseCompiler3"));
66
		suite.addTest(new CompilerToolTests("testCompilerOneClassWithEclipseCompiler3"));
66
		suite.addTest(new CompilerToolTests("testCompilerOneClassWithEclipseCompiler4"));
67
		suite.addTest(new CompilerToolTests("testCompilerOneClassWithEclipseCompiler4"));
67
		suite.addTest(new CompilerToolTests("testCompilerOneClassWithEclipseCompiler5"));
68
		suite.addTest(new CompilerToolTests("testCompilerOneClassWithEclipseCompiler5"));
69
		suite.addTest(new CompilerToolTests("testCompilerOneClassWithEclipseCompiler6"));
70
		suite.addTest(new CompilerToolTests("testCompilerOneClassWithEclipseCompiler7"));
68
		suite.addTest(new CompilerToolTests("testCleanUp"));
71
		suite.addTest(new CompilerToolTests("testCleanUp"));
69
		return suite;
72
		return suite;
70
	}
73
	}
Lines 688-693 Link Here
688
		assertTrue("delete failed", inputFile.delete());
691
		assertTrue("delete failed", inputFile.delete());
689
	}
692
	}
690
693
694
	public void testCompilerOneClassWithEclipseCompiler6() {
695
		String tmpFolder = System.getProperty("java.io.tmpdir");
696
		File packageFolder = new File(tmpFolder, "p");
697
		if (!packageFolder.mkdirs()) {
698
			return;
699
		}
700
		File inputFile = new File(packageFolder, "X.java");
701
		BufferedWriter writer = null;
702
		try {
703
			writer = new BufferedWriter(new FileWriter(inputFile));
704
			writer.write(
705
				"package p;\n" +
706
				"public class X extends File {}");
707
			writer.flush();
708
			writer.close();
709
		} catch (IOException e) {
710
			// ignore
711
		} finally {
712
			if (writer != null) {
713
				try {
714
					writer.close();
715
				} catch (IOException e) {
716
					// ignore
717
				}
718
			}
719
		}
720
		// System compiler
721
		StandardJavaFileManager manager = Compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset());
722
		List<File> files = new ArrayList<File>();
723
		files.add(inputFile);
724
		Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromFiles(files);
725
	
726
		List<String> options = new ArrayList<String>();
727
		options.add("-d");
728
		options.add(tmpFolder);
729
		options.add("-sourcepath");
730
		options.add(tmpFolder);
731
		ByteArrayOutputStream errBuffer = new ByteArrayOutputStream();
732
		PrintWriter err = new PrintWriter(errBuffer);
733
		CompilerInvocationDiagnosticListener compilerInvocationDiagnosticListener = new CompilerInvocationDiagnosticListener(err) {
734
			@Override
735
			public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
736
				JavaFileObject source = diagnostic.getSource();
737
				assertNotNull("No source", source);
738
				super.report(diagnostic);
739
			}
740
		};
741
		CompilationTask task = Compiler.getTask(null, manager, compilerInvocationDiagnosticListener, options, null, units);
742
		// check the classpath location
743
		Boolean result = task.call();
744
		err.flush();
745
		err.close();
746
		assertFalse(errBuffer.toString().isEmpty());
747
		assertTrue(compilerInvocationDiagnosticListener.kind != CompilerInvocationDiagnosticListener.NONE);
748
		if (!result.booleanValue()) {
749
			assertFalse("Compilation did not fail", false);
750
		}
751
		// check that the .class file exist for X
752
		assertTrue("delete failed", inputFile.delete());
753
		assertTrue("delete failed", packageFolder.delete());
754
	}
755
756
	public void testCompilerOneClassWithEclipseCompiler7() {
757
		String tmpFolder = System.getProperty("java.io.tmpdir");
758
		File inputFile = new File(tmpFolder, "X.java");
759
		BufferedWriter writer = null;
760
		try {
761
			writer = new BufferedWriter(new FileWriter(inputFile));
762
			writer.write(
763
				"package p;\n" +
764
				"public class X extends File {}");
765
			writer.flush();
766
			writer.close();
767
		} catch (IOException e) {
768
			// ignore
769
		} finally {
770
			if (writer != null) {
771
				try {
772
					writer.close();
773
				} catch (IOException e) {
774
					// ignore
775
				}
776
			}
777
		}
778
		// System compiler
779
		StandardJavaFileManager manager = Compiler.getStandardFileManager(null, Locale.getDefault(), Charset.defaultCharset());
780
		List<File> files = new ArrayList<File>();
781
		files.add(inputFile);
782
		Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromFiles(files);
783
	
784
		List<String> options = new ArrayList<String>();
785
		options.add("-d");
786
		options.add(tmpFolder);
787
		options.add("-sourcepath");
788
		options.add(tmpFolder);
789
		ByteArrayOutputStream errBuffer = new ByteArrayOutputStream();
790
		PrintWriter err = new PrintWriter(errBuffer);
791
		CompilerInvocationDiagnosticListener compilerInvocationDiagnosticListener = new CompilerInvocationDiagnosticListener(err) {
792
			@Override
793
			public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
794
				JavaFileObject source = diagnostic.getSource();
795
				assertNotNull("No source", source);
796
				super.report(diagnostic);
797
			}
798
		};
799
		CompilationTask task = Compiler.getTask(null, manager, compilerInvocationDiagnosticListener, options, null, units);
800
		// check the classpath location
801
		Boolean result = task.call();
802
		err.flush();
803
		err.close();
804
		assertFalse(errBuffer.toString().isEmpty());
805
		assertTrue(compilerInvocationDiagnosticListener.kind != CompilerInvocationDiagnosticListener.NONE);
806
		if (!result.booleanValue()) {
807
			assertFalse("Compilation did not fail", false);
808
		}
809
		// check that the .class file exist for X
810
		assertTrue("delete failed", inputFile.delete());
811
	}
812
691
	public void testFileManager() {
813
	public void testFileManager() {
692
		String tmpFolder = System.getProperty("java.io.tmpdir");
814
		String tmpFolder = System.getProperty("java.io.tmpdir");
693
		File dir = new File(tmpFolder, "src" + System.currentTimeMillis());
815
		File dir = new File(tmpFolder, "src" + System.currentTimeMillis());

Return to bug 308256