From 046e8566c25461b503b58cfb1c248b05ca095dd0 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Thu, 23 Jan 2025 12:28:00 +0100 Subject: [PATCH] Regenerate Java model and senders and receivers --- .../python/remote/PythonReceiver.java | 23 +++++ .../python/remote/PythonSender.java | 11 +++ .../python/remote/PythonValidator.java | 7 ++ rewrite/rewrite/java/remote/receiver.py | 19 ++++ rewrite/rewrite/java/remote/sender.py | 9 ++ rewrite/rewrite/java/tree.py | 90 ++++++++++++++++++- rewrite/rewrite/java/visitor.py | 7 ++ rewrite/rewrite/python/remote/receiver.py | 19 ++++ rewrite/rewrite/python/remote/sender.py | 9 ++ 9 files changed, 193 insertions(+), 1 deletion(-) diff --git a/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonReceiver.java b/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonReceiver.java index ebde95b7..4a7af812 100644 --- a/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonReceiver.java +++ b/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonReceiver.java @@ -758,6 +758,17 @@ public J.InstanceOf visitInstanceOf(J.InstanceOf instanceOf, ReceiverContext ctx return instanceOf; } + @Override + public J.DeconstructionPattern visitDeconstructionPattern(J.DeconstructionPattern deconstructionPattern, ReceiverContext ctx) { + deconstructionPattern = deconstructionPattern.withId(ctx.receiveNonNullValue(deconstructionPattern.getId(), UUID.class)); + deconstructionPattern = deconstructionPattern.withPrefix(ctx.receiveNonNullNode(deconstructionPattern.getPrefix(), PythonReceiver::receiveSpace)); + deconstructionPattern = deconstructionPattern.withMarkers(ctx.receiveNonNullNode(deconstructionPattern.getMarkers(), ctx::receiveMarkers)); + deconstructionPattern = deconstructionPattern.withDeconstructor(ctx.receiveNonNullNode(deconstructionPattern.getDeconstructor(), ctx::receiveTree)); + deconstructionPattern = deconstructionPattern.getPadding().withNested(ctx.receiveNonNullNode(deconstructionPattern.getPadding().getNested(), PythonReceiver::receiveContainer)); + deconstructionPattern = deconstructionPattern.withType(ctx.receiveValue(deconstructionPattern.getType(), JavaType.class)); + return deconstructionPattern; + } + @Override public J.IntersectionType visitIntersectionType(J.IntersectionType intersectionType, ReceiverContext ctx) { intersectionType = intersectionType.withId(ctx.receiveNonNullValue(intersectionType.getId(), UUID.class)); @@ -1240,6 +1251,7 @@ protected Function computeValue(Class type) { if (type == J.If.Else.class) return Factory::createJIfElse; if (type == J.Import.class) return Factory::createJImport; if (type == J.InstanceOf.class) return Factory::createJInstanceOf; + if (type == J.DeconstructionPattern.class) return Factory::createJDeconstructionPattern; if (type == J.IntersectionType.class) return Factory::createJIntersectionType; if (type == J.Label.class) return Factory::createJLabel; if (type == J.Lambda.class) return Factory::createJLambda; @@ -1984,6 +1996,17 @@ private static J.InstanceOf createJInstanceOf(ReceiverContext ctx) { ); } + private static J.DeconstructionPattern createJDeconstructionPattern(ReceiverContext ctx) { + return new J.DeconstructionPattern( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, PythonReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, PythonReceiver::receiveContainer), + ctx.receiveValue(null, JavaType.class) + ); + } + private static J.IntersectionType createJIntersectionType(ReceiverContext ctx) { return new J.IntersectionType( ctx.receiveNonNullValue(null, UUID.class), diff --git a/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonSender.java b/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonSender.java index 51fba2d4..f9f1a870 100644 --- a/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonSender.java +++ b/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonSender.java @@ -748,6 +748,17 @@ public J.InstanceOf visitInstanceOf(J.InstanceOf instanceOf, SenderContext ctx) return instanceOf; } + @Override + public J.DeconstructionPattern visitDeconstructionPattern(J.DeconstructionPattern deconstructionPattern, SenderContext ctx) { + ctx.sendValue(deconstructionPattern, J.DeconstructionPattern::getId); + ctx.sendNode(deconstructionPattern, J.DeconstructionPattern::getPrefix, PythonSender::sendSpace); + ctx.sendNode(deconstructionPattern, J.DeconstructionPattern::getMarkers, ctx::sendMarkers); + ctx.sendNode(deconstructionPattern, J.DeconstructionPattern::getDeconstructor, ctx::sendTree); + ctx.sendNode(deconstructionPattern, e -> e.getPadding().getNested(), PythonSender::sendContainer); + ctx.sendTypedValue(deconstructionPattern, J.DeconstructionPattern::getType); + return deconstructionPattern; + } + @Override public J.IntersectionType visitIntersectionType(J.IntersectionType intersectionType, SenderContext ctx) { ctx.sendValue(intersectionType, J.IntersectionType::getId); diff --git a/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonValidator.java b/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonValidator.java index 97613cc8..4717cba5 100644 --- a/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonValidator.java +++ b/rewrite-python-remote/src/main/java/org/openrewrite/python/remote/PythonValidator.java @@ -494,6 +494,13 @@ public J.InstanceOf visitInstanceOf(J.InstanceOf instanceOf, P p) { return instanceOf; } + @Override + public J.DeconstructionPattern visitDeconstructionPattern(J.DeconstructionPattern deconstructionPattern, P p) { + visitAndValidateNonNull(deconstructionPattern.getDeconstructor(), Expression.class, p); + visitAndValidate(deconstructionPattern.getNested(), J.class, p); + return deconstructionPattern; + } + @Override public J.IntersectionType visitIntersectionType(J.IntersectionType intersectionType, P p) { visitAndValidate(intersectionType.getBounds(), TypeTree.class, p); diff --git a/rewrite/rewrite/java/remote/receiver.py b/rewrite/rewrite/java/remote/receiver.py index d5656d4c..555d5d9a 100644 --- a/rewrite/rewrite/java/remote/receiver.py +++ b/rewrite/rewrite/java/remote/receiver.py @@ -303,6 +303,15 @@ def visit_instance_of(self, instance_of: InstanceOf, ctx: ReceiverContext) -> J: instance_of = instance_of.with_type(ctx.receive_value(instance_of.type, JavaType)) return instance_of + def visit_deconstruction_pattern(self, deconstruction_pattern: DeconstructionPattern, ctx: ReceiverContext) -> J: + deconstruction_pattern = deconstruction_pattern.with_id(ctx.receive_value(deconstruction_pattern.id, UUID)) + deconstruction_pattern = deconstruction_pattern.with_prefix(ctx.receive_node(deconstruction_pattern.prefix, JavaReceiver.receive_space)) + deconstruction_pattern = deconstruction_pattern.with_markers(ctx.receive_node(deconstruction_pattern.markers, ctx.receive_markers)) + deconstruction_pattern = deconstruction_pattern.with_deconstructor(ctx.receive_node(deconstruction_pattern.deconstructor, ctx.receive_tree)) + deconstruction_pattern = deconstruction_pattern.padding.with_nested(ctx.receive_node(deconstruction_pattern.padding.nested, JavaReceiver.receive_container)) + deconstruction_pattern = deconstruction_pattern.with_type(ctx.receive_value(deconstruction_pattern.type, JavaType)) + return deconstruction_pattern + def visit_intersection_type(self, intersection_type: IntersectionType, ctx: ReceiverContext) -> J: intersection_type = intersection_type.with_id(ctx.receive_value(intersection_type.id, UUID)) intersection_type = intersection_type.with_prefix(ctx.receive_node(intersection_type.prefix, JavaReceiver.receive_space)) @@ -958,6 +967,16 @@ def create(self, type: str, ctx: ReceiverContext) -> Tree: ctx.receive_value(None, JavaType) ) + if type in ["rewrite.java.tree.DeconstructionPattern", "org.openrewrite.java.tree.J$DeconstructionPattern"]: + return DeconstructionPattern( + ctx.receive_value(None, UUID), + ctx.receive_node(None, JavaReceiver.receive_space), + ctx.receive_node(None, ctx.receive_markers), + ctx.receive_node(None, ctx.receive_tree), + ctx.receive_node(None, JavaReceiver.receive_container), + ctx.receive_value(None, JavaType) + ) + if type in ["rewrite.java.tree.IntersectionType", "org.openrewrite.java.tree.J$IntersectionType"]: return IntersectionType( ctx.receive_value(None, UUID), diff --git a/rewrite/rewrite/java/remote/sender.py b/rewrite/rewrite/java/remote/sender.py index b9b24295..5ed70d83 100644 --- a/rewrite/rewrite/java/remote/sender.py +++ b/rewrite/rewrite/java/remote/sender.py @@ -297,6 +297,15 @@ def visit_instance_of(self, instance_of: InstanceOf, ctx: SenderContext) -> J: ctx.send_typed_value(instance_of, attrgetter('_type')) return instance_of + def visit_deconstruction_pattern(self, deconstruction_pattern: DeconstructionPattern, ctx: SenderContext) -> J: + ctx.send_value(deconstruction_pattern, attrgetter('_id')) + ctx.send_node(deconstruction_pattern, attrgetter('_prefix'), JavaSender.send_space) + ctx.send_node(deconstruction_pattern, attrgetter('_markers'), ctx.send_markers) + ctx.send_node(deconstruction_pattern, attrgetter('_deconstructor'), ctx.send_tree) + ctx.send_node(deconstruction_pattern, attrgetter('_nested'), JavaSender.send_container) + ctx.send_typed_value(deconstruction_pattern, attrgetter('_type')) + return deconstruction_pattern + def visit_intersection_type(self, intersection_type: IntersectionType, ctx: SenderContext) -> J: ctx.send_value(intersection_type, attrgetter('_id')) ctx.send_node(intersection_type, attrgetter('_prefix'), JavaSender.send_space) diff --git a/rewrite/rewrite/java/tree.py b/rewrite/rewrite/java/tree.py index 1a5c7dfa..bb593bff 100644 --- a/rewrite/rewrite/java/tree.py +++ b/rewrite/rewrite/java/tree.py @@ -1289,7 +1289,8 @@ def padding(self) -> PaddingHelper: return p def printer(self, cursor: Cursor) -> TreeVisitor[Tree, PrintOutputCapture[P]]: - return PrinterFactory.current().create_printer(cursor) + factory = PrinterFactory.current() + return factory.create_printer(cursor) if factory else JavaPrinter[PrintOutputCapture[P]]() def accept_java(self, v: JavaVisitor[P], p: P) -> J: return v.visit_compilation_unit(self, p) @@ -2487,6 +2488,93 @@ def padding(self) -> PaddingHelper: def accept_java(self, v: JavaVisitor[P], p: P) -> J: return v.visit_instance_of(self, p) +# noinspection PyShadowingBuiltins,PyShadowingNames,DuplicatedCode +@dataclass(frozen=True, eq=False) +class DeconstructionPattern(TypedTree): + _id: UUID + + @property + def id(self) -> UUID: + return self._id + + def with_id(self, id: UUID) -> DeconstructionPattern: + return self if id is self._id else replace(self, _id=id) + + _prefix: Space + + @property + def prefix(self) -> Space: + return self._prefix + + def with_prefix(self, prefix: Space) -> DeconstructionPattern: + return self if prefix is self._prefix else replace(self, _prefix=prefix) + + _markers: Markers + + @property + def markers(self) -> Markers: + return self._markers + + def with_markers(self, markers: Markers) -> DeconstructionPattern: + return self if markers is self._markers else replace(self, _markers=markers) + + _deconstructor: Expression + + @property + def deconstructor(self) -> Expression: + return self._deconstructor + + def with_deconstructor(self, deconstructor: Expression) -> DeconstructionPattern: + return self if deconstructor is self._deconstructor else replace(self, _deconstructor=deconstructor) + + _nested: JContainer[J] + + @property + def nested(self) -> List[J]: + return self._nested.elements + + def with_nested(self, nested: List[J]) -> DeconstructionPattern: + return self.padding.with_nested(JContainer.with_elements(self._nested, nested)) + + _type: JavaType + + @property + def type(self) -> JavaType: + return self._type + + def with_type(self, type: JavaType) -> DeconstructionPattern: + return self if type is self._type else replace(self, _type=type) + + @dataclass + class PaddingHelper: + _t: DeconstructionPattern + + @property + def nested(self) -> JContainer[J]: + return self._t._nested + + def with_nested(self, nested: JContainer[J]) -> DeconstructionPattern: + return self._t if self._t._nested is nested else replace(self._t, _nested=nested) + + _padding: weakref.ReferenceType[PaddingHelper] = None + + @property + def padding(self) -> PaddingHelper: + p: DeconstructionPattern.PaddingHelper + if self._padding is None: + p = DeconstructionPattern.PaddingHelper(self) + object.__setattr__(self, '_padding', weakref.ref(p)) + else: + p = self._padding() + # noinspection PyProtectedMember + if p is None or p._t != self: + p = DeconstructionPattern.PaddingHelper(self) + object.__setattr__(self, '_padding', weakref.ref(p)) + return p + + def accept_java(self, v: JavaVisitor[P], p: P) -> J: + return v.visit_deconstruction_pattern(self, p) + # noinspection PyShadowingBuiltins,PyShadowingNames,DuplicatedCode @dataclass(frozen=True, eq=False) class IntersectionType(TypeTree, Expression): diff --git a/rewrite/rewrite/java/visitor.py b/rewrite/rewrite/java/visitor.py index 4d15d15b..34b83146 100644 --- a/rewrite/rewrite/java/visitor.py +++ b/rewrite/rewrite/java/visitor.py @@ -351,6 +351,13 @@ def visit_instance_of(self, instance_of: InstanceOf, p: P) -> J: instance_of = instance_of.with_pattern(self.visit_and_cast(instance_of.pattern, J, p)) return instance_of + def visit_deconstruction_pattern(self, deconstruction_pattern: DeconstructionPattern, p: P) -> J: + deconstruction_pattern = deconstruction_pattern.with_prefix(self.visit_space(deconstruction_pattern.prefix, Space.Location.DECONSTRUCTION_PATTERN_PREFIX, p)) + deconstruction_pattern = deconstruction_pattern.with_markers(self.visit_markers(deconstruction_pattern.markers, p)) + deconstruction_pattern = deconstruction_pattern.with_deconstructor(self.visit_and_cast(deconstruction_pattern.deconstructor, Expression, p)) + deconstruction_pattern = deconstruction_pattern.padding.with_nested(self.visit_container(deconstruction_pattern.padding.nested, JContainer.Location.DECONSTRUCTION_PATTERN_NESTED, p)) + return deconstruction_pattern + def visit_intersection_type(self, intersection_type: IntersectionType, p: P) -> J: intersection_type = intersection_type.with_prefix(self.visit_space(intersection_type.prefix, Space.Location.INTERSECTION_TYPE_PREFIX, p)) temp_expression = cast(Expression, self.visit_expression(intersection_type, p)) diff --git a/rewrite/rewrite/python/remote/receiver.py b/rewrite/rewrite/python/remote/receiver.py index 52ce161a..59986232 100644 --- a/rewrite/rewrite/python/remote/receiver.py +++ b/rewrite/rewrite/python/remote/receiver.py @@ -596,6 +596,15 @@ def visit_instance_of(self, instance_of: InstanceOf, ctx: ReceiverContext) -> J: instance_of = instance_of.with_type(ctx.receive_value(instance_of.type, JavaType)) return instance_of + def visit_deconstruction_pattern(self, deconstruction_pattern: DeconstructionPattern, ctx: ReceiverContext) -> J: + deconstruction_pattern = deconstruction_pattern.with_id(ctx.receive_value(deconstruction_pattern.id, UUID)) + deconstruction_pattern = deconstruction_pattern.with_prefix(ctx.receive_node(deconstruction_pattern.prefix, PythonReceiver.receive_space)) + deconstruction_pattern = deconstruction_pattern.with_markers(ctx.receive_node(deconstruction_pattern.markers, ctx.receive_markers)) + deconstruction_pattern = deconstruction_pattern.with_deconstructor(ctx.receive_node(deconstruction_pattern.deconstructor, ctx.receive_tree)) + deconstruction_pattern = deconstruction_pattern.padding.with_nested(ctx.receive_node(deconstruction_pattern.padding.nested, PythonReceiver.receive_container)) + deconstruction_pattern = deconstruction_pattern.with_type(ctx.receive_value(deconstruction_pattern.type, JavaType)) + return deconstruction_pattern + def visit_intersection_type(self, intersection_type: IntersectionType, ctx: ReceiverContext) -> J: intersection_type = intersection_type.with_id(ctx.receive_value(intersection_type.id, UUID)) intersection_type = intersection_type.with_prefix(ctx.receive_node(intersection_type.prefix, PythonReceiver.receive_space)) @@ -1578,6 +1587,16 @@ def create(self, type: str, ctx: ReceiverContext) -> Tree: ctx.receive_value(None, JavaType) ) + if type in ["rewrite.python.tree.DeconstructionPattern", "org.openrewrite.java.tree.J$DeconstructionPattern"]: + return DeconstructionPattern( + ctx.receive_value(None, UUID), + ctx.receive_node(None, PythonReceiver.receive_space), + ctx.receive_node(None, ctx.receive_markers), + ctx.receive_node(None, ctx.receive_tree), + ctx.receive_node(None, PythonReceiver.receive_container), + ctx.receive_value(None, JavaType) + ) + if type in ["rewrite.python.tree.IntersectionType", "org.openrewrite.java.tree.J$IntersectionType"]: return IntersectionType( ctx.receive_value(None, UUID), diff --git a/rewrite/rewrite/python/remote/sender.py b/rewrite/rewrite/python/remote/sender.py index d20891ac..82b91106 100644 --- a/rewrite/rewrite/python/remote/sender.py +++ b/rewrite/rewrite/python/remote/sender.py @@ -590,6 +590,15 @@ def visit_instance_of(self, instance_of: InstanceOf, ctx: SenderContext) -> J: ctx.send_typed_value(instance_of, attrgetter('_type')) return instance_of + def visit_deconstruction_pattern(self, deconstruction_pattern: DeconstructionPattern, ctx: SenderContext) -> J: + ctx.send_value(deconstruction_pattern, attrgetter('_id')) + ctx.send_node(deconstruction_pattern, attrgetter('_prefix'), PythonSender.send_space) + ctx.send_node(deconstruction_pattern, attrgetter('_markers'), ctx.send_markers) + ctx.send_node(deconstruction_pattern, attrgetter('_deconstructor'), ctx.send_tree) + ctx.send_node(deconstruction_pattern, attrgetter('_nested'), PythonSender.send_container) + ctx.send_typed_value(deconstruction_pattern, attrgetter('_type')) + return deconstruction_pattern + def visit_intersection_type(self, intersection_type: IntersectionType, ctx: SenderContext) -> J: ctx.send_value(intersection_type, attrgetter('_id')) ctx.send_node(intersection_type, attrgetter('_prefix'), PythonSender.send_space)