This assignment is meant to be a complicated one. You need to spend long hours to get familiar with JDT, Eclipse, and Eclipse plug-in.
There are different ways to count the methods and classes. For instance, we see that polymophic methods are treated as either equally or differently. Both implementations will be considered acceptable, although our prefeered solution is to treat polymorphic methods as different ones.
The numbers can be also slightly different depending on your way to visit these AST nodes. Using SimpleName and MethodInvocation can cause slight difference. Either way is considered correct.
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()".
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 (x>0)?x*fac(x-1):1; }};In the above example, fac is a method call that is invoked once.
public static void main (String[] args){}has a reference for String, but there is no method call. So String class should not have one more count.
class1 classcount1; method1 methodcount1 class2 classcount2; method2 methodcount2 ... class20 classcount20; method20 methodcount20To partially verify your result, note that the most frequently used class is the String class. One thing you need to take care of is polymorphic methods. Please treat each polymorhic method as a distinct one (our preferred solution). For instance, there are many append methods, the counting of each append method in StringBuffer are given below.
java.lang.StringBuffer.append(String) <> 1480 java.lang.StringBuffer.append(char) <> 705 java.lang.StringBuffer.append(int) <> 112 java.lang.StringBuffer.append(Object) <> 38 java.lang.StringBuffer.append(char[]intint) <> 32 java.lang.StringBuffer.append(StringBuffer) <> 8 java.lang.StringBuffer.append(long) <> 3 java.lang.StringBuffer.append(char[]) <> 3 java.lang.StringBuffer.append(float) <> 3 java.lang.StringBuffer.append(CharSequenceintint) <> 3 java.lang.StringBuffer.append(CharSequence) <> 2 java.lang.StringBuffer.append(double) <> 1 java.lang.StringBuffer.append(boolean) <> 1
ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(unit); parser.setResolveBindings(true);where the unit has to be an ICompilationUnit.
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; } ... }
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; } })