Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » AST resolve bindings (Get the source code of a superclass-cannot resolve bindings)
AST resolve bindings [message #1783883] Mon, 19 March 2018 22:15 Go to next message
Laura Lala is currently offline Laura LalaFriend
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 #1783887 is a reply to message #1783883] Tue, 20 March 2018 00:58 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1651
Registered: July 2009
Senior Member
the primary question is: do you have a configured Java project at hand? If not, how is the tool supposed to find referenced classes?
Re: AST resolve bindings [message #1783941 is a reply to message #1783887] Tue, 20 March 2018 15:19 Go to previous messageGo to next message
Laura Lala is currently offline Laura LalaFriend
Messages: 6
Registered: November 2017
Junior Member
I have configured a project for the tool, but the tool should be able to find the references of any classes, no matter of their location. I will try to give the big image of what my tool is supposed to do. A user can load any java file (the location should not matter) and the tool will analyse and parse the code with the help of JDT and print the parsed code in another format (e.g. pseudocode) in a JTextArea.
Re: AST resolve bindings [message #1783947 is a reply to message #1783941] Tue, 20 March 2018 15:39 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1651
Registered: July 2009
Senior Member
Quote:
the tool should be able to find the references of any classes, no matter of their location.


this sounds like an impossible task to me.
Do you plan to search the entire internet?
Re: AST resolve bindings [message #1783950 is a reply to message #1783947] Tue, 20 March 2018 16:21 Go to previous messageGo to next message
Laura Lala is currently offline Laura LalaFriend
Messages: 6
Registered: November 2017
Junior Member
I meant any java file created by the user and stored in the computer and not necessarily in the workspace.
And no, I'm not planning to search the entire internet. You don't have to be sarcastic, I'm just trying to get some help for my project. Thank you.
Re: AST resolve bindings [message #1783955 is a reply to message #1783950] Tue, 20 March 2018 16:51 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1651
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.
Re: AST resolve bindings [message #1783956 is a reply to message #1783955] Tue, 20 March 2018 16:57 Go to previous messageGo to next message
Laura Lala is currently offline Laura LalaFriend
Messages: 6
Registered: November 2017
Junior Member
I understand now, I will then define the search scope for the projects. Sorry if my questions seemed dumb, I'm a beginner. Thanks a lot for the help.
Re: AST resolve bindings [message #1783958 is a reply to message #1783956] Tue, 20 March 2018 17:12 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1651
Registered: July 2009
Senior Member
Good luck :)
Previous Topic:Support official javac
Next Topic:java decompilers not working
Goto Forum:
  


Current Time: Mon Nov 19 07:51:28 GMT 2018

Powered by FUDForum. Page generated in 0.02319 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top