コード例 #1
0
    def execute(self, input_source):
        parser = JavaParser(CommonTokenStream(JavaLexer(FileStream(input_source, encoding="utf-8"))))
        walker = ParseTreeWalker()
        walker.walk(self.listener, parser.compilationUnit())
        # print(self.listener.called_methods)
        # print(self.listener.methods)
        # print(self.listener.calsses)
        print(self.listener.calledMethodToMethod)
        for key in self.listener.calledMethodToMethod:
            print(key)
        
        for value in self.listener.calledMethodToMethod.values():
            print(value)

        save_row = {}

        with open("a.csv",'w') as f:
            fieldnames = ['called method', 'method']
            writer = csv.DictWriter(f, fieldnames=fieldnames, delimiter=",",quotechar='"')
            writer.writeheader()

            for calledMethod in self.listener.calledMethodToMethod.keys():
                writer.writerow({'called method': calledMethod, 'method': self.listener.calledMethodToMethod[calledMethod] })
                print(calledMethod)
                print(self.listener.calledMethodToMethod[calledMethod])
コード例 #2
0
    def execute(self, input_source):
        parser = JavaParser(
            CommonTokenStream(
                JavaLexer(FileStream(input_source, encoding="utf-8"))))
        walker = ParseTreeWalker()
        walker.walk(self.listener, parser.compilationUnit())

        return self.listener.called_methods
コード例 #3
0
 def execute(self, input_source):
     parser = JavaParser(CommonTokenStream(JavaLexer(FileStream(input_source, encoding="utf-8"))))
     walker = ParseTreeWalker()
     walker.walk(self.listener, parser.compilationUnit())
     # self.logger.debug('Display all data extracted by AST. \n' + pformat(self.listener.ast_info, width=160))
     # print(self.listener.call_methods)
     # print(self.listener.ast_info['methods'])
     return self.listener.ast_info
コード例 #4
0
    def execute(self, input_source):
        parser = JavaParser(
            CommonTokenStream(
                JavaLexer(FileStream(input_source, encoding="utf-8"))))
        walker = ParseTreeWalker()
        walker.walk(self.listener, parser.compilationUnit())

        cnt = 1
        listmethods = []  #テストメソッドを格納するリスト
        listcallmethods = []  #テストメソッド内で呼び出されているメソッド呼び出しを格納するリスト

        ProductionPath = 'C:/Users/ryosuke-ku/Desktop/NiCad-5.1/systems/maven/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java'
        methods_list = AstProcessor2(None, BasicInfoListener()).execute(
            ProductionPath)  #プロダクションファイルから取得したメソッド名(リスト)
        for method in self.listener.methods:
            num = len(self.listener.called_methods[method][0])  #メソッド呼び出しの個数
            # print(num)

            for i in range(num):
                # print(method + '/' + self.listener.called_methods[method][0][i])
                listmethods.append(
                    method)  #listmethodsにテストメソッド名をメソッド呼び出しの数だけ格納する
                listcallmethods.append(
                    str(cnt) + ' ' + self.listener.called_methods[method][0][i]
                )  #テストメソッド内で呼び出されているメソッド呼び出しを格納する、同じ名前のメソッド呼び出しを区別するためのcntのつける
                cnt += 1

        d = dict(zip(listcallmethods, listmethods)
                 )  #テストメソッド名とテストメソッド内で呼び出されているメソッド呼び出しを 1:リスト(複数) で対応付ける
        # print(d)

        rd = rdict(d)

        # print(listmethods)
        # print(len(listmethods))
        # print(listcallmethods)
        # print(len(listcallmethods))

        # print(methods_list)
        # print('------------------------')

        dicMethods = defaultdict(
            list)  #プロダクションファイルのすべてのメソッド名とテストファイルのすべてのメソッド名を対応付け
        for key in methods_list:
            # print(key)

            if rd["^(?=.*" + key + ").*$"] == []:  #keyを正規表現で与えて、valueが空の時
                pass

            else:
                dicMethods[key].append(
                    rd["^(?=.*" + key + ").*$"]
                )  #プロダクションファイルのすべてのメソッド名とテストファイルのすべてのメソッド名を 1:リスト(複数) で対応付ける
                # print(key)
                print(rd["^(?=.*" + key + ").*$"])
