1- package com .sandwich .util .io ;
1+ package com .sandwich .util .io . filecompiler ;
22
33import java .io .File ;
44import java .io .FileNotFoundException ;
55import java .io .IOException ;
6+ import java .util .HashMap ;
7+ import java .util .Map ;
68
9+ import com .sandwich .util .io .FileUtils ;
710import com .sandwich .util .io .ui .DefaultErrorPresenter ;
811import com .sandwich .util .io .ui .ErrorPresenter ;
912
1013public class FileCompiler {
1114
15+ private static final Map <String , String > sourceFileToClassFile = new HashMap <String , String >();
16+ private static final Map <String , String > classFileToSourceFile = new HashMap <String , String >();
17+
1218 private static final String DOLLAR_SIGN = "$" ;
13- public static final String JAVA_SUFFIX = ".java" ;
1419 public static final String CLASS_SUFFIX = ".class" ;
20+ public static final String JAVA_SUFFIX = ".java" ;
1521
1622 public static void compile (String src , String bin ) throws IOException {
1723 compile (new DefaultErrorPresenter (), new File (src ), new File (bin ));
@@ -40,32 +46,46 @@ public static void compile(File src, File bin,
4046 }
4147 }
4248 FileUtils .forEachFile (src , bin , new FileCompilerAction (bin , listener , timeout , classpath ));
49+ String srcPath = src .getAbsolutePath ();
50+ String classPath = srcPath ;
51+ for (String suffix : CompilerConfig .getSupportedFileSuffixes ()){
52+ if (classPath .endsWith (suffix )){
53+ classPath = classPath .replace (suffix , CLASS_SUFFIX );
54+ }
55+ }
56+ sourceFileToClassFile .put (srcPath , classPath );
57+ classFileToSourceFile .put (classPath , srcPath );
4358 }
4459
4560 public static String getContentsOfJavaFile (String sourceDir , String className ) {
4661 return FileUtils .readFileAsString (getSourceFileFromClass (sourceDir , className ));
4762 }
4863
4964 public static File getSourceFileFromClass (String sourceDir , String className ) {
50- File sourceFile = new File (
51- sourceDir + System .getProperty ("file.separator" ) +
52- classNameToJavaFileName (className ));
53- if (!sourceFile .exists ()) {
54- throw new IllegalArgumentException (new FileNotFoundException (
55- sourceFile .getAbsolutePath () + " does not exist" ));
56- }
57- return sourceFile ;
58- }
59-
60- public static String classNameToJavaFileName (String className ) {
61- className = className .replace ("." , System .getProperty ("file.separator" ));
6265 if (className .contains (DOLLAR_SIGN )){
6366 className = className .substring (0 , className .indexOf (DOLLAR_SIGN ));
6467 }
65- return className + JAVA_SUFFIX ;
68+ File possibleSourceFile = new File (sourceDir );
69+ File sourceFile = null ;
70+ for (String folder : className .split ("\\ ." )){
71+ possibleSourceFile = new File (possibleSourceFile , folder );
72+ }
73+ for (String suffix : CompilerConfig .getSupportedFileSuffixes ()){
74+ File file = new File (possibleSourceFile .getAbsolutePath () + suffix );
75+ if (file .exists ()){
76+ sourceFile = file ;
77+ break ;
78+ }
79+ }
80+ if (sourceFile == null || !sourceFile .exists ()) {
81+ throw new IllegalArgumentException (new FileNotFoundException (
82+ sourceFile == null ? null : sourceFile .getAbsolutePath () + " does not exist" ));
83+ }
84+ return sourceFile ;
6685 }
6786
6887 public static File sourceToClass (String sourceDir , String binDir , File file ) {
88+ //C:\Users\sandwich\Development\koans\koans\app\bin\beginner\AboutKoans.class
6989 return new File (file .getAbsolutePath ()
7090 .replace (sourceDir , binDir ).replace (JAVA_SUFFIX , CLASS_SUFFIX ));
7191 }
@@ -81,6 +101,7 @@ public static File classToClassFile(Class<?> clazz) {
81101 }
82102
83103 public static File classToSource (String binDir , String sourceDir , String absolutePath ) {
104+ //C:\Users\sandwich\Development\koans\koans\src\beginner\AboutKoans.java
84105 return new File (absolutePath
85106 .replace (binDir , sourceDir ).replace (CLASS_SUFFIX , JAVA_SUFFIX ));
86107 }
0 commit comments