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
master
David Pichardie 4 years ago committed by Facebook GitHub Bot
parent 8f13e6ecb3
commit 2c6fd7a617

1
.gitignore vendored

@ -51,6 +51,7 @@ duplicates.txt
/infer/tests/build_systems/incremental_analysis_cost_change/src /infer/tests/build_systems/incremental_analysis_cost_change/src
/infer/tests/build_systems/incremental_analysis_add_procedure/src /infer/tests/build_systems/incremental_analysis_add_procedure/src
/infer/tests/build_systems/java_test_determinator/*.test /infer/tests/build_systems/java_test_determinator/*.test
/infer/tests/build_systems/java_source_parser/*.test
/_release /_release
/infer-source /infer-source

@ -137,6 +137,7 @@ BUILD_SYSTEMS_TESTS += \
differential_skip_duplicated_types_on_filenames \ differential_skip_duplicated_types_on_filenames \
differential_skip_duplicated_types_on_filenames_with_renamings \ differential_skip_duplicated_types_on_filenames_with_renamings \
gradle \ gradle \
java_source_parser \
java_test_determinator \ java_test_determinator \
javac \ javac \
resource_leak_exception_lines \ resource_leak_exception_lines \

@ -402,7 +402,31 @@ and skip_comments action = parse
In_channel.close cin In_channel.close cin
) )
let debug_on_file _path = let debug_on_file path =
(* TODO: do something here *) if String.is_suffix path ~suffix:".java" then (
() let cin = In_channel.create path in
let stack = [] in
let record_location ~classname ~col ~line =
let cn : JBasics.class_name = JBasics.make_cn classname in
Printf.printf "class %s at line %d, column %d\n"
(JBasics.cn_name cn) line col in
try (
let state = { record_location; stack; } in
class_scan state (from_channel cin) ;
In_channel.close cin )
with
| Failure s ->
Printf.printf "Error parsing source file: %s" s;
In_channel.close cin
| Missing_opening_bracket ->
Printf.printf
"Missing opening bracket error while parsing source file\n";
In_channel.close cin
| Missing_opening_parenthesis ->
Printf.printf
"Missing opening parenthesis error while parsing source file\n";
In_channel.close cin
)
} }

@ -0,0 +1,13 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package java.source.parser;
@interface ExampleCustomAnnotation {
String name();
String address();
}

@ -0,0 +1,120 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package java.source.parser;
public class Main {
// Tests for annotations
static interface Interface {
public Object foo();
}
@ExampleCustomAnnotation(name = "Chaitanya", address = "Agra, India")
class AnnotatedClass {
@Deprecated
void method1() {
return;
}
@SuppressWarnings("deprecation")
void method2() {
return;
}
}
static class Impl implements Interface {
@Override
public Object foo() {
return null;
}
}
// tests for anonymous inner classes
static class MyThread {
MyThread(Object o) {}
public static void main(String[] args) {
Thread t1 =
new Thread() {
public void run() {
System.out.println("Child Thread");
}
};
t1.start();
Thread t2 =
new Thread(
new Runnable() {
public void run() {
System.out.println("Child Thread");
}
});
t2.start();
// nested anonymous classes
MyThread mt =
new MyThread(
new Object() {
private int counter;
int get_counter() {
return this.counter;
}
}) {
private String label;
String get_label() {
return this.label;
}
};
}
}
// tests for enum
public enum Block {
NONE(""),
WALL("Wall") {
@Override
public boolean good() {
return true;
}
},
PIT("Pit") {
@Override
public boolean good() {
return true;
}
},
FOG("Fog") {
@Override
public boolean good() {
return true;
}
};
private class C {};
private String name;
private Block(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public boolean good() {
return false;
}
}
}

@ -0,0 +1,28 @@
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
TESTS_DIR = ../..
INFER_OPTIONS = --java-debug-source-file-info
SOURCES = Main.java
test: parser.output.test
$(call check_no_diff,parser.output,parser.output.test)
replace: parser.output.test
cp $< parser.output
clean:
rm -fr infer-out parser.output.test *.class
# we check if the java source file is valid for javac
compile:
javac *.java
.PHONY: parser.output.test
parser.output.test: $(SOURCES) $(INFER_BIN)
$(INFER_BIN) $(INFER_OPTIONS) $(SOURCES) > parser.output.test
include $(TESTS_DIR)/base.make

@ -0,0 +1,11 @@
class java.source.parser.Main at line 9, column 13
class java.source.parser.Main$Interface at line 12, column 19
class java.source.parser.Main$AnnotatedClass at line 17, column 8
class java.source.parser.Main$Impl at line 29, column 15
class java.source.parser.Main$MyThread at line 37, column 15
class java.source.parser.Main$MyThread$1 at line 43, column 23
class java.source.parser.Main$MyThread$2 at line 52, column 29
class java.source.parser.Main$MyThread$3 at line 62, column 27
class java.source.parser.Main$MyThread$4 at line 68, column 17
class java.source.parser.Main$Block at line 80, column 14
class java.source.parser.Main$Block$C at line 104, column 18
Loading…
Cancel
Save