Ejemplo n.º 1
0
def cache_file(input_filename,
               instruction_file,
               output_filename,
               tree_callback=None):
    """
    Takes the input filename (not a folder) and runs the instructions on it.
    :param tree_callback: A callback that accepts three arguments: the tree of
                          the input_file, the input filename and the output
                          filename. Use this to apply any modifications before
                          the file is written out. Note that by this point the
                          caching has been applied.
    """
    # Parse input file
    parser = Parser()
    tree = parser.parse_file(input_filename)

    # Run all instructions.
    package = ""
    if tree.package_declaration is not None:
        package = tree.package_declaration.name.value + "."

    cached = []
    for type_decl in tree.type_declarations:
        if isinstance(type_decl, ClassDeclaration):
            name = package + type_decl.name.value
            cached += instruction_file.rewrite_class_decl(name, type_decl)

    if tree_callback is not None:
        tree_callback(tree, input_filename, output_filename)

    # Write tree to output.
    with open(output_filename, "w") as f:
        f.write(tree.serialize())

    return cached
Ejemplo n.º 2
0
    def test_print_empty_class(self):
        parser = Parser()
        tree = parser.parse_string('public class Foo extends Bar {}')
        printer = JavaPrinter()
        tree.accept(printer)
        self.assertEqual('''
public class Foo extends Bar {
}
        '''.strip(), printer.result)
Ejemplo n.º 3
0
    def test_print_empty_class(self):
        parser = Parser()
        tree = parser.parse_string('public class Foo extends Bar {}')
        printer = JavaPrinter()
        tree.accept(printer)
        self.assertEqual(
            '''
public class Foo extends Bar {
}
        '''.strip(), printer.result)
Ejemplo n.º 4
0
    def get_tree(self):
        """
        Parse a .java file and get its declarations, then initialise the references
        """
        with open(path.join(path.dirname(__file__), './', 'app.java')) as j_file:
            parser = Parser()
            tree = parser.parse_file(j_file)
            initial_classes = tree.type_declarations

        self.initialise_references(initial_classes)
Ejemplo n.º 5
0
    def __init__(self,
                 iterations,
                 fitness_method,
                 probabilities,
                 exp_condition,
                 add_state,
                 logging,
                 initial_classes=None):
        """
        :param initial_classes: assuming these classes has no method calls to or inheritances from each other
        :return: None
        """
        # params
        self.iterations = iterations
        self.fitness_method = fitness_method
        self.probabilities = probabilities
        self.exp_condition = exp_condition
        self.add_state = add_state

        if initial_classes is None:
            with open(path.join(path.dirname(__file__), '..',
                                'App.java')) as java_file:
                parser = Parser()
                tree = parser.parse_file(java_file)
                initial_classes = tree.type_declarations
        self.p_create_class = 0.1
        self.p_no_inherit = 0.2
        self.code_modifier = CodeModifier()
        self.inheritance_graph = DiGraph()
        self.reference_graph = DiGraph()
        self.fitness = []
        self.step_n = 0

        for c in initial_classes:
            self.inheritance_graph.add_node(c.name, {'class': c})
            for m in c.body:
                if isinstance(m, MethodDeclaration):
                    self.reference_graph.add_node(m.name, {
                        'method': m,
                        'class': c,
                        'fitness': random(),
                        'lines': 0
                    })

        # Analysis lists
        self.list_fmin = []
        self.list_action = []
        self.list_fitnesses = []
        self.list_fit_stats = []
        self.total_code_size = []
        self.changes = []
Ejemplo n.º 6
0
    def test_print_statement(self):
        parser = Parser()
        tree = parser.parse_string('''public class Foo{
                                        static void bar() { Math.random(); }
                                    }''')
        printer = JavaPrinter()
        tree.accept(printer)
        self.assertEqual('''
public class Foo {
    static void bar() {
        Math.random();
    }
}
        '''.strip(), printer.result)
Ejemplo n.º 7
0
    def test_print_statement(self):
        parser = Parser()
        tree = parser.parse_string('''public class Foo{
                                        static void bar() { Math.random(); }
                                    }''')
        printer = JavaPrinter()
        tree.accept(printer)
        self.assertEqual(
            '''
public class Foo {
    static void bar() {
        Math.random();
    }
}
        '''.strip(), printer.result)
Ejemplo n.º 8
0
    def __init__(self):
        self.counter = 0
        self._revisions = []
        with open(path.join(path.dirname(__file__), '..', 'App.java')) as java_file:
            parser = Parser()
            tree = parser.parse_file(java_file)
            initial_classes = tree.type_declarations
        self._inheritance_graph = DiGraph()
        self._method_call_graph = DiGraph()

        for c in initial_classes:
            self._inheritance_graph.add_node(c.name, {'class': c})
            for m in c.body:
                if isinstance(m, MethodDeclaration):
                    self._method_call_graph.add_node(m.name,
                                                  {'method': m,
                                                   'class_name': c.name,
                                                   'fitness': random()
                                                   })
Ejemplo n.º 9
0
    def test_print_method_with_argument(self):
        parser = Parser()
        tree = parser.parse_string('''
        public class Foo {
            static void main(String[] args) {
                System.out.println(args);
                System.out.println(3);
            }
        }
        ''')
        printer = JavaPrinter()
        tree.accept(printer)
        self.assertEqual('''
public class Foo {
    static void main(String[] args) {
        System.out.println(args);
        System.out.println(3);
    }
}
        '''.strip(), printer.result)
