Java + Called classes + memory



I have created a GUI that is supposed to take a source file (c, c++, java) and convert it to XML and then analyse the XML. So far it works except for one peculiarity I can't figure out. I am currently testing it with two C files, one that works (sum_avg.c) and one that doesn't (stats.c) (By doesn't work I mean the analyses class can't analyse the resultant XML file but that is irrelevant to this question, it is someone else's issue to fix). What happens is when I run the GUI I can load the file that works and the whole thing works just fine, it creates the XML, analyses it and then displays the results. I then load the file that doesn't work and the analyses fails as expected but it displays the proper error as expected. The odd thing is when I run them sequentially, i.e. I run the file that works then without restarting the GUI I try to run the file that doesn't work the GUI remembers the output of the file that works and displays it again. The really weird thing is that if I run the file that doesn't work first, then the one that does it works it doesn't remember the error output.


In the code below I call UnitXMLReader.ChosenFile (the analyses class) and set it's input file to null in the hopes that it will forget any files it may have received previously but it doesn't work. Is there a better way to clear the memory of called classes?



//---- Select File Button ---- //
JButton btnSelectFile = new JButton("Select File");
btnSelectFile.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
JFileChooser FileChooser = new JFileChooser();
filePath = null;
selectedFile = null;
UnitXMLReader.ChosenFile = null;
FileNameExtensionFilter filter = new FileNameExtensionFilter("C, C++, Java or XML Files", "c", "cpp", "java", "xml");
FileChooser.setFileFilter(filter);
int returnValue = FileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION){
selectedFile = FileChooser.getSelectedFile();
filePath = selectedFile.getAbsolutePath();
Path FROM = Paths.get(filePath);
Path TO = Paths.get(System.getProperty("user.dir"), selectedFile.getName());
try{
Files.copy(FROM, TO, COPY_ATTRIBUTES);
}
catch(Exception fail){};
textArea.setText("File Loaded: " + selectedFile.getName() + "\n\n\n" + "Hit 'Run Code'");
}
else System.out.println("Failed to Load");
}
});
btnSelectFile.setBounds(441,39,118,25);
contentPane.add(btnSelectFile);

//--- Run Source Code Button---//
JButton btnRunSourceCode = new JButton("Run Source Code");
btnRunSourceCode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//------Check for loaded file ----//
if(filePath == null){
textArea.setText("Please Load a source file (C, C++, Java)");
}
else{
try{
//int c;
textArea.setText("Converting Source Code to XML");
String workspace = System.getProperty("user.dir"); //Find location of project workspace
String classPath = System.getProperty("java.class.path"); //Location for targetFile.xml to be stored
//create list of commands to be passed to src2srcml executable. Same as command line
String[] commands = {"/bin/bash", "-c", "cd " + workspace + " && ./src2srcml --position " + selectedFile.getName() + " -o " + workspace + "/targetFile.xml"};

Process src2XML = Runtime.getRuntime().exec(commands); //Send Commands to command line
src2XML.waitFor();
}

catch(Exception exc){/*src2srcml Fail*/}
//---- Detect Dependencies ---- //
ParallelXMlGUI c = new ParallelXMlGUI();
Class<? extends ParallelXMlGUI> cls = c.getClass();
File target = new File(System.getProperty("user.dir") + "/targetFile.xml");
System.out.println("Target File: " + target.getAbsolutePath());
if(filePath != null){
UnitXMLReader.ChosenFile = target.getAbsolutePath(); //Send targetFile.xml to XMLUnitReader
UnitXMLReader.main(null);
}
else{textArea.setText("Please Choose a Source File");}
TreeMap<Integer, ArrayList<Integer>> dependencies = DependencyListener.getDependencies();
textArea.append("\n");
textArea.append("Dependencies found at line numbers:" + dependencies + "\n");
textArea.append("<--------------- "+ XMLReader.sendSize()+"X" +XMLReader.sendSize()+"--------------> ");
textArea.append("\n");
int [][] dependencyMatrix = XMLReader.getMatrix();
List<Integer> lineNumberList = XMLReader.getLineNumbersList();
for(int i=0; i< XMLReader.sendSize(); i++){
if(i < lineNumberList.size()){
textArea.append(Integer.toString(lineNumberList.get(i)));
}
for(int j=0; j< XMLReader.sendSize(); j++){
textArea.append(" "+dependencyMatrix[i][j]);
}
textArea.append("\n");
}
textArea.append("");
}
}
});
btnRunSourceCode.setBounds(612, 39, 156, 25);
contentPane.add(btnRunSourceCode);

No comments:

Post a Comment