Example #1
0
    def exitMethodDeclaration(self, ctx: JavaParser.MethodDeclarationContext):

        # ★ポイント6
        lineList = []
        # c1 = ctx.getChild(0).getText()  # ---> return type
        startLine = str(ctx.start.line)
        endLine = str(ctx.stop.line)
        lineList.append(startLine)
        lineList.append(endLine)

        c2 = ctx.getChild(1).getText()  # ---> method name
        # c3 = ctx.getChild(2).getText()  # ---> params
        # params = self.parse_method_params_block(ctx.getChild(2))
        c3 = ctx.getChild(1).getText()  # ---> method name
        c4 = c3 + '_' + startLine + '_' + endLine

        method_info = {
            # 'returnType': c1,
            'methodName': c2,
            # 'params': params,
            'callMethods': self.call_methods
        }
        self.ast_info['methods'].append(method_info)
        self.called_methods[c4] = self.call_methods
        self.methods.append(c4)
        self.methodLine[c4] = lineList
    def exitMethodDeclaration(self, ctx: JavaParser.MethodDeclarationContext):

        c1 = ctx.getChild(0).getText()  # ---> return type
        c2 = ctx.getChild(1).getText()  # ---> method name
        # c3 = ctx.getChild(2).getText()  # ---> params
        params = self.parse_method_params_block(ctx.getChild(2))

        method_info = {
            'returnType': c1,
            'methodName': c2,
            'params': params,
            'callMethods': self.call_methods
        }
        self.ast_info['methods'].append(method_info)
Example #3
0
    def exitMethodDeclaration(self, ctx: JavaParser.MethodDeclarationContext):

        # ★ポイント6
        # AST(抽象構文木)の名前が示す通りctxは木構造になっている。getChild関数でそのコンテキストの子ノードにアクセスすることができる。子ノードの内容はコンテキストによって異なる
        c1 = ctx.getChild(0).getText()  # ---> return type
        c2 = ctx.getChild(1).getText()  # ---> method name
        # c3 = ctx.getChild(2).getText()  # ---> params
        params = self.parse_method_params_block(ctx.getChild(2))

        method_info = {
            'returnType': c1,
            'methodName': c2, # メソッド名
            'params': params,
            'callMethods': self.call_methods # 呼び出しメソッド名
        }
        self.ast_info['methods'].append(method_info)
        # ターゲットメソッド名の表示
        # pprint.pprint(method_info['methodName'])
        # 呼び出しメソッド名の表示
        # pprint.pprint(method_info['callMethods'])

        # ターゲットメソッド名を配列に格納
        methodName_list = []
        methodName_list.append(method_info['methodName'])
        # print(methodName_list)

        # 呼び出しメソッド名を配列に格納
        callMethods_list = []
        callMethods_list.append(method_info['callMethods']) # これがソースコードを行で抽出していて困っている
        # pprint.pprint(callMethods_list)

        # ターゲットメソッドと呼び出しメソッドを辞書で紐づける
        
        link_methodName_callMethods = dict(zip(methodName_list, callMethods_list))
        # pprint.pprint(link_methodName_callMethods)

        # csvに出力
        for key in link_methodName_callMethods: # keyにメソッド名が入ってる
            for val in link_methodName_callMethods[key]: #valに呼び出されるメソッド名が入ってる
                #csvファイルにメソッド名を抽出する
                with open("calleMethod_methodName_cassandra.csv", 'a', newline="", encoding="utf-8") as f:
                    fieldnames = ['called method', 'method']
                    writer = csv.DictWriter(
                        f, fieldnames=fieldnames, delimiter=",", quotechar='"')
                    # writer.writeheader()
                    for calledMethod in link_methodName_callMethods.keys():
                        writer.writerow({'called method': val, 'method': key})
    def exitMethodDeclaration(self, ctx: JavaParser.MethodDeclarationContext):

        # ★ポイント6
        # c1 = ctx.getChild(0).getText()  # ---> return type
        c2 = ctx.getChild(1).getText()  # ---> method name
        # c3 = ctx.getChild(2).getText()  # ---> params
        # params = self.parse_method_params_block(ctx.getChild(2))
        startline_number = str(ctx.start.line)
        endline_number = str(ctx.stop.line)

        # print(c2)
        for calledMethod in self.call_methods:
            self.calledMethodToMethod[calledMethod] = c2
