Quellcode durchsuchen

Fixes Import of Classes, while runnning via gradle

Andreas T. Meyer-Berg vor 5 Jahren
Ursprung
Commit
dbd83d1c73

+ 51 - 15
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/ImportController.java

@@ -304,6 +304,17 @@ public class ImportController {
 	 */
 	public static Class<?> importJavaClass(File javaFile)
 			throws ClassNotFoundException, MalformedURLException {
+		try{
+		return importJavaClass(javaFile, new File("bin/main/").getAbsolutePath());
+		}catch(Exception e){
+			System.out.println("Import failed: "+e.getMessage());
+			return null;
+		}
+	}
+	
+	public static Class<?> importJavaClass(File javaFile,String path)
+			throws ClassNotFoundException, MalformedURLException {
+		//System.out.println("File Name: "+javaFile.getPath());
 		/**
 		 * Compiler, to compile the File
 		 */
@@ -314,7 +325,7 @@ public class ImportController {
 		 * ClassLoader to load the compiled class
 		 */
 		URLClassLoader classLoader = URLClassLoader/* src/ */
-				.newInstance(new URL[] { new File("/bin/main/").toURI().toURL() });
+				.newInstance(new URL[] { new File(path).toURI().toURL() });
 
 		/**
 		 * Package name
@@ -339,17 +350,21 @@ public class ImportController {
 		 * Check if compiled class file exists in the projects class path - if not, copy it into the project 
 		 */
 		File destination = new File("bin/main/"+packageName.replace('.', '/')+(packageName.isEmpty()?"":"/")+className+".class");
-		if(!destination.exists())destination.mkdirs();
+		if(destination!=null&&destination.getParent()!=null&&!destination.getParentFile().exists())destination.getParentFile().mkdirs();
 		
 		File source = new File(javaFile.getParent()+"/"+className+".class");
-		System.out.println("Copy:");
-		System.out.println("From: " + source.getAbsolutePath());
-		System.out.println("To:   " + destination.getAbsolutePath());
+		//System.out.println("Copy:");
+		//System.out.println("From: " + source.getAbsolutePath());
+		//System.out.println("To:   " + destination.getAbsolutePath());
 		FileChannel sourceChannel = null;
 	    FileChannel destChannel = null;
+	    FileInputStream fIn = null;
+	    FileOutputStream fOut = null;
 	    try {
-	    	sourceChannel = new FileInputStream(source).getChannel();
-	        destChannel = new FileOutputStream(destination).getChannel();
+	    	fIn = new FileInputStream(source);
+	    	sourceChannel = fIn.getChannel();
+	    	fOut = new FileOutputStream(destination);
+	        destChannel = fOut.getChannel();
 	        destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
 	        sourceChannel.close();
 	    	destChannel.close();
@@ -360,13 +375,8 @@ public class ImportController {
 	    	destChannel.close();
 	    	}catch(Exception e){}
 	    }
-		try {
-			System.out.println(destination.getCanonicalPath()+" exists: "+destination.exists());
-		} catch (IOException e1) {
-			// TODO Auto-generated catch block
-			e1.printStackTrace();
-		}
-		
+		//System.out.println(destination.getCanonicalPath()+" exists: "+destination.exists());
+
 		/**
 		 * Loaded Class
 		 */
@@ -376,9 +386,10 @@ public class ImportController {
 		}catch(Exception e){
 			cls = classLoader.loadClass(packageName + className);
 		}
+		source.delete();
 		return cls;
 	}
-
+	
 	/**
 	 * Returns the package path of a given Java File. Returns null if the
 	 * exception occurred and returns an empty string if no package declaration
@@ -435,4 +446,29 @@ public class ImportController {
 		packageName = packageName.trim();
 		return packageName;
 	}
+	
+	public static void main(String[] args) {
+		testFileCompilation("src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/MQTT_protocol.java", "src/");
+		testFileCompilation("testCompilation/project1/MQTT_protocolProject1.java", "/bin/main/");
+		testFileCompilation("testCompilation/packageTest/MQTT_protocolPackageTest.java", "/bin/main/");
+		testFileCompilation("src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/MQTT_protocol.java", "src/");
+		testFileCompilation("testCompilation/project1/MQTT_protocolProject1.java", "src/");
+		testFileCompilation("testCompilation/packageTest/MQTT_protocolPackageTest.java", "../");
+	}
+	
+	private static void testFileCompilation(String pathToFile, String classPath){
+		System.out.println("Test: " + pathToFile);
+		System.out.println("Class Path: " + classPath);
+		try {
+			Class<?> c  = importJavaClass(new File(pathToFile), classPath);
+			if(c!=null)
+				System.out.println("Import success: "+c.getSimpleName());
+			else
+				System.out.println("Import null");
+		} catch (Exception e) {
+			System.out.println("Import failed: "+e.toString());
+		}
+		System.out.println();
+	}
+	
 }