Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replaced usages of var keyword with explicit type
  • Loading branch information
XHawk87 committed Aug 21, 2024
commit 39f371fec89bd9b2ab5695bfdca43bf09d4cbb2d
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class AnnotationUtils {
* Get the AnnotationMirror for a specific annotation on an element
*/
public Optional<? extends AnnotationMirror> getAnnotationMirror(Element element, Class<? extends Annotation> annotationClass) {
var className = annotationClass.getCanonicalName();
String className = annotationClass.getCanonicalName();
return element.getAnnotationMirrors().stream()
.filter(mirror -> mirror.getAnnotationType().toString().equals(className))
.findFirst();
Expand Down Expand Up @@ -77,7 +77,7 @@ public boolean hasAnnotation(Element element, Class<? extends Annotation> annota
*/
@SafeVarargs
public final boolean hasAnyAnnotation(Element element, Class<? extends Annotation>... annotationClasses) {
for (var annotationClass : annotationClasses) {
for (Class<? extends Annotation> annotationClass : annotationClasses) {
if (hasAnnotation(element, annotationClass)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.auto.service.AutoService;
import dev.jorel.commandapi.annotations.reloaded.annotations.Command;
import dev.jorel.commandapi.annotations.reloaded.generators.IndentedWriter;
import dev.jorel.commandapi.annotations.reloaded.modules.base.CommandsClassGeneratorContext;
import dev.jorel.commandapi.annotations.reloaded.modules.base.CommandsClassModule;
import dev.jorel.commandapi.annotations.reloaded.semantics.SemanticRuleContextData;
import dev.jorel.commandapi.annotations.reloaded.parser.ParserUtils;

Expand All @@ -36,6 +38,7 @@
import java.io.PrintWriter;
import java.time.ZonedDateTime;
import java.util.Comparator;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -75,15 +78,15 @@ public SourceVersion getSupportedSourceVersion() {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
var logging = new Logging(processingEnv);
var baseModule = configuration.getBaseModule();
Logging logging = new Logging(processingEnv);
CommandsClassModule baseModule = configuration.getBaseModule();

// We need to do multiple "phases".
// Firstly, we perform semantic analysis (checking that we've not got two @Default
// annotations, type checking of annotations to method parameter types, ensuring
// suggestions map to what they should), ensuring we've not got two commands of
// the same name...
var semanticsContext = new SemanticRuleContextData(
SemanticRuleContextData semanticsContext = new SemanticRuleContextData(
logging,
processingEnv,
roundEnv,
Expand All @@ -97,7 +100,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
// for each @Command class, which outlines the list of suggestion methods, its
// varying types, etc. You can think of this as a lexing/syntax analysis step.

var commandClasses = roundEnv.getElementsAnnotatedWith(Command.class).stream()
TreeSet<TypeElement> commandClasses = roundEnv.getElementsAnnotatedWith(Command.class).stream()
.map(TypeElement.class::cast) // Change the type of commandClasses -
// we're asserting it's a TypeElement (it literally can't be anything else)
.collect(Collectors.toCollection(() -> // We want things sorted :)
Expand All @@ -110,13 +113,13 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}

logging.info("Parsing contexts");
var parserUtils = new ParserUtils(
ParserUtils parserUtils = new ParserUtils(
logging,
processingEnv,
configuration.getImportsBuilder(),
configuration.getAnnotationUtils()
);
var maybeContexts = baseModule.parseAllContexts(
Optional<CommandsClassGeneratorContext> maybeContexts = baseModule.parseAllContexts(
parserUtils,
configuration.getCommandsClassName(),
ZonedDateTime.now(),
Expand All @@ -128,13 +131,13 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
logging.info("Aborting");
return true;
}
var allContexts = maybeContexts.orElseThrow();
CommandsClassGeneratorContext allContexts = maybeContexts.orElseThrow();
try {
logging.info("Generating %s class".formatted(configuration.getCommandsClassName()));
JavaFileObject builderFile = processingEnv
.getFiler()
.createSourceFile(configuration.getCommandsClassName());
try (var out = new PrintWriter(builderFile.openWriter())) {
try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {
baseModule.generate(new IndentedWriter(out), allContexts);
}
logging.info("%s class generated".formatted(configuration.getCommandsClassName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Set<String> getSupportedAnnotationTypes() {

private SubcommandClassModule getSubcommandClassModule() {
BackReference<SubcommandClassModule> subcommandClassModuleRef = new BackReference<>();
var subcommandClassModule = new SubcommandClassModule(
SubcommandClassModule subcommandClassModule = new SubcommandClassModule(
new SubcommandClassesModule(
subcommandClassModuleRef
), new SubcommandMethodsModule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public class ArgumentAnnotations {
.collect(Collectors.toCollection(LinkedHashSet::new));

public static ArgumentAnnotation from(Annotation annotation, String nodeName) {
var type = annotation.annotationType();
var primitiveType = new PrimitiveType(type.getAnnotation(Primitive.class).value());
Class<? extends Annotation> type = annotation.annotationType();
PrimitiveType primitiveType = new PrimitiveType(type.getAnnotation(Primitive.class).value());
if (annotation instanceof AAdvancementArgument arg) { return new ArgumentAnnotationProperties(type, AdvancementArgument.class, primitiveType, nodeName, arg.optional());
} else if (annotation instanceof AAdventureChatArgument arg) { return new ArgumentAnnotationProperties(type, AdventureChatArgument.class, primitiveType, nodeName, arg.optional());
} else if (annotation instanceof AAdventureChatComponentArgument arg) { return new ArgumentAnnotationProperties(type, AdventureChatComponentArgument.class, primitiveType, nodeName, arg.optional());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ConstructorClassNameRenderer implements Renderer<Class<?>> {

@Override
public String render(Class<?> clazz) {
var nestHost = clazz.getNestHost();
Class<?> nestHost = clazz.getNestHost();
if (clazz != nestHost) {
return "%s.%s".formatted(render(nestHost), clazz.getSimpleName());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void appendInLine(String text) {
* @return The buffered text
*/
public String buffer(Consumer<IndentedWriter> withWriter) {
var buffer = new StringWriter();
StringWriter buffer = new StringWriter();
withWriter.accept(new IndentedWriter(buffer, prefix));
return buffer.toString();
}
Expand All @@ -136,7 +136,7 @@ public void indent(Consumer<IndentedWriter> withWriter) {
* @see #indent(Consumer)
*/
public String indentToBuffer(Consumer<IndentedWriter> withWriter) {
var buffer = new StringWriter();
StringWriter buffer = new StringWriter();
withWriter.accept(new IndentedWriter(buffer, prefix + "\t"));
return buffer.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*******************************************************************************/
package dev.jorel.commandapi.annotations.reloaded.modules.arguments;

import dev.jorel.commandapi.annotations.reloaded.arguments.utils.ArgumentAnnotation;
import dev.jorel.commandapi.annotations.reloaded.generators.InvocationParametersRenderer;
import dev.jorel.commandapi.annotations.reloaded.semantics.SemanticAnalyzer;
import dev.jorel.commandapi.annotations.reloaded.semantics.SemanticRule;
Expand Down Expand Up @@ -67,8 +68,8 @@ public ArgumentModule(

@Override
public void generate(IndentedWriter out, ArgumentGeneratorContext context) {
var argumentAnnotation = context.argumentAnnotation();
var options = out.indentToBuffer(buffer -> {
ArgumentAnnotation argumentAnnotation = context.argumentAnnotation();
String options = out.indentToBuffer(buffer -> {
suggestionsModule.generate(buffer, context.argumentSuggestionsGeneratorContext());
permissionsModule.generate(buffer, context.argumentPermissionGeneratorContext());
requirementsModule.generate(buffer, context.argumentRequirementsGeneratorContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class NodeNameParser implements AnnotationParser<Element, AnnotationParserContext<Element>, String> {
@Override
public Optional<String> parse(AnnotationParserContext<Element> context) {
var element = context.element();
Element element = context.element();
if (element.getAnnotation(NodeName.class) != null) {
return Optional.of(element.getAnnotation(NodeName.class).value());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class ArgumentPermissionsModule implements VariableElementAnalyzerParserG

@Override
public void generate(IndentedWriter out, ArgumentPermissionGeneratorContext context) {
var permission = context.permission();
CommandPermission permission = context.permission();
if (permission.equals(CommandPermission.NONE)) {
// Do nothing
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import dev.jorel.commandapi.annotations.reloaded.generators.CodeGenerator;
import dev.jorel.commandapi.annotations.reloaded.generators.IndentedWriter;
import dev.jorel.commandapi.annotations.reloaded.modules.commands.CommandRegisterMethodGeneratorContext;

/**
* Generates the Javadoc for the Commands class
Expand All @@ -33,7 +34,7 @@ public void generate(IndentedWriter out, CommandsClassGeneratorContext context)
out.printOnNewLine(" * This class was automatically generated by the CommandAPI");
out.printOnNewLine(" * Generation time: %tc".formatted(context.generatorStarted()));
out.printOnNewLine(" * @Command classes used:");
for (var methodContext : context.registerMethods()) {
for (CommandRegisterMethodGeneratorContext methodContext : context.registerMethods()) {
out.printOnNewLine(" * - {@link %s}".formatted(methodContext.qualifiedCommandClassName()));
}
out.printOnNewLine(" */");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import dev.jorel.commandapi.annotations.reloaded.generators.CodeGenerator;
import dev.jorel.commandapi.annotations.reloaded.generators.IndentedWriter;
import dev.jorel.commandapi.annotations.reloaded.modules.commands.CommandRegisterMethodGeneratorContext;
import dev.jorel.commandapi.annotations.reloaded.modules.commands.CommandRegisterMethodModule;
import dev.jorel.commandapi.annotations.reloaded.parser.ParserUtils;
import dev.jorel.commandapi.annotations.reloaded.parser.TypeElementParserContext;
Expand Down Expand Up @@ -61,7 +62,7 @@ public Optional<CommandsClassGeneratorContext> parseAllContexts(
ZonedDateTime generatorStarted,
Set<TypeElement> commandClasses
) {
var maybeAllContexts = commandClasses.stream()
List<Optional<CommandRegisterMethodGeneratorContext>> maybeAllContexts = commandClasses.stream()
.map(element -> commandRegisterMethodModule.parse(new TypeElementParserContext(
parserUtils,
element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
*******************************************************************************/
package dev.jorel.commandapi.annotations.reloaded.modules.commands;

import dev.jorel.commandapi.annotations.reloaded.AnnotationUtils;
import dev.jorel.commandapi.annotations.reloaded.parser.ExecutableElementParser;
import dev.jorel.commandapi.annotations.reloaded.parser.ExecutableElementParserContext;
import dev.jorel.commandapi.annotations.reloaded.parser.ParserUtils;
import dev.jorel.commandapi.annotations.reloaded.parser.TypeElementParserContext;

import javax.lang.model.element.TypeElement;
import java.util.Optional;

/**
Expand All @@ -38,9 +41,9 @@ public CommandExecutorMethodBaseCommandNameParser(CommandNamesParser commandName

@Override
public Optional<String> parse(ExecutableElementParserContext context) {
var utils = context.utils();
var annotationUtils = utils.annotationUtils();
var topLevelClass = annotationUtils.getTopLevelClass(context.element());
ParserUtils utils = context.utils();
AnnotationUtils annotationUtils = utils.annotationUtils();
TypeElement topLevelClass = annotationUtils.getTopLevelClass(context.element());
return commandNamesParser
.parse(new TypeElementParserContext(utils, topLevelClass))
.map(CommandNames::name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@

import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.annotations.reloaded.generators.IndentedWriter;
import dev.jorel.commandapi.annotations.reloaded.modules.arguments.CommandExecutorMethodArgumentsGeneratorContext;
import dev.jorel.commandapi.annotations.reloaded.modules.arguments.CommandExecutorMethodArgumentsModule;
import dev.jorel.commandapi.annotations.reloaded.modules.permissions.CommandExecutorMethodPermissionsGeneratorContext;
import dev.jorel.commandapi.annotations.reloaded.modules.permissions.CommandExecutorMethodPermissionsModule;
import dev.jorel.commandapi.annotations.reloaded.parser.ParserUtils;
import dev.jorel.commandapi.annotations.reloaded.semantics.SemanticAnalyzer;
import dev.jorel.commandapi.annotations.reloaded.semantics.SemanticRule;
import dev.jorel.commandapi.annotations.reloaded.modules.ExecutableElementAnalyzerParserGeneratorModule;
Expand Down Expand Up @@ -71,11 +74,11 @@ public void generate(IndentedWriter out, CommandExecutorMethodGeneratorContext c

@Override
public Optional<CommandExecutorMethodGeneratorContext> parse(ExecutableElementParserContext context) {
var utils = context.utils();
var maybeBaseCommandName = commandNameParser.parse(context);
var maybePermissions = permissionsModule.parse(context);
var maybeArguments = argumentsModule.parse(context);
var maybeExecutor = executorModule.parse(context);
ParserUtils utils = context.utils();
Optional<String> maybeBaseCommandName = commandNameParser.parse(context);
Optional<CommandExecutorMethodPermissionsGeneratorContext> maybePermissions = permissionsModule.parse(context);
Optional<CommandExecutorMethodArgumentsGeneratorContext> maybeArguments = argumentsModule.parse(context);
Optional<CommandExecutorMethodExecutorGeneratorContext> maybeExecutor = executorModule.parse(context);
if (
maybeBaseCommandName.isEmpty() ||
maybePermissions.isEmpty() ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
*******************************************************************************/
package dev.jorel.commandapi.annotations.reloaded.modules.commands;

import dev.jorel.commandapi.annotations.reloaded.AnnotationUtils;
import dev.jorel.commandapi.annotations.reloaded.annotations.Executes;
import dev.jorel.commandapi.annotations.reloaded.generators.IndentedWriter;
import dev.jorel.commandapi.annotations.reloaded.parser.ParserUtils;
import dev.jorel.commandapi.annotations.reloaded.semantics.SemanticAnalyzer;
import dev.jorel.commandapi.annotations.reloaded.semantics.SemanticRule;
import dev.jorel.commandapi.annotations.reloaded.modules.TypeElementAnalyzerParserGeneratorModule;
Expand All @@ -43,16 +45,17 @@ public CommandExecutorsModule(CommandExecutorMethodModule commandExecutorMethodM

@Override
public void generate(IndentedWriter out, CommandExecutorsGeneratorContext context) {
for (var executorContext : context.list()) {
for (CommandExecutorMethodGeneratorContext executorContext : context.list()) {
commandExecutorMethodModule.generate(out, executorContext);
}
}

@Override
public Optional<CommandExecutorsGeneratorContext> parse(TypeElementParserContext context) {
var utils = context.utils();
var annotationUtils = utils.annotationUtils();
var maybeExecutors = annotationUtils.getEnclosedMethodsWithAnnotation(context.element(), Executes.class).stream()
ParserUtils utils = context.utils();
AnnotationUtils annotationUtils = utils.annotationUtils();
List<Optional<CommandExecutorMethodGeneratorContext>> maybeExecutors = annotationUtils
.getEnclosedMethodsWithAnnotation(context.element(), Executes.class).stream()
.map(executorMethod -> new ExecutableElementParserContext(utils, executorMethod))
.map(commandExecutorMethodModule::parse)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
*******************************************************************************/
package dev.jorel.commandapi.annotations.reloaded.modules.commands;

import dev.jorel.commandapi.annotations.reloaded.Logging;
import dev.jorel.commandapi.annotations.reloaded.annotations.Command;
import dev.jorel.commandapi.annotations.reloaded.annotations.Help;
import dev.jorel.commandapi.annotations.reloaded.parser.ParserUtils;
import dev.jorel.commandapi.annotations.reloaded.parser.TypeElementParser;
import dev.jorel.commandapi.annotations.reloaded.parser.TypeElementParserContext;

import javax.lang.model.element.TypeElement;
import java.util.Optional;

/**
Expand All @@ -34,9 +37,9 @@ public class CommandHelpParser implements TypeElementParser<CommandHelp> {

@Override
public Optional<CommandHelp> parse(TypeElementParserContext context) {
var commandClass = context.element();
var utils = context.utils();
var logging = utils.logging();
TypeElement commandClass = context.element();
ParserUtils utils = context.utils();
Logging logging = utils.logging();
Help helpAnnotation = commandClass.getAnnotation(Help.class);
if (helpAnnotation == null) {
return Optional.of(new CommandHelp("", ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
*******************************************************************************/
package dev.jorel.commandapi.annotations.reloaded.modules.commands;

import dev.jorel.commandapi.annotations.reloaded.Logging;
import dev.jorel.commandapi.annotations.reloaded.annotations.Command;
import dev.jorel.commandapi.annotations.reloaded.parser.ParserUtils;
import dev.jorel.commandapi.annotations.reloaded.parser.TypeElementParser;
import dev.jorel.commandapi.annotations.reloaded.parser.TypeElementParserContext;

import javax.lang.model.element.TypeElement;
import java.util.Arrays;
import java.util.Optional;

Expand All @@ -33,23 +36,23 @@
public class CommandNamesParser implements TypeElementParser<CommandNames> {
@Override
public Optional<CommandNames> parse(TypeElementParserContext context) {
var commandClass = context.element();
var utils = context.utils();
var logging = utils.logging();
TypeElement commandClass = context.element();
ParserUtils utils = context.utils();
Logging logging = utils.logging();
logging.info(commandClass, "Parsing command names");
Command command = commandClass.getAnnotation(Command.class);
if (command == null) {
logging.complain(commandClass, "@%s annotation missing on command class"
.formatted(Command.class.getSimpleName()));
return Optional.empty();
}
var names = command.value();
String[] names = command.value();
if (names.length == 0) {
logging.complain(commandClass, "@%s annotation must have at least one value"
.formatted(Command.class.getSimpleName()));
return Optional.empty();
}
var aliases = names.length > 1 ? Arrays.copyOfRange(names, 1, names.length) : new String[0];
String[] aliases = names.length > 1 ? Arrays.copyOfRange(names, 1, names.length) : new String[0];
logging.info(commandClass, "Parsed command names %s and aliases %s"
.formatted(names[0], String.join(", ", aliases)));
return Optional.of(new CommandNames(names[0], aliases));
Expand Down
Loading
Loading