Skip to content

Commit

Permalink
[LOGTOOL-142] Allow suppliers to be passed as arguments. If the level…
Browse files Browse the repository at this point in the history
… is enabled on the logger the get method will be called on the supplier.

https://issues.redhat.com/browse/LOGTOOL-142
  • Loading branch information
jamezp committed Jun 10, 2021
1 parent e9dad5b commit 1a915e3
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.function.Supplier;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
Expand All @@ -48,7 +49,6 @@
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;

import org.jboss.logging.Logger;
import org.jboss.logging.annotations.Cause;
import org.jboss.logging.annotations.Field;
import org.jboss.logging.annotations.LogMessage;
Expand All @@ -58,6 +58,8 @@
import org.jboss.logging.annotations.Pos;
import org.jboss.logging.annotations.Property;
import org.jboss.logging.annotations.Suppressed;
import org.jboss.logging.annotations.Transform;
import org.jboss.logging.processor.model.LoggerMessageMethod;
import org.jboss.logging.processor.model.MessageMethod;
import org.jboss.logging.processor.model.Parameter;
import org.jboss.logging.processor.model.ReturnType;
Expand Down Expand Up @@ -93,7 +95,7 @@ Set<MessageMethod> build() {
final Elements elements = processingEnv.getElementUtils();
final Set<MessageMethod> result = new LinkedHashSet<>();
for (ExecutableElement elementMethod : methods) {
final AptMessageMethod resultMethod = new AptMessageMethod(elements, elementMethod);
final AptMessageMethod resultMethod = createMessageMethod(elements, elementMethod);
resultMethod.inheritsMessage = inheritsMessage(methods, elementMethod);
resultMethod.message = findMessage(methods, elementMethod);
resultMethod.isOverloaded = isOverloaded(methods, elementMethod);
Expand Down Expand Up @@ -282,23 +284,30 @@ static MessageMethodBuilder create(final ProcessingEnvironment processingEnv, fi
return new MessageMethodBuilder(processingEnv, expressionProperties);
}

private static AptMessageMethod createMessageMethod(final Elements elements, final ExecutableElement elementMethod) {
if (elementMethod.getAnnotation(LogMessage.class) == null) {
return new AptMessageMethod(elements, elementMethod);
}
return new AptLoggerMessageMethod(elements, elementMethod);
}

/**
* An implementation for the MessageMethod interface.
*/
private static class AptMessageMethod implements MessageMethod {

private final Elements elements;
private final Map<TypeMirror, Set<Parameter>> parameters;
private final Set<ThrowableType> thrownTypes;
private final ExecutableElement method;
private ReturnType returnType;
private Parameter cause;
private boolean inheritsMessage;
private boolean isOverloaded;
private Message message;
private String messageMethodName;
private String translationKey;
private int formatParameterCount;
final Elements elements;
final Map<TypeMirror, Set<Parameter>> parameters;
final Set<ThrowableType> thrownTypes;
final ExecutableElement method;
ReturnType returnType;
Parameter cause;
boolean inheritsMessage;
boolean isOverloaded;
Message message;
String messageMethodName;
String translationKey;
int formatParameterCount;

/**
* Private constructor for the
Expand Down Expand Up @@ -409,37 +418,11 @@ public Parameter cause() {
return cause;
}

@Override
public String loggerMethod() {
switch (message.format()) {
case MESSAGE_FORMAT:
return "logv";
case NO_FORMAT:
return "log";
case PRINTF:
return "logf";
default:
// Should never be hit
return "log";
}
}

@Override
public String logLevel() {
final LogMessage logMessage = method.getAnnotation(LogMessage.class);
return (logMessage.level() == null ? Logger.Level.INFO.name() : logMessage.level().name());
}

@Override
public int formatParameterCount() {
return formatParameterCount;
}

@Override
public boolean isLoggerMethod() {
return isAnnotatedWith(LogMessage.class);
}

@Override
public int hashCode() {
return HashCodeBuilder.builder()
Expand All @@ -448,11 +431,6 @@ public int hashCode() {
.add(returnType()).toHashCode();
}

@Override
public ExecutableElement getDelegate() {
return method;
}

@Override
public boolean equals(final Object obj) {
if (obj == this) {
Expand All @@ -472,8 +450,12 @@ public String toString() {
return ToStringBuilder.of(this)
.add("name", name())
.add("returnType", returnType())
.add("parameters", parameters())
.add("loggerMethod", loggerMethod()).toString();
.add("parameters", parameters()).toString();
}

@Override
public ExecutableElement getDelegate() {
return method;
}

@Override
Expand Down Expand Up @@ -507,6 +489,63 @@ public String getComment() {
}
}

private static class AptLoggerMessageMethod extends AptMessageMethod implements LoggerMessageMethod {


/**
* Private constructor for the
*
* @param elements the elements utility.
* @param method the method to describe.
*/
AptLoggerMessageMethod(final Elements elements, final ExecutableElement method) {
super(elements, method);
}

@Override
public String loggerMethod() {
switch (message.format()) {
case MESSAGE_FORMAT:
return "logv";
case NO_FORMAT:
return "log";
case PRINTF:
return "logf";
default:
// Should never be hit
return "log";
}
}

@Override
public String logLevel() {
final LogMessage logMessage = method.getAnnotation(LogMessage.class);
return logMessage.level().name();
}

@Override
public boolean wrapInEnabledCheck() {
if (!parametersAnnotatedWith(Transform.class).isEmpty()) {
return true;
}
for (Parameter parameter : parameters()) {
if (parameter.isSubtypeOf(Supplier.class)) {
return true;
}
}
return false;
}

@Override
public String toString() {
return ToStringBuilder.of(this)
.add("name", name())
.add("returnType", returnType())
.add("parameters", parameters())
.add("loggerMethod", loggerMethod()).toString();
}
}

private static class AptMessage implements MessageMethod.Message {

private final Message message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.Transform;
import org.jboss.logging.annotations.Transform.TransformType;
import org.jboss.logging.processor.model.LoggerMessageMethod;
import org.jboss.logging.processor.model.MessageInterface;
import org.jboss.logging.processor.model.MessageMethod;
import org.jboss.logging.processor.model.Parameter;
Expand Down Expand Up @@ -258,8 +259,8 @@ private void writeSkeletonMessageMethod(final BufferedWriter writer, final Messa
final MessageMethod.Message msg = messageMethod.message();
writer.write(String.format("# Id: %s", (msg.hasId() ? msg.id() : "none")));
writer.newLine();
if (messageMethod.isLoggerMethod()) {
writer.write(String.format("# Level: %s", messageMethod.logLevel()));
if (messageMethod instanceof LoggerMessageMethod) {
writer.write(String.format("# Level: %s", ((LoggerMessageMethod) messageMethod).logLevel()));
writer.newLine();
}
writer.write(String.format("# Message: %s", msg.value()));
Expand Down Expand Up @@ -322,7 +323,12 @@ private Map<String, String> parseParameterComments(final MessageMethod messageMe
}

private boolean isMethodWritable(final MessageMethod method) {
return !(comparator != null && method.isLoggerMethod()) || (comparator.compareTo(method.logLevel()) >= 0);
if (method instanceof LoggerMessageMethod) {
if (comparator != null) {
return comparator.compareTo(((LoggerMessageMethod) method).logLevel()) >= 0;
}
}
return true;
}

private static void writeSeparatorLine(final BufferedWriter writer) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.BufferedWriter;
import java.io.IOException;

import org.jboss.logging.processor.model.LoggerMessageMethod;
import org.jboss.logging.processor.model.MessageInterface;
import org.jboss.logging.processor.model.MessageMethod;

Expand Down Expand Up @@ -84,8 +85,8 @@ public void writeDetail(final MessageMethod messageMethod) throws IOException {
writer.newLine();
writer.append('|').append(escape(msg.value()));
writer.newLine();
if (messageMethod.isLoggerMethod()) {
writer.append('|').append(getLogLevel(messageMethod));
if (messageMethod instanceof LoggerMessageMethod) {
writer.append('|').append(getLogLevel((LoggerMessageMethod) messageMethod));
writer.newLine();
writer.append("|void");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import org.jboss.logging.annotations.BaseUrl;
import org.jboss.logging.annotations.ResolutionDoc;
import org.jboss.logging.processor.model.LoggerMessageMethod;
import org.jboss.logging.processor.model.MessageInterface;
import org.jboss.logging.processor.model.MessageMethod;
import org.jboss.logging.processor.util.Expressions;
Expand Down Expand Up @@ -124,16 +125,13 @@ public static ReportWriter of(final ReportType reportType, final MessageInterfac
*
* @return the log level or an empty string
*/
String getLogLevel(final MessageMethod method) {
if (method.isLoggerMethod()) {
final String logLevel = method.logLevel();
final int index = logLevel.lastIndexOf('.');
if (index > 0) {
return logLevel.substring(index + 1);
}
return logLevel;
String getLogLevel(final LoggerMessageMethod method) {
final String logLevel = method.logLevel();
final int index = logLevel.lastIndexOf('.');
if (index > 0) {
return logLevel.substring(index + 1);
}
return "";
return logLevel;
}

String getUrl(final MessageMethod messageMethod, final String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.jboss.logging.processor.model.LoggerMessageMethod;
import org.jboss.logging.processor.model.MessageInterface;
import org.jboss.logging.processor.model.MessageMethod;

Expand Down Expand Up @@ -77,8 +78,8 @@ public void writeDetail(final MessageMethod messageMethod) throws IOException {
if (!url.isEmpty()) {
xmlWriter.writeAttribute("resolutionUrl", url);
}
if (messageMethod.isLoggerMethod()) {
xmlWriter.writeAttribute("logLevel", getLogLevel(messageMethod));
if (messageMethod instanceof LoggerMessageMethod) {
xmlWriter.writeAttribute("logLevel", getLogLevel((LoggerMessageMethod) messageMethod));
} else {
xmlWriter.writeAttribute("returnType", messageMethod.returnType().name());
}
Expand Down
Loading

0 comments on commit 1a915e3

Please sign in to comment.