Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Using ASTParser to parse variable declarations found within a method
Using ASTParser to parse variable declarations found within a method [message #221299] Fri, 30 December 2005 19:29 Go to next message
Eclipse UserFriend
Originally posted by: cvanderw.ics.uci.edu

Hi all,

I am trying to find all variable declarations within a method based on
simply having that method's source code (obtained by calling
method.getSource()).

The approach I am taking is to create an ASTParser that will accept an
ASTVisitor which specifically visits VariableDeclarationStatements and
SingleVariableDeclarations. When I set the parser's source to the
method's compilation unit then my visitor works fine. However, when
setting the parser's source to the method's source (via
"parser.setSource(method.getSource().toCharArray())"), none of the
visitor's visit methods get called.

If anyone has any advice on how I should set up the parser for the
visitor to work on simply the source, please let me know.

Thanks in advance,
Christopher
Re: Using ASTParser to parse variable declarations found within a method [message #221307 is a reply to message #221299] Sat, 31 December 2005 10:48 Go to previous messageGo to next message
Eclipse UserFriend
Chris Van der Westhuizen a écrit :
> The approach I am taking is to create an ASTParser that will accept an
> ASTVisitor which specifically visits VariableDeclarationStatements and
> SingleVariableDeclarations. When I set the parser's source to the
> method's compilation unit then my visitor works fine. However, when
> setting the parser's source to the method's source (via
> "parser.setSource(method.getSource().toCharArray())"), none of the
> visitor's visit methods get called.
> If anyone has any advice on how I should set up the parser for the
> visitor to work on simply the source, please let me know.
Could you please provide a test case and a code snippet showing the way
you have done it?
Thanks,
--
Olivier
Re: Using ASTParser to parse variable declarations found within a method [message #221315 is a reply to message #221299] Sat, 31 December 2005 10:58 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: joo.sjtu.org

Chris Van der Westhuizen wrote:
> Hi all,
>
> I am trying to find all variable declarations within a method based on
> simply having that method's source code (obtained by calling
> method.getSource()).
>
> The approach I am taking is to create an ASTParser that will accept an
> ASTVisitor which specifically visits VariableDeclarationStatements and
> SingleVariableDeclarations. When I set the parser's source to the
> method's compilation unit then my visitor works fine. However, when
> setting the parser's source to the method's source (via
> "parser.setSource(method.getSource().toCharArray())"), none of the
> visitor's visit methods get called.
>
> If anyone has any advice on how I should set up the parser for the
> visitor to work on simply the source, please let me know.
>
> Thanks in advance,
> Christopher
Is it about binding?
setResolveBindings to true will work for compilation unit but not for
source.

josson smith
Re: Using ASTParser to parse variable declarations found within a method [message #221323 is a reply to message #221307] Sat, 31 December 2005 15:09 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cvanderw.ics.uci.edu

This is a multi-part message in MIME format.
--------------000608070404060205030400
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Olivier Thomann wrote:
> Chris Van der Westhuizen a
Re: Using ASTParser to parse variable declarations found within a method [message #221492 is a reply to message #221323] Wed, 04 January 2006 08:42 Go to previous message
Eclipse UserFriend
If you don't pass the source for the whole compilation unit, you have to set
the correct kind with parser.setKind(int). Note that you can only get
bindings for K_COMPILATION_UNIT.

Another problem could be that you e.g. try to parse 1.5 code with 1.4
compiler compliance. When you call parser.setSource(ICompilationUnit) or
parser.setProject(IJavaProject), then the compiler options are automatically
set to the project's options. When you just use parser.setSource(char[]),
then the parser uses the workspace options and not project-specific ones.

HTH
Markus

Chris Van der Westhuizen wrote:

> Olivier Thomann wrote:
>
>> Chris Van der Westhuizen a écrit :
>>
>>> The approach I am taking is to create an ASTParser that will accept
>>> an ASTVisitor which specifically visits VariableDeclarationStatements
>>> and SingleVariableDeclarations. When I set the parser's source to
>>> the method's compilation unit then my visitor works fine. However,
>>> when setting the parser's source to the method's source (via
>>> "parser.setSource(method.getSource().toCharArray())"), none of the
>>> visitor's visit methods get called.
>>> If anyone has any advice on how I should set up the parser for the
>>> visitor to work on simply the source, please let me know.
>>
>> Could you please provide a test case and a code snippet showing the
>> way you have done it?
>> Thanks,
>> --
>> Olivier
>
>
>
> The code that I'm using to create the ASTParser and visitor is as follows:
>
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setSource(method.getSource().toCharArray());
> parser.setResolveBindings(true);
> CompilationUnit cu = (CompilationUnit)parser.createAST(null);
> cu.accept(new ASTMethodVisitor());
>
>
> However, when I set the source using setSource(char[]), the visitor does
> not seem to visit the variable declarations inside the method.
>
> Changing the code to the following does work, however:
>
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setSource(compilationUnit);
> parser.setSourceRange(method.getSourceRange().getOffset(),
> method.getSourceRange().getLength());
> parser.setResolveBindings(true);
> CompilationUnit cu = (CompilationUnit)parser.createAST(null);
> cu.accept(new ASTMethodVisitor());
>
> Still, while this does work for me, I would prefer to simply use the
> method source if possible. I'm testing the code by placing it within a
> plug-in and creating the parser within an IElementChangedListener so
> that whenever the user changes some code the listing of method variable
> declarations is printed out to the user.
>
> I attached the Java source code I am referring to. Thanks for any
> assistance in this regard,
>
> Chris
>
>
> ------------------------------------------------------------ ------------
>
> /*
> * EventChangedListener.java
> * Created on Sep 15, 2005
> * Chris
> */
> package eventtesting;
>
> import org.eclipse.core.runtime.CoreException;
> import org.eclipse.jdt.core.ElementChangedEvent;
> import org.eclipse.jdt.core.ICompilationUnit;
> import org.eclipse.jdt.core.IElementChangedListener;
> import org.eclipse.jdt.core.IField;
> import org.eclipse.jdt.core.IJavaElement;
> import org.eclipse.jdt.core.IJavaElementDelta;
> import org.eclipse.jdt.core.IMethod;
> import org.eclipse.jdt.core.IType;
> import org.eclipse.jdt.core.JavaModelException;
> import org.eclipse.jdt.core.Signature;
> import org.eclipse.jdt.core.dom.AST;
> import org.eclipse.jdt.core.dom.ASTParser;
> import org.eclipse.jdt.core.dom.CompilationUnit;
>
>
> /**
> * @author Christopher Van der Westhuizen
> */
> public class SourceChangedListener implements IElementChangedListener
> {
>
> /* (non-Javadoc)
> * @see org.eclipse.jdt.core.IElementChangedListener#elementChanged( org.eclipse.jdt.core.ElementChangedEvent)
> */
> public void elementChanged(ElementChangedEvent event)
> {
> IJavaElementDelta delta = event.getDelta();
> IJavaElement element = delta.getElement();
>
> if(element.getElementType() != IJavaElement.COMPILATION_UNIT)
> return;
>
> ICompilationUnit compilationUnit = (ICompilationUnit)element;
>
> try
> {
>
> IType type = compilationUnit.findPrimaryType();
> IMethod[] methods = type.getMethods();
> for(IMethod method : methods)
> {
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setSource(compilationUnit);
> parser.setSourceRange(method.getSourceRange().getOffset(), method.getSourceRange().getLength());
> //parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
> //parser.setSource(method.getSource().toCharArray());
> //parser.setProject(method.getJavaProject());
> parser.setResolveBindings(true);
> CompilationUnit cu = (CompilationUnit)parser.createAST(null);
> cu.accept(new ASTMethodVisitor());
> }
> }
> catch(JavaModelException e)
> {
> e.printStackTrace();
> }
> }
> }
>
>
> ------------------------------------------------------------ ------------
>
> /*
> * ASTMethodVisitor.java
> * Created on Dec 29, 2005
> * Chris
> */
> package eventtesting;
>
> import org.eclipse.jdt.core.IType;
> import org.eclipse.jdt.core.JavaModelException;
> import org.eclipse.jdt.core.Signature;
> import org.eclipse.jdt.core.dom.ASTVisitor;
> import org.eclipse.jdt.core.dom.FieldDeclaration;
> import org.eclipse.jdt.core.dom.ITypeBinding;
> import org.eclipse.jdt.core.dom.MethodDeclaration;
> import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
> import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
> import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
>
> /**
> * @author Christopher Van der Westhuizen
> */
> public class ASTMethodVisitor extends ASTVisitor
> {
> public boolean visit(VariableDeclarationStatement node)
> {
> System.out.println("Visiting variable declaration statement.");
> for(int i = 0; i < node.fragments().size(); ++i)
> {
> VariableDeclarationFragment frag = (VariableDeclarationFragment)node.fragments().get(i);
> System.out.println("Fragment: " + node.getType() + " " + frag.getName());
> }
>
> System.out.println("");
> return true;
> }
>
> public boolean visit(SingleVariableDeclaration node)
> {
> System.out.println("Visiting single variable declaration: " + node.getName());
> System.out.println("Type is: " + node.getType());
> ITypeBinding binding = node.getType().resolveBinding();
> if(binding == null)
> {
> System.out.println("No binding exists");
> }
> else
> {
> String qualifiedName = binding.getQualifiedName();
> System.out.println("Fully qualified name is: " + qualifiedName);
> }
>
> System.out.println("");
>
> return true;
> }
> }
Previous Topic:getNonJavaResources() for default package
Next Topic:How to retrieve back one package
Goto Forum:
  


Current Time: Sat Nov 08 05:42:47 EST 2025

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

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

Back to the top