mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 10:18:46 +00:00
[configuration] Initial API for declaring ans reading single settings.
This commit is contained in:
1 parent
930b17b85b
commit
404956374e
3 files changed
+76
-7
No files matched your search
@@ -36,6 +36,8 @@ import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import lombok.core.LombokConfiguration.ConfigurationKey;
|
||||
|
||||
/**
|
||||
* Lombok wraps the AST produced by a target platform into its own AST system, mostly because both Eclipse and javac
|
||||
* do not allow upward traversal (from a method to its owning type, for example).
|
||||
@@ -415,4 +417,8 @@ public abstract class AST<A extends AST<A, L, N>, L extends LombokNode<A, L, N>,
|
||||
buildWithCollection(nodeType, v, list, dim-1);
|
||||
}
|
||||
}
|
||||
|
||||
public final <T> T readConfiguration(ConfigurationKey<T> key) {
|
||||
return LombokConfiguration.read(key, this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class LombokConfiguration {
|
||||
|
||||
private LombokConfiguration() {
|
||||
// prevent instantiation
|
||||
}
|
||||
|
||||
/*
|
||||
* Typical usage: use this as a supertypetoken.
|
||||
*/
|
||||
public abstract static class ConfigurationKey<T> {
|
||||
private final String keyName;
|
||||
|
||||
public ConfigurationKey(String keyName) {
|
||||
this.keyName = keyName;
|
||||
System.out.println("registering " + keyName);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T> T read(ConfigurationKey<T> key, AST<?, ?, ?> ast) {
|
||||
Type it = key.getClass().getGenericSuperclass();
|
||||
if (key.keyName.equals("lombok.log.varName")) return (T)"loggertje";
|
||||
if (key.keyName.equals("lombok.log.static")) return (T)Boolean.FALSE;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -21,15 +21,17 @@
|
||||
*/
|
||||
package lombok.eclipse.handlers;
|
||||
|
||||
import static lombok.eclipse.Eclipse.*;
|
||||
import static lombok.eclipse.Eclipse.fromQualifiedName;
|
||||
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.core.LombokConfiguration.ConfigurationKey;
|
||||
import lombok.eclipse.EclipseAnnotationHandler;
|
||||
import lombok.eclipse.EclipseNode;
|
||||
import lombok.eclipse.handlers.EclipseHandlerUtil.MemberExistsResult;
|
||||
|
||||
import org.eclipse.jdt.internal.compiler.ast.Annotation;
|
||||
import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess;
|
||||
@@ -44,14 +46,25 @@ 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();
|
||||
}
|
||||
|
||||
public static void processAnnotation(LoggingFramework framework, AnnotationValues<? extends java.lang.annotation.Annotation> annotation, Annotation source, EclipseNode annotationNode) {
|
||||
EclipseNode owner = annotationNode.up();
|
||||
|
||||
switch (owner.getKind()) {
|
||||
case TYPE:
|
||||
|
||||
String logFieldName = annotationNode.getAst().readConfiguration(LOG_VARIABLE_NAME);
|
||||
if (logFieldName == null) {
|
||||
logFieldName = "log";
|
||||
}
|
||||
|
||||
TypeDeclaration typeDecl = null;
|
||||
if (owner.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) owner.get();
|
||||
int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
|
||||
@@ -64,14 +77,14 @@ public class HandleLog {
|
||||
return;
|
||||
}
|
||||
|
||||
if (fieldExists("log", owner) != MemberExistsResult.NOT_EXISTS) {
|
||||
annotationNode.addWarning("Field 'log' already exists.");
|
||||
if (fieldExists(logFieldName, owner) != MemberExistsResult.NOT_EXISTS) {
|
||||
annotationNode.addWarning("Field '" + logFieldName + "' already exists.");
|
||||
return;
|
||||
}
|
||||
|
||||
ClassLiteralAccess loggingType = selfType(owner, source);
|
||||
|
||||
FieldDeclaration fieldDeclaration = createField(framework, source, loggingType);
|
||||
FieldDeclaration fieldDeclaration = createField(framework, source, loggingType, logFieldName);
|
||||
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);
|
||||
@@ -97,13 +110,12 @@ public class HandleLog {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static FieldDeclaration createField(LoggingFramework framework, Annotation source, ClassLiteralAccess loggingType) {
|
||||
private static FieldDeclaration createField(LoggingFramework framework, Annotation source, ClassLiteralAccess loggingType, String logFieldName) {
|
||||
int pS = source.sourceStart, pE = source.sourceEnd;
|
||||
long p = (long)pS << 32 | pE;
|
||||
|
||||
// private static final <loggerType> log = <factoryMethod>(<parameter>);
|
||||
|
||||
FieldDeclaration fieldDecl = new FieldDeclaration("log".toCharArray(), 0, -1);
|
||||
FieldDeclaration fieldDecl = new FieldDeclaration(logFieldName.toCharArray(), 0, -1);
|
||||
setGeneratedBy(fieldDecl, source);
|
||||
fieldDecl.declarationSourceEnd = -1;
|
||||
fieldDecl.modifiers = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
|
||||
|
||||
Reference in new issue
Block a user