From: Alexis Laferrière Date: Wed, 29 Jul 2015 19:04:48 +0000 (-0400) Subject: contrib/jwrapper: build class hierarchy into a POSet X-Git-Tag: v0.7.8~112^2~16 X-Git-Url: http://nitlanguage.org contrib/jwrapper: build class hierarchy into a POSet Signed-off-by: Alexis Laferrière --- diff --git a/contrib/jwrapper/src/jwrapper.nit b/contrib/jwrapper/src/jwrapper.nit index 826ce6c..fa709c2 100644 --- a/contrib/jwrapper/src/jwrapper.nit +++ b/contrib/jwrapper/src/jwrapper.nit @@ -194,9 +194,14 @@ var visitor = new JavaVisitor(model) visitor.enter_visit root_node sys.perfs["core model"].add clock.lapse +# Resolve types model.resolve_types sys.perfs["core resolve"].add clock.lapse +# Build class hierarchy +model.build_class_hierarchy +sys.perfs["core hierarchy"].add clock.lapse + if opt_verbose.value > 0 then print "# Generating Nit code" var use_comment = opt_unknown.value == 0 diff --git a/contrib/jwrapper/src/model.nit b/contrib/jwrapper/src/model.nit index e27ab80..d288658 100644 --- a/contrib/jwrapper/src/model.nit +++ b/contrib/jwrapper/src/model.nit @@ -20,6 +20,7 @@ module model import more_collections import opts +import poset import jtype_converter @@ -183,6 +184,9 @@ class JavaClass # Super classes of this class var extends = new HashSet[JavaType] + # Position of self in `model.class_hierarchy` + var in_hierarchy: nullable POSetElement[JavaClass] = null is noserialize + redef fun to_s do return class_type.to_s # Resolve the types in `other` in the context of this class @@ -322,6 +326,48 @@ class JavaModel end end end + + # Specialization hierarchy of `classes` + var class_hierarchy = new POSet[JavaClass] + + # Fill `class_hierarchy` + fun build_class_hierarchy + do + var object_type = new JavaType + object_type.identifier = ["java","lang","Object"] + + # Fill POSet + for name, java_class in classes do + # Skip anonymous classes + if java_class.class_type.is_anonymous then continue + + java_class.in_hierarchy = class_hierarchy.add_node(java_class) + + # Collect explicit super classes + var super_classes = new Array[JavaType] + super_classes.add_all java_class.implements + super_classes.add_all java_class.extends + + # Remove unavailable super classes + for super_type in super_classes.reverse_iterator do + # Is the super class available? + if not classes.keys.has(super_type.package_name) then super_classes.remove(super_type) + end + + # If the is no explicit supers, add `java.lang.Object` + if super_classes.is_empty and java_class.class_type != object_type then + super_classes.add object_type + end + + for super_type in super_classes do + # Is the super class available? + if not classes.keys.has(super_type.package_name) then continue + + var super_class = classes[super_type.package_name] + class_hierarchy.add_edge(java_class, super_class) + end + end + end end # A property to a Java class