コード例 #5
0
    def execute(self, input_source):
        parser = JavaParser(CommonTokenStream(JavaLexer(FileStream(input_source, encoding="utf-8"))))
        walker = ParseTreeWalker()
        walker.walk(self.listener, parser.compilationUnit())
        print(self.listener.called_methods)

        methodName = list(self.listener.called_methods.keys())
        print("<メソッド名➀>")
        print(methodName[0])
        print("<メソッド呼び出し➀>")
        # print(self.listener.called_methods[methodName[0]][0])
        for callmethod1 in self.listener.called_methods[methodName[0]][0]:
            print(callmethod1)
        # print(len(self.listener.called_methods[methodName[0]][0]))
        print("-------------------------------------------------")
        print("<メソッド名➁>")
        print(methodName[1])
        print("<メソッド呼び出し➁>")
        for callmethod2 in self.listener.called_methods[methodName[1]][0]:
            print(callmethod2)
        # print(self.listener.called_methods[methodName[1]][0])
        # print(len(self.listener.called_methods[methodName[1]][0]))
       
        print("-------------------------------------------------")
        if len(self.listener.called_methods[methodName[0]][0]) <= len(self.listener.called_methods[methodName[1]][0]):
            num = len(self.listener.called_methods[methodName[1]][0])
            print("メソッド呼び出しの数 " + str(num))
            callmethod1 = self.listener.called_methods[methodName[1]][0]
            callmethod2 = self.listener.called_methods[methodName[0]][0]
        else:
            num = len(self.listener.called_methods[methodName[0]][0])
            print("メソッド呼び出しの数 " + str(num))
            callmethod1 = self.listener.called_methods[methodName[0]][0]
            callmethod2 = self.listener.called_methods[methodName[1]][0]

        callMethodnum = []
        for method1 in callmethod1:
            for method2 in callmethod2:
                if method1 == method2:
                    index = callmethod2.index(method2)
                    # print(index)
                    # print(method1)
                    callMethodnum.append(method1)
                    callmethod2[index] = ""
                    pass
                else:
                    pass

        print("メソッド呼び出しの差異 " + str(len(callMethodnum)) + "/" + str(num))
コード例 #6
0
    def execute(self, input_source):
        parser = JavaParser(
            CommonTokenStream(
                JavaLexer(FileStream(input_source, encoding="utf-8"))))
        walker = ParseTreeWalker()
        walker.walk(self.listener, parser.compilationUnit())

        cnt = 1
        listmethods = []
        listcallmethods = []
        ProductionPath = 'C:/Users/ryosuke-ku/Desktop/NiCad-5.1/systems/maven/maven-model-builder/src/main/java/org/apache/maven/model/interpolation/StringSearchModelInterpolator.java'
        for method in self.listener.methods:
            num = len(self.listener.called_methods[method][0])
            # print(num)

            for i in range(num):
                print(method + '/' +
                      self.listener.called_methods[method][0][i])
                listmethods.append(method)
                listcallmethods.append(
                    str(cnt) + ' ' +
                    self.listener.called_methods[method][0][i])
                cnt += 1

        d = dict(zip(listcallmethods, listmethods))
        # print(d)

        rd = rdict(d)

        # print(listmethods)
        # print(len(listmethods))
        # print(listcallmethods)
        # print(len(listcallmethods))
        methods_list = AstProcessor2(
            None, BasicInfoListener()).execute(ProductionPath)
        print(methods_list)
        print('------------------------')

        for key in methods_list:
            # print(key)

            if rd["^(?=.*" + key + ").*$"] == []:
                pass

            else:
                print(key)
                print(rd["^(?=.*" + key + ").*$"])
                print(len(rd["^(?=.*" + key + ").*$"]))
コード例 #7
0
ファイル: ast_processor.py プロジェクト: ryosuke-ku/parser
 def execute(self, input_source):
     parser = JavaParser(CommonTokenStream(JavaLexer(FileStream(input_source, encoding="utf-8"))))
     walker = ParseTreeWalker()
     walker.walk(self.listener, parser.compilationUnit())
     # self.logger.debug('Display all data extracted by AST. \n' + pformat(self.listener.ast_info, width=160))
     # print(self.listener.ast_info)
     # return self.listener.ast_info
     # print(self.listener.call_methods)
     for method in self.listener.methods:
         # num = sum(len(v) for v in self.listener.called_methods[method])
         # print(self.listener.called_methods[method][0])
         num = len(self.listener.called_methods[method][0])
         # print(num)
         for i in range(num):
             print(self.listener.called_methods[method][0][i])
             # print(method)
コード例 #8
0
    def execute(self, input_source):
        parser = JavaParser(
            CommonTokenStream(
                JavaLexer(FileStream(input_source, encoding="utf-8"))))
        walker = ParseTreeWalker()
        walker.walk(self.listener, parser.compilationUnit())
        # self.logger.debug('Display all data extracted by AST. \n' + pformat(self.listener.ast_info, width=160))
        # print(self.listener.ast_info)
        # print(self.listener.call_methods)
        # print(self.listener.called_methods)
        # print(self.listener.called_methods['setUp'])
        # print(self.listener.methods['testLocationTrackerShouldBeExcludedFromInterpolation'])
        # print(len(self.listener.methods['testLocationTrackerShouldBeExcludedFromInterpolation']))
        # print(sum(len(v) for v in self.listener.called_methods['testLocationTrackerShouldBeExcludedFromInterpolation']))
        # print(self.listener.methods)

        return self.listener.methods