1
|
package asttrav.handlers;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.*;
|
5
|
|
6
|
import org.eclipse.jdt.core.dom.*;
|
7
|
import org.eclipse.core.resources.IProject;
|
8
|
import org.eclipse.core.resources.ResourcesPlugin;
|
9
|
import org.eclipse.jdt.core.ICompilationUnit;
|
10
|
import org.eclipse.jdt.core.IJavaProject;
|
11
|
import org.eclipse.jdt.core.IMethod;
|
12
|
import org.eclipse.jdt.core.IPackageFragment;
|
13
|
import org.eclipse.jdt.core.IPackageFragmentRoot;
|
14
|
import org.eclipse.jdt.core.IType;
|
15
|
import org.eclipse.jdt.core.JavaCore;
|
16
|
import org.eclipse.core.runtime.*;
|
17
|
|
18
|
public class InfoGatherer {
|
19
|
|
20
|
// ---
|
21
|
// This method returns the number of user defined methods in the
|
22
|
// workspace on which this plug-in was invoked
|
23
|
// ---
|
24
|
public int getNumberOfUserMethods() {
|
25
|
int total = 0;
|
26
|
// ---
|
27
|
// This gets the list of projects in the "Package Explorer" frame in the
|
28
|
// instance of eclipse on which this plug-in is invoked
|
29
|
// ----
|
30
|
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
|
31
|
// ---
|
32
|
// Go through all the projects return above and save only the ones that
|
33
|
// are Java-capable.
|
34
|
// ---
|
35
|
List javaprojects = new ArrayList();
|
36
|
for (int i = 0; i < projects.length; i++) {
|
37
|
IProject project = projects[i];
|
38
|
//
|
39
|
try {// throws exceptions if the project is not open or does not exist
|
40
|
// ---
|
41
|
// Check and see if we can access the project and if the project might
|
42
|
// contain java code
|
43
|
// ---
|
44
|
if (project.isAccessible() && project.hasNature(JavaCore.NATURE_ID)) {
|
45
|
javaprojects.add(JavaCore.create(project));
|
46
|
}
|
47
|
} catch (CoreException ex) {
|
48
|
ex.printStackTrace();
|
49
|
}
|
50
|
} // ends for(int i = 0; i<projects.length; i++)
|
51
|
|
52
|
// ---
|
53
|
// Now that we have all the projects that we can access and that might also
|
54
|
// contain Java code we need to go through them and count the number of user
|
55
|
// defined methods.
|
56
|
// ---
|
57
|
try {
|
58
|
for (int i = 0; i < javaprojects.size(); i++) {
|
59
|
|
60
|
// ---
|
61
|
// Now we must get all the Packages in the current project. To do this we
|
62
|
// get the package fragments. A package fragment is a portion of the workspace
|
63
|
// corresponding to an entire package, or to a portion thereof. The distinction
|
64
|
// between a package fragment and a package is that a package with some name is
|
65
|
// the union of all package fragments in the class path which have the same
|
66
|
// name.
|
67
|
// Note the casting to a IJavaProject we know this is a safe action because
|
68
|
// we checked the projects nature above
|
69
|
// ---
|
70
|
IPackageFragment[] pfrag = ((IJavaProject) javaprojects.get(i)).getPackageFragments();
|
71
|
|
72
|
// ---
|
73
|
// now we iterator over the packages contained in the current project
|
74
|
// ---
|
75
|
for (int k = 0; k < pfrag.length; k++) {
|
76
|
|
77
|
// ---
|
78
|
// If the package does not contain Java source code we don't want to look
|
79
|
// at it. IPackageFragmentRoot.K_SOURCE indicates that the package does contain
|
80
|
// source IPackageFragmentRoot.K_BINARY would indicate that the package
|
81
|
// contained
|
82
|
// binary files (EX a package in the Java library).
|
83
|
// ---
|
84
|
if (pfrag[k].getKind() != IPackageFragmentRoot.K_SOURCE)
|
85
|
continue;
|
86
|
|
87
|
// ---
|
88
|
// Now that we've determined that we've determined that the fragment contains
|
89
|
// source we need to get the compilation units contained in it.
|
90
|
// An ICompilationUnit represents an entire Java compilation unit (.java source
|
91
|
// file).
|
92
|
// ---
|
93
|
ICompilationUnit[] icu = pfrag[k].getCompilationUnits();
|
94
|
|
95
|
// ---
|
96
|
// Now we iterate over all the .java files contained in this package
|
97
|
// ---
|
98
|
for (int y = 0; y < icu.length; y++) {
|
99
|
|
100
|
// ---
|
101
|
// Each java file (compilationUnit) may define multiple types (ie classes).
|
102
|
// So we must get each type and iterate through them to find the methods.
|
103
|
// From the types
|
104
|
// ---
|
105
|
IType[] allTypes = icu[y].getAllTypes();
|
106
|
for (int tc = 0; tc < allTypes.length; tc++) {
|
107
|
|
108
|
// ---
|
109
|
// once we have a type be can get all the methods in the type
|
110
|
// ---
|
111
|
IMethod[] im = allTypes[tc].getMethods();
|
112
|
|
113
|
// ----
|
114
|
// Here we iterate through the methods, we double check and make sure they
|
115
|
// are user defined print their name and increase our total
|
116
|
// ---
|
117
|
for (int mc = 0; mc < im.length; mc++) {
|
118
|
|
119
|
if (im[mc].isBinary())
|
120
|
continue;
|
121
|
// System.out.println(im[mc].getElementName());
|
122
|
total++;
|
123
|
} // end for(int mc = 0; mc<im.length; mc++)
|
124
|
} // end for(int tc = 0; tc<allTypes.length; tc++)
|
125
|
} // end for(int y = 0; y<icu.length; y++)
|
126
|
} // end for(int k = 0; k<pfrag.length; k++)
|
127
|
} // end for(int i=0; i<javaprojects.size(); i++)
|
128
|
} catch (Exception ex) {
|
129
|
ex.printStackTrace();
|
130
|
}
|
131
|
return total;
|
132
|
}// end getNumberOfUserMethods()
|
133
|
|
134
|
// ---
|
135
|
// This method takes an IMethod and returns the number of method
|
136
|
// calls made from all the methods in the declaring class of the method.
|
137
|
// To do this the method creates an Abstract Syntax Tree (AST) for the
|
138
|
// Declaring Class of the IMethod and traverses it looking for methods
|
139
|
// invocations.
|
140
|
// It also prints the number of nodes in the AST for the class.
|
141
|
// ---
|
142
|
public int getNumberOfMethodCalls(IMethod method) {
|
143
|
// sanity check just ensures that method isn't null
|
144
|
if (method == null) {
|
145
|
System.out.println("The IMethod pasted to getNumberOfMethodCalls is null");
|
146
|
return -1;
|
147
|
}
|
148
|
|
149
|
// ---
|
150
|
// This is a Java language parser for creating abstract syntax trees (ASTs).
|
151
|
// The AST.JLS2 actual specifies that the parser will should work with
|
152
|
// JLS2 (J2SE 1.4)
|
153
|
// ---
|
154
|
ASTParser parser = ASTParser.newParser(AST.JLS2);
|
155
|
|
156
|
// ---
|
157
|
// If setResolveBindings(true), the various names and types appearing in the
|
158
|
// AST can be resolved to "bindings" by calling the resolveBinding methods.
|
159
|
// These bindings draw connections between the different parts of a program,
|
160
|
// and generally afford a more powerful vantage point for clients who wish
|
161
|
// to analyze a program's structure more deeply. However they are very
|
162
|
// expensive.
|
163
|
// ---
|
164
|
parser.setResolveBindings(true);
|
165
|
|
166
|
// ---
|
167
|
// Since the IMethod passed in is expected to be user defined we
|
168
|
// get the ICompilationUnit (.java) however this would work just as
|
169
|
// well for IMethods that are Binary since but insted of getting the
|
170
|
// ICompilationUnit which is null for binary methods we could get the
|
171
|
// IClassFile (.class) and set that as the source for the parser
|
172
|
// ---
|
173
|
ICompilationUnit icu = method.getCompilationUnit();
|
174
|
// sanity check to make sure that icu is not null;
|
175
|
if (icu == null) {
|
176
|
System.out.println("The IMethod past to getNumberOfMethodCalls is not user defined");
|
177
|
return -1;
|
178
|
}
|
179
|
|
180
|
// ---
|
181
|
// The source can be an IClassFile or an ICompilationUnit.
|
182
|
// ---
|
183
|
parser.setSource(icu);
|
184
|
// ---
|
185
|
// Creates an abstract syntax tree. We pass null for the progressmonitor
|
186
|
// ---
|
187
|
ASTNode node = parser.createAST(null);
|
188
|
// ---
|
189
|
// ASTs support the visitor pattern. Read the notes in the MyVisitor.java file.
|
190
|
// ---
|
191
|
MyVisitor visitor = new MyVisitor();
|
192
|
node.accept(visitor);
|
193
|
|
194
|
System.out.println("There were " + visitor.getNumberOfNodes() + " node in "
|
195
|
+ method.getCompilationUnit().getElementName());
|
196
|
return visitor.getNumberOfMethodCalls();
|
197
|
|
198
|
}// end getNumberOfMethodCalls(IMethod method)
|
199
|
|
200
|
}// end InfoGatherer
|