-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathParser.java
More file actions
1192 lines (899 loc) · 48.5 KB
/
Parser.java
File metadata and controls
1192 lines (899 loc) · 48.5 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.core.dom.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
import java.util.Set;
import java.util.HashSet;
class Parser {
private static ArrayList<TypeInfo> variableMap =
new ArrayList<TypeInfo>();
private static int scopeLevel = 0;
static class TypeInfo {
public String varName;
public int level;
public String varType;
TypeInfo(String varName, int level, String varType) {
this.varName = varName;
this.level = level;
this.varType = varType;
}
public static String getMappedType(String variableName, ArrayList<TypeInfo> mapList) {
//for (TypeInfo map : mapList) {
// System.out.println(map.varName + " " + map.varType);
// }
for (int i = mapList.size() - 1; i >= 0; i--) {
TypeInfo map = mapList.get(i);
if (map.varName.equals(variableName)) {
return map.varType;
}
}
return null;
}
}
/* Convert a file into a String */
private static String fileToString(String path) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, StandardCharsets.UTF_8);
}
private static void clearLocalVariables() {
for (int i = variableMap.size()-1; i >= 0; i--) {
if (variableMap.get(i).level == scopeLevel) {
//variableMap.remove(variableMap.get(i));
variableMap.remove(i);
}
}
}
/* Convert files that are in String format to AST */
public static Tokenizer parseAST2Tokens(String absPath, int minNumLines, boolean debug, boolean retry) {
String source = "";
try {
source = fileToString(absPath);
if (retry == true) {
String identifier = "[a-zA-Z\\_\\$][a-zA-Z\\_\\$0-9]*";
String dotLine = identifier + "(\\." + identifier + ")*";
Pattern pattern1 = Pattern.compile("import\\s" + dotLine + ";");
Pattern pattern2 = Pattern.compile("package\\s" + dotLine + ";");
Pattern pattern3 = Pattern.compile("\\s*(public|private)\\s+(\\w+\\s)?class\\s+(\\w+)\\s+((extends\\s+\\w+)|(implements\\s+\\w+( ,\\w+)*))?\\s*\\{", Pattern.DOTALL); // class
Pattern pattern4 = Pattern.compile("(public|protected|private|static|\\s) +[\\w\\<\\>\\[\\]]+\\s+(\\w+) *\\([^\\)]*\\) *(\\{?|[^;])");
Matcher matcher1 = pattern1.matcher(source);
Matcher matcher2 = pattern2.matcher(source);
Matcher matcher3 = pattern3.matcher(source);
Matcher matcher4 = pattern4.matcher(source);
if (!matcher1.find() && !matcher2.find()) {
if (!matcher4.find()) {
source = "public class A {public static void main(String[] args) {" + source + "}}";
if (debug) {
System.out.println("Parser error: cannot find method!");
System.out.println(source);
}
} else if (!matcher3.find()) {
source = "public class A {" + source + "}";
if (debug) {
System.out.println("Parsing error");
System.out.println(source);
}
}
}
if (matcher4.find()) {
}
} else {
source = fileToString(absPath);
}
} catch (IOException e) {
e.printStackTrace();
}
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
char[] content = source.toCharArray();
parser.setSource(content);
parser.setUnitName(absPath);
Map<String, String> options = JavaCore.getOptions();
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
JavaCore.VERSION_1_7);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
// new String[] {"UTF-8"}
String[] sources = {};
String[] classPaths = {};
parser.setEnvironment(classPaths, sources,
null, true);
//parser.setBindingsRecovery(true);
parser.setResolveBindings(false);
parser.setCompilerOptions(options);
parser.setStatementsRecovery(true);
//System.out.println(absPath);
try {
final CompilationUnit unit = (CompilationUnit) parser.createAST(null);
final AST ast = unit.getAST();
Message[] listMessages = unit.getMessages();
if (debug == true) {
for (Message msg : listMessages) {
System.out.println(msg.getMessage());
}
}
if (listMessages.length > 0 && retry != true) {
// do not attempt repair on complete java files
if (absPath.contains(".java")) {
final Tokenizer tk = new Tokenizer(minNumLines, debug);
return tk;
}
Tokenizer tk = parseAST2Tokens(absPath, minNumLines, debug, true);
return tk;
}
// Process the main body
final Tokenizer tk = new Tokenizer(minNumLines, debug);
try {
unit.accept(new ASTVisitor() {
/*
// JavaDoc comment that starts with "/** for type declarations"
public boolean visit(AnnotationTypeDeclaration node) {
int startLine = unit.getLineNumber(node.getStartPosition());
return true;
}
// JavaDoc comment that starts with "/** for type member declarations"
public boolean visit(AnnotationTypeMemberDeclaration node) {
int startLine = unit.getLineNumber(node.getStartPosition());
return true;
}
*/
public boolean visit(AnonymousClassDeclaration node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.AnonymousClassDeclaration.ordinal(), startLine);
return true;
}
public boolean visit(ArrayAccess node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.ArrayAccess.ordinal(), startLine);
return true;
}
public boolean visit(ArrayCreation node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.ArrayCreation.ordinal(), startLine);
return true;
}
public boolean visit(ArrayInitializer node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.ArrayInitializer.ordinal(), startLine);
return true;
}
public boolean visit(ArrayType node) {
int startLine = unit.getLineNumber(node.getStartPosition());
ITypeBinding binding = node.getElementType().resolveBinding();
if (binding != null) {
tk.addHash(TokenType.ArrayType.ordinal(),binding.getQualifiedName(), startLine);
}
else {
tk.addHash(TokenType.ArrayType.ordinal(), startLine);
}
return true;
}
public boolean visit(AssertStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.AssertStatement.ordinal(), startLine);
return true;
}
public void endVisit(AssertStatement node) {
tk.statementEnd(scopeLevel);
}
public boolean visit(Assignment node) {
int startLine = unit.getLineNumber(node.getStartPosition());
String operator = node.getOperator().toString();
tk.addHash(TokenType.Assignment.ordinal(), operator, startLine);
return true;
}
public boolean visit(Block node) {
scopeLevel = scopeLevel + 1;
tk.statementEnd(scopeLevel);
return true;
}
public void endVisit(Block node) {
clearLocalVariables();
scopeLevel = scopeLevel - 1;
}
public boolean visit(BooleanLiteral node) {
int startLine = unit.getLineNumber(node.getStartPosition());
if (node.booleanValue()) {
tk.addHash(TokenType.BooleanLiteral.ordinal(), "true", startLine);
} else {
tk.addHash(TokenType.BooleanLiteral.ordinal(), "false", startLine);
}
return true;
}
public boolean visit(BreakStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.BreakStatement.ordinal(), startLine);
return true;
}
public void endVisit(BreakStatement node) {
tk.statementEnd(scopeLevel);
}
public boolean visit(CastExpression node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.CastExpression.ordinal(), startLine); //reminder
return true;
}
public boolean visit(CatchClause node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.CatchClause.ordinal(), startLine);
return true;
}
public boolean visit(CharacterLiteral node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.CharacterLiteral.ordinal(), node.getEscapedValue(), startLine);
return true;
}
public boolean visit(ClassInstanceCreation node) {
scopeLevel = scopeLevel + 1;
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.ClassInstanceCreation.ordinal(), startLine);
return true;
}
public void endVisit(ClassInstanceCreation node) {
scopeLevel = scopeLevel - 1;
}
/*
public boolean visit(CompilationUnit node) {
int startLine = unit.getLineNumber(node.getStartPosition());
return true;
}
*/
public boolean visit(ConditionalExpression node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.ConditionalExpression.ordinal(), startLine);
return true;
}
public boolean visit(ConstructorInvocation node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.SuperConstructorInvocation.ordinal(), startLine);
return true;
}
public void endVisit(ConstructorInvocation node) {
tk.statementEnd(scopeLevel);
}
public boolean visit(ContinueStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.ContinueStatement.ordinal(), startLine);
return true;
}
public void endVisit(ContinueStatement node) {
tk.statementEnd(scopeLevel);
}
public boolean visit(DoStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.DoStatement.ordinal(), startLine);
return true;
}
public void endVisit(DoStatement node) {
tk.statementEnd(scopeLevel);
}
/*
public boolean visit(EmptyStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.statementStart(startLine);
tk.addHash(78, startLine);
tk.statementEnd(scopeLevel);
return true;
}*/
public boolean visit(EnhancedForStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.EnhancedForStatement.ordinal(), startLine);
return true;
}
/*
public boolean visit(EnumConstantDeclaration node) {
System.out.println("EnumConstantDeclaration");
int startLine = unit.getLineNumber(node.getStartPosition());
System.out.println("EnumConstantDeclaration at line "+ Integer.toString(startLine));
return true;
}*/
/*
public boolean visit(EnumDeclaration node) {
int startLine = unit.getLineNumber(node.getStartPosition());
return true;
}
*/
public boolean visit(ExpressionStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
Expression exp = node.getExpression();
return true;
}
public void endVisit(ExpressionStatement node) {
tk.statementEnd(scopeLevel);
}
public boolean visit(FieldAccess node) {
int startLine = unit.getLineNumber(node.getStartPosition());
String fieldName = node.getName().toString();
tk.addHash(TokenType.FieldAccess.ordinal(), fieldName, startLine);
return true;
}
/*
* always together with VariableDeclarationFragment
*/
public boolean visit(FieldDeclaration node) {
int startLine = unit.getLineNumber(node.getStartPosition());
Type type = node.getType();
ITypeBinding binding = type.resolveBinding();
List<VariableDeclarationFragment> fragments = node.fragments();
for (VariableDeclarationFragment fragment : fragments) {
String name = fragment.getName().toString();
if (binding != null) {
TypeInfo typeInfo = new TypeInfo(
name, scopeLevel, binding.getName());
variableMap.add(typeInfo);
/*
if (!within_method.isEmpty()) {
// In a method
tk.statementStart(startLine);
tk.addHash(85, binding.getName(), startLine);
}
*/
} else {
String varType = node.getType().toString();
TypeInfo typeInfo = new TypeInfo(
name, scopeLevel, varType);
variableMap.add(typeInfo);
/*
if (!within_method.isEmpty()) {
// In a method
tk.addHash(85, varType, startLine);
}
*/
}
}
return true;
}
public boolean visit(ForStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.ForStatement.ordinal(),startLine);
return true;
}
public boolean visit(IfStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
ASTNode parent = node.getParent();
boolean if_else_happen = false;
if (parent != null) {
if (parent.getNodeType() == 25) {
Statement else_statement = ((IfStatement) parent).getElseStatement();
if (else_statement != null) {
if (else_statement.equals(node)) {
if_else_happen = true;
}
}
}
}
if (if_else_happen) {
tk.addHash(TokenType.Elseif.ordinal(), startLine);
} else {
tk.addHash(TokenType.IfStatement.ordinal(),startLine);
}
return true;
}
public void endVisit(IfStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition()+node.getLength());
ASTNode parent = node.getParent();
if (parent != null) {
if (parent.getNodeType() == 25) {
Statement else_statement = ((IfStatement) parent).getElseStatement();
if (else_statement != null) {
if (else_statement.equals(node)) {
return;
}
}
}
}
}
public void preVisit(ASTNode node) {
ASTNode parent = node.getParent();
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
if (parent != null) {
if (parent.getNodeType() == 25 && node.getNodeType() != 25) {
Statement else_statement = ((IfStatement) parent).getElseStatement();
if (else_statement != null) {
if (else_statement.equals(node)) {
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.Else.ordinal(), startLine);
tk.statementEnd(scopeLevel);
}
}
}
}
}
public boolean visit(InfixExpression node) {
int startLine = unit.getLineNumber(node.getStartPosition());
String operator = node.getOperator().toString();
tk.addHash(TokenType.InfixExpression.ordinal(), operator,startLine);
return true;
}
/*
public boolean visit(Initializer node) {
System.out.println("Initializer");
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(28,startLine);
return true;
}*/
public boolean visit(InstanceofExpression node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.InstanceofExpression.ordinal(),startLine);
return true;
}
/* Comments */
// public boolean visit(Javadoc node) {
//
// int startLine = unit.getLineNumber(node.getStartPosition());
//
// System.out.println("Javadoc at line "+ Integer.toString(startLine));
//
// return true;
// }
public boolean visit(LabeledStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.LabeledStatement.ordinal(), startLine);
tk.statementEnd(scopeLevel);
return true;
}
/*
public boolean visit(Annotation node) {
System.out.println("annotation node");
return true;
}
public boolean visit(MarkerAnnotation node) {
return false;
//int startLine = unit.getLineNumber(node.getStartPosition());
//tk.addHash(31, node.getTypeName().getFullyQualifiedName(),startLine);
//return true;
}
*/
public boolean visit(MemberRef node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.MemberRef.ordinal(), node.resolveBinding().getName(), startLine);
return true;
}
public boolean visit(MemberValuePair node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.MemberValuePair.ordinal(), startLine);
return true;
}
public boolean visit(MethodDeclaration node) {
tk.inMethod = true;
// Get Line number
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.methodStart(node.getName().toString(), startLine);
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.MethodDeclaration.ordinal(), startLine);
return true;
}
public void endVisit(MethodDeclaration node) {
tk.inMethod = false;
int startLine = unit.getLineNumber(node.getStartPosition()+node.getLength());
//tk.statementStart(startLine);
//tk.addHash(75, startLine);
//tk.statementEnd(scopeLevel);
//IMethodBinding methodBinding = node.resolveBinding();
tk.methodEnd(node.getName().toString(), startLine);
//System.out.println(node.getName().toString());
}
public boolean visit(MethodInvocation node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.MethodInvocation.ordinal(), startLine);
tk.hasMethodInvocation();
return true;
}
/*
public boolean visit(MethodRef node) {
int startLine = unit.getLineNumber(node.getStartPosition());
System.out.println("MethodRef at line "+ Integer.toString(startLine));
return true;
}
public boolean visit(MethodRefParameter node) {
int startLine = unit.getLineNumber(node.getStartPosition());
System.out.println("MethodRefParameter at line "+ Integer.toString(startLine));
return true;
}*/
/* public, protected, private, static, abstract, final, native, synchronized, transient
* volatile, strictfp
*/
/*
public boolean visit(Modifier node) {
System.out.println("modifier node");
return true;
}
*/
/*
public boolean visit(NormalAnnotation node) {
int startLine = unit.getLineNumber(node.getStartPosition());
System.out.println("Initializer at line "+ Integer.toString(startLine));
return true;
}
*/
public boolean visit(NullLiteral node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.NullLiteral.ordinal(), startLine);
return true;
}
public boolean visit(NumberLiteral node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.NumberLiteral.ordinal(), startLine);
//System.out.println("NumberLiteral at line "+ Integer.toString(startLine));
return true;
}
public boolean visit(ImportDeclaration node) {
return false;
}
public boolean visit(PackageDeclaration node) {
return false;
//
// //int startLine = unit.getLineNumber(node.getStartPosition());
//
//// SimpleName name = (SimpleName) node.getName();
//// System.out.println("PackageDeclaration with name of "+name+" at line "+ unit.getLineNumber(name.getStartPosition()));
// System.out.println("PackageDeclaration with name of ");
//
// return true;
}
/*
public boolean visit(ParameterizedType node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(43, startLine);
return true;
}*/
public boolean visit(ParenthesizedExpression node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.ParenthesizedExpression.ordinal(), startLine);
return true;
}
public boolean visit(PostfixExpression node) {
int startLine = unit.getLineNumber(node.getStartPosition());
Expression operand = node.getOperand();
tk.addHash(TokenType.PostfixExpression.ordinal(),node.getOperator().toString(), startLine);
return true;
}
public boolean visit(PrefixExpression node) {
int startLine = unit.getLineNumber(node.getStartPosition());
String operator = node.getOperator().toString();
tk.addHash(TokenType.InfixOperator.ordinal(), operator, startLine);
ITypeBinding binding = node.getOperand().resolveTypeBinding();
if (binding != null) {
tk.addHash(TokenType.PrefixExpression.ordinal(), binding.getQualifiedName(), startLine);
}
else {
tk.addHash(TokenType.PrefixExpression.ordinal(), startLine);
}
return true;
}
/*
public boolean visit(Name node) {
System.out.println("ddd");
return true;
}*/
/*
* always together with VariableDeclarationFragment
*/
// public boolean visit(PrimitiveType node) {
//
// int startLine = unit.getLineNumber(node.getStartPosition());
// tk.addHash(47, node.toString(), startLine);
// return true;
// }
public boolean visit(QualifiedName node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.QualifiedName.ordinal(), node.toString(), startLine);
return true;
}
/*
public boolean visit(AbstractTypeDeclaration node) {
System.out.println("AbstractTypeDeclaration");
return true;
}
public boolean visit(BodyDeclaration node) {
System.out.println("BodyDeclaration node");
return true;
}
public boolean visit(QualifiedType node) {
System.out.println("QualifiedType");
int startLine = unit.getLineNumber(node.getStartPosition());
System.out.println("QualifiedType at line "+ Integer.toString(startLine));
return true;
}
*/
public boolean visit(ReturnStatement node) {
/*
int startLine = unit.getLineNumber(node.getStartPosition());
tk.statementStart(startLine);
Expression exp = node.getExpression();
if (exp == null) {
tk.addHash(48, startLine);
return true;
}
ITypeBinding bind = exp.resolveTypeBinding();
if (bind != null) {
String s = bind.getQualifiedName();
tk.addHash(48,s,startLine);
}
else {
tk.addHash(48, startLine);
}
*/
return false;
}
/*
public void endVisit(ReturnStatement node) {
tk.statementEnd(scopeLevel);
}
*/
public boolean visit(SimpleName node) {
int startLine = unit.getLineNumber(node.getStartPosition());
String varType = TypeInfo.getMappedType(node.toString(), variableMap);
if (varType != null) {
tk.insertSimpleName(node.toString(), true);
tk.addHash(TokenType.SimpleName.ordinal(), varType, node.toString(), startLine);
} else {
tk.insertSimpleName(node.toString(), false);
tk.addHash(TokenType.SimpleName.ordinal(), node.toString(), startLine);
}
return true;
}
/*
public boolean visit(SimpleType node) {
System.out.println("ddd " + node.toString());
int startLine = unit.getLineNumber(node.getStartPosition());
ITypeBinding typeBinding = node.resolveBinding();
if (typeBinding != null) {
tk.addHash(49, typeBinding.getQualifiedName(), startLine);
}
else {
tk.addHash(49, node.getName().getFullyQualifiedName(), startLine);
}
return true;
}*/
public boolean visit(SingleMemberAnnotation node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.SingleMemberAnnotation.ordinal(), node.getTypeName().toString(), startLine);
return true;
}
public boolean visit(SingleVariableDeclaration node) {
int startLine = unit.getLineNumber(node.getStartPosition());
IVariableBinding binding = node.resolveBinding();
String varName = node.getName().toString();
if (binding != null) {
String variableType = binding.getType().getName();
tk.addHash(TokenType.SingleVariableDeclaration.ordinal(), variableType, startLine);
TypeInfo typeInfo = new TypeInfo(varName, scopeLevel, variableType);
variableMap.add(typeInfo);
} else {
tk.addHash(TokenType.SingleVariableDeclaration.ordinal(), startLine);
TypeInfo typeInfo = new TypeInfo(varName, scopeLevel, node.getType().toString());
variableMap.add(typeInfo);
}
return true;
}
public boolean visit(StringLiteral node) {
int startLine = unit.getLineNumber(node.getStartPosition());
String strValue = node.getEscapedValue();
strValue = strValue.substring(1,strValue.length() - 1);
tk.addHash(TokenType.StringLiteral.ordinal(), strValue, startLine);
Set<String> termSet = Utilities.extractTermsFromSentence2(strValue);
for (String word : termSet) {
tk.insertSimpleName(word, true);
}
return true;
}
public boolean visit(SuperConstructorInvocation node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.SuperConstructorInvocation.ordinal(), startLine);
return true;
}
public void endVisit(SuperConstructorInvocation node) {
tk.statementEnd(scopeLevel);
}
public boolean visit(SuperFieldAccess node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.SuperFieldAccess.ordinal(), startLine);
return true;
}
public boolean visit(SuperMethodInvocation node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(TokenType.SuperMethodInvocation.ordinal(), node.getName().toString(), startLine);
/*
if (node.getExpression() != null) {
tk.addHash(86, node.getExpression().toString(), startLine);
}*/
return true;
}
/*
public boolean visit(SwitchCase node) {
int startLine = unit.getLineNumber(node.getStartPosition());
tk.addHash(54, startLine);
return true;
}*/
public boolean visit(SwitchStatement node) {
/*
int startLine = unit.getLineNumber(node.getStartPosition());
tk.statementStart(startLine);
tk.addHash(55, startLine);
tk.statementEnd(scopeLevel);
*/
return false;
}
public boolean visit(SynchronizedStatement node) {
int startLine = unit.getLineNumber(node.getStartPosition());
int endLine = unit.getLineNumber(node.getStartPosition() + node.getLength());
tk.statementStart(startLine, endLine);
tk.addHash(TokenType.SynchronizedStatement.ordinal(), startLine);
return true;