def test_java_lang(): with open(TEST_FILE, "r") as fp: data = fp.read() tree = javalang.parse.parse(data) for path, node in tree.filter(javalang.tree.FieldDeclaration): # print(node.attrs, node.modifiers) if 'value' in node.declarators[0].initializer.attrs: print(node.declarators[0].name, node.declarators[0].initializer.value) else: print(node.declarators[0].name, node)
def __traverse_node(self, tree: javalang.ast.Node) -> List[LineNumber]: lines: List[LineNumber] = list() for _, class_declaration in tree.filter( javalang.tree.ClassDeclaration): primary_ctors = list( filter(_is_primary, class_declaration.constructors)) if len(primary_ctors) > 1: lines.extend(ctor.position.line for ctor in primary_ctors) return lines
def get_method_calling_by_method_key(self, mtd_key): tree = self.parser.tree current_node = None print("analysis", mtd_key) for path, node in tree.filter(javalang.tree.MethodDeclaration): node_key = self.parser.extract_method_declare(path, node) if node_key == mtd_key: current_node = node break if current_node: for path, node in current_node: if isinstance(node, (javalang.tree.MethodInvocation, javalang.tree.SuperMethodInvocation)): print(path[-1]) qulifier = node.qualifier member = node.member print("calling", qulifier, member)
def test_java_expr(): data = """ class Test { static { Register(AAA.class); } public void test() { T t = new T(); t.run_test(); } } """ tokens = javalang.tokenizer.tokenize(data) parser = javalang.parser.Parser(tokens) tree = parser.parse() current_node = None for path, node in tree.filter(javalang.tree.MethodDeclaration): if node.name == "test": current_node = node break # print(current_node) for path, node in current_node: print(node)
def class_def(tree): for _, node in tree.filter(javalang.tree.ClassDeclaration): cd = onto['ClassDeclaration']() cd.jname = [node.name] extract_member(node, cd)