Example #5
0
    def exitMethodDeclaration(self, ctx: JavaParser.MethodDeclarationContext):

        # ★ポイント6
        # c1 = ctx.getChild(0).getText()  # ---> return type
        c2 = ctx.getChild(1).getText()  # ---> method name
        # c3 = ctx.getChild(2).getText()  # ---> params
        # params = self.parse_method_params_block(ctx.getChild(2))
        startline_number = str(ctx.start.line)
        endline_number = str(ctx.stop.line)

        method_info = {
            # 'returnType': c1,
            'methodName': c2,
            # 'params': params,
            'callMethods': self.call_methods
        }
        self.ast_info['methods'].append(method_info)
        self.called_methods[c2].append(self.call_methods)
        self.methods.append(startline_number + ' ' + endline_number + ' ' + c2)
Example #6
0
    def enterMethodDeclaration(self, ctx:JavaParser.MethodDeclarationContext):

        print("{0} {1} {2}".format(ctx.start.line, ctx.start.column, ctx.getText()))
        self.call_methods = []
Example #7
0
    def exitMethodDeclaration(self, ctx: JavaParser.MethodDeclarationContext):

        # ★ポイント6
        # AST(抽象構文木)の名前が示す通りctxは木構造になっています。getChild関数でそのコンテキストの子ノードにアクセスすることができます。子ノードの内容はコンテキストによって異なります。
        c1 = ctx.getChild(0).getText()  # ---> return type
        c2 = ctx.getChild(1).getText()  # ---> method name
        # c3 = ctx.getChild(2).getText()  # ---> params
        params = self.parse_method_params_block(ctx.getChild(2))

        method_info = {
            'returnType': c1,
            'methodName': c2,
            'params': params,
            'callMethods': self.call_methods
        }
        self.ast_info['methods'].append(method_info)
        # print(self.ast_info['methods'])
        # ターゲットメソッド名の表示
        # pprint.pprint(method_info['methodName'])
        # 呼び出しメソッド名の表示
        # pprint.pprint(method_info['callMethods'])

        # ターゲットメソッド名を配列に格納
        methodName_list = []
        methodName_list.append(method_info['methodName'])
        # print(methodName_list)

        # 呼び出しメソッド名を配列に格納
        callMethods_list = []
        callMethods_list.append(method_info['callMethods'])
        # print(callMethods_list)

        # ターゲットメソッドと呼び出しメソッドを辞書で紐づける
        # link_methodName_callMethods = dict(zip(callMethods_list,methodName_list))
        link_methodName_callMethods = dict(
            zip(methodName_list, callMethods_list))
        # pprint.pprint(link_methodName_callMethods)

        # csvに出力
        for key in link_methodName_callMethods:
            # print(key)
            # print(link_methodName_callMethods[key])
            for val in link_methodName_callMethods[key]:
                # print(key)
                # print(val)
                # print("called method:" + val + "target method:" + key)

                # with open("a.csv",'a', newline="") as f:
                #     fieldnames = ['called method', 'method']
                #     writer = csv.DictWriter(f, fieldnames=fieldnames, delimiter=",",quotechar='"')
                #     writer.writeheader()
                #     for calledMethod in link_methodName_callMethods.keys():
                #         print(key)
                #         writer.writerow({'called method': val, 'method': key })
                # pprint.pprint(calledMethod)
                # pprint.pprint(link_methodName_callMethods[calledMethod])

                with open("calleMethod_methodName.csv", 'a', newline="", encoding="utf-8") as f:
                    fieldnames = ['called method', 'method']
                    writer = csv.DictWriter(
                        f, fieldnames=fieldnames, delimiter=",", quotechar='"')
                    # writer.writeheader()
                    for calledMethod in link_methodName_callMethods.keys():
                        writer.writerow({'called method': val, 'method': key})