Ejemplo n.º 10
0
def go(in_f, out_f):
    parser = JavaParser()
    print("Parsing...")

    if in_f is not None:
        with open(in_f, "r") as f:
            contents = f.read()
    else:
        contents = sys.stdin.read()

    ast = parser.parse_string(contents)
    # print(ast)

    print("Finding 'generate' function...")
    clazz = ast.type_declarations[0]
    decls_to_keep = []
    stm = None
    for decl in clazz.body:
        if isinstance(decl, MethodDeclaration) and decl.name == "generate":
            if stm is not None:
                die("found duplicate generate() method")
            generated_type = decl.return_type
            args = decl.parameters
            stm = Block(decl.body)
        else:
            decls_to_keep.append(decl)
    if stm is None:
        die("found no generate() method")

    print("Extracting declarations...")
    decls = []
    stm = extract_declarations(stm, decls.append)
    # print(decls)

    print("Numbering yield statements...")
    stm.accept(YieldNumberer())

    print("Constructing iterator...")
    _hn = FieldDeclaration(modifiers=["private"],
                           type="boolean",
                           variable_declarators=[
                               VariableDeclarator(Variable(fresh_name()),
                                                  Literal("false"))
                           ])
    _next = FieldDeclaration(modifiers=["private"],
                             type=generated_type,
                             variable_declarators=[
                                 VariableDeclarator(Variable(fresh_name()),
                                                    Literal("null"))
                             ])
    _state = FieldDeclaration(modifiers=["private"],
                              type="int",
                              variable_declarators=[
                                  VariableDeclarator(Variable(fresh_name()),
                                                     Literal("0"))
                              ])

    _hn_var = Name(_hn.variable_declarators[0].variable.name)
    _next_var = Name(_next.variable_declarators[0].variable.name)
    conts = list(enumerate_conts(stm))
    _state_var = Name(_state.variable_declarators[0].variable.name
                      ) if len(conts) > 1 else None

    init = ConstructorDeclaration(
        clazz.name,
        modifiers=["public"],
        parameters=args,
        block=[
            Assignment(operator="=",
                       lhs=FieldAccess(target="this", name=a.variable.name),
                       rhs=Name(a.variable.name)) for a in args
        ] + run_to_first_yield(stm, _hn_var, _next_var, _state_var))

    has_next = MethodDeclaration(
        "hasNext",
        modifiers=["public"],
        parameters=[],
        return_type="boolean",
        body=[Return(Name(_hn.variable_declarators[0].variable.name))])

    _tmpnext = VariableDeclarator(
        Variable(fresh_name()),
        Name(_next.variable_declarators[0].variable.name))
    get_next = MethodDeclaration("next",
                                 modifiers=["public"],
                                 parameters=[],
                                 return_type=generated_type,
                                 body=[
                                     VariableDeclaration(
                                         generated_type, [_tmpnext]),
                                     MethodInvocation("advance", []),
                                     Return(Name(_tmpnext.variable.name))
                                 ])

    advance = MethodDeclaration(
        "advance",
        modifiers=["private"],
        parameters=[],
        return_type="void",
        body=[
            Assignment(operator="=", lhs=_hn_var, rhs=Literal("false")),
            Switch(_state_var, [
                SwitchCase([Literal(str(i))],
                           run_to_first_yield(
                               k, _hn_var, _next_var, _state_var, k=Break()))
                for (i, k) in conts
            ]) if _state_var else Block(
                run_to_first_yield(conts[0][1], _hn_var, _next_var,
                                   _state_var)) if conts else Empty()
        ])

    it = CompilationUnit(
        package_declaration=ast.package_declaration,
        import_declarations=ast.import_declarations,
        type_declarations=[
            ClassDeclaration(
                clazz.name,
                modifiers=clazz.modifiers,
                type_parameters=clazz.type_parameters,
                extends=clazz.extends,
                implements=clazz.implements,
                body=decls_to_keep + [
                    FieldDeclaration(a.type, [VariableDeclarator(a.variable)],
                                     modifiers=["private"]) for a in args
                ] + [
                    FieldDeclaration(d.type, [
                        VariableDeclarator(v.variable)
                        for v in d.variable_declarators
                    ], ["private"] + d.modifiers) for d in decls
                ] + [_hn, _next] + ([_state] if _state_var else []) +
                [init, has_next, get_next, advance])
        ])

    # print(it)
    print("Writing output...")
    if out_f is not None:
        with open(out_f, "w") as f:
            dump(it, f.write)
    else:
        dump(it, sys.stdout.write)

    print("Done.")
Ejemplo n.º 11
0
 def __parse_file(self, file_path):
     parser = Parser()
     tree = parser.parse_file(file_path)
     self.errors += [PlyjSyntaxError(file_path, e) for e in parser.errors()]
     return tree
Ejemplo n.º 12
0
 def setUp(self):
     self.parser = Parser()
Ejemplo n.º 13
0
class TestCaseWithParser(TestCase):
    def setUp(self):
        self.parser = Parser()

    def parse(self, text):
        return self.parser.parse_string(text)