def test_interface_support_default_methods(self): parse.parse(""" interface Foo { default void foo() { System.out.println("foo"); } } """)
def test_interface_support_static_methods(self): parse.parse(""" interface Foo { void foo(); static Foo create() { return new Foo() { @Override void foo() { System.out.println("foo"); } }; } } """)
def value(self) -> CompilationUnit: if os.path.splitext(self._filename)[1] != '.java': raise TypeError('Invalid file extension') with open(self._filename, encoding='utf-8') as file: return parse(file.read())
def _parse_file(self, file): full_path = os.path.join(file) file_result = list() with open(full_path, 'r') as file: file_str = file.read() cu = parse.parse(file_str) for path, node in cu.filter(self._node_type): children = self._extract_children(node) file_result.append(children) return file_result
def test_parameter_no_type_expression_body(self): """ tests support for lambda with parameters with inferred types. """ test_classes = [ setup_java_class("(bar) -> bar + 1;"), setup_java_class("bar -> bar + 1;"), setup_java_class("x -> x.length();"), setup_java_class("y -> { y.boom(); };"), ] for test_class in test_classes: clazz = parse.parse(test_class) self.assert_contains_lambda_expression_in_m(clazz)
def test_parameter_with_type_expression_body(self): """ tests support for lambda with parameters with formal types. """ test_classes = [ setup_java_class("(int foo) -> { return foo + 2; };"), setup_java_class("(String s) -> s.length();"), setup_java_class("(int foo) -> foo + 1;"), setup_java_class("(Thread th) -> { th.start(); };"), setup_java_class("(String foo, String bar) -> " "foo + bar;"), ] for test_class in test_classes: clazz = parse.parse(test_class) self.assert_contains_lambda_expression_in_m(clazz)
def _parse_src(self): i = 0 for name in self._java_namelist: i += 1 file = os.path.join(self._java_src_extract_path, name) msg = "Parsing %s (%s/%s)..." % (name, i, len(self._java_namelist)) self.status_text = msg self.logger.debug(msg) with open(file, 'r') as javafile: src = javafile.read() ast = parse(src) clsdescriptor = JavaFileRepresentation(ast) print clsdescriptor
def test_lambda_support_no_parameters_expression_body(self): """ tests support for lambda with no parameters and an expression body. """ test_classes = [ setup_java_class("() -> 3;"), setup_java_class("() -> null;"), setup_java_class("() -> { return 21; };"), setup_java_class("() -> { System.exit(1); };"), ] for test_class in test_classes: clazz = parse.parse(test_class) self.assert_contains_lambda_expression_in_m(clazz)
def check_source_well_formed(file_content): """ Check if javalang can parse the file :param file_path: the java file dir """ try: tree = parse.parse(file_content) return True except javalang.parser.JavaSyntaxError as error: return False except StopIteration as error: return False except: return False
def test_lambda_support_no_parameters_complex_expression(self): """ tests support for lambda with no parameters and a complex expression body. """ code = """ () -> { if (true) return 21; else { int result = 21; return result / 2; } };""" self.assert_contains_lambda_expression_in_m( parse.parse(setup_java_class(code)))
def scanner(filename): file_stream = open(filename, 'r') _contents = file_stream.read() file_stream.close() # 字符串判断快速过滤 if "InitialContext(" not in _contents: return False try: root_tree = parse(_contents) except: return False class_declaration_list = get_class_declaration(root_tree) for class_declaration in class_declaration_list: for method_declare in class_declaration.methods: if ack(method_declare) is True: string = "{file} {method}".format(file=filename, method=method_declare.name) print string write_file("./result.txt", string)
fnames = [] for root, d_names, f_names in walk(gitrepo): for f in f_names: file_path = path.join(root, f) if any([regex.match(file_path) for regex in blacklist_regexes]): continue if any([regex.match(file_path) for regex in whitelist_regexes]): fnames.append(file_path) # print("fname = %s" % fname) for fname in fnames: treeObj = None with open(fname, 'r') as f: treeObj = parse.parse(f.read()) find_classes(treeObj) find_interfaces(treeObj) rest_controllers = [ cont for cont in classes.values() if cont.is_rest_controller ] feign_clients = { interface.name: interface for interface in interfaces.values() if interface.is_feign_client } analyze_rest_controllers() for cont in rest_controllers: for name, feign_calls in cont.feign_calls.items(): for feign_call in feign_calls:
def test_method_reference_to_the_new_method_with_explict_type(self): """ test support for method references to 'new' with an explicit type. """ self.assert_contains_method_reference_expression_in_m( parse.parse(setup_java_class("String::<String> new;")))
def test_method_reference_from_array_type(self): """ currently there is no support for method references from a primary type. """ self.assert_contains_method_reference_expression_in_m( parse.parse(setup_java_class("int[]::new;")))
def test_method_reference_explicit_type_arguments(self): """ test support for method references with an explicit type. """ self.assert_contains_method_reference_expression_in_m( parse.parse(setup_java_class("Arrays::<String> sort;")))
def test_method_reference_explicit_type_arguments_for_generic_type(self): """ currently there is no support for method references for an explicit type. """ self.assert_contains_method_reference_expression_in_m( parse.parse(setup_java_class("List<String>::size;")))
def test_method_reference_from_super_with_identifier(self): """ test support for method references from Identifier.super. """ self.assert_contains_method_reference_expression_in_m( parse.parse(setup_java_class("String.super::toString;")))
def test_method_reference_from_super(self): """ test support for method references from 'super'. """ self.assert_contains_method_reference_expression_in_m( parse.parse(setup_java_class("super::toString;")))
def test_lambda_support_no_parameters_no_body(self): """ tests support for lambda with no parameters and no body. """ self.assert_contains_lambda_expression_in_m( parse.parse(setup_java_class("() -> {};")))
'Mirror.java', 'Player.java', ] class_declerations = [] # Find All Packages for dirname, filename in files: #if filename not in parsed_filenames: # continue file_path = join(dirname, filename) with open(file_path, 'r') as stream: java_str = stream.read() tree = parse(java_str) package_name = tree.package.name package_path = join(wrapped_path, package_name) if package_name not in python_packages: python_packages[package_name] = {} python_package = python_packages[package_name] if not isdir(package_path): mkdir(package_path) open(join(package_path, '__init__.py'), 'w+') class_decs = tree.filter(ClassDeclaration) for path, class_dec in class_decs:
def test_method_reference_to_the_new_method(self): """ test support for method references to 'new'. """ self.assert_contains_method_reference_expression_in_m( parse.parse(setup_java_class("String::new;")))