diff options
author | Robbert Jan Grootjans <grootjans@gmail.com> | 2013-03-08 16:54:02 +0100 |
---|---|---|
committer | Robbert Jan Grootjans <grootjans@gmail.com> | 2013-03-08 16:54:02 +0100 |
commit | 4be46113e81292a88cd5fdb3a5ce18fbcffd570d (patch) | |
tree | bf7d19e2121472d0d0370f3908fd6ac5f995e98b | |
parent | 12771880260d57209afcda15fd2b00f3181c38a6 (diff) | |
download | lombok-4be46113e81292a88cd5fdb3a5ce18fbcffd570d.tar.gz lombok-4be46113e81292a88cd5fdb3a5ce18fbcffd570d.tar.bz2 lombok-4be46113e81292a88cd5fdb3a5ce18fbcffd570d.zip |
Compiler options can now be specified for JDK 8 or JDK 6/7 or lower.
After this was finalized I realize that we might consider moving entirely
to String based options, instead of inferring the options from the
provided enum. This setup does have the benefit of throwing exceptions when
options are not present.
-rw-r--r-- | src/core/lombok/javac/Javac6BasedLombokOptions.java | 45 | ||||
-rw-r--r-- | src/core/lombok/javac/Javac8BasedLombokOptions.java | 45 | ||||
-rw-r--r-- | src/core/lombok/javac/LombokOptions.java | 14 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/Delombok.java | 11 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/LombokOptionsFactory.java | 62 | ||||
-rw-r--r-- | src/stubs/com/sun/tools/javac/main/Option.java | 10 |
6 files changed, 171 insertions, 16 deletions
diff --git a/src/core/lombok/javac/Javac6BasedLombokOptions.java b/src/core/lombok/javac/Javac6BasedLombokOptions.java new file mode 100644 index 00000000..4bb2bdc3 --- /dev/null +++ b/src/core/lombok/javac/Javac6BasedLombokOptions.java @@ -0,0 +1,45 @@ +/* + * 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.javac; + +import com.sun.tools.javac.main.OptionName; +import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.Options; + +public class Javac6BasedLombokOptions extends LombokOptions { + + public static Javac6BasedLombokOptions replaceWithDelombokOptions(Context context) { + Options options = Options.instance(context); + context.put(optionsKey, (Options)null); + Javac6BasedLombokOptions result = new Javac6BasedLombokOptions(context); + result.putAll(options); + return result; + } + + private Javac6BasedLombokOptions(Context context) { + super(context); + } + + @Override public void putJavacOption(String optionName, String value) { + put(OptionName.valueOf(optionName), value); + } +} diff --git a/src/core/lombok/javac/Javac8BasedLombokOptions.java b/src/core/lombok/javac/Javac8BasedLombokOptions.java new file mode 100644 index 00000000..3fdea890 --- /dev/null +++ b/src/core/lombok/javac/Javac8BasedLombokOptions.java @@ -0,0 +1,45 @@ +/* + * 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.javac; + +import com.sun.tools.javac.main.Option; +import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.Options; + +public class Javac8BasedLombokOptions extends LombokOptions { + public static Javac8BasedLombokOptions replaceWithDelombokOptions(Context context) { + Options options = Options.instance(context); + context.put(optionsKey, (Options)null); + Javac8BasedLombokOptions result = new Javac8BasedLombokOptions(context); + result.putAll(options); + return result; + } + + private Javac8BasedLombokOptions(Context context) { + super(context); + } + + @Override public void putJavacOption(String optionName, String value) { + String optionText = Option.valueOf(optionName).text; + put(optionText, value); + } +} diff --git a/src/core/lombok/javac/LombokOptions.java b/src/core/lombok/javac/LombokOptions.java index 1a73a8cc..66e42fe7 100644 --- a/src/core/lombok/javac/LombokOptions.java +++ b/src/core/lombok/javac/LombokOptions.java @@ -28,18 +28,10 @@ import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Options; -public class LombokOptions extends Options { +public abstract class LombokOptions extends Options { private boolean deleteLombokAnnotations = true; private final Set<JCCompilationUnit> changed = new HashSet<JCCompilationUnit>(); - public static LombokOptions replaceWithDelombokOptions(Context context) { - Options options = Options.instance(context); - context.put(optionsKey, (Options)null); - LombokOptions result = new LombokOptions(context); - result.putAll(options); - return result; - } - public boolean isChanged(JCCompilationUnit ast) { return changed.contains(ast); } @@ -54,7 +46,9 @@ public class LombokOptions extends Options { return (options instanceof LombokOptions) && ((LombokOptions) options).deleteLombokAnnotations; } - private LombokOptions(Context context) { + protected LombokOptions(Context context) { super(context); } + + public abstract void putJavacOption(String optionName, String value); } diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index f4e02786..7291ac29 100644 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -52,7 +52,6 @@ import lombok.javac.LombokOptions; import com.sun.tools.javac.comp.Todo; import com.sun.tools.javac.main.JavaCompiler; -import com.sun.tools.javac.main.OptionName; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.util.Context; import com.zwitserloot.cmdreader.CmdReader; @@ -359,11 +358,11 @@ public class Delombok { } public boolean delombok() throws IOException { - LombokOptions options = LombokOptions.replaceWithDelombokOptions(context); - options.put(OptionName.ENCODING, charset.name()); - if (classpath != null) options.put(OptionName.CLASSPATH, classpath); - if (sourcepath != null) options.put(OptionName.SOURCEPATH, sourcepath); - if (bootclasspath != null) options.put(OptionName.BOOTCLASSPATH, bootclasspath); + LombokOptions options = LombokOptionsFactory.getDelombokOptions(context); + options.putJavacOption("ENCODING", charset.name()); + if (classpath != null) options.putJavacOption("CLASSPATH", classpath); + if (sourcepath != null) options.putJavacOption("SOURCEPATH", sourcepath); + if (bootclasspath != null) options.putJavacOption("BOOTCLASSPATH", bootclasspath); options.put("compilePolicy", "attr"); CommentCatcher catcher = CommentCatcher.create(context); diff --git a/src/delombok/lombok/delombok/LombokOptionsFactory.java b/src/delombok/lombok/delombok/LombokOptionsFactory.java new file mode 100644 index 00000000..8d3b2528 --- /dev/null +++ b/src/delombok/lombok/delombok/LombokOptionsFactory.java @@ -0,0 +1,62 @@ +/* + * 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.delombok; + +import lombok.javac.Javac6BasedLombokOptions; +import lombok.javac.Javac8BasedLombokOptions; +import lombok.javac.LombokOptions; + +import com.sun.tools.javac.main.JavaCompiler; +import com.sun.tools.javac.util.Context; + +public class LombokOptionsFactory { + + enum LombokOptionCompilerVersion { + JDK6_7 { + @Override LombokOptions createAndRegisterOptions(Context context) { + return Javac6BasedLombokOptions.replaceWithDelombokOptions(context); + } + }, + + JDK8 { + @Override LombokOptions createAndRegisterOptions(Context context) { + return Javac8BasedLombokOptions.replaceWithDelombokOptions(context); + } + }; + + abstract LombokOptions createAndRegisterOptions(Context context); + } + + + public static LombokOptions getDelombokOptions(Context context) { + LombokOptions options; + if (JavaCompiler.version().startsWith("1.6") || JavaCompiler.version().startsWith("1.7")) { + options = LombokOptionCompilerVersion.JDK6_7.createAndRegisterOptions(context); + } else if (JavaCompiler.version().startsWith("1.8")) { + options = LombokOptionCompilerVersion.JDK8.createAndRegisterOptions(context); + } else { + throw new IllegalStateException("No support for compiler version " + JavaCompiler.version() + " for delombok"); + } + return options; + + } +} diff --git a/src/stubs/com/sun/tools/javac/main/Option.java b/src/stubs/com/sun/tools/javac/main/Option.java new file mode 100644 index 00000000..f3229c78 --- /dev/null +++ b/src/stubs/com/sun/tools/javac/main/Option.java @@ -0,0 +1,10 @@ +/* + * These are stub versions of various bits of javac-internal API (for various different versions of javac). Lombok is compiled against these. + */ +package com.sun.tools.javac.main; + + +public enum Option { + ; + public String text; +} |