|
@@ -7,7 +7,6 @@ import java.io.IOException;
|
|
import java.net.MalformedURLException;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.net.URLClassLoader;
|
|
import java.net.URLClassLoader;
|
|
-import java.nio.file.Files;
|
|
|
|
import java.util.LinkedList;
|
|
import java.util.LinkedList;
|
|
|
|
|
|
import javax.tools.JavaCompiler;
|
|
import javax.tools.JavaCompiler;
|
|
@@ -299,21 +298,9 @@ public class ImportController {
|
|
* @return Class which was compiled
|
|
* @return Class which was compiled
|
|
* @throws ClassNotFoundException on invalid package declaration or invalid file name
|
|
* @throws ClassNotFoundException on invalid package declaration or invalid file name
|
|
* @throws MalformedURLException if the URL was malformed
|
|
* @throws MalformedURLException if the URL was malformed
|
|
|
|
+ * @throws ClassImportException
|
|
*/
|
|
*/
|
|
- 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());
|
|
|
|
- e.printStackTrace();
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static Class<?> importJavaClass(File javaFile,String path)
|
|
|
|
- throws ClassNotFoundException, MalformedURLException {
|
|
|
|
- //System.out.println("File Name: "+javaFile.getPath());
|
|
|
|
|
|
+ public static Class<?> importJavaClass(File javaFile) throws ClassImportException {
|
|
/**
|
|
/**
|
|
* Package name
|
|
* Package name
|
|
*/
|
|
*/
|
|
@@ -336,134 +323,59 @@ public class ImportController {
|
|
compiler.run(null, null, null, javaFile.getPath());
|
|
compiler.run(null, null, null, javaFile.getPath());
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Get Root File of the imported Protocol (if in package)
|
|
|
|
|
|
+ * Root File of the imported Protocol (if the Java File is inside a package)
|
|
*/
|
|
*/
|
|
File root = javaFile.getParentFile();
|
|
File root = javaFile.getParentFile();
|
|
- //de.tu_darmstadt.tk.SmartHomeNetworkSim.control
|
|
|
|
if(!packageName.isEmpty()){
|
|
if(!packageName.isEmpty()){
|
|
- System.out.println("Checking package name: "+packageName);
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Array of all parent package names e.g. "pack1.pack2.pack3" -> {"pack1","pack2","pack3"}
|
|
|
|
+ */
|
|
String[] packageFolders = packageName.split("\\.");
|
|
String[] packageFolders = packageName.split("\\.");
|
|
if(packageFolders.length<2){
|
|
if(packageFolders.length<2){
|
|
|
|
+ //A single level
|
|
if(root.getName().compareTo(packageName)!=0){
|
|
if(root.getName().compareTo(packageName)!=0){
|
|
- System.out.println("Parent Folder of class was unexpected: "+root.getName()+" instead of "+packageName);
|
|
|
|
- return null;
|
|
|
|
|
|
+ throw new ClassImportException("Invalid ParentFolderName: Expected \""+packageName+"\" but was \""+root.getName()+"\"");
|
|
}
|
|
}
|
|
root = root.getParentFile();
|
|
root = root.getParentFile();
|
|
}else{
|
|
}else{
|
|
for(int i = packageFolders.length-1;i>=0;i--){
|
|
for(int i = packageFolders.length-1;i>=0;i--){
|
|
if(root.getName().compareTo(packageFolders[i])!=0){
|
|
if(root.getName().compareTo(packageFolders[i])!=0){
|
|
- System.out.println("Parent Folder at level "+i+ " of class was unexpected: "+root.getName()+" instead of "+packageFolders[i]);
|
|
|
|
- return null;
|
|
|
|
|
|
+ throw new ClassImportException("Invalid ParentFolderName at level "+i+": Expected \""+packageFolders[i]+"\" but was \""+root.getName()+"\"");
|
|
}
|
|
}
|
|
root = root.getParentFile();
|
|
root = root.getParentFile();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- System.out.println("Package structure valid");
|
|
|
|
- for(int i = 0; i<packageFolders.length; i++)
|
|
|
|
- System.out.println(packageFolders[i]);
|
|
|
|
- System.out.println("Root: "+root.getPath());
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- * ClassLoader to load the compiled class
|
|
|
|
|
|
+ * ClassLoader to load the compiled class, given it's root
|
|
*/
|
|
*/
|
|
- //URLClassLoader classLoader = URLClassLoader/* src/ */
|
|
|
|
- // .newInstance(new URL[] { new File(path).toURI().toURL() });
|
|
|
|
- URLClassLoader classLoader = URLClassLoader/* src/ */
|
|
|
|
- .newInstance(new URL[] { root.toURI().toURL() });
|
|
|
|
|
|
+ URLClassLoader classLoader;
|
|
|
|
+ try {
|
|
|
|
+ classLoader = URLClassLoader
|
|
|
|
+ .newInstance(new URL[] { root.toURI().toURL() });
|
|
|
|
+ } catch (MalformedURLException e1) {
|
|
|
|
+ throw new ClassImportException("Invalid URL/File at the root of the imported Class");
|
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
|
- // If packageName is not empty, and dot is required:
|
|
|
|
- // "packagePath.ClassName"
|
|
|
|
|
|
+ // If packageName is not empty, and dot is required: e.g. "packagePath.ClassName"
|
|
if (!packageName.isEmpty())
|
|
if (!packageName.isEmpty())
|
|
packageName += ".";
|
|
packageName += ".";
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Imported Class
|
|
|
|
+ */
|
|
Class<?> cls = null;
|
|
Class<?> cls = null;
|
|
try{
|
|
try{
|
|
cls = Class.forName(packageName + className, true, classLoader);
|
|
cls = Class.forName(packageName + className, true, classLoader);
|
|
}catch(Exception e){
|
|
}catch(Exception e){
|
|
try{
|
|
try{
|
|
|
|
+ //Second try to load class
|
|
cls = classLoader.loadClass(packageName + className);
|
|
cls = classLoader.loadClass(packageName + className);
|
|
}catch(Exception e2){
|
|
}catch(Exception e2){
|
|
e2.printStackTrace();
|
|
e2.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if(cls != null){
|
|
|
|
- System.out.println("Preload succes");
|
|
|
|
- return cls;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 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!=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());
|
|
|
|
- try {
|
|
|
|
- Files.copy(source.toPath(), destination.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
- System.out.println("File moved ?");
|
|
|
|
- /**
|
|
|
|
- * Wait until file was fully moved
|
|
|
|
- *//*
|
|
|
|
- int i = 0;
|
|
|
|
- int maxTrys = 50;
|
|
|
|
- long lastSpace = -1;
|
|
|
|
- while(i < maxTrys && (!destination.exists() || destination.length()!= lastSpace)){
|
|
|
|
- System.out.print("i: "+i);
|
|
|
|
- if(destination.exists()){
|
|
|
|
- lastSpace = destination.length();
|
|
|
|
- System.out.print(" Space: "+lastSpace);
|
|
|
|
- }
|
|
|
|
- System.out.println("");
|
|
|
|
- i++;
|
|
|
|
- try {
|
|
|
|
- Thread.sleep(100);
|
|
|
|
- } catch (InterruptedException e) {
|
|
|
|
- System.out.println("Sleep failed");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- } catch (IOException e1) {
|
|
|
|
- System.out.println("Moving File failed while Importing Class");
|
|
|
|
- e1.printStackTrace();
|
|
|
|
- }
|
|
|
|
- /*
|
|
|
|
- FileChannel sourceChannel = null;
|
|
|
|
- FileChannel destChannel = null;
|
|
|
|
- FileInputStream fIn = null;
|
|
|
|
- FileOutputStream fOut = null;
|
|
|
|
- try {
|
|
|
|
- fIn = new FileInputStream(source);
|
|
|
|
- sourceChannel = fIn.getChannel();
|
|
|
|
- fOut = new FileOutputStream(destination);
|
|
|
|
- destChannel = fOut.getChannel();
|
|
|
|
- destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
|
|
|
|
- sourceChannel.close();
|
|
|
|
- destChannel.close();
|
|
|
|
- }catch(Exception e){}
|
|
|
|
- finally{
|
|
|
|
- try{
|
|
|
|
- sourceChannel.close();
|
|
|
|
- destChannel.close();
|
|
|
|
- }catch(Exception e){}
|
|
|
|
- }*
|
|
|
|
- //System.out.println(destination.getCanonicalPath()+" exists: "+destination.exists());
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Loaded Class
|
|
|
|
- *//*
|
|
|
|
- System.out.println("Destination exists?: " + destination.exists());
|
|
|
|
- //Class<?> cls;
|
|
|
|
- try{
|
|
|
|
- cls = Class.forName(packageName + className, true, classLoader);
|
|
|
|
- }catch(Exception e){
|
|
|
|
- cls = classLoader.loadClass(packageName + className);
|
|
|
|
- }*/
|
|
|
|
- //source.delete();
|
|
|
|
return cls;
|
|
return cls;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -476,6 +388,7 @@ public class ImportController {
|
|
* File to be searched for the package path
|
|
* File to be searched for the package path
|
|
* @return PackagePath of the JavaFile, EmptyString, if no declaration was
|
|
* @return PackagePath of the JavaFile, EmptyString, if no declaration was
|
|
* found, null on errors
|
|
* found, null on errors
|
|
|
|
+ * @throws ClassImportException
|
|
*/
|
|
*/
|
|
public static String getJavaPackageName(File javaFile) {
|
|
public static String getJavaPackageName(File javaFile) {
|
|
/**
|
|
/**
|
|
@@ -497,6 +410,7 @@ public class ImportController {
|
|
while (currentLine != null) {
|
|
while (currentLine != null) {
|
|
currentLine = currentLine.trim();
|
|
currentLine = currentLine.trim();
|
|
if (!currentLine.isEmpty()) {
|
|
if (!currentLine.isEmpty()) {
|
|
|
|
+ //Check if line begins with package
|
|
if (currentLine.length() >= 7 && currentLine.startsWith("package")) {
|
|
if (currentLine.length() >= 7 && currentLine.startsWith("package")) {
|
|
packageName = currentLine.substring(8, currentLine.length() - 1);
|
|
packageName = currentLine.substring(8, currentLine.length() - 1);
|
|
}
|
|
}
|
|
@@ -508,11 +422,6 @@ public class ImportController {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- try {
|
|
|
|
- reader.close();
|
|
|
|
- } catch (IOException e1) {
|
|
|
|
- }
|
|
|
|
- return null;
|
|
|
|
} finally {
|
|
} finally {
|
|
try {
|
|
try {
|
|
reader.close();
|
|
reader.close();
|
|
@@ -523,30 +432,6 @@ public class ImportController {
|
|
packageName = packageName.trim();
|
|
packageName = packageName.trim();
|
|
return packageName;
|
|
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", "../");
|
|
|
|
- testFileCompilation("testCompilation/packageTest/deepPackage/reallyDeepPackage/MQTT_protocolDeepPackageTest.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();
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
|
|
}
|
|
}
|