package struktoGen;
import java.io.*;
import java.lang.reflect.Method;
/**
* Splits the given *.java in *.txt files which each contain exactly 1 method
* @author Norman
*/
public class FileSplitter {
boolean classFolderWasCleared = false;
File dir = new File(this.getClass().getName()); /* directory (classname) including classe's methods */
int methodCount = 0; /* number of methods */
Method[] methods;
int open; /* number of opened bracets (to get method's end)*/
/**
* @param javaFile file-object which will be splittet in *.txt files for each method
* @param classFileLocation String like "package.className"
* @throws ClassNotFoundException
*/
FileSplitter(File javaFile, String classFileLocation) throws ClassNotFoundException {
Class c1 = Class.forName(classFileLocation); /* throws ClassNotFoundException */
methods = c1.getDeclaredMethods();
String[] methodNames = new String[methods.length];
for (int i=0; i< methods.length; i++) {
methodNames[i] = methods[i].getName();
System.out.println("MethodenNamen: " + methodNames[i]);
}
System.out.println("Datei: " + javaFile.getAbsolutePath());
System.out.println("Größe: " + javaFile.length() + " Bytes\n\n");
try {
BufferedReader reader= new BufferedReader(new FileReader(javaFile));
while (true) {
try {
String line = reader.readLine();
System.out.println("aktuelle Zeile: " + line);
if (line != null) {
if (isMethodStart(line)) { /* falls methode beginnt, suche nach ende */
System.out.println("Datei wird erstellt: " +methodNames[methodCount]+".txt");
createTxtFile(methodNames[methodCount]+".txt");
System.out.println("Datei wird gefüllt: " +methodNames[methodCount]+".txt");
fillMethodFile(methodNames[methodCount]+".txt", line);
if (line.contains("{"))
open=1; /* opened braces of method's first line */
else
open=0;
while (true) {
String line2 = reader.readLine();
System.out.println("aktuelle Zeile: " + line2);
System.out.println("Datei wird gefüllt: " +methodNames[methodCount]+".txt");
fillMethodFile(methodNames[methodCount]+".txt", line2);
if (isMethodEnd(line2)) {
break;
}
}
methodCount++;
}
}
else
break;
}
catch (IOException e) {
e.printStackTrace();
}
}
}
catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
}
/**
* deletes files in classFolder
* @return true if all files in classFolder have been deleted
*/
private boolean clearClassFolder() {
File[] filesInDir = dir.listFiles();
int deletionsCount = 0;
for (int i=0; i < filesInDir.length; i++) {
if (filesInDir[i].delete())
deletionsCount++;
}
return (deletionsCount==filesInDir.length)? true : false;
}
/**
* creates new folder named [classname] if not exists, otherwise deletes folder's files.
* creates new txtFile if a method was Found. TxtFile will be named "methods[number_of_method]" and
* stored in [classname]-folder
* @param file file to create
* @return true if the file did not exist yet and was created successfully
*/
private boolean createTxtFile(String filename) {
// String filename = "methods[" + methodCount + "].txt";
try {
if (dir.exists() == false) {
dir.mkdir();
System.out.println("Verzeichnis wurde erstellt: " + dir);
classFolderWasCleared = true;
}
else if (classFolderWasCleared == false) {
classFolderWasCleared = clearClassFolder();
}
return new File(dir, filename).createNewFile();
}
catch (IOException e) {
System.out.println("Konnte Datei nicht erstellen:");
e.printStackTrace();
}
return false; /* this line will never be reached */
}
/**
* writes lines of code into the methodFile
* @param methodFileName name of methodFile
* @param line line of code to be written
*/
public void fillMethodFile(String methodFileName, String line) {
File methodFile = new File(dir, methodFileName);
line = line.concat("\n");
try {
Writer out = new BufferedWriter(new FileWriter(methodFile, true));
out.append(line);
System.out.println("Zeile: " + line + "wurde geschrieben in: " + methodFile);
out.flush();
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param line line of code to check
* @return true if line of code shows that a method ends here
*/
private boolean isMethodEnd(String line) {
boolean isMethodEnd = false;
if (line.contains("{"))
open+=1;
if (line.contains("}"))
open-=1;
if (open==0)
isMethodEnd = true;
return isMethodEnd;
}
/**
* @param line line of code to check
* @return true if line of code shows that a method starts here
*/
private boolean isMethodStart(String line) {
boolean isMethodStart = false;
if ( (!line.contains("=")) && (!line.contains("class")) &&
( (line.contains("void")) || (line.contains("static")) || (line.contains("public"))
|| (line.contains("int")) || (line.contains("String")) || (line.contains("long")) ) )
isMethodStart=true;
return isMethodStart;
}
}