Skip to content

Commit 2c6fd7a

Browse files
davidpichardiefacebook-github-bot
authored andcommitted
Running Java source parser from command line
Summary: Testing procedure for java source parser - we can run directly the parser without compiling and analysing the source file - we add a test file Reviewed By: ngorogiannis Differential Revision: D23705199 fbshipit-source-id: 2103c1681
1 parent 8f13e6e commit 2c6fd7a

File tree

7 files changed

+201
-3
lines changed

7 files changed

+201
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ duplicates.txt
5151
/infer/tests/build_systems/incremental_analysis_cost_change/src
5252
/infer/tests/build_systems/incremental_analysis_add_procedure/src
5353
/infer/tests/build_systems/java_test_determinator/*.test
54+
/infer/tests/build_systems/java_source_parser/*.test
5455
/_release
5556
/infer-source
5657

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ BUILD_SYSTEMS_TESTS += \
137137
differential_skip_duplicated_types_on_filenames \
138138
differential_skip_duplicated_types_on_filenames_with_renamings \
139139
gradle \
140+
java_source_parser \
140141
java_test_determinator \
141142
javac \
142143
resource_leak_exception_lines \

infer/src/java/jSourceFileInfo.mll

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,31 @@ and skip_comments action = parse
402402
In_channel.close cin
403403
)
404404

405-
let debug_on_file _path =
406-
(* TODO: do something here *)
407-
()
405+
let debug_on_file path =
406+
if String.is_suffix path ~suffix:".java" then (
407+
let cin = In_channel.create path in
408+
let stack = [] in
409+
let record_location ~classname ~col ~line =
410+
let cn : JBasics.class_name = JBasics.make_cn classname in
411+
Printf.printf "class %s at line %d, column %d\n"
412+
(JBasics.cn_name cn) line col in
413+
try (
414+
let state = { record_location; stack; } in
415+
class_scan state (from_channel cin) ;
416+
In_channel.close cin )
417+
with
418+
| Failure s ->
419+
Printf.printf "Error parsing source file: %s" s;
420+
In_channel.close cin
421+
| Missing_opening_bracket ->
422+
Printf.printf
423+
"Missing opening bracket error while parsing source file\n";
424+
In_channel.close cin
425+
| Missing_opening_parenthesis ->
426+
Printf.printf
427+
"Missing opening parenthesis error while parsing source file\n";
428+
In_channel.close cin
429+
)
430+
431+
408432
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
package java.source.parser;
8+
9+
@interface ExampleCustomAnnotation {
10+
String name();
11+
12+
String address();
13+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
package java.source.parser;
8+
9+
public class Main {
10+
11+
// Tests for annotations
12+
static interface Interface {
13+
public Object foo();
14+
}
15+
16+
@ExampleCustomAnnotation(name = "Chaitanya", address = "Agra, India")
17+
class AnnotatedClass {
18+
@Deprecated
19+
void method1() {
20+
return;
21+
}
22+
23+
@SuppressWarnings("deprecation")
24+
void method2() {
25+
return;
26+
}
27+
}
28+
29+
static class Impl implements Interface {
30+
@Override
31+
public Object foo() {
32+
return null;
33+
}
34+
}
35+
36+
// tests for anonymous inner classes
37+
static class MyThread {
38+
MyThread(Object o) {}
39+
40+
public static void main(String[] args) {
41+
42+
Thread t1 =
43+
new Thread() {
44+
public void run() {
45+
System.out.println("Child Thread");
46+
}
47+
};
48+
t1.start();
49+
50+
Thread t2 =
51+
new Thread(
52+
new Runnable() {
53+
public void run() {
54+
System.out.println("Child Thread");
55+
}
56+
});
57+
t2.start();
58+
59+
// nested anonymous classes
60+
MyThread mt =
61+
new MyThread(
62+
new Object() {
63+
private int counter;
64+
65+
int get_counter() {
66+
return this.counter;
67+
}
68+
}) {
69+
private String label;
70+
71+
String get_label() {
72+
return this.label;
73+
}
74+
};
75+
}
76+
}
77+
78+
// tests for enum
79+
80+
public enum Block {
81+
NONE(""),
82+
83+
WALL("Wall") {
84+
@Override
85+
public boolean good() {
86+
return true;
87+
}
88+
},
89+
90+
PIT("Pit") {
91+
@Override
92+
public boolean good() {
93+
return true;
94+
}
95+
},
96+
97+
FOG("Fog") {
98+
@Override
99+
public boolean good() {
100+
return true;
101+
}
102+
};
103+
104+
private class C {};
105+
106+
private String name;
107+
108+
private Block(String name) {
109+
this.name = name;
110+
}
111+
112+
public String getName() {
113+
return this.name;
114+
}
115+
116+
public boolean good() {
117+
return false;
118+
}
119+
}
120+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
TESTS_DIR = ../..
7+
8+
INFER_OPTIONS = --java-debug-source-file-info
9+
SOURCES = Main.java
10+
11+
test: parser.output.test
12+
$(call check_no_diff,parser.output,parser.output.test)
13+
14+
replace: parser.output.test
15+
cp $< parser.output
16+
17+
clean:
18+
rm -fr infer-out parser.output.test *.class
19+
20+
# we check if the java source file is valid for javac
21+
compile:
22+
javac *.java
23+
24+
.PHONY: parser.output.test
25+
parser.output.test: $(SOURCES) $(INFER_BIN)
26+
$(INFER_BIN) $(INFER_OPTIONS) $(SOURCES) > parser.output.test
27+
28+
include $(TESTS_DIR)/base.make
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class java.source.parser.Main at line 9, column 13
2+
class java.source.parser.Main$Interface at line 12, column 19
3+
class java.source.parser.Main$AnnotatedClass at line 17, column 8
4+
class java.source.parser.Main$Impl at line 29, column 15
5+
class java.source.parser.Main$MyThread at line 37, column 15
6+
class java.source.parser.Main$MyThread$1 at line 43, column 23
7+
class java.source.parser.Main$MyThread$2 at line 52, column 29
8+
class java.source.parser.Main$MyThread$3 at line 62, column 27
9+
class java.source.parser.Main$MyThread$4 at line 68, column 17
10+
class java.source.parser.Main$Block at line 80, column 14
11+
class java.source.parser.Main$Block$C at line 104, column 18

0 commit comments

Comments
 (0)