Class TemplatePolicy

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

public final class TemplatePolicy extends Object
What add-template decides regardless of the language it writes.

A dropped component has to be named, and the name depends on what the palette sent rather than on how any language spells a declaration: an id if it has one, otherwise its text, otherwise its label, otherwise the class. Java and Kotlin must agree on that, or the same drop produces saveButton in one and button in the other for no reason a developer could see.

Extracted from JavaRewriterUtil, which delegates here rather than keeping its own copy — the way StylePolicy and BadgePolicy were before it. Only the policy moved; both languages keep their own rendering and their own idea of which names are already taken.

The delegation is the point, not tidiness. This started as a copy, and the first thing the Java tests found when it stopped being one was a divergence already in it: an empty label was ignored here and became the identifier "empty" there. Two copies of a decision drift, which is the mismatch this class exists to prevent. Raised in review of #9803.

  • Field Details

    • ATTACH_CALLS

      public static final Set<String> ATTACH_CALLS
      Every call by which a container takes a component.

      The union of what attachMethod(java.lang.Class<?>, java.lang.Class<?>) may write and the plain forms a developer writes by hand, because a locator looking for "what attached this" has to recognise both. Shared so that the writer and the locator cannot disagree about what an attach looks like — one deciding to write addItem while the other only knows add would mean a component Copilot had just added could not be found again.

      Exact names, never a prefix test: addClassNames and addThemeVariants configure the receiver and begin with the same three letters, so matching on add would read them as attaches and put new configuration above them instead of below.

  • Method Details

    • baseVariableName

      public static String baseVariableName(CopilotComponent component, String classSimpleName, UnaryOperator<String> toIdentifier)
      The name a dropped component wants, before anything checks whether it is free.

      The order is what the palette makes most recognisable in the source: a component the developer gave an id is called that, one with text or a label is called after it, and anything else falls back to its class. The caller decides what to do when the name is taken, because "taken" means something different in each language's scoping.

      Parameters:
      component - the component as the palette described it
      classSimpleName - the simple name of the class that will be instantiated
      toIdentifier - how to turn a human string into an identifier, which is the one piece of this that a caller already has
      Returns:
      the base name, which may already be in use
    • baseVariableName

      public static String baseVariableName(String classSimpleName, String id, String innerText, String text, String label, UnaryOperator<String> toIdentifier)
      The same rule from the parts rather than from a component.

      For a caller that has the strings and no CopilotComponent — renaming one that already exists, rather than naming one being created. The order is the rule, and it lives in one place whichever way it is reached.

      Parameters:
      classSimpleName - the simple name of the component's class
      id - the id property, or null
      innerText - the text between the tags, or null
      text - the text property, or null
      label - the label property, or null
      toIdentifier - how to turn a human string into an identifier
      Returns:
      the base name, which may already be in use
    • isKotlinHardKeyword

      public static boolean isKotlinHardKeyword(String name)
      Parameters:
      name - a candidate identifier
      Returns:
      whether Kotlin would reject it as a declaration name
    • attachMethod

      public static Optional<String> attachMethod(Class<?> parentType, Class<?> childType)
      The call that attaches a child to its parent, read from the parent's API.

      Asked of the type rather than the tag, because the tag cannot answer it. A table from tag to method name was wrong in both directions: it had no entry for a custom component, or for FormLayout.addFormRow, and it answered add for everything it did not know — so a Checkbox dropped into a CheckboxGroup got checkboxGroup.add(orderId), and a CheckboxGroup has no add(Component) at all. Reflection over the parent knows what the table could only guess, including for a class Copilot has never heard of. Raised in review of #9803.

      Parameters:
      parentType - the container being added to
      childType - the component being added
      Returns:
      the method to call, or empty when the parent takes this child through none of the known calls — the caller refuses rather than writing a call that does not compile
    • requestedVariableName

      public static String requestedVariableName(CopilotComponent component)
      Returns:
      the name the palette asked for, or null when it did not ask. A copied or pasted component carries the name it had, and keeping it is what makes a paste read like the thing it was copied from
    • constructorFor

      public static TemplatePolicy.ConstructorCall constructorFor(CopilotComponent component, String classSimpleName)
      How to construct this component, for the components that cannot be constructed with nothing.

      Mirrors JavaRewriterUtil.createComponentConstructor, which made these decisions in JavaParser objects — the decision is the same in any language and only the rendering differs, so it moved here when Kotlin needed it.

      Grid is deliberately absent. Its argument is a class literal, which is DataEntity.class in Java and DataEntity::class.java in Kotlin, and it needs a record generated alongside it — more than a spelling difference, so it stays with the Java path until someone writes it properly for both.

      Parameters:
      component - the component as the palette described it
      classSimpleName - the simple name of the class being instantiated
      Returns:
      the call, never null; TemplatePolicy.ConstructorCall.EMPTY when nothing is needed