コード例 #1
0
def getPackagename(filename: str) -> str:
    retval = ""
    with open(filename) as f:
        in_comment = False
        for line in f.readlines():
            if retval: break
            tokenized = Util.splitToken(line)
            if tokenized:
                for i, token in enumerate(tokenized):
                    if token == "//" and not in_comment: break
                    if token == "/*" and not in_comment:
                        in_comment = True
                        continue
                    elif token == "*/" and in_comment:
                        in_comment = False
                        continue
                    if token == "package" and not in_comment:
                        try:
                            buf = ""
                            token_ = tokenized[i + 1]
                            j = i + 1
                            while token_ != ";":
                                buf += token_
                                j += 1
                                token_ = tokenized[j]
                            retval = buf
                            break
                        except IndexError as e:
                            print("Error line: " + line.rstrip(os.linesep))
    return retval
コード例 #2
0
def getClassname(filename: str) -> list:
    retval = []
    with open(filename) as f:
        in_comment = False
        for line in f.readlines():
            tokenized = Util.splitToken(line)
            if tokenized:
                if tokenized[0] == "//": continue
                for i, token in enumerate(tokenized):
                    if token == "//" and not in_comment: break
                    if token == "/*" and not in_comment:
                        in_comment = True
                        continue
                    elif token == "*/" and in_comment:
                        in_comment = False
                        continue
                    if token == "class" and not in_comment:
                        try:
                            if not tokenized[i + 1] in Util.symbol:
                                retval.append(tokenized[i + 1])
                        except IndexError as e:
                            print("Error line: " + line.rstrip(os.linesep))
    return retval
コード例 #3
0
 def _addTestNumber(self, ESTest_path):
     print("_addTestNumber start")
     try:
         source_lines = self._fileCopyAndMakeLineList(
             ESTest_path,
             path.join(self.getThisProjectPath(), "temp", "temp2.java"))
         # source_lines = self._removeComment(source_lines)
         # print(source_lines)
         with open(ESTest_path, mode='w') as f:
             func_dive = 0
             in_func = False
             objectname = []
             imported = False
             for line in source_lines:
                 tokenized_line = Util.splitToken(line)
                 if len(tokenized_line) >= 3:
                     if tokenized_line[0] == "public" and tokenized_line[
                             1] == "void" and "test" in tokenized_line[2]:
                         test_number = tokenized_line[2].replace("test", "")
                         print(line, file=f)
                         print("System.out.println(\"ESTest_test[" +
                               test_number + "]\");",
                               file=f)
                         func_dive = 1
                         in_func = True
                         continue
                 if in_func:
                     func_dive += tokenized_line.count('{')
                     func_dive -= tokenized_line.count('}')
                     if len(tokenized_line) >= 2:
                         if getClassname(
                                 self.javasource_path
                         )[0] == tokenized_line[0] and Util.isIdentifier(
                                 tokenized_line[1]):
                             objectname.append(tokenized_line[1])
                     if func_dive == 0:
                         in_func = False
                         for o in objectname:
                             print(
                                 "System.out.println(\"FilallyObjectAttributes_start["
                                 + o + "]\");",
                                 file=f)
                             print(
                                 "try{System.out.println(new XStream().toXML("
                                 + o +
                                 "));}catch(Exception e){e.printStackTrace();}",
                                 file=f)
                             print(
                                 "System.out.println(\"FilallyObjectAttributes_end["
                                 + o + "]\");",
                                 file=f)
                         objectname = []
                         print(line, file=f)
                         continue
                 if not imported and "import" in tokenized_line:
                     print("import com.thoughtworks.xstream.XStream;",
                           file=f)
                     imported = True
                 print(line.replace("mockJVMNonDeterminism = true",
                                    "mockJVMNonDeterminism = false"),
                       file=f)
     except:
         raise