From d6ca8d5e81881c69f8f52c33375b07cef24f7d85 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 19 Jan 2014 03:54:46 +0100 Subject: [configuration] added facilities to override conf file resolution for test cases, and added first tests to test implementations of configuration-based stuff (FlagUsages, specifically). Error reporter has been refactored. --- src/core/lombok/core/LombokConfiguration.java | 40 +++++++++++----------- .../configuration/ConfigurationErrorReporter.java | 26 -------------- .../ConfigurationErrorReporterFactory.java | 38 -------------------- .../ConfigurationProblemReporter.java | 32 +++++++++++++++++ .../ConfigurationResolverFactory.java | 28 +++++++++++++++ .../core/configuration/FileSystemSourceCache.java | 21 +++++++----- .../configuration/StringConfigurationSource.java | 24 +++++++------ 7 files changed, 105 insertions(+), 104 deletions(-) delete mode 100644 src/core/lombok/core/configuration/ConfigurationErrorReporter.java delete mode 100644 src/core/lombok/core/configuration/ConfigurationErrorReporterFactory.java create mode 100644 src/core/lombok/core/configuration/ConfigurationProblemReporter.java create mode 100644 src/core/lombok/core/configuration/ConfigurationResolverFactory.java (limited to 'src/core') 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 read(ConfigurationKey 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 problems = new ArrayList(); - 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/ConfigurationErrorReporter.java b/src/core/lombok/core/configuration/ConfigurationErrorReporter.java deleted file mode 100644 index 901d313b..00000000 --- a/src/core/lombok/core/configuration/ConfigurationErrorReporter.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (C) 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 - * 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; - -public interface ConfigurationErrorReporter { - void report(String error, int lineNumber, String line); -} \ No newline at end of file diff --git a/src/core/lombok/core/configuration/ConfigurationErrorReporterFactory.java b/src/core/lombok/core/configuration/ConfigurationErrorReporterFactory.java deleted file mode 100644 index 2ae1cb08..00000000 --- a/src/core/lombok/core/configuration/ConfigurationErrorReporterFactory.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 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 - * 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; - -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); - } - }; - } - }; - - ConfigurationErrorReporter createFor(String description); -} diff --git a/src/core/lombok/core/configuration/ConfigurationProblemReporter.java b/src/core/lombok/core/configuration/ConfigurationProblemReporter.java new file mode 100644 index 00000000..5dbf99a8 --- /dev/null +++ b/src/core/lombok/core/configuration/ConfigurationProblemReporter.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 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 + * 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; + +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); + } + }; +} diff --git a/src/core/lombok/core/configuration/ConfigurationResolverFactory.java b/src/core/lombok/core/configuration/ConfigurationResolverFactory.java new file mode 100644 index 00000000..83b58c2f --- /dev/null +++ b/src/core/lombok/core/configuration/ConfigurationResolverFactory.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 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 + * 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 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 cache = new ConcurrentHashMap(); - public Iterable sourcesForJavaFile(URI javaFile, final ConfigurationErrorReporterFactory reporterFactory) { + public Iterable sourcesForJavaFile(URI javaFile, final ConfigurationProblemReporter reporter) { if (javaFile == null) return Collections.emptyList(); final File directory = new File(javaFile.normalize()).getParentFile(); return new Iterable() { @@ -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 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 values = new TreeMap(String.CASE_INSENSITIVE_ORDER); Map> 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 values, String keyName, String operator, String value, ConfigurationDataType type, ConfigurationErrorReporter reporter, int lineNumber, String line) { + private static void processResult(Map 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; } -- cgit