-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCallTags.java
241 lines (193 loc) · 7.79 KB
/
CallTags.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package jtags;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.ast.expr.*;
import java.io.File;
import java.util.*;
public class CallTags {
public HashMap<String, CallProperty> callPool;
public FuncTags ftags;
public VarTags vtags;
//public List apiList;
//public List callist;
CallTags(FuncTags ftags, VarTags vtags){
callPool = new HashMap<>();
this.ftags = ftags;
this.vtags = vtags;
// apiList = new ArrayList();
// callist = new ArrayList();
}
public void buildCallPool(Node node){
if(node == null) return ;
else if(node instanceof MethodCallExpr){
Optional<Expression> fExp = ((MethodCallExpr)node).getScope();
String fieldname = ((MethodCallExpr)node).getNameAsString();
List<FuncProperty> fps = ftags.funcPool.get(fieldname);
List<FuncProperty> fps2 = new ArrayList<>();
// CompilationUnit cu = ApiExtractor.jumptoCu(node);
// List imports = cu.getImports();
if(fps != null){
for (FuncProperty fp : fps) {
String filepath2 =fp.getFilepath();
Node resNode = fp.funcnode;
// }
if(sameFuncProcess((MethodCallExpr)node,(MethodDeclaration) resNode)){
fps2.add(fp);
}
}
}
if((fExp.toString()).equals("Optional.empty")){
String type = "MethodCall";
String locate = node.getRange().toString();
CallProperty cp = new CallProperty(locate,type);
callPool.put(fieldname,cp);
if(fps2.size()!=0){
cp.setFuncProperties(fps2);
}
}
else{
Expression formerExp = fExp.get();
if(formerExp instanceof NameExpr){
String clasname = ((NameExpr)formerExp).getNameAsString();
String type = "MethodCall";
String locate = formerExp.getRange().toString();
List<VarProperty> vps = vtags.varPool.get(clasname);
CallProperty cp = new CallProperty(locate,type);
if(fps2.size()!=0){
cp.setFuncProperties(fps2);
}
if(vps!=null){
cp.setVarProperties(vps);
}
//callPool.put(clasname+"."+fieldname,cp);
callPool.put(fieldname,cp);
}
else{
String type = "MethodCall";
String locate = formerExp.getRange().toString();
CallProperty cp = new CallProperty(locate,type);
// callPool.put(node.toString(),cp);
callPool.put(fieldname,cp);
buildCallPool(formerExp);
}
}
}
else{
List nodes = node.getChildNodes();
for (Object o : nodes) {
buildCallPool((Node)o);
}
}
}
public static Node locateNode(Node node,String range){//给定根节点和代码块的位置,定位出代码块在AST中的位置
String nodeRange = node.getRange().toString();
if(nodeRange.equals(range)){
return node;
}
else{
List childs = node.getChildNodes();
for (Object child : childs) {
Node resNode = locateNode((Node)child,range);
if(resNode != null){
return resNode;
}
}
return null;
}
}
public static CompilationUnit getAst(String filepath){
File file = new File(filepath);
try{
String source = FileIO.readStringFromFile(file.getAbsolutePath());
CompilationUnit compilationUnit = JavaParser.parse(source);
return compilationUnit;
}
catch (Exception e){
System.out.println(e);
System.out.println(filepath);
return null;
}
}
public List<String> getCallerParam(MethodCallExpr caller){
List params = caller.getArguments();
List paramtypes = new ArrayList();
for (Object param : params) {
if(param instanceof CastExpr){
paramtypes.add(((CastExpr)param).getType().asString());
}
else if(param instanceof NameExpr){
String pname = ((NameExpr) param).getName().asString();
//暂时默认取该变量名的第一个元素,之后再实现变量的确认方法。
if(vtags.varPool.size() == 0)return paramtypes;
if(vtags.varPool.get(pname) == null) return paramtypes;
VarProperty vp = vtags.varPool.get(pname).get(0);
String ptype = vp.getCtype();
paramtypes.add(ptype);
}
}
return paramtypes;
}
public List<String> getCalleeParam(MethodDeclaration callee){
List params = callee.getParameters();
List paramtypes = new ArrayList();
for (Object param : params) {
String ptype = ((Parameter)param).getType().asString();
paramtypes.add(ptype);
}
return paramtypes;
}
public boolean sameFuncProcess(MethodCallExpr caller, MethodDeclaration callee){
List callerparams = getCallerParam(caller);
List calleeparams = getCalleeParam(callee);
boolean flag = false;
if(calleeparams.size() == callerparams.size()){
flag = true;
for(int i = 0; i < calleeparams.size(); i++){
if(!(calleeparams.get(i).equals(callerparams.get(i)))){
//如果类型不一致,则flag为false,注意这里的!
flag = false;
}
}
}
return flag;
}
public static void main(String[] args) {
// String filepath = "/home/bohong/文档/myapi_analyser/src/jtags/CallTags.java";
// String projectpath = "/home/bohong/文档/myapi_analyser/src/jtags";
// FuncTags ftags = new FuncTags();
// ftags.getFuncs(projectpath);
// VarTags varTags = new VarTags();
// varTags.getVars(filepath);
// CallTags ct = new CallTags(ftags,varTags);
//
// CompilationUnit cu = getAst(filepath);
// ct.buildCallPool(cu);
// Iterator iter = ct.callPool.entrySet().iterator();
// while (iter.hasNext()){
// Map.Entry entry = (Map.Entry) iter.next();
// Object key = entry.getKey();
// CallProperty val = (CallProperty) entry.getValue();
// Iterator iter2 = val.funcProperties.iterator();
// while (iter2.hasNext()){
// System.out.println(key);
// FuncProperty fp = (FuncProperty) iter2.next();
// String filepath2 =fp.getFilepath();
// CompilationUnit compilationUnit = getAst(filepath2);
// Node resNode = locateNode(compilationUnit,fp.getLocaterange());
// System.out.println(key);
// }
// Iterator iter3 = val.varProperties.iterator();
// while (iter3.hasNext()){
// System.out.println(key);
// VarProperty vp = (VarProperty) iter3.next();
// CompilationUnit compilationUnit = getAst(filepath);
// Node resNode = locateNode(compilationUnit,vp.getRange());
// System.out.println(key);
// }
//
// }
// System.out.println();
}
}