AST resolve bindings [message #1783883] |
Mon, 19 March 2018 22:15 |
Laura Lala Messages: 6 Registered: November 2017 |
Junior Member |
|
|
I would need a little bit of help with my project. I am trying to make an app in Java that can load specific java files chosen by the user, with the help of JFileChooser, and get information about the source code in them using JDT AST. I have configured an ASTParser that parses the char[] format of the java source code selected by the user. I managed to get information about the package, class and field declarations. The problem is that if a class extends another class, I would like to be able to get access to the superclass' source code and I do not know how to do that. I think I need to resolve bindings and because my parser parses a char[] source, I additionally need to setProject or setEnvironment and setUnitName according to the API and that's where I get stuck.
This is my file chooser class:
public class LoadFileChooser {
private StringBuilder printData;
private String text;
char[] textArea;
private int returnValue;
protected File selectedFile;
public LoadFileChooser(MainFrame mf) {
JFileChooser fileChooser = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("Java source", "java");
fileChooser.setFileFilter(filter);
fileChooser.setAcceptAllFileFilterUsed(false);
returnValue = fileChooser.showDialog(mf, "Load");
if (returnValue == JFileChooser.APPROVE_OPTION) {
selectedFile = fileChooser.getSelectedFile();
try {
BufferedReader buffer = new BufferedReader(new FileReader(selectedFile));
String line = null;
printData = new StringBuilder();
while ((line = buffer.readLine()) != null) {
printData.append(line);
printData.append("\n");
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
mf.showAction.setText("File"+selectedFile.getName() +" loaded up."+"\n"+"\n"+"\n");
mf.showAction.append(printData.toString());
}
else {
mf.showAction.append("Action cancelled by the user..."+"\n");
text = mf.showAction.getText();
}
mf.showAction.setCaretPosition(0);
fileChooser.setSelectedFile(null);
}
}
This is snippet for ASTParser creation:
public Parser(char[] source) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source);
Map<String, String> options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_7, options);
parser.setCompilerOptions(options);
result = (CompilationUnit) parser.createAST(null);
}
Could anyone give a hand? Thanks.
|
|
|
|
|
|
|
Re: AST resolve bindings [message #1783955 is a reply to message #1783950] |
Tue, 20 March 2018 16:51 |
Stephan Herrmann Messages: 1853 Registered: July 2009 |
Senior Member |
|
|
Quote:I meant any java file created by the user and stored in the computer and not necessarily in the workspace.
That restricts the search to the filesystem of that computer, right? Still a lot of files to read.
I simply can't believe what I read: If you want to resolve type references, you need to define some scope where to search for those types.
In Eclipse, Java projects serve this exact purpose by defining a Build Path.
If you want to avoid using a project, you will inevitably have to re-invent that concept.
Just imagine your class depends on a library, of which your hard disk holds several versions, which one will you use?
Also imagine how long it takes to scan all jar files on your hard disk for all contained types?
Or do you pretend, a Java program is written without any libraries, consisting only of source files created by one user?
Bottom line, I strongly recommend using some kind of project definition - easiest: an Eclipse project - whenever you need type resolution across files.
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02328 seconds