Example #1
0
  def __init__(self, ast):
    # reset ids and lists of meta-classes: Field, Method, and Clazz

    fields_reset()
    methods_reset()
    classes_reset()
    self._frozen = False

    # class declarations in this template
    self._classes = [] # [ Clazz... ]

    # primitive classes
    cls_obj = Clazz(pkg=u"java.lang", name=C.J.OBJ)
    cls_obj.sup = None # Object is the root
    self._classes.append(cls_obj)

    annos = []
    pkg = None
    mods = []
    for _ast in ast.getChildren():
      tag = _ast.getText()
      if tag in [C.T.CLS, C.T.ITF, C.T.ENUM]:
        clazz = parse_class(_ast)
        clazz.annos = annos
        if pkg:
          for cls in util.flatten_classes([clazz], "inners"): cls.pkg = pkg
        clazz.mods = mods
        self._classes.append(clazz)

        annos = []
        pkg = None
        mods = []

      elif tag == C.T.ANNO:
        annos.append(parse_anno(_ast))
      elif tag == C.T.PKG:
        p_node = util.mk_v_node_w_children(_ast.getChildren())
        pkg = util.implode_id(p_node)
      else: # modifiers
        mods.append(tag)
    ## parsing done
    ## post manipulations go below

    logging.debug("# class(es): {}".format(len(classes())))
    logging.debug("# method(s): {}".format(len(methods())))
    logging.debug("# field(s): {}".format(len(fields())))

    self.consist()
Example #2
0
  def __init__(self, ast):
    # reset ids and lists of meta-classes: Field, Method, and Clazz

    fields_reset()
    methods_reset()
    classes_reset()
    self._frozen = False

    # class declarations in this template
    self._classes = [] # [ Clazz... ]
    # event involved in this template
    self._events = {} # [ event_kind0 : 0, ... ]
    self._evt_annotated = False
    # aux types for observer patterns
    self._obs_auxs = {} # { Aux...1 : [ C1, D1 ], ... }
    # aux types for accessor patterns
    self._acc_auxs = [] # [ Aux...1, ... ]
    # aux types for singleton pattern
    self._sng_auxs = [] # [ Aux...1, ... ]

    # primitive classes
    cls_obj = Clazz(pkg=u"java.lang", name=C.J.OBJ)
    cls_obj.sup = None # Object is the root
    self._classes.append(cls_obj)

    find_obs = lambda anno: anno.by_name(C.A.OBS)
    annos = []
    pkg = None
    mods = []
    events = []
    for _ast in ast.getChildren():
      tag = _ast.getText()
      if tag in [C.T.CLS, C.T.ITF, C.T.ENUM]:
        clazz = parse_class(_ast)
        clazz.annos = annos
        if pkg:
          for cls in util.flatten_classes([clazz], "inners"): cls.pkg = pkg
        clazz.mods = mods
        self._classes.append(clazz)

        # collect event kinds
        for cls in util.flatten_classes([clazz], "inners"):
          # 1) class itself is sort of event
          if cls.is_event:
            events.append(repr(cls))
          # 2) might be annotated with explicit event sorts
          elif util.exists(find_obs, annos):
            _anno = util.find(find_obs, annos)
            events.extend(_anno.events)
            self._evt_annotated = True

        annos = []
        pkg = None
        mods = []

      elif tag == C.T.ANNO:
        annos.append(parse_anno(_ast))
      elif tag == C.T.PKG:
        p_node = util.mk_v_node_w_children(_ast.getChildren())
        pkg = util.implode_id(p_node)
      else: # modifiers
        mods.append(tag)
    ## parsing done
    ## post manipulations go below

    logging.debug("# class(es): {}".format(len(classes())))
    logging.debug("# method(s): {}".format(len(methods())))
    logging.debug("# field(s): {}".format(len(fields())))

    self.consist()

    # remove duplicates in events
    events = util.rm_dup(events)
    if events:
      logging.debug("event(s) in the template: {}: {}".format(len(events), events))
      # numbering the event kinds
      for i, event in enumerate(events):
        self._events[event] = i

    # if there exists java.util.EventObject (i.e., cmd == "gui")
    # no additional class is required to represent events
    evt_obj = class_lookup(C.GUI.EVT)
    if evt_obj:
      # artificial field to record subtype events' kinds
      fld = Field(clazz=evt_obj, mods=[C.mod.PB], typ=C.J.i, name=u"kind")
      evt_obj.flds.append(fld)

    else:
      # if there exists android.os.Message (i.e., cmd == "android")
      # no additional class is required, too
      msg = class_lookup(C.ADR.MSG)
      if msg: pass

      # o.w. introduce artificial class Event that implodes all event kinds
      # class Event { int kind; E_kind$n$ evt$n$; }
      elif events:
        cls_e = merge_layer(u"Event", map(class_lookup, events))
        cls_e.add_default_init()
        self._classes.append(cls_e)
        add_artifacts([u"Event"])
