mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-15 12:14:31 +00:00
[configuration] implementations for log's log.any.fieldIsStatic config.
This commit is contained in:
1 parent
e24ebbbf59
commit
51805b4e0c
3 files changed
+39
-23
No files matched your search
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013 The Project Lombok Authors.
|
||||
* Copyright (C) 2013-2014 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
|
||||
@@ -38,11 +38,27 @@ public class ConfigurationKeys {
|
||||
public static final ConfigurationKey<FlagUsageType> ANY_CONSTRUCTOR_FLAG_USAGE = new ConfigurationKey<FlagUsageType>("lombok.AnyConstructor.flagUsage") {};
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.log.any.flagUsage} = {@code WARNING} | {@code ERROR}.
|
||||
* lombok configuration: {@code lombok.log.flagUsage} = {@code WARNING} | {@code ERROR}.
|
||||
*
|
||||
* If set, <em>any</em> usage of any of the log annotations in {@code lombok.extern}{@code @Slf4j} results in a warning / error.
|
||||
*/
|
||||
public static final ConfigurationKey<FlagUsageType> ANY_LOG_FLAG_USAGE = new ConfigurationKey<FlagUsageType>("lombok.log.any.flagUsage") {};
|
||||
public static final ConfigurationKey<FlagUsageType> ANY_LOG_FLAG_USAGE = new ConfigurationKey<FlagUsageType>("lombok.log.flagUsage") {};
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.log.fieldName} = "aJavaIdentifier".
|
||||
*
|
||||
* If set the various log annotations (which make a log field) will use the stated identifier instead of {@code log} as a name.
|
||||
*/
|
||||
public static final ConfigurationKey<String> ANY_LOG_FIELD_NAME = new ConfigurationKey<String>("lombok.log.fieldName"){};
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.log.fieldIsStatic} = {@code true} | {@code false}.
|
||||
*
|
||||
* If not set, or set to {@code true}, the log field generated by the various log annotations will be {@code static}.
|
||||
*
|
||||
* If set to {@code false}, these will be generated as instance fields instead.
|
||||
*/
|
||||
public static final ConfigurationKey<Boolean> ANY_LOG_FIELD_IS_STATIC = new ConfigurationKey<Boolean>("lombok.log.fieldIsStatic"){};
|
||||
|
||||
/**
|
||||
* lombok configuration: {@code lombok.experimental.flagUsage} = {@code WARNING} | {@code ERROR}.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2013 The Project Lombok Authors.
|
||||
* Copyright (C) 2010-2014 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
|
||||
@@ -30,7 +30,6 @@ import java.util.Arrays;
|
||||
|
||||
import lombok.ConfigurationKeys;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.configuration.ConfigurationKey;
|
||||
import lombok.eclipse.EclipseAnnotationHandler;
|
||||
import lombok.eclipse.EclipseNode;
|
||||
import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult;
|
||||
@@ -48,9 +47,6 @@ import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
|
||||
import org.mangosdk.spi.ProviderFor;
|
||||
|
||||
public class HandleLog {
|
||||
private static final ConfigurationKey<String> LOG_VARIABLE_NAME = new ConfigurationKey<String>("lombok.log.varName"){};
|
||||
private static final ConfigurationKey<Boolean> STATIC = new ConfigurationKey<Boolean>("lombok.log.static"){};
|
||||
|
||||
private HandleLog() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@@ -60,11 +56,10 @@ public class HandleLog {
|
||||
|
||||
switch (owner.getKind()) {
|
||||
case TYPE:
|
||||
String logFieldName = annotationNode.getAst().readConfiguration(ConfigurationKeys.ANY_LOG_FIELD_NAME);
|
||||
if (logFieldName == null) logFieldName = "log";
|
||||
|
||||
String logFieldName = annotationNode.getAst().readConfiguration(LOG_VARIABLE_NAME);
|
||||
if (logFieldName == null) {
|
||||
logFieldName = "log";
|
||||
}
|
||||
boolean useStatic = !Boolean.FALSE.equals(annotationNode.getAst().readConfiguration(ConfigurationKeys.ANY_LOG_FIELD_IS_STATIC));
|
||||
|
||||
TypeDeclaration typeDecl = null;
|
||||
if (owner.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) owner.get();
|
||||
@@ -85,7 +80,7 @@ public class HandleLog {
|
||||
|
||||
ClassLiteralAccess loggingType = selfType(owner, source);
|
||||
|
||||
FieldDeclaration fieldDeclaration = createField(framework, source, loggingType, logFieldName);
|
||||
FieldDeclaration fieldDeclaration = createField(framework, source, loggingType, logFieldName, useStatic);
|
||||
fieldDeclaration.traverse(new SetGeneratedByVisitor(source), typeDecl.staticInitializerScope);
|
||||
// TODO temporary workaround for issue 217. http://code.google.com/p/projectlombok/issues/detail?id=217
|
||||
// injectFieldSuppressWarnings(owner, fieldDeclaration);
|
||||
@@ -111,7 +106,7 @@ public class HandleLog {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static FieldDeclaration createField(LoggingFramework framework, Annotation source, ClassLiteralAccess loggingType, String logFieldName) {
|
||||
private static FieldDeclaration createField(LoggingFramework framework, Annotation source, ClassLiteralAccess loggingType, String logFieldName, boolean useStatic) {
|
||||
int pS = source.sourceStart, pE = source.sourceEnd;
|
||||
long p = (long)pS << 32 | pE;
|
||||
|
||||
@@ -119,7 +114,7 @@ public class HandleLog {
|
||||
FieldDeclaration fieldDecl = new FieldDeclaration(logFieldName.toCharArray(), 0, -1);
|
||||
setGeneratedBy(fieldDecl, source);
|
||||
fieldDecl.declarationSourceEnd = -1;
|
||||
fieldDecl.modifiers = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
|
||||
fieldDecl.modifiers = Modifier.PRIVATE | (useStatic ? Modifier.STATIC : 0) | Modifier.FINAL;
|
||||
|
||||
fieldDecl.type = createTypeReference(framework.getLoggerTypeName(), source);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2010-2013 The Project Lombok Authors.
|
||||
* Copyright (C) 2010-2014 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
|
||||
@@ -56,18 +56,23 @@ public class HandleLog {
|
||||
JavacNode typeNode = annotationNode.up();
|
||||
switch (typeNode.getKind()) {
|
||||
case TYPE:
|
||||
if ((((JCClassDecl)typeNode.get()).mods.flags & Flags.INTERFACE)!= 0) {
|
||||
String logFieldName = annotationNode.getAst().readConfiguration(ConfigurationKeys.ANY_LOG_FIELD_NAME);
|
||||
if (logFieldName == null) logFieldName = "log";
|
||||
|
||||
boolean useStatic = !Boolean.FALSE.equals(annotationNode.getAst().readConfiguration(ConfigurationKeys.ANY_LOG_FIELD_IS_STATIC));
|
||||
|
||||
if ((((JCClassDecl)typeNode.get()).mods.flags & Flags.INTERFACE) != 0) {
|
||||
annotationNode.addError("@Log is legal only on classes and enums.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (fieldExists("log", typeNode)!= MemberExistsResult.NOT_EXISTS) {
|
||||
annotationNode.addWarning("Field 'log' already exists.");
|
||||
if (fieldExists(logFieldName, typeNode)!= MemberExistsResult.NOT_EXISTS) {
|
||||
annotationNode.addWarning("Field '" + logFieldName + "' already exists.");
|
||||
return;
|
||||
}
|
||||
|
||||
JCFieldAccess loggingType = selfType(typeNode);
|
||||
createField(framework, typeNode, loggingType, annotationNode.get());
|
||||
createField(framework, typeNode, loggingType, annotationNode.get(), logFieldName, useStatic);
|
||||
break;
|
||||
default:
|
||||
annotationNode.addError("@Log is legal only on types.");
|
||||
@@ -81,7 +86,7 @@ public class HandleLog {
|
||||
return maker.Select(maker.Ident(name), typeNode.toName("class"));
|
||||
}
|
||||
|
||||
private static boolean createField(LoggingFramework framework, JavacNode typeNode, JCFieldAccess loggingType, JCTree source) {
|
||||
private static boolean createField(LoggingFramework framework, JavacNode typeNode, JCFieldAccess loggingType, JCTree source, String logFieldName, boolean useStatic) {
|
||||
JavacTreeMaker maker = typeNode.getTreeMaker();
|
||||
|
||||
// private static final <loggerType> log = <factoryMethod>(<parameter>);
|
||||
@@ -92,8 +97,8 @@ public class HandleLog {
|
||||
JCMethodInvocation factoryMethodCall = maker.Apply(List.<JCExpression>nil(), factoryMethod, List.<JCExpression>of(loggerName));
|
||||
|
||||
JCVariableDecl fieldDecl = recursiveSetGeneratedBy(maker.VarDef(
|
||||
maker.Modifiers(Flags.PRIVATE | Flags.FINAL | Flags.STATIC),
|
||||
typeNode.toName("log"), loggerType, factoryMethodCall), source, typeNode.getContext());
|
||||
maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (useStatic ? Flags.STATIC : 0)),
|
||||
typeNode.toName(logFieldName), loggerType, factoryMethodCall), source, typeNode.getContext());
|
||||
|
||||
injectFieldSuppressWarnings(typeNode, fieldDecl);
|
||||
return true;
|
||||
|
||||
Reference in new issue
Block a user