Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » analyse .java content(analyse java src by AST)
icon9.gif  analyse .java content [message #509226] Thu, 21 January 2010 12:37 Go to next message
Eclipse UserFriend
Dear all,
I have a task need to analyse the .java file content.
For example:
1: import com.mypackage.ClassA;
2: import com.mypackage.ClassB;
3: public class ClassC{
4:   ClassA a = new ClassA();
5:
6:    public void classCMethod(){
7:          ClassB b = a.getClassbMethod(); // analyse this code  :( 
8:    }
9:}


I want to know the java code line 7 ClassA and its getClassbMethod method has been used.
If AST can do that? If it can please give me a demo. Thanks!
            String content = ""; 
		try { 
			File file = new File("D:\\WorkSpace\\ClassC.java");
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String record = null;
			  int recCount = 0;
			   while ((record = br.readLine()) != null) {
			       recCount++;
			       //System.out.println("Line " + recCount + ": " + record);
			       content = content + record;
			   }
			   br.close();
			   fr.close();
		} catch (IOException e) { 
		         e.printStackTrace(); 
		}

		ASTParser parsert = ASTParser.newParser(AST.JLS3); 
		
		parsert.setSource(content.toCharArray()); 
		
		CompilationUnit result = (CompilationUnit) parsert.createAST(null);
		AST ast = result.getAST(); 
		
		List types = result.types(); 
		TypeDeclaration typeDec = (TypeDeclaration) types.get(0); 

		List importList = result.imports(); 
		PackageDeclaration packetDec = result.getPackage(); 
		String className = typeDec.getName().toString(); 
		MethodDeclaration methodDec[] = typeDec.getMethods(); 
		FieldDeclaration fieldDec[] = typeDec.getFields(); 

		System.out.println("Method:"); 
		for(MethodDeclaration method : methodDec) { 
			System.out.println(method.getName()); 
			//System.out.println(method.getBody());
			Block block = method.getBody();
			.
                        .How to continue get what i want??
                        .I want to know the java code line 7 ClassA and its getClassbMethod method has been used.
                        .
		} 

[Updated on: Thu, 21 January 2010 13:22] by Moderator

Re: analyse .java content [message #509231 is a reply to message #509226] Thu, 21 January 2010 12:47 Go to previous messageGo to next message
Eclipse UserFriend
Le 2010-01-21 12:37, bengan a écrit :
> I want to know the java code line 7 ClassA and its getClassbMethod
> method has been used.
> If AST can do that? If it can please give me a demo. Thanks!
> .How to continue get what i want??
> .I want to know the java code line 7 ClassA and its getClassbMethod
> method has been used.
If you want the bindings to be resolved, you should use a java project
and set it inside the ASTParser.
If you don't need the bindings, then you are fine with your code.
It could be simpler for you to write an ASTVisitor instance.
You then need to override:
public boolean visit(MethodInvocation node) {
return true;
}
And test for the method invocation name.
Does this help?
--
Olivier
Re: analyse .java content [message #509240 is a reply to message #509231] Thu, 21 January 2010 13:35 Go to previous messageGo to next message
Eclipse UserFriend
Olivier Thomann wrote on Thu, 21 January 2010 12:47
Le 2010-01-21 12:37, bengan a écrit :
> I want to know the java code line 7 ClassA and its getClassbMethod
> method has been used.
> If AST can do that? If it can please give me a demo. Thanks!
> .How to continue get what i want??
> .I want to know the java code line 7 ClassA and its getClassbMethod
> method has been used.
If you want the bindings to be resolved, you should use a java project
and set it inside the ASTParser.
If you don't need the bindings, then you are fine with your code.
It could be simpler for you to write an ASTVisitor instance.
You then need to override:
public boolean visit(MethodInvocation node) {
return true;
}
And test for the method invocation name.
Does this help?
--
Olivier


sorry, I make mistaken. line 7 is "ClassB b = a.getClassbMethod();"
for your suggest I write a visitor class
public class MyAstVisitor extends ASTVisitor {

	public boolean visit(MethodInvocation node) {
		System.out.println(node);  //this print  "a.getClassbMethod()" but I want to get "a-->ClassA and used getClassbMethod"
		return true;
	}
}

How to do next step? Thanks!
Re: analyse .java content [message #509279 is a reply to message #509240] Thu, 21 January 2010 15:02 Go to previous messageGo to next message
Eclipse UserFriend
I am not sure I understand what you are trying to do.
Could you please describe that you mean by:
"a-->ClassA and used getClassbMethod"

Thanks.

Olivier
Re: analyse .java content [message #509335 is a reply to message #509279] Thu, 21 January 2010 23:21 Go to previous messageGo to next message
Eclipse UserFriend
Olivier Thomann wrote on Thu, 21 January 2010 15:02
I am not sure I understand what you are trying to do.
Could you please describe that you mean by:
"a-->ClassA and used getClassbMethod"

Thanks.

Olivier


public class MyAstVisitor extends ASTVisitor {
	public boolean visit(MethodInvocation node) {
                                           
		System.out.println(node);  
                //this print "a.getClassbMethod();"
                //now I had got a string or a expression  "a.getClassbMethod();" 
                //I need to analyse continue :
                //step 1: get the (dot) left object "a" and right object "getClassbMethod"
                //step 2: I want to know what class the "a" really is? IF the a is a ClassA  object or ClassB object ?
		return true;
	}
}

At last I need to generate a report :
Class: ClassC
Method: ClassC.classCMethod
BeUsedClass: ClassA
BeUsedClass Mehtod: ClassA.getClassbMethod

Re: analyse .java content [message #509459 is a reply to message #509335] Fri, 22 January 2010 08:58 Go to previous messageGo to next message
Eclipse UserFriend
Le 2010-01-21 23:21, bengan a écrit :
> //step 1: get the (dot) left object "a" and right object "getClassbMethod"
> //step 2: I want to know what class the "a" really is? IF the a is a
The method invocation has a receiver that you can get using
getExpression() and you can retrieve the method name using
org.eclipse.jdt.core.dom.MethodInvocation.getName().

For the step2, I have a bad news for you. You need the bindings to be
resolved and this means that you need to use a java project to resolve them.
--
Olivier
Re: analyse .java content [message #509545 is a reply to message #509459] Fri, 22 January 2010 14:14 Go to previous message
Eclipse UserFriend
O~~ So a big bad news but thank you very much for your reply!

[Updated on: Fri, 22 January 2010 14:20] by Moderator

Previous Topic:How to hide Errors and Warnings from other (not current) packages/projects on "Problems" t
Next Topic:Migrating to Eclipse from Ant
Goto Forum:
  


Current Time: Thu Mar 27 03:16:15 EDT 2025

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

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

Back to the top