Home » Language IDEs » Java Development Tools (JDT) » JDT PRASER HELP
JDT PRASER HELP [message #259761] |
Sat, 25 April 2009 05:14  |
Eclipse User |
|
|
|
Hello Everyone,
Thank you all for your previous help. I appreciate all your time you
have spent already to answer my questions.
I am confuse with the folloing questions and want to ask for help:
1)
I want to know the difference between Abstract Syntax Tree and the Java
Model because am a little bit confuse about the two.
In the Java Model there are things like:
IackageFragmentRoot
IPackageFragment
IPackageDeclaration
IType
IField
IMethod
...
And also there exist the followng also in the AST:
FieldDeclaration
TypeDeclaration
MethodDeclaration
...
I don't know how these two work together and it makes me confuse. That is
why i ask the difference between the two. What are the difference and how
do they work together.
2)
In my first question i asked the difference between Abstract Syntax Tree
and the Java Model because i tried to get all the declared variables in a
class but it is not working. I have used the "CompilationUnit"
"ICompilationUnit" classes to get access to the class already. I have
methods
already which finds the project and locate the class that i specify.
// these two lines of code works fine
ICompilationUnit icomp = this.findProject();
CompilationUnit comp = this.parse(icomp);
but when i try to get all the declared variables like this:
FieldDeclaration[] iff =
((TypeDeclaration)comp.types().get(0)).getFields();
IField irr = (IField)iff[0];
String name = irr.getElementName();
label.setText(name);
i get an error that " FieldDeclaration cannot be casted to IField ". I got
confuse and it is part of the reason i asked the first question because
i don't understand how the AST and Java Model classes works together.
3)
I am parsing existing java projcts (source files and packages). This is
what am doing. I have some data from a server like these:
Person // classname
firstname //attribute
lastname //attribute
getfirtname() // method
getlastname() // method
Foo // classname
age //attribute
height //attribute
laugh() // method
go() // method
...
...
The data can also be package names and the class names inside the package
etc..
I have to compare these data to the existing java project(source files).
For example i will parse the existing source files and if a source file
has a "Person" as a name and the above attributes and methods, then i will
show the class name(in these case "Person") ,its attributes(firstname and
lastname) and its methods(getfirtname(),getlastname()) --the class name
will be parent and the attributes and mehtods will be children-- in a tree
form inside a view. After that i will repeat the above procedure with
class "Foo" and its attributes and methods as well as other classes that
may come from the server.
I am asking how i can achieve this. I would like you to give me if
possible
an indepth description about what i should do and if possible the classes
and the methods that i can use in the JDT core library.
3)
Following up with the second question, After i show them in a view, when
a user clicks on a class name in the view, then the source file of that
class will open in the editor or when he clicks on an attribute in the
view then the source file must be opened in the editor showing the
attribute on which the user clicked.
The whole project am doing works like the "SemmleCode eclipse plugin".
Thank you all for your help.
Regards,
Eddy.
|
|
|
Re: JDT PRASER HELP [message #259768 is a reply to message #259761] |
Sat, 25 April 2009 17:08   |
Eclipse User |
|
|
|
"Eddy Freeman" <win1for@yahoo.com> wrote in message
news:0a4d4ec4feb688460a0e11a89c0284d6$1@www.eclipse.org...
> Hello Everyone,
>
> Thank you all for your previous help. I appreciate all your time you have
> spent already to answer my questions.
>
> I am confuse with the folloing questions and want to ask for help:
>
> 1)
> I want to know the difference between Abstract Syntax Tree and the Java
> Model because am a little bit confuse about the two.
The Java Model and the AST represent overlapping information, but the AST is
more detailed: it provides much more information. For instance, the Java
Model won't let you see actual code. On the other hand, you can get Java
Model information even for classes that come from jar files, that you don't
have source code for.
One way to think about it is that if you think about a particular class, the
Java Model presents the view of the class that some *other* class cares
about - for instance, if I'm trying to compile class "A" and it has a
reference to class "B", then I need to know the AST for A but I only need to
know the Java Model for B. A local variable declared inside a method body
in B cannot possibly affect the compilation of A, so it's not in the Java
Model.
The tradeoff for getting all the extra information in an AST is that it is
much slower and takes more memory. So, usually you only get the AST for
classes you are actually working on, and you use the Java Model information
for classes you are referencing.
An "AST" is an Abstract Syntax Tree. It describes a source file that has
been broken down into syntactic chunks (that's what "parse" means). For
instance, a CompilationUnit contains a PackageDeclaration and maybe some
ImportDeclarations and some TypeDeclarations; a TypeDeclaration can be
either a ClassDeclaration or an InterfaceDeclaration; a ClassDeclaration
contains JavaDoc, some Modifiers, an Identifier, and a ClassBodyDeclaration;
and so forth. An AST is a tree structure made of nodes where each node has
some syntactic function and each node is (possibly) made up of smaller
nodes.
> [...] when i try to get all the declared variables like this:
>
> FieldDeclaration[] iff =
> ((TypeDeclaration)comp.types().get(0)).getFields();
> IField irr = (IField)iff[0];
> String name = irr.getElementName();
> label.setText(name);
>
> i get an error that " FieldDeclaration cannot be casted to IField ". I got
> confuse and it is part of the reason i asked the first question because
> i don't understand how the AST and Java Model classes works together.
They are different descriptions of the same underlying concept (a field).
If you inspect the class hierarchies of FieldDeclaration and IField, you
will see that they are not related, that is, there is no base class (other
than Object) that you can cast them both to. The FieldDeclaration is an
ASTNode, so it is describing a piece of source code in a .java file. The
IField is an IJavaElement from the Java Model, so it is describing a field,
which might be coming from a .java file or might be coming from a .class
file in a jar somewhere.
So, a FieldDeclaration refers to a line of source code like, for instance,
int iFoo;
or
private volatile List<String> handlerNames =
(List<String>)Config.getDefaults().getNames("handlers");
My point being that a field declaration is not just the name of a field, it
can be a pretty complex object that itself can be broken into smaller
pieces.
In the second example above, the Java Model would say that there is an
IField whose name is handerNames and whose type is List<String>, but the AST
would also say that the structure of the declaration is an assignment and
the right side of the assignment is broken into a series of nested function
calls. The FieldDeclaration is the code that causes there to be an IField
in the Java Model.
I think if you want to get the field name from a FieldDeclaration, you need
to use FieldDeclaration.fragments().getName(). Notice that you could also,
if you wanted, call FieldDeclaration.fragments().getInitializer(), and that
would return you an Expression representing the right side of the "=" sign.
You can't get that level of information from the Java Model.
> 3)
> I am parsing existing java projcts (source files and packages). This is
> what am doing. I have some data from a server like these:
>
> Person // classname
> firstname //attribute
> lastname //attribute
> getfirtname() // method
> getlastname() // method
>
>
> Foo // classname
> age //attribute
> height //attribute
> laugh() // method
> go() // method
>
>
> ..
> ..
>
>
> The data can also be package names and the class names inside the package
> etc..
> I have to compare these data to the existing java project(source files).
> For example i will parse the existing source files and if a source file
> has a "Person" as a name and the above attributes and methods, then i will
> show the class name(in these case "Person") ,its attributes(firstname and
> lastname) and its methods(getfirtname(),getlastname()) --the class name
> will be parent and the attributes and mehtods will be children-- in a tree
> form inside a view. After that i will repeat the above procedure with
> class "Foo" and its attributes and methods as well as other classes that
> may come from the server.
>
> I am asking how i can achieve this. I would like you to give me if
> possible an indepth description about what i should do and if possible the
> classes and the methods that i can use in the JDT core library.
Sounds like you are asking someone else to do your work for you. Perhaps
instead, you should say what you've tried and what problem you encountered;
or cite some examples you have already found and what questions they left
unanswered for you.
|
|
| |
Re: JDT PRASER HELP [message #259775 is a reply to message #259771] |
Sun, 26 April 2009 21:13   |
Eclipse User |
|
|
|
"Eddy Freeman" <win1for@yahoo.com> wrote in message
news:37379c84efd78fbef987217a22ac21e2$1@www.eclipse.org...
\> 1A)
> I think the simplest way to ask the first question is this: Is there a
> class in the JDT library that generates the Abstract Syntax Tree of a
> source file. For instance if there is a class called "Foo.java" in the
> project is there a class that i can use to generate the Abstract Syntax
> Tree of the "Foo.java" source file or class
> so that may be i can get something like:
>
> +Foo
> -name
> -age
> -height
> -fly()
> -laugh()
Yes. That class is CompilationUnit. You already posted the necessary code
in your message of 4/19:
protected CompilationUnit parse(ICompilationUnit unit) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit); // set source
parser.setResolveBindings(true); // we need bindings later on
return (CompilationUnit)parser.createAST(null /* IProgressMonitor */);
}
So I'm not sure what the difficulty is? You do realize that CompilationUnit
extends ASTNode, that is, the CompilationUnit is the root node of a tree of
ASTNodes that represent the parsed file?
So, to do what you want, you need to walk the AST (that is, the tree whose
root is a CompilationUnit) and find each TypeDeclaration, and then for each
TypeDeclaration walk it and find its MethodDeclarations and
FieldDeclarations (and, if you care, its nested TypeDeclarations).
If you do this without an ASTVisitor (see discussion below), your first few
lines of code will probably look something like this:
process(CompilationUnit cu) {
for (AbstractTypeDeclaration type : cu.types()) {
System.out.println("+" + type.getName());
for (BodyDeclaration body : type.getBodyDeclarations()) {
switch(body.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
// ...
and so forth. I'm oversimplifying some, and the "right" way to do this is
with an ASTVisitor rather than with a bunch of nested switch statements and
loops, but I hope this helps you understand the structure of an AST.
By the way, it is important to realize that a .java file may contain more
than one top-level class. You cannot assume that Foo.java contains only
type 'Foo'. For instance, this is a valid Java source file:
foo.java:
public class Foo {
Foo foo() { return new Foo2(); }
}
class Foo2 extends Foo {
}
Notice that Foo2 is not nested inside Foo, it's just another class within
foo.java. So the CompilationUnit contains two ClassDeclarations. That's
why CompilationUnit has a "types()" method.
> 2)
> What are the uses of the ASTNode and ASTVisitor classes. I want to know if
> i can use them because sometimes i get from the code completion a method
> "getAST()" but i don't know if i need it. If i can use it, how can i use
> it.
ASTNode is the base class from which each of the specialized classes (like
TypeDeclaration and MethodDeclaration) are derived. Every node in the
abstract syntax tree is an ASTNode. The most important method in ASTNode is
probably getNodeType().
ASTVisitor is a base class which you can extend in order to write a class
that will visit each node of the AST. The Visitor pattern is a very common
and important pattern used to process tree data structures. I'm not going
to describe it here, but if you don't know about it I encourage reading the
"Design Patterns" book by Gamma et al., or the Wikipedia article at
http://en.wikipedia.org/wiki/Visitor_pattern . And if you don't have the
Design Patterns book, you should probably get it :-)
> 3)
> From this tutorials:
>
> http://www.eclipse.org/articles/article.php?file=Article-Jav aCodeManipulation_AST/index.html
> there is a sub heading called "How to find an AST Node" which gives some
> methods like :
>
> preVisit(ASTNode node)
> visit(MethodInvocation node) endVisit(MethodInvocation node)
> postVisit(ASTNode node)
>
> Please can u explain to me how these methods are used. I have read it so
> many times but can't figure out how i can use them. That is why i asked
> the second question what the ASTNode and ASTVisitor does.
This has to do with the Visitor design pattern, so if you learn about that,
I think you will be able to understand these methods.
You don't *need* to use an ASTVisitor to process an AST; it's just a
convenient way to do it. In fact I recommend writing your code without
using the Visitor pattern, first, to see what that looks like; then you can
rewrite it based an ASTVisitor to get a sense of how that helps.
You can also search http://code.google.com for "ASTVisitor" to find examples
of how it is used.
|
|
| | | | |
Re: JDT PRASER HELP [message #259802 is a reply to message #259779] |
Mon, 27 April 2009 19:42   |
Eclipse User |
|
|
|
Hi,
I am a little bitstuck and need help in the implementation:
I have the following in my "plugin.xml" file
<extension
id="JavaSearchResultPage"
point="org.eclipse.search.searchResultViewPages">
<viewPage
id="org.eclipse.jdt.ui.JavaSearchResultPage"
searchResultClass="org.eclipse.jdt.internal.ui.search.JavaSearchResult "
class="yukplugins.SearchEng">
</viewPage>
</extension>
*************Questions**********
1)
Is this entry in the plugin file correct?
The "SearchEng" is the class below:
public class SearchEng {
Testing t = new Testing();
public SearchEng(){}
protected CompilationUnit parse(ICompilationUnit unit) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit); // set source
parser.setResolveBindings(true); // we need bindings later on
return (CompilationUnit)parser.createAST(null /* IProgressMonitor */);
// parse
}
public IType findProject(){
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("smalltalkJavaProject");
if (project.exists() && !project.isOpen())
try {
project.open(null /* IProgressMonitor */);
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
IJavaProject javaProject = JavaCore.create(project);
IType lwType = null;
try {
lwType = javaProject.findType("files.Person");
} catch (JavaModelException e) {
e.printStackTrace();
}
ICompilationUnit lwCompilationUnit = lwType.getCompilationUnit();
SearchPattern pattern = SearchPattern.createPattern("get*",
IJavaSearchConstants.TYPE,
IJavaSearchConstants.DECLARATIONS,
SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE);
IJavaSearchScope scope = null;
try{
scope = SearchEngine.createHierarchyScope(lwType);
}catch(JavaModelException jme){
jme.printStackTrace();
}
//SearchRequestor requestor = ...;
SearchReq requestor = new SearchReq();
SearchEngine searchEngine = new SearchEngine();
try {
searchEngine.search(pattern, new SearchParticipant[]
{SearchEngine.getDefaultSearchParticipant()}, scope, requestor, null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lwType;
}
}
*************Questions**********
1)
Which class should "SearchEng" extends
2)
In the SearchEng class, what method should i implement for it to show the
view results for instance in the normal view, you have to extend the
"ViewPart" class and implement the "createPartControl()" and "setFocus()"
methods to get the work done.
3)
How should the inherited methods be implemented
Below is the "SearchReq" class:
public class SearchReq extends SearchRequestor{
public void acceptSearchMatch(SearchMatch match)
throws CoreException{
if (match.getAccuracy() == SearchMatch.A_ACCURATE && !
match.isInsideDocComment())
System.out.println(match);
//classesThatExtendFoo.add(match);
}
}
*************Questions**********
1)
How should i implement the "acceptSearchMatch(SearchMatch match)" method
to find the results.
I am asking these questions because they are totally new to me and i don't
know how to tackle it. I was getting understanding about the AST and you
suggested this to me and i know it was a good suggestion and very simple
but i am finding the implementation a little hard to realize it. Need
some help.
Thanks.
|
|
|
Re: JDT PRASER HELP [message #259810 is a reply to message #259802] |
Tue, 28 April 2009 04:31  |
Eclipse User |
|
|
|
Originally posted by: benno_baumgartner.ch.ibm.com
Eddy Freeman wrote:
> I am asking these questions because they are totally new to me and i
> don't know how to tackle it. I was getting understanding about the AST
> and you suggested this to me and i know it was a good suggestion and
> very simple but i am finding the implementation a little hard to
> realize it. Need some help.
>
> Thanks.
Hi Eddy
Maybe you should try an iterative approach to solve your problem: First
try to find the java elements you are looking for without the java
search (see my code snippet in the first post). Once you have this
working, try to implement a search result view. I've never done that,
but I guess it will look very similar to the java search result view.
Once you have that working you can try improving your implementation by
using the java search and other fancy stuff.
HTH
Benno
|
|
|
Goto Forum:
Current Time: Sun Jun 08 14:45:01 EDT 2025
Powered by FUDForum. Page generated in 0.25033 seconds
|