Class BaseCommandGenerator

java.lang.Object
io.github.projectunified.craftcommand.processor.generator.BaseCommandGenerator
All Implemented Interfaces:
CommandGenerator
Direct Known Subclasses:
BukkitCommandGenerator, PaperCommandGenerator, StandaloneCommandGenerator

public abstract class BaseCommandGenerator extends Object implements CommandGenerator
Base generator shared by every platform.

A platform only supplies its entry points and platform types; routing, parameter resolution and suggestions live here. The generator reads CommandModels only and never parses annotations, so a new platform does not need to know how the parser works.

To add a platform, extend this class, implement generateEntryMethods(com.palantir.javapoet.TypeSpec.Builder, io.github.projectunified.craftcommand.processor.model.CommandModel), getWrapperClassSuffix(), getSenderTypeName(), and getManagerType(), then return it from CommandGeneratorFactory.create(javax.annotation.processing.ProcessingEnvironment).

  • Field Details

  • Constructor Details

  • Method Details

    • generate

      public void generate(CommandModel model) throws IOException
      Description copied from interface: CommandGenerator
      Writes the wrapper class of the given command model.
      Specified by:
      generate in interface CommandGenerator
      Parameters:
      model - the parsed command tree
      Throws:
      IOException - when the source file cannot be written
    • getWrapperClassSuffix

      protected abstract String getWrapperClassSuffix()
      The suffix appended to the command class name, for example "$BukkitCommand".
    • getSenderTypeName

      public abstract com.palantir.javapoet.ClassName getSenderTypeName()
      The platform's native sender type.
    • getManagerType

      protected abstract com.palantir.javapoet.TypeName getManagerType()
      The CommandManager type, including its sender type argument.
    • configureClass

      protected void configureClass(com.palantir.javapoet.TypeSpec.Builder typeSpec, CommandModel model)
      Configures the generated class declaration (superclass, interfaces).
    • addConstructorStatements

      protected void addConstructorStatements(com.palantir.javapoet.MethodSpec.Builder constructorBuilder, CommandModel model)
      Adds platform-specific constructor statements before the instance and manager are assigned.
    • generateEntryMethods

      protected abstract void generateEntryMethods(com.palantir.javapoet.TypeSpec.Builder typeSpec, CommandModel model)
      Adds the platform entry points (execute, tabComplete, getCommandNode, ...).
    • generatePlatformHelpers

      protected void generatePlatformHelpers(com.palantir.javapoet.TypeSpec.Builder typeSpec, CommandModel model)
      Adds platform helper methods referenced from parameter types or entry points.
    • onBeforeExecute

      protected void onBeforeExecute(com.palantir.javapoet.MethodSpec.Builder methodSpec, Element element, String returnStatement)
      Runs before a command method is executed, for example a permission check.
    • generateUnknownSubcommandMessage

      protected void generateUnknownSubcommandMessage(com.palantir.javapoet.MethodSpec.Builder methodSpec, CommandModel model)
      Emits the message shown when a command without arguments has no default action.
    • getSenderExpression

      protected com.palantir.javapoet.CodeBlock getSenderExpression(String senderVar)
      Wraps the raw sender variable in the platform expression that yields the sender object.
    • generateHelpers

      protected void generateHelpers(com.palantir.javapoet.TypeSpec.Builder typeSpec, CommandModel model)
      Generates the execution and suggestion routing plus the sender helpers of the whole command tree.
    • generateSubcommandFieldsAndConstructors

      protected void generateSubcommandFieldsAndConstructors(CommandModel model, com.palantir.javapoet.TypeSpec.Builder typeSpec, com.palantir.javapoet.MethodSpec.Builder constructor, String parentFieldName)
    • getSubcommandFieldName

      protected String getSubcommandFieldName(CommandModel child)
    • getInstanceVarExpression

      public String getInstanceVarExpression(CommandModel classModel, CommandModel rootModel)
    • getResolverInstanceExpr

      public String getResolverInstanceExpr(MethodModel resolver, CommandModel rootModel)
    • generateSenderResolution

      public void generateSenderResolution(com.palantir.javapoet.MethodSpec.Builder spec, CommandModel rootModel, ParameterModel senderParam, com.palantir.javapoet.TypeName senderType, String senderVar)
      Emits the resolution of the command sender: a named @Resolve method, a registered sender resolver, or a platform cast.
    • generateExecuteMethodBody

      public void generateExecuteMethodBody(com.palantir.javapoet.MethodSpec.Builder executeSpec, CommandModel model, String returnStatement)
    • generateSubcommandClassExecutors

      protected void generateSubcommandClassExecutors(com.palantir.javapoet.TypeSpec.Builder typeSpec, CommandModel model, CommandModel rootModel)
    • buildSuggestionRouting

      protected void buildSuggestionRouting(com.palantir.javapoet.MethodSpec.Builder methodSpec, CommandModel model, String argsVar, CommandModel rootModel)
      Generates tab completion routing for a command class, delegating to the suggestion generator.
    • buildSenderCastHelpers

      protected void buildSenderCastHelpers(com.palantir.javapoet.TypeSpec.Builder typeSpec, CommandModel model)
      Generates the sender cast helpers needed by a command class.
    • buildTypeSuggestionHelpers

      protected void buildTypeSuggestionHelpers(com.palantir.javapoet.TypeSpec.Builder typeSpec, CommandModel model)
      Generates the suggestion helpers of type-driven suggestions (boolean / platform built-ins). Platforms that inline custom providers call this from their helper step.
    • getParameterSuggestionMethodName

      protected String getParameterSuggestionMethodName(CommandModel classModel, MethodModel method, int index)
    • getResolverParamSuggestionMethodName

      protected String getResolverParamSuggestionMethodName(CommandModel classModel, ParameterModel parameter)
    • buildCommandInfo

      protected void buildCommandInfo(com.palantir.javapoet.TypeSpec.Builder typeSpec, CommandModel model)
    • typeSupport

      public TypeSupport typeSupport()
    • getBuiltInWidth

      public int getBuiltInWidth(com.palantir.javapoet.TypeName typeName)
    • isSenderType

      public boolean isSenderType(com.palantir.javapoet.TypeName typeName)
    • isSenderBaseType

      public boolean isSenderBaseType(com.palantir.javapoet.TypeName typeName)
    • isSenderParam

      public boolean isSenderParam(com.palantir.javapoet.TypeName typeName, MethodModel method)
    • senderTypeRegistry

      protected SenderTypeRegistry senderTypeRegistry()
    • firstParamIsSender

      public boolean firstParamIsSender(ExecutableElement method)
    • firstParamIsSender

      public boolean firstParamIsSender(ExecutableElement resolverMethod, MethodModel commandMethod)
    • getLocalResolverMinWidth

      public int getLocalResolverMinWidth(MethodModel resolverModel, MethodModel commandMethod)
    • getLocalResolverMaxWidth

      public int getLocalResolverMaxWidth(MethodModel resolverModel, MethodModel commandMethod)
    • getResolverSenderExpression

      public com.palantir.javapoet.CodeBlock getResolverSenderExpression(ExecutableElement localResolver, String rawSourceExpr, String castSenderVar, com.palantir.javapoet.TypeName commandSenderType)
    • buildAliasesExpression

      protected com.palantir.javapoet.CodeBlock buildAliasesExpression(CommandModel model)
    • anyMethod

      protected static boolean anyMethod(CommandModel model, Predicate<ExecutableElement> predicate)
      Traverses the methods of a command tree.
    • call

      public static com.palantir.javapoet.CodeBlock call(String instance, String method, com.palantir.javapoet.CodeBlock... args)
      Builds an instance method invocation, for example instance.resolve(sender, arg).
    • getSubcommandNames

      protected static String getSubcommandNames(CommandModel model)
    • isI18nKey

      protected static boolean isI18nKey(String message)
    • i18nKey

      protected static String i18nKey(String message)
    • runParameterAnnotationHandlers

      public void runParameterAnnotationHandlers(ParameterModel param, String varName, String instanceExpr, String senderVar, com.palantir.javapoet.MethodSpec.Builder methodSpec)
      Runs the SPI parameter handlers of a resolved parameter.
    • generateResolverInvocation

      public void generateResolverInvocation(com.palantir.javapoet.MethodSpec.Builder methodSpec, MethodModel resolverModel, CommandModel rootModel, com.palantir.javapoet.TypeName pTypeName, String varName, com.palantir.javapoet.CodeBlock senderExpr, List<String> resolverArgVarNames, boolean includeSender)
      Emits the call to a resolver method that produces a parameter value.