Method Call Counter
The problem
Your task is to analyse Java source code so that you can count all the method calls in JDK (src.zip) (Java Development Kit). This zip file contains 7471 java files, 371k lines of code. Among all these java files,
- for each class, count the method calls it received;
- for each method, count the number of times this method is called;
For instance, when you see the following statement,
aString.toUpperCase();
The String class receives one more method call, and the toUpperCase() method is used one more time. The object ‘aString’ can be of any name, and you should not count the occurrence of variable names. You need to utilize the parsing result to infer that it is of type String. To avoid name conflict, you should use the complete package path in your program and in your report. For example, String should be written as “java.lang.String”, toUpperCase should be “java.lang.String.toUpperCase()”.
Method
You need to parse the source code so that you can extract the relevant information. You can choose one of the existing Java parsers. One choice is Eclipse JDTthat can generate an AST (abstract syntax tree). It won’t suffice to manipilate the source code directly using tools such as regular expression. One reason is that there are commnets, which can be very complex and can contain code snippets. In addition, there are a few other cases you need to consider:
- String[] toppings = “cheese”, “pepperoni”, “black olives”;
- int arrayLength = toppings.length;
In the above example, length is an atrtibute instead of a method. So we will not count that as a method call.
- String toppings = “cheese, pepperoni, black olives”;
- String[] tokens= toppings.toUpperCase().split(“,”);
In the above example, toppings, i.e., the String class, received one method call. toppings.toUpperCase(), which is also a String, received another method call. Thus, in this line, String received two method calls.
- System.out.println(“.out is not a method call”);
In the above example, out is not a method call. But println is a method call.
- Class A int fac(int x) return (fac(x>0))?fac(x-1):1; ;
In the above example, fac is a method call that is invoked once.
What to submit
Submit one zippped file that contains the following files:
- One pdf file of one page length, describing briefly the method you used, the challenges you encounter, and a table that lists the top 20 classes and top 20 methods. Use the following format to list these classes and methods.
- class1 classcount1; method1 methodcount1
- class2 classcount2; method2 methodcount2
- …
- class20 classcount20; method20 methodcount20
- Your source code. You can use a programming language of your choice, but the source code to be analysed is JDK, which is in Java.
Hints on using JDT
- Create the AST (abstract syntax tree) using the following:
- ASTParser parser = ASTParser.newParser(AST.JLS3);
- parser.setKind(ASTParser.K_COMPILATION_UNIT);
- parser.setSource(unit);
- parser.setResolveBindings(true);
where the unithas to be an ICompilationUnit.
- To obtain an ICompilation object, you need to use the JDT Java Model, which is only available in Eclipse plug-in. You can follow this tutorial on plug-in. You can use a sample Eclipse plugin (say the hello world sample). Once it is ready, use the the following example (from Vogel’s tutorial) to process (analyse) a project. Import all the jdk source files into the project so that you can analyse it.
- public class SampleHandler extends AbstractHandler
- public Object execute(ExecutionEvent event) throws ExecutionException
- // Get the root of the workspace
- IWorkspace workspace = ResourcesPlugin.getWorkspace();
- IWorkspaceRoot root = workspace.getRoot();
- // Get all projects in the workspace
- IProject[] projects = root.getProjects();
- // Loop over all projects
- for (IProject project : projects)
- try
- printProjectInfo(project);
- catch (CoreException e)
- e.printStackTrace();
- return null;
- …
- Once you have the AST, you can use the ASTVisitor to access various nodes and their bindings as below. The following code is from Martin’s slides.
- STNode root= parser.createAST(null);
- root.accept(new ASTVisitor()
- public boolean visit(CastExpression node)
- fCastCount++;
- return true;
- public boolean visit(SimpleName node)
- IBinding binding= node.resolveBinding();
- if (binding instanceof IVariableBinding)
- IVariableBinding varBinding= (IVariableBinding) binding;
- ITypeBinding declaringType= varBinding.getDeclaringClass();
- if (varBinding.isField() &&
- “java.lang.System”.equals(declaringType.getQualifiedName()))
- fAccessesToSystemFields++;
- return true;
- )
JDT resources
- How to train the jdt dragon , Ayushman Jain and Stephen Herrmann, ppt slides
- Matin (IBM)’s ppt on JDT
- Vogel’s JDT tutorial
- Wang’s JDT tutorial
The post Method Call Counter appeared first on My Assignment Online.