diff options
Diffstat (limited to 'src/core/lombok')
-rw-r--r-- | src/core/lombok/core/LombokConfiguration.java | 40 | ||||
-rw-r--r-- | src/core/lombok/core/configuration/ConfigurationProblemReporter.java (renamed from src/core/lombok/core/configuration/ConfigurationErrorReporterFactory.java) | 18 | ||||
-rw-r--r-- | src/core/lombok/core/configuration/ConfigurationResolverFactory.java (renamed from src/core/lombok/core/configuration/ConfigurationErrorReporter.java) | 8 | ||||
-rw-r--r-- | src/core/lombok/core/configuration/FileSystemSourceCache.java | 21 | ||||
-rw-r--r-- | src/core/lombok/core/configuration/StringConfigurationSource.java | 24 |
5 files changed, 56 insertions, 55 deletions
diff --git a/src/core/lombok/core/LombokConfiguration.java b/src/core/lombok/core/LombokConfiguration.java index e64ddbbc..690c05ee 100644 --- a/src/core/lombok/core/LombokConfiguration.java +++ b/src/core/lombok/core/LombokConfiguration.java @@ -26,40 +26,40 @@ import java.util.ArrayList; import java.util.List; import lombok.core.configuration.BubblingConfigurationResolver; -import lombok.core.configuration.ConfigurationErrorReporter; -import lombok.core.configuration.ConfigurationErrorReporterFactory; +import lombok.core.configuration.ConfigurationProblemReporter; import lombok.core.configuration.ConfigurationKey; +import lombok.core.configuration.ConfigurationResolver; +import lombok.core.configuration.ConfigurationResolverFactory; import lombok.core.configuration.FileSystemSourceCache; public class LombokConfiguration { - private static FileSystemSourceCache cache = new FileSystemSourceCache(); + private static ConfigurationResolverFactory configurationResolverFactory = createFileSystemBubblingResolverFactory(); private LombokConfiguration() { // prevent instantiation } + public static void overrideConfigurationResolverFactory(ConfigurationResolverFactory crf) { + configurationResolverFactory = crf == null ? createFileSystemBubblingResolverFactory() : crf; + } + static <T> T read(ConfigurationKey<T> key, AST<?, ?, ?> ast) { - return createResolver(ast, ConfigurationErrorReporterFactory.CONSOLE, cache).resolve(key); + return configurationResolverFactory.createResolver(ast).resolve(key); } public static void writeConfiguration(AST<?, ?, ?> ast, PrintStream stream) { final List<String> problems = new ArrayList<String>(); - ConfigurationErrorReporterFactory reporterFactory = new ConfigurationErrorReporterFactory() { - @Override public ConfigurationErrorReporter createFor(final String description) { - return new ConfigurationErrorReporter() { - @Override - public void report(String error, int lineNumber, String line) { - problems.add(String.format("%s (%s:%d)", error, description, lineNumber)); - } - }; + ConfigurationProblemReporter reporter = new ConfigurationProblemReporter() { + @Override public void report(String sourceDescription, String problem, int lineNumber, CharSequence line) { + problems.add(String.format("%s (%s:%d)", problem, sourceDescription, lineNumber)); } }; stream.printf("Combined lombok configuration for '%s'\n\n", ast.getAbsoluteFileLocation()); // create a new empty 'cache' to make sure all problems are reported - FileSystemSourceCache sourceCache = new FileSystemSourceCache(); - BubblingConfigurationResolver resolver = createResolver(ast, reporterFactory, sourceCache); + cache.reset(); + ConfigurationResolver resolver = new BubblingConfigurationResolver(cache.sourcesForJavaFile(ast.getAbsoluteFileLocation(), reporter)); for (ConfigurationKey<?> key : ConfigurationKey.registeredKeys()) { Object value = resolver.resolve(key); if (value == null || value instanceof List<?> && ((List<?>)value).isEmpty()) continue; @@ -77,11 +77,11 @@ public class LombokConfiguration { } } - private static BubblingConfigurationResolver createResolver(AST<?, ?, ?> ast, ConfigurationErrorReporterFactory reporterFactory, FileSystemSourceCache sourceCache) { - return new BubblingConfigurationResolver(sourceCache.sourcesForJavaFile(ast.getAbsoluteFileLocation(), reporterFactory)); - } - - public static void main(String[] args) { - System.out.println(" \n \n ".trim().length()); + private static ConfigurationResolverFactory createFileSystemBubblingResolverFactory() { + return new ConfigurationResolverFactory() { + @Override public ConfigurationResolver createResolver(AST<?, ?, ?> ast) { + return new BubblingConfigurationResolver(cache.sourcesForJavaFile(ast.getAbsoluteFileLocation(), ConfigurationProblemReporter.CONSOLE)); + } + }; } } diff --git a/src/core/lombok/core/configuration/ConfigurationErrorReporterFactory.java b/src/core/lombok/core/configuration/ConfigurationProblemReporter.java index 2ae1cb08..5dbf99a8 100644 --- a/src/core/lombok/core/configuration/ConfigurationErrorReporterFactory.java +++ b/src/core/lombok/core/configuration/ConfigurationProblemReporter.java @@ -21,18 +21,12 @@ */ package lombok.core.configuration; -public interface ConfigurationErrorReporterFactory { - ConfigurationErrorReporterFactory CONSOLE = new ConfigurationErrorReporterFactory() { - @Override - public ConfigurationErrorReporter createFor(final String description) { - return new ConfigurationErrorReporter() { - @Override - public void report(String error, int lineNumber, String line) { - System.err.printf("%s (%s:%d)", error, description, lineNumber); - } - }; +public interface ConfigurationProblemReporter { + void report(String sourceDescription, String problem, int lineNumber, CharSequence line); + + ConfigurationProblemReporter CONSOLE = new ConfigurationProblemReporter() { + @Override public void report(String sourceDescription, String problem, int lineNumber, CharSequence line) { + System.err.printf("%s (%s:%d)\n", problem, sourceDescription, lineNumber); } }; - - ConfigurationErrorReporter createFor(String description); } diff --git a/src/core/lombok/core/configuration/ConfigurationErrorReporter.java b/src/core/lombok/core/configuration/ConfigurationResolverFactory.java index 901d313b..83b58c2f 100644 --- a/src/core/lombok/core/configuration/ConfigurationErrorReporter.java +++ b/src/core/lombok/core/configuration/ConfigurationResolverFactory.java @@ -21,6 +21,8 @@ */ package lombok.core.configuration; -public interface ConfigurationErrorReporter { - void report(String error, int lineNumber, String line); -}
\ No newline at end of file +import lombok.core.AST; + +public interface ConfigurationResolverFactory { + ConfigurationResolver createResolver(AST<?, ?, ?> ast); +} diff --git a/src/core/lombok/core/configuration/FileSystemSourceCache.java b/src/core/lombok/core/configuration/FileSystemSourceCache.java index 61e060ed..22d458b6 100644 --- a/src/core/lombok/core/configuration/FileSystemSourceCache.java +++ b/src/core/lombok/core/configuration/FileSystemSourceCache.java @@ -36,14 +36,13 @@ import lombok.ConfigurationKeys; import lombok.core.configuration.ConfigurationSource.Result; public class FileSystemSourceCache { - private static String LOMBOK_CONFIG_FILENAME = "lombok.config"; private static final long RECHECK_FILESYSTEM = TimeUnit.SECONDS.toMillis(2); private static final long MISSING = -1; private final ConcurrentMap<File, Content> cache = new ConcurrentHashMap<File, Content>(); - public Iterable<ConfigurationSource> sourcesForJavaFile(URI javaFile, final ConfigurationErrorReporterFactory reporterFactory) { + public Iterable<ConfigurationSource> sourcesForJavaFile(URI javaFile, final ConfigurationProblemReporter reporter) { if (javaFile == null) return Collections.emptyList(); final File directory = new File(javaFile.normalize()).getParentFile(); return new Iterable<ConfigurationSource>() { @@ -72,7 +71,7 @@ public class FileSystemSourceCache { private ConfigurationSource findNext() { while (currentDirectory != null && next == null) { - next = getSourceForDirectory(currentDirectory, reporterFactory); + next = getSourceForDirectory(currentDirectory, reporter); currentDirectory = currentDirectory.getParentFile(); } if (next != null) { @@ -91,7 +90,7 @@ public class FileSystemSourceCache { }; } - ConfigurationSource getSourceForDirectory(File directory, ConfigurationErrorReporterFactory reporterFactory) { + ConfigurationSource getSourceForDirectory(File directory, ConfigurationProblemReporter reporter) { if (!directory.exists() && !directory.isDirectory()) throw new IllegalArgumentException("Not a directory: " + directory); long now = System.currentTimeMillis(); File configFile = new File(directory, LOMBOK_CONFIG_FILENAME); @@ -104,7 +103,7 @@ public class FileSystemSourceCache { content.lastChecked = now; long previouslyModified = content.lastModified; content.lastModified = getLastModified(configFile); - if (content.lastModified != previouslyModified) content.source = content.lastModified == MISSING ? null : parse(configFile, reporterFactory); + if (content.lastModified != previouslyModified) content.source = content.lastModified == MISSING ? null : parse(configFile, reporter); return content.source; } } @@ -118,12 +117,12 @@ public class FileSystemSourceCache { return cache.get(directory); } - private ConfigurationSource parse(File configFile, ConfigurationErrorReporterFactory reporterFactory) { - ConfigurationErrorReporter reporter = reporterFactory.createFor(configFile.getAbsolutePath()); + private ConfigurationSource parse(File configFile, ConfigurationProblemReporter reporter) { + String contentDescription = configFile.getAbsolutePath(); try { - return StringConfigurationSource.forString(fileToString(configFile), reporter); + return StringConfigurationSource.forString(fileToString(configFile), reporter, contentDescription); } catch (Exception e) { - reporter.report("Exception while reading file: " + e.getMessage(), 0, null); + reporter.report(contentDescription, "Exception while reading file: " + e.getMessage(), 0, null); return null; } } @@ -170,4 +169,8 @@ public class FileSystemSourceCache { return new Content(null, MISSING, MISSING); } } + + public void reset() { + cache.clear(); + } }
\ No newline at end of file diff --git a/src/core/lombok/core/configuration/StringConfigurationSource.java b/src/core/lombok/core/configuration/StringConfigurationSource.java index 25889055..27887a6e 100644 --- a/src/core/lombok/core/configuration/StringConfigurationSource.java +++ b/src/core/lombok/core/configuration/StringConfigurationSource.java @@ -36,21 +36,23 @@ public class StringConfigurationSource implements ConfigurationSource { private final Map<String, Result> values; - public static ConfigurationSource forString(String content, ConfigurationErrorReporter reporter) { + private static final Pattern NEWLINE_FINDER = Pattern.compile("^\\s*(.*?)\\s*$", Pattern.MULTILINE); + public static ConfigurationSource forString(CharSequence content, ConfigurationProblemReporter reporter, String contentDescription) { if (reporter == null) throw new NullPointerException("reporter"); Map<String, Result> values = new TreeMap<String, Result>(String.CASE_INSENSITIVE_ORDER); Map<String, ConfigurationKey<?>> registeredKeys = ConfigurationKey.registeredKeysMap(); int lineNumber = 0; - for (String line : content.split("\\n")) { - line = line.trim(); + Matcher lineMatcher = NEWLINE_FINDER.matcher(content); + while (lineMatcher.find()) { + CharSequence line = content.subSequence(lineMatcher.start(1), lineMatcher.end(1)); lineNumber++; - if (line.isEmpty() || line.startsWith("#")) continue; + if (line.length() == 0 || line.charAt(0) == '#') continue; Matcher matcher = LINE.matcher(line); if (!matcher.matches()) { - reporter.report("No valid line", lineNumber, line); + reporter.report(contentDescription, "No valid line", lineNumber, line); continue; } @@ -68,22 +70,22 @@ public class StringConfigurationSource implements ConfigurationSource { } ConfigurationKey<?> key = registeredKeys.get(keyName); if (key == null) { - reporter.report("Unknown key '" + keyName + "'", lineNumber, line); + reporter.report(contentDescription, "Unknown key '" + keyName + "'", lineNumber, line); continue; } ConfigurationDataType type = key.getType(); boolean listOperator = operator.equals("+=") || operator.equals("-="); if (listOperator && !type.isList()) { - reporter.report("'" + keyName + "' is not a list and doesn't support " + operator + " (only = and clear)", lineNumber, line); + reporter.report(contentDescription, "'" + keyName + "' is not a list and doesn't support " + operator + " (only = and clear)", lineNumber, line); continue; } if (operator.equals("=") && type.isList()) { - reporter.report("'" + keyName + "' is a list and cannot be assigned to (use +=, -= and clear instead)", lineNumber, line); + reporter.report(contentDescription, "'" + keyName + "' is a list and cannot be assigned to (use +=, -= and clear instead)", lineNumber, line); continue; } - processResult(values, keyName, operator, value, type, reporter, lineNumber, line); + processResult(values, keyName, operator, value, type, reporter, contentDescription, lineNumber, line); } return new StringConfigurationSource(values); @@ -101,12 +103,12 @@ public class StringConfigurationSource implements ConfigurationSource { } } - private static void processResult(Map<String, Result> values, String keyName, String operator, String value, ConfigurationDataType type, ConfigurationErrorReporter reporter, int lineNumber, String line) { + private static void processResult(Map<String, Result> values, String keyName, String operator, String value, ConfigurationDataType type, ConfigurationProblemReporter reporter, String contentDescription, int lineNumber, CharSequence line) { Object element = null; if (value != null) try { element = type.getParser().parse(value); } catch (Exception e) { - reporter.report("Error while parsing the value for '" + keyName + "' value '" + value + "' (should be a " + type.getParser().description() + ")", lineNumber, line); + reporter.report(contentDescription, "Error while parsing the value for '" + keyName + "' value '" + value + "' (should be a " + type.getParser().description() + ")", lineNumber, line); return; } |