Skip to content

Commit

Permalink
Add a way to override how the @generated(date=?) is rendered
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Jun 11, 2024
1 parent 2cb2a12 commit 1159677
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,17 @@
LoggingToolsProcessor.DEBUG_OPTION,
LoggingToolsProcessor.EXPRESSION_PROPERTIES,
LoggingToolsProcessor.ADD_GENERATED_ANNOTATION,
LoggingToolsProcessor.GENERATED_DATE_VALUE_PROVIDER,
LoggingToolsProcessor.GENERATED_DATE_VALUE_PROVIDER_DATE
})
public class LoggingToolsProcessor extends AbstractProcessor {

public static final String DEBUG_OPTION = "debug";
static final String EXPRESSION_PROPERTIES = "org.jboss.logging.tools.expressionProperties";
static final String ADD_GENERATED_ANNOTATION = "org.jboss.logging.tools.addGeneratedAnnotation";
public static final String GENERATED_DATE_VALUE_PROVIDER = "org.jboss.logging.tools.generatedDateValueProvider";
public static final String GENERATED_DATE_VALUE_PROVIDER_DATE = "org.jboss.logging.tools.generatedDateValueProvider.date";

private final List<String> interfaceAnnotations = Arrays.asList(MessageBundle.class.getName(),
MessageLogger.class.getName());
private final List<AbstractGenerator> generators;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public abstract class ClassModel {
private final String format;

private final Map<String, JMethodDef> messageMethods;
private final GeneratedDateValueProvider generatedDateValueProvider;

final JSourceFile sourceFile;
final ProcessingEnvironment processingEnv;
Expand Down Expand Up @@ -106,6 +107,7 @@ public abstract class ClassModel {
format = "%s%d: %s";
}
messageMethods = new HashMap<>();
generatedDateValueProvider = GeneratedDateValueProvider.of(processingEnv.getOptions());
}

/**
Expand Down Expand Up @@ -144,7 +146,7 @@ JClassDef generateModel() throws IllegalStateException {
sourceFile._import(generatedType);
classDef.annotate(generatedType)
.value("value", getClass().getName())
.value("date", JExprs.str(ClassModelHelper.generatedDateValue()));
.value("date", JExprs.str(generatedDateValueProvider.date()));
}

// Create the default JavaDoc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.jboss.logging.processor.generator.model;

import java.util.Locale;
import java.util.Map;

import org.jboss.logging.processor.apt.LoggingToolsProcessor;

public abstract class GeneratedDateValueProvider {
enum Type {
DEFAULT,
NONE,
FIXED;
}

abstract String date();

public static GeneratedDateValueProvider of(Map<String, String> options) {
Type type = Type.valueOf(options.getOrDefault(LoggingToolsProcessor.GENERATED_DATE_VALUE_PROVIDER, Type.DEFAULT.name())
.toUpperCase(Locale.ROOT));
switch (type) {
case DEFAULT:
return DefaultGeneratedDateValueProvider.INSTANCE;
case NONE:
return NoneGeneratedDateValueProvider.INSTANCE;
case FIXED:
String date = options.get(LoggingToolsProcessor.GENERATED_DATE_VALUE_PROVIDER_DATE);
if (date == null) {
throw new IllegalArgumentException(
"Fixed date generated date value provider is requested, but the date is not specified. "
+ "Use " + LoggingToolsProcessor.GENERATED_DATE_VALUE_PROVIDER_DATE
+ " annotation processor option to provide the date.");
}
return new FixedGeneratedDateValueProvider(date);
default:
throw new IllegalStateException("Unknown generated date value provider type: " + type);
}
}

private static class DefaultGeneratedDateValueProvider extends GeneratedDateValueProvider {

private static final DefaultGeneratedDateValueProvider INSTANCE = new DefaultGeneratedDateValueProvider();

@Override
String date() {
return ClassModelHelper.generatedDateValue();
}
}

private static class NoneGeneratedDateValueProvider extends GeneratedDateValueProvider {

private static final NoneGeneratedDateValueProvider INSTANCE = new NoneGeneratedDateValueProvider();

@Override
String date() {
return "";
}
}

private static class FixedGeneratedDateValueProvider extends GeneratedDateValueProvider {

private final String date;

private FixedGeneratedDateValueProvider(String date) {
this.date = date;
}

@Override
String date() {
return date;
}
}
}

0 comments on commit 1159677

Please sign in to comment.