From 4ea4b8d27bb13ad71e3cce2312dce6d488959fd7 Mon Sep 17 00:00:00 2001 From: Adam Juraszek Date: Thu, 9 May 2019 08:29:52 +0200 Subject: IdentifierName instead of String --- .../javac/handlers/HandleFieldNameConstants.java | 23 +++++++++++++++------- src/core/lombok/javac/handlers/HandleLog.java | 11 +++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) (limited to 'src/core/lombok/javac/handlers') diff --git a/src/core/lombok/javac/handlers/HandleFieldNameConstants.java b/src/core/lombok/javac/handlers/HandleFieldNameConstants.java index 628e83dc..ec4015c7 100644 --- a/src/core/lombok/javac/handlers/HandleFieldNameConstants.java +++ b/src/core/lombok/javac/handlers/HandleFieldNameConstants.java @@ -30,6 +30,7 @@ import lombok.AccessLevel; import lombok.ConfigurationKeys; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; +import lombok.core.configuration.IdentifierName; import lombok.core.handlers.HandlerUtil; import lombok.experimental.FieldNameConstants; import lombok.javac.JavacAnnotationHandler; @@ -54,7 +55,9 @@ import com.sun.tools.javac.util.Name; @ProviderFor(JavacAnnotationHandler.class) public class HandleFieldNameConstants extends JavacAnnotationHandler { - public void generateFieldNameConstantsForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean asEnum, String innerTypeName, boolean onlyExplicit, boolean uppercase) { + private static final IdentifierName FIELDS = IdentifierName.valueOf("Fields"); + + public void generateFieldNameConstantsForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean asEnum, IdentifierName innerTypeName, boolean onlyExplicit, boolean uppercase) { JCClassDecl typeDecl = null; if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) typeNode.get(); @@ -115,24 +118,30 @@ public class HandleFieldNameConstants extends JavacAnnotationHandler fields, boolean asEnum, String innerTypeName, boolean uppercase) { + private void createInnerTypeFieldNameConstants(JavacNode typeNode, JavacNode errorNode, JCTree pos, AccessLevel level, java.util.List fields, boolean asEnum, IdentifierName innerTypeName, boolean uppercase) { if (fields.isEmpty()) return; JavacTreeMaker maker = typeNode.getTreeMaker(); JCModifiers mods = maker.Modifiers(toJavacModifier(level) | (asEnum ? Flags.ENUM : Flags.STATIC | Flags.FINAL)); - Name fieldsName = typeNode.toName(innerTypeName); + Name fieldsName = typeNode.toName(innerTypeName.getName()); - JavacNode fieldsType = findInnerClass(typeNode, innerTypeName); + JavacNode fieldsType = findInnerClass(typeNode, innerTypeName.getName()); boolean genConstr = false; if (fieldsType == null) { JCClassDecl innerType = maker.ClassDef(mods, fieldsName, List.nil(), null, List.nil(), List.nil()); diff --git a/src/core/lombok/javac/handlers/HandleLog.java b/src/core/lombok/javac/handlers/HandleLog.java index 6d742e76..da180cec 100644 --- a/src/core/lombok/javac/handlers/HandleLog.java +++ b/src/core/lombok/javac/handlers/HandleLog.java @@ -28,6 +28,7 @@ import java.lang.annotation.Annotation; import lombok.ConfigurationKeys; import lombok.core.AnnotationValues; +import lombok.core.configuration.IdentifierName; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; import lombok.javac.JavacTreeMaker; @@ -46,6 +47,8 @@ import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.Name; public class HandleLog { + private static final IdentifierName LOG = IdentifierName.valueOf("log"); + private HandleLog() { throw new UnsupportedOperationException(); } @@ -56,8 +59,8 @@ public class HandleLog { JavacNode typeNode = annotationNode.up(); switch (typeNode.getKind()) { case TYPE: - String logFieldName = annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_NAME); - if (logFieldName == null) logFieldName = "log"; + IdentifierName logFieldName = annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_NAME); + if (logFieldName == null) logFieldName = LOG; boolean useStatic = !Boolean.FALSE.equals(annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_IS_STATIC)); @@ -65,13 +68,13 @@ public class HandleLog { annotationNode.addError("@Log is legal only on classes and enums."); return; } - if (fieldExists(logFieldName, typeNode) != MemberExistsResult.NOT_EXISTS) { + if (fieldExists(logFieldName.getName(), typeNode) != MemberExistsResult.NOT_EXISTS) { annotationNode.addWarning("Field '" + logFieldName + "' already exists."); return; } JCFieldAccess loggingType = selfType(typeNode); - createField(framework, typeNode, loggingType, annotationNode.get(), logFieldName, useStatic, loggerTopic); + createField(framework, typeNode, loggingType, annotationNode.get(), logFieldName.getName(), useStatic, loggerTopic); break; default: annotationNode.addError("@Log is legal only on types."); -- cgit From bb66465751b8f0b129e53e639854f95fcdb3dca8 Mon Sep 17 00:00:00 2001 From: Adam Juraszek Date: Tue, 14 May 2019 11:57:21 +0200 Subject: Custom log declaration --- src/core/lombok/ConfigurationKeys.java | 31 +++ src/core/lombok/CustomLog.java | 70 +++++++ .../lombok/core/configuration/LogDeclaration.java | 179 ++++++++++++++++++ .../lombok/core/handlers/LoggingFramework.java | 77 ++++++++ src/core/lombok/eclipse/handlers/HandleLog.java | 207 +++++++++------------ .../lombok/extern/apachecommons/CommonsLog.java | 1 + src/core/lombok/extern/flogger/Flogger.java | 1 + src/core/lombok/extern/java/Log.java | 1 + src/core/lombok/extern/jbosslog/JBossLog.java | 3 +- src/core/lombok/extern/log4j/Log4j.java | 1 + src/core/lombok/extern/log4j/Log4j2.java | 1 + src/core/lombok/extern/slf4j/Slf4j.java | 1 + src/core/lombok/extern/slf4j/XSlf4j.java | 1 + src/core/lombok/javac/handlers/HandleLog.java | 137 ++++++-------- .../resource/after-delombok/LoggerCustom.java | 13 ++ .../after-delombok/LoggerCustomWithPackage.java | 14 ++ .../LoggerCustomWithTopicAndName.java | 10 + .../transform/resource/after-ecj/LoggerCustom.java | 21 +++ .../after-ecj/LoggerCustomWithPackage.java | 22 +++ .../after-ecj/LoggerCustomWithTopicAndName.java | 16 ++ test/transform/resource/before/LoggerCustom.java | 13 ++ .../resource/before/LoggerCustomWithPackage.java | 14 ++ .../before/LoggerCustomWithTopicAndName.java | 10 + .../LoggerSlf4jTypes.java.messages | 4 +- 24 files changed, 647 insertions(+), 201 deletions(-) create mode 100644 src/core/lombok/CustomLog.java create mode 100644 src/core/lombok/core/configuration/LogDeclaration.java create mode 100644 src/core/lombok/core/handlers/LoggingFramework.java create mode 100644 test/transform/resource/after-delombok/LoggerCustom.java create mode 100644 test/transform/resource/after-delombok/LoggerCustomWithPackage.java create mode 100644 test/transform/resource/after-delombok/LoggerCustomWithTopicAndName.java create mode 100644 test/transform/resource/after-ecj/LoggerCustom.java create mode 100644 test/transform/resource/after-ecj/LoggerCustomWithPackage.java create mode 100644 test/transform/resource/after-ecj/LoggerCustomWithTopicAndName.java create mode 100644 test/transform/resource/before/LoggerCustom.java create mode 100644 test/transform/resource/before/LoggerCustomWithPackage.java create mode 100644 test/transform/resource/before/LoggerCustomWithTopicAndName.java (limited to 'src/core/lombok/javac/handlers') diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index f70590f9..951b2893 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -25,6 +25,7 @@ import java.util.List; import lombok.core.configuration.CallSuperType; import lombok.core.configuration.ConfigurationKey; +import lombok.core.configuration.LogDeclaration; import lombok.core.configuration.FlagUsageType; import lombok.core.configuration.IdentifierName; import lombok.core.configuration.NullCheckExceptionType; @@ -428,6 +429,36 @@ public class ConfigurationKeys { */ public static final ConfigurationKey LOG_ANY_FIELD_IS_STATIC = new ConfigurationKey("lombok.log.fieldIsStatic", "Make the generated logger fields static (default: true).") {}; + // ----- Custom Logging ----- + + /** + * lombok configuration: {@code lombok.log.custom.flagUsage} = {@code WARNING} | {@code ERROR}. + * + * If set, any usage of {@code @CustomLog} results in a warning / error. + */ + public static final ConfigurationKey LOG_CUSTOM_FLAG_USAGE = new ConfigurationKey("lombok.log.custom.flagUsage", "Emit a warning or error if @CustomLog is used.") {}; + + /** + * lombok configuration: {@code lombok.log.custom.declaration} = <String: logDeclaration>. + * + * The log declaration must follow the pattern: + *
+ * {@code [LoggerType ]LoggerFactoryType.loggerFactoryMethod(loggerFactoryMethodParams)[(loggerFactoryMethodParams)]} + *
+ * It consists of: + *
    + *
  • Optional fully qualified logger type, e.g. {@code my.cool.Logger}, followed by space. If not specified, it defaults to the logger factory type. + *
  • Fully qualified logger factory type, e.g. {@code my.cool.LoggerFactory}, followed by dot. + *
  • Factory method, e.g. {@code createLogger}. The method must be defined on the logger factory type and must be static. + *
  • At least one definition of factory method parameters, e.g. {@code ()} or {@code (TOPIC,TYPE)}. The format is comma-separated list of parameters wrapped in parentheses. + * The allowed parameters are: {@code TYPE} | {@code NAME} | {@code TOPIC} | {@code NULL}. + * There can be at most one parameter definition with {@code TOPIC} and at most one without {@code TOPIC}. + *
+ * + * If not set, any usage of {@code @CustomLog} will result in an error. + */ + public static final ConfigurationKey LOG_CUSTOM_DECLARATION = new ConfigurationKey("lombok.log.custom.declaration", "Define the generated custom logger field.") {}; + // ##### Experimental ##### /** diff --git a/src/core/lombok/CustomLog.java b/src/core/lombok/CustomLog.java new file mode 100644 index 00000000..48c9f2ab --- /dev/null +++ b/src/core/lombok/CustomLog.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2019 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Causes lombok to generate a logger field. + *

+ * Complete documentation is found at the project lombok features page for lombok log annotations. + *

+ * Example: + *

+ * @CustomLog
+ * public class LogExample {
+ * }
+ * 
+ * With configuration: + *
+ * lombok.log.custom.declaration=my.cool.Logger my.cool.LoggerFactory.getLogger(NAME)
+ * 
+ * + * will generate: + * + *
+ * public class LogExample {
+ *     private static final my.cool.Logger log = my.cool.LoggerFactory.getLogger(LogExample.class.getName());
+ * }
+ * 
+ *

+ * Configuration must be provided in lombok.config, otherwise any usage will lead to errors. + * + * This annotation is valid for classes and enumerations.
+ * @see lombok.extern.java.Log @Log + * @see lombok.extern.apachecommons.CommonsLog @CommonsLog + * @see lombok.extern.log4j.Log4j @Log4j + * @see lombok.extern.log4j.Log4j2 @Log4j2 + * @see lombok.extern.slf4j.Slf4j @Slf4j + * @see lombok.extern.slf4j.XSlf4j @XSlf4j + * @see lombok.extern.jbosslog.JBossLog @JBossLog + * @see lombok.extern.flogger.Flogger @Flogger + */ +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.TYPE) +public @interface CustomLog { + /** @return The category of the constructed Logger. By default, it will use the type where the annotation is placed. */ + String topic() default ""; +} diff --git a/src/core/lombok/core/configuration/LogDeclaration.java b/src/core/lombok/core/configuration/LogDeclaration.java new file mode 100644 index 00000000..36f280e2 --- /dev/null +++ b/src/core/lombok/core/configuration/LogDeclaration.java @@ -0,0 +1,179 @@ +/* + * Copyright (C) 2013 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.core.configuration; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public final class LogDeclaration implements ConfigurationValueType { + private static final Pattern PARAMETERS_PATTERN = Pattern.compile("(?:\\(([A-Z,]*)\\))"); + private static final Pattern DECLARATION_PATTERN = Pattern.compile("^(?:([^ ]+) )?([^(]+)\\.([^(]+)(" + PARAMETERS_PATTERN.pattern() + "+)$"); + + public enum LogFactoryParameter { + TYPE, NAME, TOPIC, NULL; + } + + private final TypeName loggerType; + private final TypeName loggerFactoryType; + private final IdentifierName loggerFactoryMethod; + private final List parametersWithoutTopic; + private final List parametersWithTopic; + + private LogDeclaration(TypeName loggerType, TypeName loggerFactoryType, IdentifierName loggerFactoryMethod, List parametersWithoutTopic, List parametersWithTopic) { + this.loggerType = loggerType; + this.loggerFactoryType = loggerFactoryType; + this.loggerFactoryMethod = loggerFactoryMethod; + this.parametersWithoutTopic = parametersWithoutTopic; + this.parametersWithTopic = parametersWithTopic; + } + + public static LogDeclaration valueOf(String declaration) { + if (declaration == null) { + return null; + } + + Matcher matcher = DECLARATION_PATTERN.matcher(declaration); + if (!matcher.matches()) { + throw new IllegalArgumentException("The declaration must follow the pattern: [LoggerType ]LoggerFactoryType.loggerFactoryMethod(loggerFactoryMethodParams)[(loggerFactoryMethodParams)]"); + } + TypeName loggerFactoryType = TypeName.valueOf(matcher.group(2)); + TypeName loggerType = TypeName.valueOf(matcher.group(1)); + if (loggerType == null) loggerType = loggerFactoryType; + IdentifierName loggerFactoryMethod = IdentifierName.valueOf(matcher.group(3)); + List> allParameters = parseParameters(matcher.group(4)); + + List parametersWithoutTopic = null; + List parametersWithTopic = null; + for (List parameters: allParameters) { + if (parameters.contains(LogFactoryParameter.TOPIC)) { + if (parametersWithTopic != null) { + throw new IllegalArgumentException("There are too many parameters with TOPIC: " + parametersWithTopic + " and " + parameters); + } + parametersWithTopic = parameters; + } else { + if (parametersWithoutTopic != null) { + throw new IllegalArgumentException("There are too many parameters without TOPIC: " + parametersWithoutTopic + " and " + parameters); + } + parametersWithoutTopic = parameters; + } + } + if (parametersWithoutTopic == null && parametersWithTopic == null) { + // shouldn't happen the pattern does not allow it + throw new IllegalArgumentException("No logger factory method parameters specified."); + } + + return new LogDeclaration(loggerType, loggerFactoryType, loggerFactoryMethod, parametersWithoutTopic, parametersWithTopic); + } + + private static List> parseParameters(String parametersDefinitions) { + List> allParameters = new ArrayList>(); + Matcher matcher = PARAMETERS_PATTERN.matcher(parametersDefinitions); + while (matcher.find()) { + String parametersDefinition = matcher.group(1); + List parameters = new ArrayList(); + if (!parametersDefinition.isEmpty()) { + for (String parameter : parametersDefinition.split(",")) { + parameters.add(LogFactoryParameter.valueOf(parameter)); + } + } + allParameters.add(parameters); + } + return allParameters; + } + + public static String description() { + return "custom-log-declartation"; + } + + public static String exampleValue() { + return "my.cool.Logger my.cool.LoggerFactory.createLogger()(TOPIC,TYPE)"; + } + + @Override public boolean equals(Object obj) { + if (!(obj instanceof LogDeclaration)) return false; + return loggerType.equals(((LogDeclaration) obj).loggerType) + && loggerFactoryType.equals(((LogDeclaration) obj).loggerFactoryType) + && loggerFactoryMethod.equals(((LogDeclaration) obj).loggerFactoryMethod) + && parametersWithoutTopic == ((LogDeclaration) obj).parametersWithoutTopic || parametersWithoutTopic.equals(((LogDeclaration) obj).parametersWithoutTopic) + && parametersWithTopic == ((LogDeclaration) obj).parametersWithTopic || parametersWithTopic.equals(((LogDeclaration) obj).parametersWithTopic); + } + + @Override public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + loggerType.hashCode(); + result = prime * result + loggerFactoryType.hashCode(); + result = prime * result + loggerFactoryMethod.hashCode(); + result = prime * result + ((parametersWithTopic == null) ? 0 : parametersWithTopic.hashCode()); + result = prime * result + ((parametersWithoutTopic == null) ? 0 : parametersWithoutTopic.hashCode()); + return result; + } + + @Override public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(loggerType); + sb.append(" "); + sb.append(loggerFactoryType); + sb.append("."); + sb.append(loggerFactoryMethod); + appendParams(sb, parametersWithoutTopic); + appendParams(sb, parametersWithTopic); + return sb.toString(); + } + + private static void appendParams(StringBuilder sb, List params) { + if (params != null) { + sb.append("("); + boolean first = true; + for (LogFactoryParameter param : params) { + if (!first) { + sb.append(","); + } + first = false; + sb.append(param); + } + sb.append(")"); + } + } + + public TypeName getLoggerType() { + return loggerType; + } + + public TypeName getLoggerFactoryType() { + return loggerFactoryType; + } + + public IdentifierName getLoggerFactoryMethod() { + return loggerFactoryMethod; + } + + public List getParametersWithoutTopic() { + return parametersWithoutTopic; + } + + public List getParametersWithTopic() { + return parametersWithTopic; + } +} diff --git a/src/core/lombok/core/handlers/LoggingFramework.java b/src/core/lombok/core/handlers/LoggingFramework.java new file mode 100644 index 00000000..38eadd17 --- /dev/null +++ b/src/core/lombok/core/handlers/LoggingFramework.java @@ -0,0 +1,77 @@ +package lombok.core.handlers; + +import java.lang.annotation.Annotation; + +import lombok.core.configuration.LogDeclaration; + +public class LoggingFramework { + // private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(TargetType.class); + public static final LoggingFramework COMMONS = new LoggingFramework( + lombok.extern.apachecommons.CommonsLog.class, + LogDeclaration.valueOf("org.apache.commons.logging.Log org.apache.commons.logging.LogFactory.getLog(TYPE)(TOPIC)") + ); + + // private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(TargetType.class.getName()); + public static final LoggingFramework JUL = new LoggingFramework( + lombok.extern.java.Log.class, + LogDeclaration.valueOf("java.util.logging.Logger java.util.logging.Logger.getLogger(NAME)(TOPIC)") + ); + + // private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(TargetType.class); + public static final LoggingFramework LOG4J = new LoggingFramework( + lombok.extern.log4j.Log4j.class, + LogDeclaration.valueOf("org.apache.log4j.Logger org.apache.log4j.Logger.getLogger(TYPE)(TOPIC)") + ); + + // private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(TargetType.class); + public static final LoggingFramework LOG4J2 = new LoggingFramework( + lombok.extern.log4j.Log4j2.class, + LogDeclaration.valueOf("org.apache.logging.log4j.Logger org.apache.logging.log4j.LogManager.getLogger(TYPE)(TOPIC)") + ); + + // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TargetType.class); + public static final LoggingFramework SLF4J = new LoggingFramework( + lombok.extern.slf4j.Slf4j.class, + LogDeclaration.valueOf("org.slf4j.Logger org.slf4j.LoggerFactory.getLogger(TYPE)(TOPIC)") + ); + + // private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(TargetType.class); + public static final LoggingFramework XSLF4J = new LoggingFramework( + lombok.extern.slf4j.XSlf4j.class, + LogDeclaration.valueOf("org.slf4j.ext.XLogger org.slf4j.ext.XLoggerFactory.getXLogger(TYPE)(TOPIC)") + ); + + // private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(TargetType.class); + public static final LoggingFramework JBOSSLOG = new LoggingFramework( + lombok.extern.jbosslog.JBossLog.class, + LogDeclaration.valueOf("org.jboss.logging.Logger org.jboss.logging.Logger.getLogger(TYPE)(TOPIC)") + ); + + // private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass(); + public static final LoggingFramework FLOGGER = new LoggingFramework( + lombok.extern.flogger.Flogger.class, + LogDeclaration.valueOf("com.google.common.flogger.FluentLogger com.google.common.flogger.FluentLogger.forEnclosingClass()") + ); + + private final Class annotationClass; + private final String annotationAsString; + private final LogDeclaration declaration; + + public LoggingFramework(Class annotationClass, LogDeclaration declaration) { + this.annotationClass = annotationClass; + this.annotationAsString = "@" + annotationClass.getSimpleName(); + this.declaration = declaration; + } + + public Class getAnnotationClass() { + return annotationClass; + } + + public String getAnnotationAsString() { + return annotationAsString; + } + + public LogDeclaration getDeclaration() { + return declaration; + } +} diff --git a/src/core/lombok/eclipse/handlers/HandleLog.java b/src/core/lombok/eclipse/handlers/HandleLog.java index 62ad8fab..e445685d 100644 --- a/src/core/lombok/eclipse/handlers/HandleLog.java +++ b/src/core/lombok/eclipse/handlers/HandleLog.java @@ -21,25 +21,20 @@ */ package lombok.eclipse.handlers; -import static lombok.core.handlers.HandlerUtil.*; +import static lombok.core.handlers.HandlerUtil.handleFlagUsage; import static lombok.eclipse.Eclipse.fromQualifiedName; import static lombok.eclipse.handlers.EclipseHandlerUtil.*; import java.lang.reflect.Modifier; import java.util.Arrays; - -import lombok.ConfigurationKeys; -import lombok.core.AnnotationValues; -import lombok.core.configuration.IdentifierName; -import lombok.eclipse.EclipseAnnotationHandler; -import lombok.eclipse.EclipseNode; -import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult; +import java.util.List; import org.eclipse.jdt.internal.compiler.ast.Annotation; import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess; import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.MessageSend; +import org.eclipse.jdt.internal.compiler.ast.NullLiteral; import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference; import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; import org.eclipse.jdt.internal.compiler.ast.StringLiteral; @@ -48,6 +43,16 @@ import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.mangosdk.spi.ProviderFor; +import lombok.ConfigurationKeys; +import lombok.core.AnnotationValues; +import lombok.core.configuration.IdentifierName; +import lombok.core.configuration.LogDeclaration; +import lombok.core.configuration.LogDeclaration.LogFactoryParameter; +import lombok.core.handlers.LoggingFramework; +import lombok.eclipse.EclipseAnnotationHandler; +import lombok.eclipse.EclipseNode; +import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult; + public class HandleLog { private static final IdentifierName LOG = IdentifierName.valueOf("log"); @@ -82,8 +87,12 @@ public class HandleLog { return; } - ClassLiteralAccess loggingType = selfType(owner, source); + if (loggerTopic != null && loggerTopic.trim().isEmpty()) loggerTopic = null; + if (framework.getDeclaration().getParametersWithTopic() == null && loggerTopic != null) { + annotationNode.addError(framework.getAnnotationAsString() + " does not allow to set a topic."); + } + ClassLiteralAccess loggingType = selfType(owner, source); FieldDeclaration fieldDeclaration = createField(framework, source, loggingType, logFieldName.getName(), useStatic, loggerTopic); fieldDeclaration.traverse(new SetGeneratedByVisitor(source), typeDecl.staticInitializerScope); // TODO temporary workaround for issue 217. http://code.google.com/p/projectlombok/issues/detail?id=217 @@ -120,24 +129,17 @@ public class HandleLog { fieldDecl.declarationSourceEnd = -1; fieldDecl.modifiers = Modifier.PRIVATE | (useStatic ? Modifier.STATIC : 0) | Modifier.FINAL; - fieldDecl.type = createTypeReference(framework.getLoggerTypeName(), source); + LogDeclaration logDeclaration = framework.getDeclaration(); + fieldDecl.type = createTypeReference(logDeclaration.getLoggerType().getName(), source); MessageSend factoryMethodCall = new MessageSend(); setGeneratedBy(factoryMethodCall, source); - factoryMethodCall.receiver = createNameReference(framework.getLoggerFactoryTypeName(), source); - factoryMethodCall.selector = framework.getLoggerFactoryMethodName().toCharArray(); - - Expression parameter; - if (!framework.passTypeName) { - parameter = null; - } else if (loggerTopic == null || loggerTopic.trim().length() == 0) { - parameter = framework.createFactoryParameter(loggingType, source); - } else { - parameter = new StringLiteral(loggerTopic.toCharArray(), pS, pE, 0); - } + factoryMethodCall.receiver = createNameReference(logDeclaration.getLoggerFactoryType().getName(), source); + factoryMethodCall.selector = logDeclaration.getLoggerFactoryMethod().getCharArray(); - factoryMethodCall.arguments = parameter != null ? new Expression[] { parameter } : null; + List parameters = loggerTopic != null ? logDeclaration.getParametersWithTopic() : logDeclaration.getParametersWithoutTopic(); + factoryMethodCall.arguments = createFactoryParameters(loggingType, source, parameters, loggerTopic); factoryMethodCall.nameSourcePosition = p; factoryMethodCall.sourceStart = pS; factoryMethodCall.sourceEnd = factoryMethodCall.statementEnd = pE; @@ -151,22 +153,61 @@ public class HandleLog { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; - TypeReference typeReference; - if (typeName.contains(".")) { - - char[][] typeNameTokens = fromQualifiedName(typeName); - long[] pos = new long[typeNameTokens.length]; - Arrays.fill(pos, p); - - typeReference = new QualifiedTypeReference(typeNameTokens, pos); - } - else { - typeReference = null; - } + char[][] typeNameTokens = fromQualifiedName(typeName); + long[] pos = new long[typeNameTokens.length]; + Arrays.fill(pos, p); + TypeReference typeReference = new QualifiedTypeReference(typeNameTokens, pos); setGeneratedBy(typeReference, source); return typeReference; } + + private static final Expression[] createFactoryParameters(ClassLiteralAccess loggingType, Annotation source, List parameters, String loggerTopic) { + Expression[] expressions = new Expression[parameters.size()]; + int pS = source.sourceStart, pE = source.sourceEnd; + + for (int i = 0; i < parameters.size(); i++) { + LogFactoryParameter parameter = parameters.get(i); + + switch(parameter) { + case TYPE: + expressions[i] = createFactoryTypeParameter(loggingType, source); + break; + case NAME: + long p = (long)pS << 32 | pE; + + MessageSend factoryParameterCall = new MessageSend(); + setGeneratedBy(factoryParameterCall, source); + + factoryParameterCall.receiver = createFactoryTypeParameter(loggingType, source); + factoryParameterCall.selector = "getName".toCharArray(); + + factoryParameterCall.nameSourcePosition = p; + factoryParameterCall.sourceStart = pS; + factoryParameterCall.sourceEnd = factoryParameterCall.statementEnd = pE; + + expressions[i] = factoryParameterCall; + break; + case TOPIC: + expressions[i] = new StringLiteral(loggerTopic.toCharArray(), pS, pE, 0); + break; + case NULL: + expressions[i] = new NullLiteral(pS, pE); + break; + default: + throw new IllegalStateException("Unknown logger factory parameter type: " + parameter); + } + } + + return expressions; + } + + private static final Expression createFactoryTypeParameter(ClassLiteralAccess loggingType, Annotation source) { + TypeReference copy = copyType(loggingType.type, source); + ClassLiteralAccess result = new ClassLiteralAccess(source.sourceEnd, copy); + setGeneratedBy(result, source); + return result; + } /** * Handles the {@link lombok.extern.apachecommons.CommonsLog} annotation for Eclipse. @@ -256,92 +297,20 @@ public class HandleLog { } } - enum LoggingFramework { - // private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(TargetType.class); - COMMONS("org.apache.commons.logging.Log", "org.apache.commons.logging.LogFactory", "getLog", "@CommonsLog"), - - // private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(TargetType.class.getName()); - JUL("java.util.logging.Logger", "java.util.logging.Logger", "getLogger", "@Log") { - @Override public Expression createFactoryParameter(ClassLiteralAccess type, Annotation source) { - int pS = source.sourceStart, pE = source.sourceEnd; - long p = (long)pS << 32 | pE; - - MessageSend factoryParameterCall = new MessageSend(); - setGeneratedBy(factoryParameterCall, source); - - factoryParameterCall.receiver = super.createFactoryParameter(type, source); - factoryParameterCall.selector = "getName".toCharArray(); - - factoryParameterCall.nameSourcePosition = p; - factoryParameterCall.sourceStart = pS; - factoryParameterCall.sourceEnd = factoryParameterCall.statementEnd = pE; - - return factoryParameterCall; + /** + * Handles the {@link lombok.CustomLog} annotation for Eclipse. + */ + @ProviderFor(EclipseAnnotationHandler.class) + public static class HandleCustomLog extends EclipseAnnotationHandler { + @Override public void handle(AnnotationValues annotation, Annotation source, EclipseNode annotationNode) { + handleFlagUsage(annotationNode, ConfigurationKeys.LOG_CUSTOM_FLAG_USAGE, "@CustomLog", ConfigurationKeys.LOG_ANY_FLAG_USAGE, "any @Log"); + LogDeclaration logDeclaration = annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_CUSTOM_DECLARATION); + if (logDeclaration == null) { + annotationNode.addError("The @CustomLog is not configured; please set log.custom.declaration in lombok.config."); + return; } - }, - - // private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(TargetType.class); - LOG4J("org.apache.log4j.Logger", "org.apache.log4j.Logger", "getLogger", "@Log4j"), - - // private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(TargetType.class); - LOG4J2("org.apache.logging.log4j.Logger", "org.apache.logging.log4j.LogManager", "getLogger", "@Log4j2"), - - // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TargetType.class); - SLF4J("org.slf4j.Logger", "org.slf4j.LoggerFactory", "getLogger", "@Slf4j"), - - // private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(TargetType.class); - XSLF4J("org.slf4j.ext.XLogger", "org.slf4j.ext.XLoggerFactory", "getXLogger", "@XSlf4j"), - - // private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(TargetType.class); - JBOSSLOG("org.jboss.logging.Logger", "org.jboss.logging.Logger", "getLogger", "@JBossLog"), - - // private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass(); - FLOGGER("com.google.common.flogger.FluentLogger", "com.google.common.flogger.FluentLogger", "forEnclosingClass", "@Flogger", false), - ; - - private final String loggerTypeName; - private final String loggerFactoryTypeName; - private final String loggerFactoryMethodName; - private final String annotationAsString; - private final boolean passTypeName; - - LoggingFramework(String loggerTypeName, String loggerFactoryTypeName, String loggerFactoryMethodName, String annotationAsString, boolean passTypeName) { - this.loggerTypeName = loggerTypeName; - this.loggerFactoryTypeName = loggerFactoryTypeName; - this.loggerFactoryMethodName = loggerFactoryMethodName; - this.annotationAsString = annotationAsString; - this.passTypeName = passTypeName; - } - - LoggingFramework(String loggerTypeName, String loggerFactoryTypeName, String loggerFactoryMethodName, String annotationAsString) { - this.loggerTypeName = loggerTypeName; - this.loggerFactoryTypeName = loggerFactoryTypeName; - this.loggerFactoryMethodName = loggerFactoryMethodName; - this.annotationAsString = annotationAsString; - this.passTypeName = true; - } - - final String getAnnotationAsString() { - return annotationAsString; - } - - final String getLoggerTypeName() { - return loggerTypeName; + LoggingFramework framework = new LoggingFramework(lombok.CustomLog.class, logDeclaration); + processAnnotation(framework, annotation, source, annotationNode, annotation.getInstance().topic()); } - - final String getLoggerFactoryTypeName() { - return loggerFactoryTypeName; - } - - final String getLoggerFactoryMethodName() { - return loggerFactoryMethodName; - } - - Expression createFactoryParameter(ClassLiteralAccess loggingType, Annotation source) { - TypeReference copy = copyType(loggingType.type, source); - ClassLiteralAccess result = new ClassLiteralAccess(source.sourceEnd, copy); - setGeneratedBy(result, source); - return result; - }; } } diff --git a/src/core/lombok/extern/apachecommons/CommonsLog.java b/src/core/lombok/extern/apachecommons/CommonsLog.java index fa3d6f09..ca808329 100644 --- a/src/core/lombok/extern/apachecommons/CommonsLog.java +++ b/src/core/lombok/extern/apachecommons/CommonsLog.java @@ -57,6 +57,7 @@ import java.lang.annotation.Target; * @see lombok.extern.slf4j.XSlf4j @XSlf4j * @see lombok.extern.jbosslog.JBossLog @JBossLog * @see lombok.extern.flogger.Flogger @Flogger + * @see lombok.CustomLog @CustomLog */ @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) diff --git a/src/core/lombok/extern/flogger/Flogger.java b/src/core/lombok/extern/flogger/Flogger.java index ecbfd28c..3446e949 100644 --- a/src/core/lombok/extern/flogger/Flogger.java +++ b/src/core/lombok/extern/flogger/Flogger.java @@ -55,6 +55,7 @@ import java.lang.annotation.Target; * @see lombok.extern.slf4j.Slf4j @Slf4j * @see lombok.extern.slf4j.XSlf4j @XSlf4j * @see lombok.extern.jbosslog.JBossLog @JBossLog + * @see lombok.CustomLog @CustomLog */ @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) diff --git a/src/core/lombok/extern/java/Log.java b/src/core/lombok/extern/java/Log.java index 9a1ee412..f2b5024f 100644 --- a/src/core/lombok/extern/java/Log.java +++ b/src/core/lombok/extern/java/Log.java @@ -56,6 +56,7 @@ import java.lang.annotation.Target; * @see lombok.extern.slf4j.XSlf4j @XSlf4j * @see lombok.extern.jbosslog.JBossLog @JBossLog * @see lombok.extern.flogger.Flogger @Flogger + * @see lombok.CustomLog @CustomLog */ @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) diff --git a/src/core/lombok/extern/jbosslog/JBossLog.java b/src/core/lombok/extern/jbosslog/JBossLog.java index 684585e0..960a111b 100644 --- a/src/core/lombok/extern/jbosslog/JBossLog.java +++ b/src/core/lombok/extern/jbosslog/JBossLog.java @@ -56,7 +56,8 @@ import java.lang.annotation.Target; * @see lombok.extern.slf4j.Slf4j @Slf4j * @see lombok.extern.slf4j.XSlf4j @XSlf4j * @see lombok.extern.flogger.Flogger @Flogger - * */ + * @see lombok.CustomLog @CustomLog + */ @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) public @interface JBossLog { diff --git a/src/core/lombok/extern/log4j/Log4j.java b/src/core/lombok/extern/log4j/Log4j.java index 249ef71a..1b4a973b 100644 --- a/src/core/lombok/extern/log4j/Log4j.java +++ b/src/core/lombok/extern/log4j/Log4j.java @@ -57,6 +57,7 @@ import java.lang.annotation.Target; * @see lombok.extern.slf4j.XSlf4j @XSlf4j * @see lombok.extern.jbosslog.JBossLog @JBossLog * @see lombok.extern.flogger.Flogger @Flogger + * @see lombok.CustomLog @CustomLog */ @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) diff --git a/src/core/lombok/extern/log4j/Log4j2.java b/src/core/lombok/extern/log4j/Log4j2.java index a6aa90c0..571b0563 100644 --- a/src/core/lombok/extern/log4j/Log4j2.java +++ b/src/core/lombok/extern/log4j/Log4j2.java @@ -57,6 +57,7 @@ import java.lang.annotation.Target; * @see lombok.extern.slf4j.XSlf4j @XSlf4j * @see lombok.extern.jbosslog.JBossLog @JBossLog * @see lombok.extern.flogger.Flogger @Flogger + * @see lombok.CustomLog @CustomLog */ @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) diff --git a/src/core/lombok/extern/slf4j/Slf4j.java b/src/core/lombok/extern/slf4j/Slf4j.java index 347d81d2..c4aded24 100644 --- a/src/core/lombok/extern/slf4j/Slf4j.java +++ b/src/core/lombok/extern/slf4j/Slf4j.java @@ -56,6 +56,7 @@ import java.lang.annotation.Target; * @see lombok.extern.slf4j.XSlf4j @XSlf4j * @see lombok.extern.jbosslog.JBossLog @JBossLog * @see lombok.extern.flogger.Flogger @Flogger + * @see lombok.CustomLog @CustomLog */ @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) diff --git a/src/core/lombok/extern/slf4j/XSlf4j.java b/src/core/lombok/extern/slf4j/XSlf4j.java index 4d53a1eb..b99554a5 100644 --- a/src/core/lombok/extern/slf4j/XSlf4j.java +++ b/src/core/lombok/extern/slf4j/XSlf4j.java @@ -56,6 +56,7 @@ import java.lang.annotation.Target; * @see lombok.extern.slf4j.Slf4j @Slf4j * @see lombok.extern.jbosslog.JBossLog @JBossLog * @see lombok.extern.flogger.Flogger @Flogger + * @see lombok.CustomLog @CustomLog */ @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) diff --git a/src/core/lombok/javac/handlers/HandleLog.java b/src/core/lombok/javac/handlers/HandleLog.java index da180cec..a00cc496 100644 --- a/src/core/lombok/javac/handlers/HandleLog.java +++ b/src/core/lombok/javac/handlers/HandleLog.java @@ -22,16 +22,19 @@ package lombok.javac.handlers; import static lombok.core.handlers.HandlerUtil.*; +import static lombok.javac.Javac.CTC_BOT; import static lombok.javac.handlers.JavacHandlerUtil.*; -import java.lang.annotation.Annotation; - import lombok.ConfigurationKeys; import lombok.core.AnnotationValues; import lombok.core.configuration.IdentifierName; +import lombok.core.configuration.LogDeclaration; +import lombok.core.configuration.LogDeclaration.LogFactoryParameter; +import lombok.core.handlers.LoggingFramework; import lombok.javac.JavacAnnotationHandler; import lombok.javac.JavacNode; import lombok.javac.JavacTreeMaker; +import lombok.javac.handlers.JavacHandlerUtil.MemberExistsResult; import org.mangosdk.spi.ProviderFor; @@ -65,13 +68,18 @@ public class HandleLog { boolean useStatic = !Boolean.FALSE.equals(annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_IS_STATIC)); if ((((JCClassDecl)typeNode.get()).mods.flags & Flags.INTERFACE) != 0) { - annotationNode.addError("@Log is legal only on classes and enums."); + annotationNode.addError(framework.getAnnotationAsString() + " is legal only on classes and enums."); return; } if (fieldExists(logFieldName.getName(), typeNode) != MemberExistsResult.NOT_EXISTS) { annotationNode.addWarning("Field '" + logFieldName + "' already exists."); return; } + + if (loggerTopic != null && loggerTopic.trim().isEmpty()) loggerTopic = null; + if (framework.getDeclaration().getParametersWithTopic() == null && loggerTopic != null) { + annotationNode.addError(framework.getAnnotationAsString() + " does not allow to set a topic."); + } JCFieldAccess loggingType = selfType(typeNode); createField(framework, typeNode, loggingType, annotationNode.get(), logFieldName.getName(), useStatic, loggerTopic); @@ -91,20 +99,14 @@ public class HandleLog { private static boolean createField(LoggingFramework framework, JavacNode typeNode, JCFieldAccess loggingType, JCTree source, String logFieldName, boolean useStatic, String loggerTopic) { JavacTreeMaker maker = typeNode.getTreeMaker(); + LogDeclaration logDeclaration = framework.getDeclaration(); // private static final log = (); - JCExpression loggerType = chainDotsString(typeNode, framework.getLoggerTypeName()); - JCExpression factoryMethod = chainDotsString(typeNode, framework.getLoggerFactoryMethodName()); - - JCExpression loggerName; - if (!framework.passTypeName) { - loggerName = null; - } else if (loggerTopic == null || loggerTopic.trim().length() == 0) { - loggerName = framework.createFactoryParameter(typeNode, loggingType); - } else { - loggerName = maker.Literal(loggerTopic); - } + JCExpression loggerType = chainDotsString(typeNode, logDeclaration.getLoggerType().getName()); + JCExpression factoryMethod = chainDotsString(typeNode, logDeclaration.getLoggerFactoryType().getName() + "." + logDeclaration.getLoggerFactoryMethod().getName()); - JCMethodInvocation factoryMethodCall = maker.Apply(List.nil(), factoryMethod, loggerName != null ? List.of(loggerName) : List.nil()); + java.util.List parameters = loggerTopic != null ? logDeclaration.getParametersWithTopic() : logDeclaration.getParametersWithoutTopic(); + JCExpression[] factoryParameters = createFactoryParameters(typeNode, loggingType, parameters, loggerTopic); + JCMethodInvocation factoryMethodCall = maker.Apply(List.nil(), factoryMethod, List.from(factoryParameters)); JCVariableDecl fieldDecl = recursiveSetGeneratedBy(maker.VarDef( maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (useStatic ? Flags.STATIC : 0)), @@ -114,6 +116,34 @@ public class HandleLog { return true; } + private static JCExpression[] createFactoryParameters(JavacNode typeNode, JCFieldAccess loggingType, java.util.List parameters, String loggerTopic) { + JCExpression[] expressions = new JCExpression[parameters.size()]; + JavacTreeMaker maker = typeNode.getTreeMaker(); + + for (int i = 0; i < parameters.size(); i++) { + LogFactoryParameter parameter = parameters.get(i); + switch (parameter) { + case TYPE: + expressions[i] = loggingType; + break; + case NAME: + JCExpression method = maker.Select(loggingType, typeNode.toName("getName")); + expressions[i] = maker.Apply(List.nil(), method, List.nil()); + break; + case TOPIC: + expressions[i] = maker.Literal(loggerTopic); + break; + case NULL: + expressions[i] = maker.Literal(CTC_BOT, null); + break; + default: + throw new IllegalStateException("Unknown logger factory parameter type: " + parameter); + } + } + + return expressions; + } + /** * Handles the {@link lombok.extern.apachecommons.CommonsLog} annotation for javac. */ @@ -202,71 +232,20 @@ public class HandleLog { } } - enum LoggingFramework { - // private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(TargetType.class); - COMMONS(lombok.extern.apachecommons.CommonsLog.class, "org.apache.commons.logging.Log", "org.apache.commons.logging.LogFactory.getLog"), - - // private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(TargetType.class.getName()); - JUL(lombok.extern.java.Log.class, "java.util.logging.Logger", "java.util.logging.Logger.getLogger") { - @Override public JCExpression createFactoryParameter(JavacNode typeNode, JCFieldAccess loggingType) { - JavacTreeMaker maker = typeNode.getTreeMaker(); - JCExpression method = maker.Select(loggingType, typeNode.toName("getName")); - return maker.Apply(List.nil(), method, List.nil()); + /** + * Handles the {@link lombok.CustomLog} annotation for javac. + */ + @ProviderFor(JavacAnnotationHandler.class) + public static class HandleCustomLog extends JavacAnnotationHandler { + @Override public void handle(AnnotationValues annotation, JCAnnotation ast, JavacNode annotationNode) { + handleFlagUsage(annotationNode, ConfigurationKeys.LOG_CUSTOM_FLAG_USAGE, "@CustomLog", ConfigurationKeys.LOG_ANY_FLAG_USAGE, "any @Log"); + LogDeclaration logDeclaration = annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_CUSTOM_DECLARATION); + if (logDeclaration == null) { + annotationNode.addError("The @CustomLog is not configured; please set log.custom.declaration in lombok.config."); + return; } - }, - - // private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(TargetType.class); - LOG4J(lombok.extern.log4j.Log4j.class, "org.apache.log4j.Logger", "org.apache.log4j.Logger.getLogger"), - - // private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(TargetType.class); - LOG4J2(lombok.extern.log4j.Log4j2.class, "org.apache.logging.log4j.Logger", "org.apache.logging.log4j.LogManager.getLogger"), - - // private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TargetType.class); - SLF4J(lombok.extern.slf4j.Slf4j.class, "org.slf4j.Logger", "org.slf4j.LoggerFactory.getLogger"), - - // private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(TargetType.class); - XSLF4J(lombok.extern.slf4j.XSlf4j.class, "org.slf4j.ext.XLogger", "org.slf4j.ext.XLoggerFactory.getXLogger"), - - // private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(TargetType.class); - JBOSSLOG(lombok.extern.jbosslog.JBossLog.class, "org.jboss.logging.Logger", "org.jboss.logging.Logger.getLogger"), - - // private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass(); - FLOGGER(lombok.extern.flogger.Flogger.class, "com.google.common.flogger.FluentLogger", "com.google.common.flogger.FluentLogger.forEnclosingClass", false), - ; - - private final Class annotationClass; - private final String loggerTypeName; - private final String loggerFactoryName; - private final boolean passTypeName; - - LoggingFramework(Class annotationClass, String loggerTypeName, String loggerFactoryName, boolean passTypeName) { - this.annotationClass = annotationClass; - this.loggerTypeName = loggerTypeName; - this.loggerFactoryName = loggerFactoryName; - this.passTypeName = passTypeName; - } - - LoggingFramework(Class annotationClass, String loggerTypeName, String loggerFactoryName) { - this.annotationClass = annotationClass; - this.loggerTypeName = loggerTypeName; - this.loggerFactoryName = loggerFactoryName; - this.passTypeName = true; - } - - final Class getAnnotationClass() { - return annotationClass; - } - - final String getLoggerTypeName() { - return loggerTypeName; - } - - final String getLoggerFactoryMethodName() { - return loggerFactoryName; - } - - JCExpression createFactoryParameter(JavacNode typeNode, JCFieldAccess loggingType) { - return loggingType; + LoggingFramework framework = new LoggingFramework(lombok.CustomLog.class, logDeclaration); + processAnnotation(framework, annotation, annotationNode, annotation.getInstance().topic()); } } } diff --git a/test/transform/resource/after-delombok/LoggerCustom.java b/test/transform/resource/after-delombok/LoggerCustom.java new file mode 100644 index 00000000..4a33e8a9 --- /dev/null +++ b/test/transform/resource/after-delombok/LoggerCustom.java @@ -0,0 +1,13 @@ +class LoggerCustomLog { + @java.lang.SuppressWarnings("all") + private static final MyLogger log = MyLoggerFactory.create(LoggerCustomLog.class); +} + +class MyLoggerFactory { + static MyLogger create(Class clazz) { + return null; + } +} + +class MyLogger { +} \ No newline at end of file diff --git a/test/transform/resource/after-delombok/LoggerCustomWithPackage.java b/test/transform/resource/after-delombok/LoggerCustomWithPackage.java new file mode 100644 index 00000000..45e741c2 --- /dev/null +++ b/test/transform/resource/after-delombok/LoggerCustomWithPackage.java @@ -0,0 +1,14 @@ +package before; +class LoggerCustomLog { + @java.lang.SuppressWarnings("all") + private static final before.MyLogger log = before.MyLoggerFactory.create(LoggerCustomLog.class); +} + +class MyLoggerFactory { + static MyLogger create(Class clazz) { + return null; + } +} + +class MyLogger { +} \ No newline at end of file diff --git a/test/transform/resource/after-delombok/LoggerCustomWithTopicAndName.java b/test/transform/resource/after-delombok/LoggerCustomWithTopicAndName.java new file mode 100644 index 00000000..abb785b9 --- /dev/null +++ b/test/transform/resource/after-delombok/LoggerCustomWithTopicAndName.java @@ -0,0 +1,10 @@ +class LoggerCustomLog { + @java.lang.SuppressWarnings("all") + private static final MyLoggerFactory log = MyLoggerFactory.create(LoggerCustomLog.class.getName(), "t", null, LoggerCustomLog.class, "t"); +} + +class MyLoggerFactory { + static MyLoggerFactory create(String name, String t1, Object o, Class clazz, String t2) { + return null; + } +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/LoggerCustom.java b/test/transform/resource/after-ecj/LoggerCustom.java new file mode 100644 index 00000000..2f60b7b6 --- /dev/null +++ b/test/transform/resource/after-ecj/LoggerCustom.java @@ -0,0 +1,21 @@ +@lombok.CustomLog class LoggerCustomLog { + private static final MyLogger log = MyLoggerFactory.create(LoggerCustomLog.class); + () { + } + LoggerCustomLog() { + super(); + } +} +class MyLoggerFactory { + MyLoggerFactory() { + super(); + } + static MyLogger create(Class clazz) { + return null; + } +} +class MyLogger { + MyLogger() { + super(); + } +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/LoggerCustomWithPackage.java b/test/transform/resource/after-ecj/LoggerCustomWithPackage.java new file mode 100644 index 00000000..d1bc1704 --- /dev/null +++ b/test/transform/resource/after-ecj/LoggerCustomWithPackage.java @@ -0,0 +1,22 @@ +package before; +@lombok.CustomLog class LoggerCustomLog { + private static final before.MyLogger log = before.MyLoggerFactory.create(LoggerCustomLog.class); + () { + } + LoggerCustomLog() { + super(); + } +} +class MyLoggerFactory { + MyLoggerFactory() { + super(); + } + static MyLogger create(Class clazz) { + return null; + } +} +class MyLogger { + MyLogger() { + super(); + } +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/LoggerCustomWithTopicAndName.java b/test/transform/resource/after-ecj/LoggerCustomWithTopicAndName.java new file mode 100644 index 00000000..91cf6d8d --- /dev/null +++ b/test/transform/resource/after-ecj/LoggerCustomWithTopicAndName.java @@ -0,0 +1,16 @@ +@lombok.CustomLog(topic = "t") class LoggerCustomLog { + private static final MyLoggerFactory log = MyLoggerFactory.create(LoggerCustomLog.class.getName(), "t", null, LoggerCustomLog.class, "t"); + () { + } + LoggerCustomLog() { + super(); + } +} +class MyLoggerFactory { + MyLoggerFactory() { + super(); + } + static MyLoggerFactory create(String name, String t1, Object o, Class clazz, String t2) { + return null; + } +} diff --git a/test/transform/resource/before/LoggerCustom.java b/test/transform/resource/before/LoggerCustom.java new file mode 100644 index 00000000..c0435a29 --- /dev/null +++ b/test/transform/resource/before/LoggerCustom.java @@ -0,0 +1,13 @@ +//CONF: lombok.log.custom.declaration = MyLogger MyLoggerFactory.create(TYPE) +@lombok.CustomLog +class LoggerCustomLog { +} + +class MyLoggerFactory { + static MyLogger create(Class clazz) { + return null; + } +} + +class MyLogger { +} \ No newline at end of file diff --git a/test/transform/resource/before/LoggerCustomWithPackage.java b/test/transform/resource/before/LoggerCustomWithPackage.java new file mode 100644 index 00000000..77c4498d --- /dev/null +++ b/test/transform/resource/before/LoggerCustomWithPackage.java @@ -0,0 +1,14 @@ +//CONF: lombok.log.custom.declaration = before.MyLogger before.MyLoggerFactory.create(TYPE) +package before; +@lombok.CustomLog +class LoggerCustomLog { +} + +class MyLoggerFactory { + static MyLogger create(Class clazz) { + return null; + } +} + +class MyLogger { +} \ No newline at end of file diff --git a/test/transform/resource/before/LoggerCustomWithTopicAndName.java b/test/transform/resource/before/LoggerCustomWithTopicAndName.java new file mode 100644 index 00000000..b40ed86a --- /dev/null +++ b/test/transform/resource/before/LoggerCustomWithTopicAndName.java @@ -0,0 +1,10 @@ +//CONF: lombok.log.custom.declaration = MyLoggerFactory.create(NAME,TOPIC,NULL,TYPE,TOPIC) +@lombok.CustomLog(topic="t") +class LoggerCustomLog { +} + +class MyLoggerFactory { + static MyLoggerFactory create(String name, String t1, Object o, Class clazz, String t2) { + return null; + } +} diff --git a/test/transform/resource/messages-delombok/LoggerSlf4jTypes.java.messages b/test/transform/resource/messages-delombok/LoggerSlf4jTypes.java.messages index 08f6bb7b..9910685c 100644 --- a/test/transform/resource/messages-delombok/LoggerSlf4jTypes.java.messages +++ b/test/transform/resource/messages-delombok/LoggerSlf4jTypes.java.messages @@ -1,2 +1,2 @@ -1 @Log is legal only on classes and enums. -4 @Log is legal only on classes and enums. \ No newline at end of file +1 @Slf4j is legal only on classes and enums. +4 @Slf4j is legal only on classes and enums. \ No newline at end of file -- cgit From 7fe489686716e72983d2c3586c7e2e1e07200ba7 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 21 May 2019 22:57:45 +0200 Subject: Code review for `@CustomLog`, and added documentation and a changelog entry. --- doc/changelog.markdown | 2 +- src/core/lombok/ConfigurationKeys.java | 25 ++++++------ src/core/lombok/CustomLog.java | 11 ++++-- .../core/configuration/ConfigurationDataType.java | 10 ++--- .../core/configuration/ConfigurationValueType.java | 3 +- .../lombok/core/configuration/IdentifierName.java | 11 ++---- .../lombok/core/configuration/LogDeclaration.java | 28 +++++--------- src/core/lombok/core/configuration/TypeName.java | 11 ++---- .../lombok/core/handlers/LoggingFramework.java | 23 ++++++++++- src/core/lombok/eclipse/handlers/HandleLog.java | 28 +++++++------- .../eclipse/handlers/HandleSynchronized.java | 2 +- src/core/lombok/javac/handlers/HandleLog.java | 18 ++++----- website/templates/features/log.html | 45 ++++++++++++++++------ 13 files changed, 127 insertions(+), 90 deletions(-) (limited to 'src/core/lombok/javac/handlers') diff --git a/doc/changelog.markdown b/doc/changelog.markdown index e0e3d49a..a1bbd8ab 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -2,7 +2,7 @@ Lombok Changelog ---------------- ### v1.18.9 "Edgy Guinea Pig" -* Nothing yet. +* FEATURE: You can now configure a custom logger framework using the new `@CustomLog` annotation in combination with the `lombok.log.custom.declaration` configuration key. See the [log documentation](https://projectlombok.org/features/Log) for more information. [Pullrequest #2086](https://github.com/rzwitserloot/lombok/pull/2086) with thanks to Adam Juraszek. ### v1.18.8 (May 7th, 2019) * FEATURE: You can now configure `@FieldNameConstants` to `CONSTANT_CASE` the generated constants, using a `lombok.config` option. See the [FieldNameConstants documentation](https://projectlombok.org/features/experimental/FieldNameConstants). [Issue #2092](https://github.com/rzwitserloot/lombok/issues/2092). diff --git a/src/core/lombok/ConfigurationKeys.java b/src/core/lombok/ConfigurationKeys.java index 951b2893..ecfddb2a 100644 --- a/src/core/lombok/ConfigurationKeys.java +++ b/src/core/lombok/ConfigurationKeys.java @@ -304,17 +304,16 @@ public class ConfigurationKeys { // ----- NonNull ----- /** - * lombok configuration: {@code lombok.nonNull.exceptionType} = <String: a java exception type; either [{@code IllegalArgumentException} or: {@code NullPointerException}]. + * lombok configuration: {@code lombok.nonNull.exceptionType} = one of: [{@code IllegalArgumentException}, {@code NullPointerException}, or {@code Assertion}]. * - * Sets the exception to throw if {@code @NonNull} is applied to a method parameter, and a caller passes in {@code null}. + * Sets the exception to throw if {@code @NonNull} is applied to a method parameter, and a caller passes in {@code null}. If the chosen configuration is {@code Assertion}, an assertion is generated instead, + * which would mean your code throws an {@code AssertionError} if assertions are enabled, and does nothing if assertions are not enabled. */ public static final ConfigurationKey NON_NULL_EXCEPTION_TYPE = new ConfigurationKey("lombok.nonNull.exceptionType", "The type of the exception to throw if a passed-in argument is null (Default: NullPointerException).") {}; /** * lombok configuration: {@code lombok.nonNull.flagUsage} = {@code WARNING} | {@code ERROR}. * - * Implementation note: This field is supposed to be lombok.NonNull itself, but jdk6 and 7 have bugs where fields in annotations don't work well. - * * If set, any usage of {@code @NonNull} results in a warning / error. */ public static final ConfigurationKey NON_NULL_FLAG_USAGE = new ConfigurationKey("lombok.nonNull.flagUsage", "Emit a warning or error if @NonNull is used.") {}; @@ -439,7 +438,7 @@ public class ConfigurationKeys { public static final ConfigurationKey LOG_CUSTOM_FLAG_USAGE = new ConfigurationKey("lombok.log.custom.flagUsage", "Emit a warning or error if @CustomLog is used.") {}; /** - * lombok configuration: {@code lombok.log.custom.declaration} = <String: logDeclaration>. + * lombok configuration: {@code lombok.log.custom.declaration} = <logDeclaration string>. * * The log declaration must follow the pattern: *
@@ -447,15 +446,19 @@ public class ConfigurationKeys { *
* It consists of: *

    - *
  • Optional fully qualified logger type, e.g. {@code my.cool.Logger}, followed by space. If not specified, it defaults to the logger factory type. + *
  • Optional fully qualified logger type, e.g. {@code my.cool.Logger}, followed by space. If not specified, it defaults to the LoggerFactoryType. *
  • Fully qualified logger factory type, e.g. {@code my.cool.LoggerFactory}, followed by dot. - *
  • Factory method, e.g. {@code createLogger}. The method must be defined on the logger factory type and must be static. - *
  • At least one definition of factory method parameters, e.g. {@code ()} or {@code (TOPIC,TYPE)}. The format is comma-separated list of parameters wrapped in parentheses. - * The allowed parameters are: {@code TYPE} | {@code NAME} | {@code TOPIC} | {@code NULL}. - * There can be at most one parameter definition with {@code TOPIC} and at most one without {@code TOPIC}. + *
  • Factory method, e.g. {@code createLogger}. This must be a {@code public static} method in the LoggerFactoryType. + *
  • At least one definition of factory method parameters, e.g. {@code ()} or {@code (TOPIC,TYPE)}. The format inside the parentheses is a comma-separated list of parameter kinds.
    + * The allowed parameters are: {@code TYPE} | {@code NAME} | {@code TOPIC} | {@code NULL}.
    + * There can be at most one parameter definition with {@code TOPIC} and at most one without {@code TOPIC}. You can specify both. *
* - * If not set, any usage of {@code @CustomLog} will result in an error. + * An example: {@code my.cool.Logger my.cool.LoggerFactory.createLogger(TYPE)(TYPE,TOPIC)}
+ * If no topic is provided in the usage of {@code @CustomLog}, the above will invoke {@code LoggerFactory}'s {@code createLogger} method, passing in the type as a {@code java.lang.Class} variable.
+ * If a topic is provided, the overload of that method is invoked with 2 parameters: First the type (as {@code Class}), then the topic (as {@code String}). + *

+ * If this configuration key is not set, any usage of {@code @CustomLog} will result in an error. */ public static final ConfigurationKey LOG_CUSTOM_DECLARATION = new ConfigurationKey("lombok.log.custom.declaration", "Define the generated custom logger field.") {}; diff --git a/src/core/lombok/CustomLog.java b/src/core/lombok/CustomLog.java index 48c9f2ab..d1f45f7c 100644 --- a/src/core/lombok/CustomLog.java +++ b/src/core/lombok/CustomLog.java @@ -27,7 +27,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Causes lombok to generate a logger field. + * Causes lombok to generate a logger field based on a custom logger implementation. *

* Complete documentation is found at the project lombok features page for lombok log annotations. *

@@ -50,7 +50,7 @@ import java.lang.annotation.Target; * } * *

- * Configuration must be provided in lombok.config, otherwise any usage will lead to errors. + * Configuration must be provided in lombok.config, otherwise any usage of this annotation will result in a compile-time error. * * This annotation is valid for classes and enumerations.
* @see lombok.extern.java.Log @Log @@ -65,6 +65,11 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.SOURCE) @Target(ElementType.TYPE) public @interface CustomLog { - /** @return The category of the constructed Logger. By default, it will use the type where the annotation is placed. */ + /** + * + * Sets a custom topic/category. Note that this requires you to specify a parameter configuration for your custom logger that includes {@code TOPIC}. + * + * @return The topic/category of the constructed Logger. By default (or for the empty string as topic), the parameter configuration without {@code TOPIC} is invoked. + */ String topic() default ""; } diff --git a/src/core/lombok/core/configuration/ConfigurationDataType.java b/src/core/lombok/core/configuration/ConfigurationDataType.java index 7dc77301..50b47a2e 100644 --- a/src/core/lombok/core/configuration/ConfigurationDataType.java +++ b/src/core/lombok/core/configuration/ConfigurationDataType.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013-2014 The Project Lombok Authors. + * Copyright (C) 2013-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -143,16 +143,16 @@ public final class ConfigurationDataType { @Override public Object parse(String value) { return invokeStaticMethod(valueOfMethod, value); } - + @Override public String description() { return invokeStaticMethod(descriptionMethod); } - + @Override public String exampleValue() { return invokeStaticMethod(exampleValueMethod); } - @SuppressWarnings("unchecked") + @SuppressWarnings("unchecked") private R invokeStaticMethod(Method method, Object... arguments) { try { return (R) method.invoke(null, arguments); @@ -237,7 +237,7 @@ public final class ConfigurationDataType { try { return argumentType.getMethod(name, parameterTypes); } catch (NoSuchMethodException e) { - throw new IllegalStateException("Method " + name + " with parameters " + Arrays.toString(parameterTypes) + "was not found.", e); + throw new IllegalStateException("Method " + name + " with parameters " + Arrays.toString(parameterTypes) + " was not found.", e); } catch (SecurityException e) { throw new IllegalStateException("Cannot inspect methods of type " + argumentType, e); } diff --git a/src/core/lombok/core/configuration/ConfigurationValueType.java b/src/core/lombok/core/configuration/ConfigurationValueType.java index f287f62a..a44a5a83 100644 --- a/src/core/lombok/core/configuration/ConfigurationValueType.java +++ b/src/core/lombok/core/configuration/ConfigurationValueType.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 The Project Lombok Authors. + * Copyright (C) 2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,6 +29,7 @@ package lombok.core.configuration; *

  • public static String description() *
  • public static String exampleValue() * + * None of them should throw checked exceptions. * Based on these methods, an instance of {@link ConfigurationValueParser} is created * and used by the configuration system. */ diff --git a/src/core/lombok/core/configuration/IdentifierName.java b/src/core/lombok/core/configuration/IdentifierName.java index 52e51bd7..db83c2cc 100644 --- a/src/core/lombok/core/configuration/IdentifierName.java +++ b/src/core/lombok/core/configuration/IdentifierName.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 The Project Lombok Authors. + * Copyright (C) 2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,13 +31,10 @@ public final class IdentifierName implements ConfigurationValueType { } public static IdentifierName valueOf(String name) { - if (name == null || name.trim().isEmpty()) { - return null; - } + if (name == null || name.trim().isEmpty()) return null; + String trimmedName = name.trim(); - if (!JavaIdentifiers.isValidJavaIdentifier(trimmedName)) { - throw new IllegalArgumentException("Invalid identifier " + trimmedName); - } + if (!JavaIdentifiers.isValidJavaIdentifier(trimmedName)) throw new IllegalArgumentException("Invalid identifier " + trimmedName); return new IdentifierName(trimmedName); } diff --git a/src/core/lombok/core/configuration/LogDeclaration.java b/src/core/lombok/core/configuration/LogDeclaration.java index 36f280e2..ad4102d4 100644 --- a/src/core/lombok/core/configuration/LogDeclaration.java +++ b/src/core/lombok/core/configuration/LogDeclaration.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 The Project Lombok Authors. + * Copyright (C) 2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -49,14 +49,11 @@ public final class LogDeclaration implements ConfigurationValueType { } public static LogDeclaration valueOf(String declaration) { - if (declaration == null) { - return null; - } + if (declaration == null) return null; Matcher matcher = DECLARATION_PATTERN.matcher(declaration); - if (!matcher.matches()) { - throw new IllegalArgumentException("The declaration must follow the pattern: [LoggerType ]LoggerFactoryType.loggerFactoryMethod(loggerFactoryMethodParams)[(loggerFactoryMethodParams)]"); - } + if (!matcher.matches()) throw new IllegalArgumentException("The declaration must follow the pattern: [LoggerType ]LoggerFactoryType.loggerFactoryMethod(loggerFactoryMethodParams)[(loggerFactoryMethodParams)]"); + TypeName loggerFactoryType = TypeName.valueOf(matcher.group(2)); TypeName loggerType = TypeName.valueOf(matcher.group(1)); if (loggerType == null) loggerType = loggerFactoryType; @@ -67,21 +64,16 @@ public final class LogDeclaration implements ConfigurationValueType { List parametersWithTopic = null; for (List parameters: allParameters) { if (parameters.contains(LogFactoryParameter.TOPIC)) { - if (parametersWithTopic != null) { - throw new IllegalArgumentException("There are too many parameters with TOPIC: " + parametersWithTopic + " and " + parameters); - } + if (parametersWithTopic != null) throw new IllegalArgumentException("There is more than one parameter definition that includes TOPIC: " + parametersWithTopic + " and " + parameters); parametersWithTopic = parameters; } else { - if (parametersWithoutTopic != null) { - throw new IllegalArgumentException("There are too many parameters without TOPIC: " + parametersWithoutTopic + " and " + parameters); - } + if (parametersWithoutTopic != null) throw new IllegalArgumentException("There is more than one parmaeter definition that does not include TOPIC: " + parametersWithoutTopic + " and " + parameters); parametersWithoutTopic = parameters; } } - if (parametersWithoutTopic == null && parametersWithTopic == null) { - // shouldn't happen the pattern does not allow it - throw new IllegalArgumentException("No logger factory method parameters specified."); - } + + // sanity check (the pattern should disallow this situation + if (parametersWithoutTopic == null && parametersWithTopic == null) throw new IllegalArgumentException("No logger factory method parameters specified."); return new LogDeclaration(loggerType, loggerFactoryType, loggerFactoryMethod, parametersWithoutTopic, parametersWithTopic); } @@ -103,7 +95,7 @@ public final class LogDeclaration implements ConfigurationValueType { } public static String description() { - return "custom-log-declartation"; + return "custom-log-declaration"; } public static String exampleValue() { diff --git a/src/core/lombok/core/configuration/TypeName.java b/src/core/lombok/core/configuration/TypeName.java index a3128527..4ac072dc 100644 --- a/src/core/lombok/core/configuration/TypeName.java +++ b/src/core/lombok/core/configuration/TypeName.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 The Project Lombok Authors. + * Copyright (C) 2013-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -31,14 +31,11 @@ public final class TypeName implements ConfigurationValueType { } public static TypeName valueOf(String name) { - if (name == null || name.trim().isEmpty()) { - return null; - } + if (name == null || name.trim().isEmpty()) return null; + String trimmedName = name.trim(); for (String identifier : trimmedName.split(".")) { - if (!JavaIdentifiers.isValidJavaIdentifier(identifier)) { - throw new IllegalArgumentException("Invalid type name " + trimmedName + " (part " + identifier + ")"); - } + if (!JavaIdentifiers.isValidJavaIdentifier(identifier)) throw new IllegalArgumentException("Invalid type name " + trimmedName + " (part " + identifier + ")"); } return new TypeName(trimmedName); } diff --git a/src/core/lombok/core/handlers/LoggingFramework.java b/src/core/lombok/core/handlers/LoggingFramework.java index 38eadd17..88c0317b 100644 --- a/src/core/lombok/core/handlers/LoggingFramework.java +++ b/src/core/lombok/core/handlers/LoggingFramework.java @@ -1,3 +1,24 @@ +/* + * Copyright (C) 2019 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.core.handlers; import java.lang.annotation.Annotation; @@ -56,7 +77,7 @@ public class LoggingFramework { private final Class annotationClass; private final String annotationAsString; private final LogDeclaration declaration; - + public LoggingFramework(Class annotationClass, LogDeclaration declaration) { this.annotationClass = annotationClass; this.annotationAsString = "@" + annotationClass.getSimpleName(); diff --git a/src/core/lombok/eclipse/handlers/HandleLog.java b/src/core/lombok/eclipse/handlers/HandleLog.java index e445685d..3dc9536d 100644 --- a/src/core/lombok/eclipse/handlers/HandleLog.java +++ b/src/core/lombok/eclipse/handlers/HandleLog.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2014 The Project Lombok Authors. + * Copyright (C) 2010-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -51,7 +51,6 @@ import lombok.core.configuration.LogDeclaration.LogFactoryParameter; import lombok.core.handlers.LoggingFramework; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; -import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult; public class HandleLog { private static final IdentifierName LOG = IdentifierName.valueOf("log"); @@ -74,8 +73,7 @@ public class HandleLog { if (owner.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) owner.get(); int modifiers = typeDecl == null ? 0 : typeDecl.modifiers; - boolean notAClass = (modifiers & - (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0; + boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0; if (typeDecl == null || notAClass) { annotationNode.addError(framework.getAnnotationAsString() + " is legal only on classes and enums."); @@ -89,13 +87,13 @@ public class HandleLog { if (loggerTopic != null && loggerTopic.trim().isEmpty()) loggerTopic = null; if (framework.getDeclaration().getParametersWithTopic() == null && loggerTopic != null) { - annotationNode.addError(framework.getAnnotationAsString() + " does not allow to set a topic."); + annotationNode.addError(framework.getAnnotationAsString() + " does not allow a topic."); } ClassLiteralAccess loggingType = selfType(owner, source); FieldDeclaration fieldDeclaration = createField(framework, source, loggingType, logFieldName.getName(), useStatic, loggerTopic); fieldDeclaration.traverse(new SetGeneratedByVisitor(source), typeDecl.staticInitializerScope); - // TODO temporary workaround for issue 217. http://code.google.com/p/projectlombok/issues/detail?id=217 + // TODO temporary workaround for issue 290. https://github.com/rzwitserloot/lombok/issues/290 // injectFieldSuppressWarnings(owner, fieldDeclaration); injectField(owner, fieldDeclaration); owner.rebuild(); @@ -107,12 +105,12 @@ public class HandleLog { public static ClassLiteralAccess selfType(EclipseNode type, Annotation source) { int pS = source.sourceStart, pE = source.sourceEnd; - long p = (long)pS << 32 | pE; - + long p = (long) pS << 32 | pE; + TypeDeclaration typeDeclaration = (TypeDeclaration)type.get(); TypeReference typeReference = new SingleTypeReference(typeDeclaration.name, p); setGeneratedBy(typeReference, source); - + ClassLiteralAccess result = new ClassLiteralAccess(source.sourceEnd, typeReference); setGeneratedBy(result, source); @@ -123,7 +121,7 @@ public class HandleLog { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; - // private static final log = (); + // private static final log = (); FieldDeclaration fieldDecl = new FieldDeclaration(logFieldName.toCharArray(), 0, -1); setGeneratedBy(fieldDecl, source); fieldDecl.declarationSourceEnd = -1; @@ -134,7 +132,7 @@ public class HandleLog { MessageSend factoryMethodCall = new MessageSend(); setGeneratedBy(factoryMethodCall, source); - + factoryMethodCall.receiver = createNameReference(logDeclaration.getLoggerFactoryType().getName(), source); factoryMethodCall.selector = logDeclaration.getLoggerFactoryMethod().getCharArray(); @@ -151,7 +149,7 @@ public class HandleLog { public static TypeReference createTypeReference(String typeName, Annotation source) { int pS = source.sourceStart, pE = source.sourceEnd; - long p = (long)pS << 32 | pE; + long p = (long) pS << 32 | pE; char[][] typeNameTokens = fromQualifiedName(typeName); long[] pos = new long[typeNameTokens.length]; @@ -161,7 +159,7 @@ public class HandleLog { setGeneratedBy(typeReference, source); return typeReference; } - + private static final Expression[] createFactoryParameters(ClassLiteralAccess loggingType, Annotation source, List parameters, String loggerTopic) { Expression[] expressions = new Expression[parameters.size()]; int pS = source.sourceStart, pE = source.sourceEnd; @@ -174,7 +172,7 @@ public class HandleLog { expressions[i] = createFactoryTypeParameter(loggingType, source); break; case NAME: - long p = (long)pS << 32 | pE; + long p = (long) pS << 32 | pE; MessageSend factoryParameterCall = new MessageSend(); setGeneratedBy(factoryParameterCall, source); @@ -306,7 +304,7 @@ public class HandleLog { handleFlagUsage(annotationNode, ConfigurationKeys.LOG_CUSTOM_FLAG_USAGE, "@CustomLog", ConfigurationKeys.LOG_ANY_FLAG_USAGE, "any @Log"); LogDeclaration logDeclaration = annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_CUSTOM_DECLARATION); if (logDeclaration == null) { - annotationNode.addError("The @CustomLog is not configured; please set log.custom.declaration in lombok.config."); + annotationNode.addError("The @CustomLog annotation is not configured; please set log.custom.declaration in lombok.config."); return; } LoggingFramework framework = new LoggingFramework(lombok.CustomLog.class, logDeclaration); diff --git a/src/core/lombok/eclipse/handlers/HandleSynchronized.java b/src/core/lombok/eclipse/handlers/HandleSynchronized.java index a45a499c..22f7f967 100644 --- a/src/core/lombok/eclipse/handlers/HandleSynchronized.java +++ b/src/core/lombok/eclipse/handlers/HandleSynchronized.java @@ -98,7 +98,7 @@ public class HandleSynchronized extends EclipseAnnotationHandler { fieldDecl.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 }); setGeneratedBy(fieldDecl.type, source); fieldDecl.initialization = arrayAlloc; - // TODO temporary workaround for issue 217. http://code.google.com/p/projectlombok/issues/detail?id=217 + // TODO temporary workaround for issue 290. https://github.com/rzwitserloot/lombok/issues/290 // injectFieldSuppressWarnings(annotationNode.up().up(), fieldDecl); injectField(annotationNode.up().up(), fieldDecl); } diff --git a/src/core/lombok/javac/handlers/HandleLog.java b/src/core/lombok/javac/handlers/HandleLog.java index a00cc496..5f0f8c90 100644 --- a/src/core/lombok/javac/handlers/HandleLog.java +++ b/src/core/lombok/javac/handlers/HandleLog.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2014 The Project Lombok Authors. + * Copyright (C) 2010-2019 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -51,14 +51,14 @@ import com.sun.tools.javac.util.Name; public class HandleLog { private static final IdentifierName LOG = IdentifierName.valueOf("log"); - + private HandleLog() { throw new UnsupportedOperationException(); } - + public static void processAnnotation(LoggingFramework framework, AnnotationValues annotation, JavacNode annotationNode, String loggerTopic) { deleteAnnotationIfNeccessary(annotationNode, framework.getAnnotationClass()); - + JavacNode typeNode = annotationNode.up(); switch (typeNode.getKind()) { case TYPE: @@ -67,7 +67,7 @@ public class HandleLog { boolean useStatic = !Boolean.FALSE.equals(annotationNode.getAst().readConfiguration(ConfigurationKeys.LOG_ANY_FIELD_IS_STATIC)); - if ((((JCClassDecl)typeNode.get()).mods.flags & Flags.INTERFACE) != 0) { + if ((((JCClassDecl) typeNode.get()).mods.flags & Flags.INTERFACE) != 0) { annotationNode.addError(framework.getAnnotationAsString() + " is legal only on classes and enums."); return; } @@ -78,9 +78,9 @@ public class HandleLog { if (loggerTopic != null && loggerTopic.trim().isEmpty()) loggerTopic = null; if (framework.getDeclaration().getParametersWithTopic() == null && loggerTopic != null) { - annotationNode.addError(framework.getAnnotationAsString() + " does not allow to set a topic."); + annotationNode.addError(framework.getAnnotationAsString() + " does not allow a topic."); } - + JCFieldAccess loggingType = selfType(typeNode); createField(framework, typeNode, loggingType, annotationNode.get(), logFieldName.getName(), useStatic, loggerTopic); break; @@ -109,8 +109,8 @@ public class HandleLog { JCMethodInvocation factoryMethodCall = maker.Apply(List.nil(), factoryMethod, List.from(factoryParameters)); JCVariableDecl fieldDecl = recursiveSetGeneratedBy(maker.VarDef( - maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (useStatic ? Flags.STATIC : 0)), - typeNode.toName(logFieldName), loggerType, factoryMethodCall), source, typeNode.getContext()); + maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (useStatic ? Flags.STATIC : 0)), + typeNode.toName(logFieldName), loggerType, factoryMethodCall), source, typeNode.getContext()); injectFieldAndMarkGenerated(typeNode, fieldDecl); return true; diff --git a/website/templates/features/log.html b/website/templates/features/log.html index 1de26836..2547af14 100644 --- a/website/templates/features/log.html +++ b/website/templates/features/log.html @@ -7,52 +7,67 @@ NEW in lombok 0.10: You can annotate any class with a log annotation to let lombok generate a logger field.
    The logger is named log and the field's type depends on which logger you have selected.

    - NEW in lombok v1.16.24: Addition of google's FluentLogger (flogger). + NEW in lombok v1.16.24: Addition of google's FluentLogger (via @Flogger). +

    + NEW in lombok v1.18.10: Addition of @CustomLog which lets you add any logger by configuring how to create them with a config key.

    <@f.overview>

    - You put the variant of @Log on your class (whichever one applies to the logging system you use); you then have a static final log field, initialized to the name of your class, which you can then use to write log statements. + You put the variant of @Log on your class (whichever one applies to the logging system you use); you then have a static final log field, initialized as is the commonly proscribed way for the logging framework you use, which you can then use to write log statements.

    There are several choices available:

    -
    +
    @CommonsLog
    Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class); -
    +
    @Flogger
    Creates private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass(); -
    +
    @JBossLog
    Creates private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LogExample.class); -
    +
    @Log
    Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName()); -
    +
    @Log4j
    Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class); -
    +
    @Log4j2
    Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class); -
    +
    @Slf4j
    Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class); -
    +
    @XSlf4j
    Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class); +
    + @CustomLog +
    + Creates private static final com.foo.your.Logger log = com.foo.your.LoggerFactory.createYourLogger(LogExample.class); +

    + This option requires that you add a configuration to your lombok.config file to specify what @CustomLog should do. +

    + For example:lombok.log.custom.declaration = com.foo.your.Logger com.foo.your.LoggerFactory.createYourLog(TYPE)(TOPIC) which would produce the above statement. First comes a type which is the type of your logger, then a space, then the type of your logger factory, then a dot, then the name of the logger factory method, and then 1 or 2 parameter definitions; at most one definition with TOPIC and at most one without TOPIC. Each parameter definition is specified as a parenthesised comma-separated list of parameter kinds. The options are: TYPE (passes this @Log decorated type, as a class), NAME (passes this @Log decorated type's fully qualified name), TOPIC (passes the explicitly chosen topic string set on the @CustomLog annotation), and NULL (passes null). +

    + The logger type is optional; if it is omitted, the logger factory type is used. (So, if your logger class has a static method that creates loggers, you can shorten your logger definition). +

    + Please contact us if there is a public, open source, somewhat commonly used logging framework that we don't yet have an explicit annotation for. The primary purpose of @CustomLog is to support your in-house, private logging frameworks. +

    - By default, the topic (or name) of the logger will be the class name of the class annotated with the @Log annotation. This can be customised by specifying the topic parameter. For example: @XSlf4j(topic="reporting"). + By default, the topic (or name) of the logger will be the (name of) the class annotated with the @Log annotation. This can be customised by specifying the topic parameter. For example: @XSlf4j(topic="reporting").

    @@ -67,10 +82,18 @@ lombok.log.fieldIsStatic = [true | false] (default: true)
    Normally the generated logger is a static field. By setting this key to false, the generated field will be an instance field instead. +
    + lombok.log.custom.declaration = LoggerType LoggerFactoryType.loggerFactoryMethod(loggerFactoryMethodParams)(loggerFactoryMethodParams) +
    + Configures what to generate when @CustomLog is used. (The italicized parts are optional). loggerFactoryMethodParams is a comma-separated list of zero to any number of parameter kinds to pass. Valid kinds: TYPE, NAME, TOPIC, and NULL. You can include a parameter definition for the case where no explicit topic is set (do not include the TOPIC in the parameter list), and for when an explicit topic is set (do include the TOPIC parameter in the list).
    lombok.log.flagUsage = [warning | error] (default: not set)
    Lombok will flag any usage of any of the various log annotations as a warning or error if configured. +
    + lombok.log.custom.flagUsage = [warning | error] (default: not set) +
    + Lombok will flag any usage of @lombok.CustomLog as a warning or error if configured.
    lombok.log.apacheCommons.flagUsage = [warning | error] (default: not set)
    -- cgit From 06004b933b6afaa58ca753c4c33e8a977f91e294 Mon Sep 17 00:00:00 2001 From: Adam Juraszek Date: Wed, 22 May 2019 18:36:42 +0200 Subject: Fixes for CustomLog, AUTHORS entry, extended changlog --- AUTHORS | 1 + doc/changelog.markdown | 1 + src/core/lombok/core/configuration/TypeName.java | 2 +- src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 2 +- src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java | 2 +- src/core/lombok/javac/handlers/JavacHandlerUtil.java | 4 ++-- website/templates/features/log.html | 2 +- 7 files changed, 8 insertions(+), 6 deletions(-) (limited to 'src/core/lombok/javac/handlers') diff --git a/AUTHORS b/AUTHORS index e77bc462..f5d4f434 100755 --- a/AUTHORS +++ b/AUTHORS @@ -1,5 +1,6 @@ Lombok contributors in alphabetical order: +Adam Juraszek Bulgakov Alexander Christian Nüssgens Christian Sterzl diff --git a/doc/changelog.markdown b/doc/changelog.markdown index a1bbd8ab..9163b305 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -3,6 +3,7 @@ Lombok Changelog ### v1.18.9 "Edgy Guinea Pig" * FEATURE: You can now configure a custom logger framework using the new `@CustomLog` annotation in combination with the `lombok.log.custom.declaration` configuration key. See the [log documentation](https://projectlombok.org/features/Log) for more information. [Pullrequest #2086](https://github.com/rzwitserloot/lombok/pull/2086) with thanks to Adam Juraszek. +* IMPROBABLE BREAKING CHANGE: Stricter validation of configuration keys dealing with identifiers and types (`lombok.log.fieldName`, `lombok.fieldNameConstants.innerTypeName`, `lombok.copyableAnnotations`). ### v1.18.8 (May 7th, 2019) * FEATURE: You can now configure `@FieldNameConstants` to `CONSTANT_CASE` the generated constants, using a `lombok.config` option. See the [FieldNameConstants documentation](https://projectlombok.org/features/experimental/FieldNameConstants). [Issue #2092](https://github.com/rzwitserloot/lombok/issues/2092). diff --git a/src/core/lombok/core/configuration/TypeName.java b/src/core/lombok/core/configuration/TypeName.java index 4ac072dc..a1eac7bd 100644 --- a/src/core/lombok/core/configuration/TypeName.java +++ b/src/core/lombok/core/configuration/TypeName.java @@ -34,7 +34,7 @@ public final class TypeName implements ConfigurationValueType { if (name == null || name.trim().isEmpty()) return null; String trimmedName = name.trim(); - for (String identifier : trimmedName.split(".")) { + for (String identifier : trimmedName.split("\\.")) { if (!JavaIdentifiers.isValidJavaIdentifier(identifier)) throw new IllegalArgumentException("Invalid type name " + trimmedName + " (part " + identifier + ")"); } return new TypeName(trimmedName); diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index c8e7b60a..f04c541f 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -760,7 +760,7 @@ public class EclipseHandlerUtil { TypeReference typeRef = annotation.type; boolean match = false; if (typeRef != null && typeRef.getTypeName() != null) { - for (TypeName cn : configuredCopyable) if (typeMatches(cn.toString(), node, typeRef)) { + for (TypeName cn : configuredCopyable) if (cn != null && typeMatches(cn.toString(), node, typeRef)) { result.add(annotation); match = true; break; diff --git a/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java b/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java index 710a26f8..2db7591c 100644 --- a/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java +++ b/src/core/lombok/eclipse/handlers/HandleFieldNameConstants.java @@ -135,7 +135,7 @@ public class HandleFieldNameConstants extends EclipseAnnotationHandler configuredCopyable = node.getAst().readConfiguration(ConfigurationKeys.COPYABLE_ANNOTATIONS); if (!annoName.isEmpty()) { - for (TypeName cn : configuredCopyable) if (typeMatches(cn.toString(), node, anno.annotationType)) return List.of(anno); + for (TypeName cn : configuredCopyable) if (cn != null && typeMatches(cn.toString(), node, anno.annotationType)) return List.of(anno); for (String bn : BASE_COPYABLE_ANNOTATIONS) if (typeMatches(bn, node, anno.annotationType)) return List.of(anno); } @@ -1459,7 +1459,7 @@ public class JavacHandlerUtil { if (child.getKind() == Kind.ANNOTATION) { JCAnnotation annotation = (JCAnnotation) child.get(); boolean match = false; - for (TypeName cn : configuredCopyable) if (typeMatches(cn.toString(), node, annotation.annotationType)) { + for (TypeName cn : configuredCopyable) if (cn != null && typeMatches(cn.toString(), node, annotation.annotationType)) { result.append(annotation); match = true; break; diff --git a/website/templates/features/log.html b/website/templates/features/log.html index 2547af14..9436d338 100644 --- a/website/templates/features/log.html +++ b/website/templates/features/log.html @@ -15,7 +15,7 @@ <@f.overview>

    - You put the variant of @Log on your class (whichever one applies to the logging system you use); you then have a static final log field, initialized as is the commonly proscribed way for the logging framework you use, which you can then use to write log statements. + You put the variant of @Log on your class (whichever one applies to the logging system you use); you then have a static final log field, initialized as is the commonly prescribed way for the logging framework you use, which you can then use to write log statements.

    There are several choices available:

    -- cgit From 2a98c4d1fd80ff0b3181da4fe8d5f3f32f168644 Mon Sep 17 00:00:00 2001 From: Adam Juraszek Date: Wed, 29 May 2019 19:16:19 +0200 Subject: Fix custom logger when topic presence != allowance --- src/core/lombok/eclipse/handlers/HandleLog.java | 5 +++++ src/core/lombok/javac/handlers/HandleLog.java | 5 +++++ 2 files changed, 10 insertions(+) (limited to 'src/core/lombok/javac/handlers') diff --git a/src/core/lombok/eclipse/handlers/HandleLog.java b/src/core/lombok/eclipse/handlers/HandleLog.java index 3dc9536d..7a140193 100644 --- a/src/core/lombok/eclipse/handlers/HandleLog.java +++ b/src/core/lombok/eclipse/handlers/HandleLog.java @@ -88,6 +88,11 @@ public class HandleLog { if (loggerTopic != null && loggerTopic.trim().isEmpty()) loggerTopic = null; if (framework.getDeclaration().getParametersWithTopic() == null && loggerTopic != null) { annotationNode.addError(framework.getAnnotationAsString() + " does not allow a topic."); + loggerTopic = null; + } + if (framework.getDeclaration().getParametersWithoutTopic() == null && loggerTopic == null) { + annotationNode.addError(framework.getAnnotationAsString() + " requires a topic."); + loggerTopic = ""; } ClassLiteralAccess loggingType = selfType(owner, source); diff --git a/src/core/lombok/javac/handlers/HandleLog.java b/src/core/lombok/javac/handlers/HandleLog.java index 5f0f8c90..522f8576 100644 --- a/src/core/lombok/javac/handlers/HandleLog.java +++ b/src/core/lombok/javac/handlers/HandleLog.java @@ -79,6 +79,11 @@ public class HandleLog { if (loggerTopic != null && loggerTopic.trim().isEmpty()) loggerTopic = null; if (framework.getDeclaration().getParametersWithTopic() == null && loggerTopic != null) { annotationNode.addError(framework.getAnnotationAsString() + " does not allow a topic."); + loggerTopic = null; + } + if (framework.getDeclaration().getParametersWithoutTopic() == null && loggerTopic == null) { + annotationNode.addError(framework.getAnnotationAsString() + " requires a topic."); + loggerTopic = ""; } JCFieldAccess loggingType = selfType(typeNode); -- cgit