def main(): # load parsing library parser = plyj.Parser() # load safe, tainted method list getSafeList() getTaintList() # load input source code input_code = openSourceCode(sys.argv[1]) # make parse tree from source code parse_tree = parser.parse_string(input_code) try: # taint analysis of source code getPrameterNames(parse_tree) analyser(parse_tree) paddingSet() solver(type_set, case, error, trace) # result of analysis showResult() except: print('Parse Error : check syntax error in source code') sys.exit(0)
def parse_methods(cls, data, class_name): parser = plyj.Parser() tree = parser.parse_string(data) LOGGER.info('Parsing methods for class %s.', class_name) methods = [] for ast_type_declaration in tree.type_declarations: if ast_type_declaration.name == class_name: for ast_declaration in ast_type_declaration.body: if isinstance(ast_declaration, ast.MethodDeclaration): method_name = ast_declaration.name parameters = Method.parse_arguments(ast_declaration) signature = '{}({})'.format( method_name, ', '.join([ parameter.signature_type for parameter in parameters ])) regex = '{}\(\s*{}\)'.format( re.escape(method_name), ',\s+'.join([ '(final\s+)?{}\s+{}'.format( re.escape(parameter.complete_type), re.escape(parameter.name)) for parameter in parameters ])) method = cls(method_name, parameters, signature, regex) methods.append(method) return methods
def javaFunctionExtractor(outFile, errFile, inFile='./Java/tmp.java'): # need to return : 1) number of functions # 2) also get types of input and return parameters parser = plyj.Parser() count = 0 tree = parser.parse_file(inFile) for t in tree.type_declarations[0].body: if str(type(t)) == "<class 'plyj.model.MethodDeclaration'>": invars = [] functionCalls = 0 outFile.write(str(t.name)) outFile.write('-') tmp = str(t.body) functionCalls = tmp.count('MethodInvocation') for r in t.parameters: if isinstance(r.type, str): invars.append(r.type) else: invars.append(str(r.type.name.value)) # Note that if has input type of List<Object>, for all s in r.type.name.type_arguments, s.name.value has Object invars = [r.strip() for r in invars] outFile.write(','.join(sorted(invars))) # input variables outFile.write('-') outFile.write(str(t.return_type)) # output variables outFile.write('-') outFile.write(str(functionCalls)) outFile.write(",") outFile.write("\n") count += 1 return count
def make_model(self): for filename in glob.iglob(self.src_path + '**/*.java', recursive=True): parser = plyj.Parser() # parse a compilation unit from a file tree = parser.parse_file(open(filename)) for type_declaration in tree.type_declarations: if isinstance(type_declaration, plyj.ClassDeclaration): new_class = self.make_class(type_declaration) self.model.append(new_class) return self.model
class SimpleJavaTestFileParser(object): parser = plyj.Parser() @staticmethod def old_get_tmethods_lines(java_tfile_path): with open(java_tfile_path, 'r') as tfile: store_next_line = False tmethods_lines = [] for idx, line in enumerate(tfile): if store_next_line: tmethods_lines.append(line) store_next_line = False continue line = line.rstrip('\n').strip('\t').strip(' ') if line == '@Test': store_next_line = True return tmethods_lines @staticmethod def old_parse(java_test_file_path): tmethods_lines = SimpleJavaTestFileParser.old_get_tmethods_lines( java_test_file_path) tmethod_names = [] for line in tmethods_lines: line_split = line.rstrip('\n').strip('\t').strip(' ').split(' ') if line_split[0] != 'public': #is not a valid case (e.g. @ignore) continue methodname_w_brackets = list(filter(lambda w: '(' in w, line_split))[0] methodname = methodname_w_brackets[:-2] tmethod_names.append(methodname) return tmethod_names @classmethod def parse_and_return_methodnameTannotations(cls, java_test_file_path): with open(java_test_file_path, 'r') as tfile: tree = cls.parser.parse_file(tfile) for type_decl in tree.type_declarations: methodnameTannotations = [ ] #(method_name, ['Test', 'Deprecated']) for method_decl in [ decl for decl in type_decl.body if type(decl) is model.MethodDeclaration ]: annotations = [] for modifier in method_decl.modifiers: if type(modifier) is model.Annotation: annotations.append(modifier.name.value) methodnameTannotations.append( (method_decl.name, annotations)) return methodnameTannotations
def create_parser(self): """ create and return a new plyj parser. """ d = self.get_plugin().get_plugin_directory() import sys sys.path.append(d) import plyj.parser as plyj parser = plyj.Parser() sys.path.pop() return parser
class JavaTestClassFileParser(object): parser = plyj.Parser() @classmethod def parse_and_return_methodnameTannotations(cls, java_test_file_path): with open(java_test_file_path, 'r') as tfile: tree = cls.parser.parse_file(tfile) for type_decl in tree.type_declarations: methodnameTannotations = [ ] # (method_name, ['Test', 'Deprecated']) for method_decl in [ decl for decl in type_decl.body if type(decl) is model.MethodDeclaration ]: annotations = [] for modifier in method_decl.modifiers: if type(modifier) is model.Annotation: annotations.append(modifier.name.value) methodnameTannotations.append( (method_decl.name, annotations)) return methodnameTannotations
def parse(code): """ Parses Java code. Uses a modified version of Plyj (line annotations) to parse Java. Args: code: A string of Java code. Returns: A File instance. Raises: ParsingError: When the source code is not valid Java. """ global parser if not parser: parser = plyj.Parser() tree = parser.parse_string(code) tree.body = tree.type_declarations classes = JavaParser.parse_tree(tree) _file = File() _file.classes = classes return _file
def setUp(self): self.parser = plyj.Parser()
methodParams = MethodDec.variable_declarators mDType = "" if hasattr(MethodDec.type, 'name'): mDType = MethodDec.type.name.value else: mDType = MethodDec.type for params in methodParams: Insert_Variable_Invocation(cName, 'baz.java', '-', params.variable.name, mDType, sayac, file_path) except Exception as e: print(e, file_path) parser = plyj.Parser() try: srczip = zipfile.ZipFile(zipName, mode='r') #onlyfiles = [f for f in listdir('srczip') if isfile(join('srczip', f))] onlyfiles = [] for file in srczip.filelist: if (len(file.filename.split('.')) > 1): if (file.filename.split('.')[1] == 'java'): onlyfiles.append(file.filename) except Exception as e: print('hata', e) conn1 = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};'
from plyj import parser import sys if __name__ == "__main__": p = parser.Parser() tokens = p.tokenize_file_clean(sys.argv[1]) for t in tokens: print(t)
for abody_count in range(len(abody_list)): body = abody_list[abody_count] global accessors accessors = aMod(body, accessors) if accessors == "+": if str(type( body)) == "<class 'plyj.model.MethodDeclaration'>": main_string = amethod(body, main_string) accessors = "" return main_string except ValueError: return main_string for aFile in glob.glob("*.java"): ajhad = plyj.Parser().parse_file(aFile) atype_dec_list = ajhad.type_declarations for atype_dec_count in range(len(atype_dec_list)): acount_meth = 0 ac_var = 0 aclass_interface = atype_dec_list[atype_dec_count] main_string_1 = aexContent(aclass_interface, main_string) main_string_1 = "" for atype_dec_count in range(len(atype_dec_list)): acount_meth = 0 aclass_interface = atype_dec_list[atype_dec_count] main_string = aprocFile(aclass_interface, main_string) am_name = set() main_string = avar_link(arefVar, amParam, main_string)
for body_count in range(len(body_list)): body = body_list[body_count] global access access = modifier(body, access) if access == "+": if str(type( body)) == "<class 'plyj.model.MethodDeclaration'>": data_string = method(body, data_string) access = "" return data_string except ValueError: return data_string for file in glob.glob("*.java"): tree = plyj.Parser().parse_file(file) type_delaration_list = tree.type_declarations for type_delaration_count in range(len(type_delaration_list)): count_method = 0 count_variable = 0 classorinterface = type_delaration_list[type_delaration_count] data_string_1 = extractingContent(classorinterface, data_string) data_string_1 = "" for type_delaration_count in range(len(type_delaration_list)): count_method = 0 classorinterface = type_delaration_list[type_delaration_count] data_string = processFile(classorinterface, data_string) methods_name = set() data_string = variable_linking(referenceVariables, methodParameters,