mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-19 04:16:15 +00:00
Rename Context to ConfigurationFile, move reading contents and metadata to it.
This commit is contained in:
1 parent
091d14bd73
commit
1444d6dfbf
6 files changed
+45
-93
No files matched your search
@@ -41,6 +41,7 @@ public class BubblingConfigurationResolver implements ConfigurationResolver {
|
||||
public <T> T resolve(ConfigurationKey<T> key) {
|
||||
boolean isList = key.getType().isList();
|
||||
List<List<ListModification>> listModificationsList = null;
|
||||
outer:
|
||||
for (ConfigurationSource source : sources) {
|
||||
Result result = source.resolve(key);
|
||||
if (result == null) continue;
|
||||
@@ -49,7 +50,7 @@ public class BubblingConfigurationResolver implements ConfigurationResolver {
|
||||
listModificationsList.add((List<ListModification>)result.getValue());
|
||||
}
|
||||
if (result.isAuthoritative()) {
|
||||
if (isList) break;
|
||||
if (isList) break outer;
|
||||
return (T) result.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2014-2018 The Project Lombok Authors.
|
||||
* Copyright (C) 2014-2020 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
|
||||
@@ -21,8 +21,6 @@
|
||||
*/
|
||||
package lombok.core.configuration;
|
||||
|
||||
import static lombok.core.configuration.FileSystemSourceCache.fileToString;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.PrintStream;
|
||||
import java.net.URI;
|
||||
@@ -53,7 +51,6 @@ import com.zwitserloot.cmdreader.Shorthand;
|
||||
import lombok.ConfigurationKeys;
|
||||
import lombok.core.LombokApp;
|
||||
import lombok.core.configuration.ConfigurationParser.Collector;
|
||||
import lombok.core.configuration.ConfigurationParser.Context;
|
||||
|
||||
@ProviderFor(LombokApp.class)
|
||||
public class ConfigurationApp extends LombokApp {
|
||||
@@ -254,12 +251,13 @@ public class ConfigurationApp extends LombokApp {
|
||||
Set<ConfigurationKey<?>> used = new HashSet<ConfigurationKey<?>>();
|
||||
|
||||
boolean stopBubbling = false;
|
||||
String previousFileName = null;
|
||||
String previousDescription = null;
|
||||
for (File currentDirectory = new File(directory); currentDirectory != null && !stopBubbling; currentDirectory = currentDirectory.getParentFile()) {
|
||||
File configFile = new File(currentDirectory, "lombok.config");
|
||||
if (!configFile.exists() || !configFile.isFile()) continue;
|
||||
|
||||
Map<ConfigurationKey<?>, List<String>> traces = trace(fileToString(configFile), Context.fromFile(configFile), keys);
|
||||
ConfigurationFile context = ConfigurationFile.fromFile(configFile);
|
||||
Map<ConfigurationKey<?>, List<String>> traces = trace(context.contents(), context, keys);
|
||||
|
||||
stopBubbling = stopBubbling(traces.get(ConfigurationKeys.STOP_BUBBLING));
|
||||
for (ConfigurationKey<?> key : keys) {
|
||||
@@ -270,17 +268,17 @@ public class ConfigurationApp extends LombokApp {
|
||||
} else {
|
||||
used.add(key);
|
||||
}
|
||||
if (previousFileName != null) {
|
||||
if (previousDescription != null) {
|
||||
modifications.add("");
|
||||
modifications.add(previousFileName + ":");
|
||||
modifications.add(previousDescription + ":");
|
||||
}
|
||||
result.get(key).addAll(0, modifications);
|
||||
}
|
||||
previousFileName = configFile.getAbsolutePath();
|
||||
previousDescription = context.description();
|
||||
}
|
||||
for (ConfigurationKey<?> key : keys) {
|
||||
if (used.contains(key)) {
|
||||
result.get(key).add(0, previousFileName + (stopBubbling ? " (stopped bubbling):" : ":"));
|
||||
result.get(key).add(0, previousDescription + (stopBubbling ? " (stopped bubbling):" : ":"));
|
||||
} else {
|
||||
result.put(key, Collections.<String>emptyList());
|
||||
}
|
||||
@@ -288,23 +286,23 @@ public class ConfigurationApp extends LombokApp {
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<ConfigurationKey<?>, List<String>> trace(String content, Context context, final Collection<ConfigurationKey<?>> keys) {
|
||||
private Map<ConfigurationKey<?>, List<String>> trace(String content, ConfigurationFile context, final Collection<ConfigurationKey<?>> keys) {
|
||||
final Map<ConfigurationKey<?>, List<String>> result = new HashMap<ConfigurationKey<?>, List<String>>();
|
||||
|
||||
Collector collector = new Collector() {
|
||||
@Override public void clear(ConfigurationKey<?> key, Context context, int lineNumber) {
|
||||
@Override public void clear(ConfigurationKey<?> key, ConfigurationFile context, int lineNumber) {
|
||||
trace(key, "clear " + key.getKeyName(), lineNumber);
|
||||
}
|
||||
|
||||
@Override public void set(ConfigurationKey<?> key, Object value, Context context, int lineNumber) {
|
||||
@Override public void set(ConfigurationKey<?> key, Object value, ConfigurationFile context, int lineNumber) {
|
||||
trace(key, key.getKeyName() + " = " + value, lineNumber);
|
||||
}
|
||||
|
||||
@Override public void add(ConfigurationKey<?> key, Object value, Context context, int lineNumber) {
|
||||
@Override public void add(ConfigurationKey<?> key, Object value, ConfigurationFile context, int lineNumber) {
|
||||
trace(key, key.getKeyName() + " += " + value, lineNumber);
|
||||
}
|
||||
|
||||
@Override public void remove(ConfigurationKey<?> key, Object value, Context context, int lineNumber) {
|
||||
@Override public void remove(ConfigurationKey<?> key, Object value, ConfigurationFile context, int lineNumber) {
|
||||
trace(key, key.getKeyName() + " -= " + value, lineNumber);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
*/
|
||||
package lombok.core.configuration;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -29,7 +28,7 @@ import java.util.regex.Pattern;
|
||||
public class ConfigurationParser {
|
||||
private static final Pattern LINE = Pattern.compile("(?:clear\\s+([^=]+))|(?:(\\S*?)\\s*([-+]?=)\\s*(.*?))");
|
||||
private static final Pattern NEWLINE_FINDER = Pattern.compile("^[\t ]*(.*?)[\t\r ]*$", Pattern.MULTILINE);
|
||||
private static final Pattern IMPORT = Pattern.compile("import\\s+(.*?)");
|
||||
private static final Pattern IMPORT = Pattern.compile("import\\s+(.+?)");
|
||||
|
||||
private ConfigurationProblemReporter reporter;
|
||||
|
||||
@@ -38,7 +37,7 @@ public class ConfigurationParser {
|
||||
this.reporter = reporter;
|
||||
}
|
||||
|
||||
public void parse(CharSequence content, Context context, Collector collector) {
|
||||
public void parse(CharSequence content, ConfigurationFile context, Collector collector) {
|
||||
Map<String, ConfigurationKey<?>> registeredKeys = ConfigurationKey.registeredKeys();
|
||||
int lineNumber = 0;
|
||||
Matcher lineMatcher = NEWLINE_FINDER.matcher(content);
|
||||
@@ -54,7 +53,10 @@ public class ConfigurationParser {
|
||||
reporter.report(context.description(), "Imports are only allowed in the top of the file", lineNumber, line);
|
||||
continue;
|
||||
}
|
||||
// collector.addImport(importMatcher.group(1), contentDescription, lineNumber);
|
||||
String imported = importMatcher.group(1);
|
||||
if (!context.importResolves(imported)) {
|
||||
reporter.report(context.description(), "Imported file does not exist", lineNumber, line);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -115,25 +117,9 @@ public class ConfigurationParser {
|
||||
}
|
||||
|
||||
public interface Collector {
|
||||
void clear(ConfigurationKey<?> key, Context context, int lineNumber);
|
||||
void set(ConfigurationKey<?> key, Object value, Context context, int lineNumber);
|
||||
void add(ConfigurationKey<?> key, Object value, Context context, int lineNumber);
|
||||
void remove(ConfigurationKey<?> key, Object value, Context context, int lineNumber);
|
||||
}
|
||||
|
||||
public static final class Context {
|
||||
private final File file;
|
||||
|
||||
public static Context fromFile(File file) {
|
||||
return new Context(file);
|
||||
}
|
||||
|
||||
private Context(File file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public String description() {
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
void clear(ConfigurationKey<?> key, ConfigurationFile context, int lineNumber);
|
||||
void set(ConfigurationKey<?> key, Object value, ConfigurationFile context, int lineNumber);
|
||||
void add(ConfigurationKey<?> key, Object value, ConfigurationFile context, int lineNumber);
|
||||
void remove(ConfigurationKey<?> key, Object value, ConfigurationFile context, int lineNumber);
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,7 @@
|
||||
*/
|
||||
package lombok.core.configuration;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
@@ -33,7 +31,6 @@ import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import lombok.ConfigurationKeys;
|
||||
import lombok.core.configuration.ConfigurationParser.Context;
|
||||
import lombok.core.configuration.ConfigurationSource.Result;
|
||||
import lombok.core.debug.ProblemReporter;
|
||||
|
||||
@@ -42,9 +39,9 @@ public class FileSystemSourceCache {
|
||||
private static final long FULL_CACHE_CLEAR_INTERVAL = TimeUnit.MINUTES.toMillis(30);
|
||||
private static final long RECHECK_FILESYSTEM = TimeUnit.SECONDS.toMillis(2);
|
||||
private static final long NEVER_CHECKED = -1;
|
||||
private static final long MISSING = -88; // Magic value; any lombok.config with this exact epochmillis last modified will never be read, so, let's ensure nobody accidentally has one with that exact last modified stamp.
|
||||
static final long MISSING = -88; // Magic value; any lombok.config with this exact epochmillis last modified will never be read, so, let's ensure nobody accidentally has one with that exact last modified stamp.
|
||||
|
||||
private final ConcurrentMap<File, Content> fileCache = new ConcurrentHashMap<File, Content>(); // caches files to the content object that tracks content.
|
||||
private final ConcurrentMap<ConfigurationFile, Content> fileCache = new ConcurrentHashMap<ConfigurationFile, Content>(); // caches files to the content object that tracks content.
|
||||
private final ConcurrentMap<URI, File> uriCache = new ConcurrentHashMap<URI, File>(); // caches URIs of java source files to the dir that contains it.
|
||||
private volatile long lastCacheClear = System.currentTimeMillis();
|
||||
|
||||
@@ -150,71 +147,42 @@ public class FileSystemSourceCache {
|
||||
}
|
||||
|
||||
ConfigurationSource getSourceForDirectory(File directory, ConfigurationProblemReporter reporter) {
|
||||
File configFile = new File(directory, LOMBOK_CONFIG_FILENAME);
|
||||
return getSourceForConfigFile(configFile, reporter);
|
||||
return getSourceForConfigFile(ConfigurationFile.fromFile(new File(directory, LOMBOK_CONFIG_FILENAME)), reporter);
|
||||
}
|
||||
|
||||
private ConfigurationSource getSourceForConfigFile(File configFile, ConfigurationProblemReporter reporter) {
|
||||
private ConfigurationSource getSourceForConfigFile(ConfigurationFile context, ConfigurationProblemReporter reporter) {
|
||||
long now = System.currentTimeMillis();
|
||||
Content content = ensureContent(configFile);
|
||||
Content content = ensureContent(context);
|
||||
synchronized (content) {
|
||||
if (content.lastChecked != NEVER_CHECKED && now - content.lastChecked < RECHECK_FILESYSTEM) {
|
||||
return content.source;
|
||||
}
|
||||
content.lastChecked = now;
|
||||
long previouslyModified = content.lastModified;
|
||||
content.lastModified = getLastModifiedOrMissing(configFile);
|
||||
if (content.lastModified != previouslyModified) content.source = content.lastModified == MISSING ? null : parse(configFile, reporter);
|
||||
content.lastModified = context.getLastModifiedOrMissing();
|
||||
if (content.lastModified != previouslyModified) content.source = content.lastModified == MISSING ? null : parse(context, reporter);
|
||||
return content.source;
|
||||
}
|
||||
}
|
||||
|
||||
private Content ensureContent(File configFile) {
|
||||
Content content = fileCache.get(configFile);
|
||||
private Content ensureContent(ConfigurationFile context) {
|
||||
Content content = fileCache.get(context);
|
||||
if (content != null) {
|
||||
return content;
|
||||
}
|
||||
fileCache.putIfAbsent(configFile, Content.empty());
|
||||
return fileCache.get(configFile);
|
||||
fileCache.putIfAbsent(context, Content.empty());
|
||||
return fileCache.get(context);
|
||||
}
|
||||
|
||||
private ConfigurationSource parse(File configFile, ConfigurationProblemReporter reporter) {
|
||||
Context context = Context.fromFile(configFile);
|
||||
private ConfigurationSource parse(ConfigurationFile context, ConfigurationProblemReporter reporter) {
|
||||
try {
|
||||
return StringConfigurationSource.forString(fileToString(configFile), reporter, context);
|
||||
return StringConfigurationSource.forString(context.contents(), reporter, context);
|
||||
} catch (Exception e) {
|
||||
reporter.report(context.description(), "Exception while reading file: " + e.getMessage(), 0, null);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static final ThreadLocal<byte[]> buffers = new ThreadLocal<byte[]>() {
|
||||
protected byte[] initialValue() {
|
||||
return new byte[65536];
|
||||
}
|
||||
};
|
||||
|
||||
static String fileToString(File configFile) throws Exception {
|
||||
byte[] b = buffers.get();
|
||||
FileInputStream fis = new FileInputStream(configFile);
|
||||
try {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
while (true) {
|
||||
int r = fis.read(b);
|
||||
if (r == -1) break;
|
||||
out.write(b, 0, r);
|
||||
}
|
||||
return new String(out.toByteArray(), "UTF-8");
|
||||
} finally {
|
||||
fis.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static final long getLastModifiedOrMissing(File file) {
|
||||
if (!file.exists() || !file.isFile()) return MISSING;
|
||||
return file.lastModified();
|
||||
}
|
||||
|
||||
private static class Content {
|
||||
ConfigurationSource source;
|
||||
long lastModified;
|
||||
|
||||
@@ -29,28 +29,27 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import lombok.core.configuration.ConfigurationParser.Collector;
|
||||
import lombok.core.configuration.ConfigurationParser.Context;
|
||||
|
||||
public class StringConfigurationSource implements ConfigurationSource {
|
||||
private final Map<ConfigurationKey<?>, Result> values;
|
||||
|
||||
public static ConfigurationSource forString(CharSequence content, ConfigurationProblemReporter reporter, Context context) {
|
||||
public static ConfigurationSource forString(CharSequence content, ConfigurationProblemReporter reporter, ConfigurationFile context) {
|
||||
final Map<ConfigurationKey<?>, Result> values = new HashMap<ConfigurationKey<?>, Result>();
|
||||
|
||||
Collector collector = new Collector() {
|
||||
@Override public void clear(ConfigurationKey<?> key, Context context, int lineNumber) {
|
||||
@Override public void clear(ConfigurationKey<?> key, ConfigurationFile context, int lineNumber) {
|
||||
values.put(key, new Result(null, true));
|
||||
}
|
||||
|
||||
@Override public void set(ConfigurationKey<?> key, Object value, Context context, int lineNumber) {
|
||||
@Override public void set(ConfigurationKey<?> key, Object value, ConfigurationFile context, int lineNumber) {
|
||||
values.put(key, new Result(value, true));
|
||||
}
|
||||
|
||||
@Override public void add(ConfigurationKey<?> key, Object value, Context context, int lineNumber) {
|
||||
@Override public void add(ConfigurationKey<?> key, Object value, ConfigurationFile context, int lineNumber) {
|
||||
modifyList(key, value, true);
|
||||
}
|
||||
|
||||
@Override public void remove(ConfigurationKey<?> key, Object value, Context context, int lineNumber) {
|
||||
@Override public void remove(ConfigurationKey<?> key, Object value, ConfigurationFile context, int lineNumber) {
|
||||
modifyList(key, value, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ import org.junit.Assert;
|
||||
|
||||
import lombok.core.LombokImmutableList;
|
||||
import lombok.core.configuration.BubblingConfigurationResolver;
|
||||
import lombok.core.configuration.ConfigurationParser.Context;
|
||||
import lombok.core.configuration.ConfigurationFile;
|
||||
import lombok.core.configuration.ConfigurationProblemReporter;
|
||||
import lombok.core.configuration.ConfigurationResolver;
|
||||
import lombok.core.configuration.StringConfigurationSource;
|
||||
@@ -245,7 +245,7 @@ public class LombokTestSource {
|
||||
}
|
||||
};
|
||||
|
||||
this.configuration = new BubblingConfigurationResolver(Collections.singleton(StringConfigurationSource.forString(conf, reporter, Context.fromFile(file))));
|
||||
this.configuration = new BubblingConfigurationResolver(Collections.singleton(StringConfigurationSource.forString(conf, reporter, ConfigurationFile.fromFile(file))));
|
||||
this.formatPreferences = Collections.unmodifiableMap(formats);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user