Example #3
0
    def __init__(self, ast):
        # reset ids and lists of meta-classes: Field, Method, and Clazz

        fields_reset()
        methods_reset()
        classes_reset()
        self._frozen = False

        # class declarations in this template
        self._classes = []  # [ Clazz... ]
        # event involved in this template
        self._events = {}  # [ event_kind0 : 0, ... ]
        self._evt_annotated = False
        # aux types for observer patterns
        self._obs_auxs = {}  # { Aux...1 : [ C1, D1 ], ... }
        # aux types for accessor patterns
        self._acc_auxs = []  # [ Aux...1, ... ]
        # aux types for singleton pattern
        self._sng_auxs = []  # [ Aux...1, ... ]

        # primitive classes
        cls_obj = Clazz(pkg=u"java.lang", name=C.J.OBJ)
        cls_obj.sup = None  # Object is the root
        self._classes.append(cls_obj)

        find_obs = lambda anno: anno.by_name(C.A.OBS)
        annos = []
        pkg = None
        mods = []
        events = []
        for _ast in ast.getChildren():
            tag = _ast.getText()
            if tag in [C.T.CLS, C.T.ITF, C.T.ENUM]:
                clazz = parse_class(_ast)
                clazz.annos = annos
                if pkg:
                    for cls in util.flatten_classes([clazz], "inners"):
                        cls.pkg = pkg
                clazz.mods = mods
                self._classes.append(clazz)

                # collect event kinds
                for cls in util.flatten_classes([clazz], "inners"):
                    # 1) class itself is sort of event
                    if cls.is_event:
                        events.append(repr(cls))
                    # 2) might be annotated with explicit event sorts
                    elif util.exists(find_obs, annos):
                        _anno = util.find(find_obs, annos)
                        events.extend(_anno.events)
                        self._evt_annotated = True

                annos = []
                pkg = None
                mods = []

            elif tag == C.T.ANNO:
                annos.append(parse_anno(_ast))
            elif tag == C.T.PKG:
                p_node = util.mk_v_node_w_children(_ast.getChildren())
                pkg = util.implode_id(p_node)
            else:  # modifiers
                mods.append(tag)
        ## parsing done
        ## post manipulations go below

        logging.debug("# class(es): {}".format(len(classes())))
        logging.debug("# method(s): {}".format(len(methods())))
        logging.debug("# field(s): {}".format(len(fields())))

        self.consist()

        # remove duplicates in events
        events = util.rm_dup(events)
        if events:
            logging.debug("event(s) in the template: {}: {}".format(
                len(events), events))
            # numbering the event kinds
            for i, event in enumerate(events):
                self._events[event] = i

        # if there exists java.util.EventObject (i.e., cmd == "gui")
        # no additional class is required to represent events
        evt_obj = class_lookup(C.GUI.EVT)
        if evt_obj:
            # artificial field to record subtype events' kinds
            fld = Field(clazz=evt_obj,
                        mods=[C.mod.PB],
                        typ=C.J.i,
                        name=u"kind")
            evt_obj.flds.append(fld)

        else:
            # if there exists android.os.Message (i.e., cmd == "android")
            # no additional class is required, too
            msg = class_lookup(C.ADR.MSG)
            if msg:
                pass

                # o.w. introduce artificial class Event that implodes all event kinds
                # class Event { int kind; E_kind$n$ evt$n$; }
            elif events:
                cls_e = merge_layer(u"Event", map(class_lookup, events))
                cls_e.add_default_init()
                self._classes.append(cls_e)
                add_artifacts([u"Event"])