Class StylePolicy

java.lang.Object
com.vaadin.copilot.rewriter.StylePolicy

public final class StylePolicy extends Object
The decisions behind writing and reading an inline style, with no syntax in them.

Setting a style is two separate questions, and only the first is about the language. Which call to write — style.setColor(…) or style.set("color", …) — is settled by Vaadin's Style API and by how the property is spelled, and the answer is the same whether the file is Java or Kotlin. Where in the source that call goes, and how to splice it in, is not.

This class holds the first kind. It exists because the second Kotlin rewriter would otherwise have to restate it, and a restatement that drifts is worse than a duplicate: the two languages would disagree about what align-items is called, and a style written in one would not be found again by the other.

No JavaParser, no PSI. That is the invariant that makes this shareable, and it is checked by eye rather than by tooling: an Expression or a Kt* type appearing in this file means the decision and the syntax have been mixed up again.

The reflection helpers it calls out to (JavaRewriterUtil.getSetterName(java.lang.String, java.lang.Class<?>, boolean), JavaRewriterUtil.getPropertyName(java.lang.String), JavaRewriterUtil.hasSetterForType(java.lang.Class<?>, java.lang.String, java.lang.Class<?>)) are pure reflection over live classes despite their package: they read Vaadin's API, not a parse tree. The Kotlin property locator already calls the same ones.

  • Method Details

    • setterFor

      public static StylePolicy.StyleCall setterFor(String dashSeparatedProperty)
      Chooses how to write a style property.

      Style has named setters for the properties it knows — setColor, setAlignItems — and a generic set(String, String) for everything else, custom properties included. The named one is preferred because it is what a developer would write, and it is only legal when the setter actually exists and takes a String: some of Style's setters take an enum, and passing a CSS string to those would not compile.

      Parameters:
      dashSeparatedProperty - the CSS property, dash-separated
      Returns:
      the call to write
    • matches

      public static boolean matches(StylePolicy.StyleInfo style, String dashSeparatedProperty)
      Whether a style found in source states the given property.

      The comparison has to be made in one spelling, because the same property reaches here both ways: set("align-items", …) and setAlignItems(…) are the same style and an edit to one must find the other. Dash-separated is the spelling chosen, since that is what the panel and CSS itself use.

      Parameters:
      style - a style read from source
      dashSeparatedProperty - the property being looked for, dash-separated
      Returns:
      true if they are the same property
    • dashSeparated

      public static String dashSeparated(StylePolicy.StyleInfo style)
      Parameters:
      style - a style read from source
      Returns:
      its property, dash-separated whichever way the source spelled it
    • dashSeparated

      public static String dashSeparated(String camelCaseProperty)
      Parameters:
      camelCaseProperty - a property in camelCase, as the sizing commands send it
      Returns:
      the same property dash-separated
    • propertyNameOf

      public static String propertyNameOf(String setter)
      The property a named Style setter sets.
      Parameters:
      setter - the setter's name, such as setAlignItems
      Returns:
      the property in camelCase, such as alignItems
    • cssValueOfEnumConstant

      public static String cssValueOfEnumConstant(String constantName)
      The CSS value an enum constant stands for.

      Style's enum setters name their constants after the CSS keywords, upper-cased with underscores, so SPACE_BETWEEN is space-between. The rule is Vaadin's, not ours — it matches what Style itself does when it writes the value out — so reading a style back has to undo it the same way.

      The locale is pinned, which the code this was lifted from did not do. A default-locale toLowerCase turns I into a dotless ı in Turkish, so ITALIC would have read back as ıtalic for a developer whose machine is set that way.

      Parameters:
      constantName - the enum constant's name as the source writes it
      Returns:
      the CSS value