Skip to content

Commit 12ef4c0

Browse files
committed
Tested class loading functionality
1 parent 94274ba commit 12ef4c0

1 file changed

Lines changed: 63 additions & 36 deletions

File tree

src/de/littlerolf/sav/loader/SorterLoader.java

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,79 +8,106 @@
88
import java.util.ArrayList;
99
import java.util.List;
1010

11-
1211
/**
13-
* The Main Class of the Loader module, used for loading and accessing the specific Sorters
12+
* The Main Class of the Loader module, used for loading and accessing the
13+
* specific Sorters
14+
*
1415
* @author ole
15-
*
16+
*
1617
*/
1718
public class SorterLoader {
1819

1920
private final String classpath;
20-
21+
private boolean debug = false;
22+
2123
private List<Class> classes = new ArrayList<Class>();
22-
24+
2325
/**
2426
* Creates a new SorterLoader looking for classes in the given path
25-
* @param path The path to look for classes
27+
*
28+
* @param path
29+
* The path to look for classes
2630
*/
2731
public SorterLoader(String path) {
2832
classpath = path;
29-
33+
34+
}
35+
36+
/**
37+
* Creates a new SorterLoader looking for classes in the given path and
38+
* offers ability to enable debug output
39+
*
40+
* @param path
41+
* The path to look for classes
42+
* @param debug
43+
* Debug Flag
44+
*/
45+
public SorterLoader(String path, boolean debug) {
46+
classpath = path;
47+
this.debug = debug;
3048
}
31-
49+
3250
/**
33-
* Loads all classes named 'Sorter' from the set folder from all the subdirectories
51+
* Loads all classes named 'Sorter' from the set folder from all the
52+
* subdirectories
3453
*/
3554
public void loadAllClasses() {
36-
// Create a File object on the root of the directory containing the class file
55+
// Create a File object on the root of the directory containing the
56+
// class file
3757
File file = new File(classpath);
3858

3959
try {
40-
// Convert File to a URL
41-
URL url = file.toURI().toURL(); // file:/c:/myclasses/
42-
URL[] urls = new URL[]{url};
60+
// Convert File to a URL
61+
URL url = file.toURI().toURL(); // file:/c:/myclasses/
62+
URL[] urls = new URL[] { url };
4363

44-
// Create a new class loader with the directory
45-
ClassLoader cl = new URLClassLoader(urls);
64+
// Create a new class loader with the directory
65+
ClassLoader cl = new URLClassLoader(urls);
4666

47-
String[] directories = getSubdirectories();
48-
49-
50-
for(String folder : directories) {
51-
classes.add(cl.loadClass(folder + ".Sorter"));
52-
}
67+
String[] directories = getSubdirectories();
68+
69+
for (String folder : directories) {
70+
classes.add(cl.loadClass(folder + ".Sorter"));
71+
}
5372

54-
5573
} catch (MalformedURLException e) {
5674
} catch (ClassNotFoundException e) {
5775
}
76+
77+
if (debug) {
78+
System.out.println("[Debug] Loaded Classes:");
79+
System.out.println("____________________");
80+
for (Class c : classes) {
81+
System.out.println(c.getPackage().getName() + " "
82+
+ c.getName());
83+
}
84+
}
5885
}
59-
60-
public Class getClassByPackageName(String packageName) {
61-
for(Class c : classes) {
62-
if(c.getPackage().getName().equals(packageName)) {
86+
87+
public Class getSorterByPackageName(String packageName) {
88+
for (Class c : classes) {
89+
if (c.getPackage().getName().equals(packageName)) {
6390
return c;
6491
}
6592
}
6693
return null;
6794
}
68-
95+
6996
public String[] getAvailablePackagesList() {
7097
String[] packages = new String[classes.size()];
71-
for(int i = 0; i < classes.size()-1;i++) {
98+
for (int i = 0; i < classes.size() - 1; i++) {
7299
packages[i] = classes.get(i).getPackage().getName();
73100
}
74101
return packages;
75102
}
76-
103+
77104
private String[] getSubdirectories() {
78-
File file = new File(classpath);
79-
String[] directories = file.list(new FilenameFilter() {
80-
public boolean accept(File current, String name) {
81-
return new File(current, name).isDirectory();
82-
}
83-
});
84-
return directories;
105+
File file = new File(classpath);
106+
String[] directories = file.list(new FilenameFilter() {
107+
public boolean accept(File current, String name) {
108+
return new File(current, name).isDirectory();
109+
}
110+
});
111+
return directories;
85112
}
86113
}

0 commit comments

Comments
 (0)