Class KotlinPropertyEditor

java.lang.Object
com.vaadin.copilot.kotlinrewriter.KotlinPropertyEditor

public final class KotlinPropertyEditor extends Object
The property commands, for a component declared in Kotlin.

One place for what set-component-property and can-be-edited do when the selected component is Kotlin, so the command handler holds a single dependency on this package rather than on the resolver, the rewriter and the component handle separately. That handler is close to Sonar's coupling limit and this change is what keeps it under: the Kotlin path is a self-contained subject, so it may as well be a class.

It is also the shape the remaining phases want. As more commands gain Kotlin support they gain a method here, rather than another branch and three more imports in a handler that already serves seventeen commands.

  • Constructor Details

    • KotlinPropertyEditor

      public KotlinPropertyEditor(KotlinFileSourceProvider sources, Function<com.vaadin.flow.component.Component,List<ComponentTypeAndSourceLocation>> siblings)
      Parameters:
      sources - the parsed Kotlin sources for this operation, so that several edits to one file accumulate into a single write
      siblings - the selected component's peers under its parent, used to work out which of several same-line creations it is
  • Method Details

    • handles

      public static boolean handles(ComponentTypeAndSourceLocation location)
      Whether this component's source is Kotlin, and so belongs to this class.

      Asked of the component's own file rather than of the project, because a project can hold both languages and often does.

      Parameters:
      location - the selected component's type and location
      Returns:
      true if the property commands should be routed here
    • setProperty

      public String setProperty(ComponentTypeAndSourceLocation location, String property, Object value) throws IOException
      Sets a property, or clears it when the value is null.

      The setter's name is resolved by the same reflection the Java path uses, because that is a question about the component's API rather than about the language. Everything after differs: the component is located by offset in a read-only parse tree and the edit is spliced into the text.

      Parameters:
      location - the selected component's type and location
      property - the JavaBean property name
      value - the new value, or null to remove the set
      Returns:
      the variable's new name if the edit renamed it, which it never does — see KotlinRewriter.replaceFunctionCall(com.vaadin.copilot.kotlinrewriter.KotlinComponentInfo, java.lang.String, java.lang.Object)
      Throws:
      IOException - if the source cannot be read
    • readProperty

      public Object readProperty(ComponentTypeAndSourceLocation location, String property) throws IOException
      Reads what the source states for a property.
      Parameters:
      location - the selected component's type and location
      property - the JavaBean property name
      Returns:
      a String, Boolean or Number for a literal, a SourceExpression for anything that has to be evaluated, or null when the source does not set it
      Throws:
      IOException - if the source cannot be read
    • decline

      public static UnsupportedSourceLanguageException decline(String operation)
      Declines an operation this class does not cover.
      Parameters:
      operation - what was asked for, in words a developer will see
      Returns:
      the refusal to throw