diff options
32 files changed, 120 insertions, 48 deletions
diff --git a/doc/changelog.markdown b/doc/changelog.markdown index 11c4bddc..fcde09da 100644 --- a/doc/changelog.markdown +++ b/doc/changelog.markdown @@ -5,6 +5,7 @@ Lombok Changelog * v1.16.20 is the latest stable release of Project Lombok. * PLATFORM: Fix for using lombok together with JDK9's new `module-info.java` feature. [Issue #985](https://github.com/rzwitserloot/lombok/issues/985) * BUGFIX: Potential fix for Netbeans < 9. [Issue #1555](https://github.com/rzwitserloot/lombok/issues/1555) +* PROMOTION: `var` has been promoted from experimental to the main package with no changes. The 'old' experimental one is still around but is deprecated, and is an alias for the new main package one. [var documentation](https://projectlombok.org/features/var.html). ### v1.16.20 (January 9th, 2018) * PLATFORM: Better support for jdk9 in the new IntelliJ, Netbeans and for Gradle. diff --git a/src/core/lombok/core/LombokInternalAliasing.java b/src/core/lombok/core/LombokInternalAliasing.java index 08764a5c..a1909df3 100644 --- a/src/core/lombok/core/LombokInternalAliasing.java +++ b/src/core/lombok/core/LombokInternalAliasing.java @@ -51,6 +51,7 @@ public class LombokInternalAliasing { Map<String, String> m2 = new HashMap<String, String>(); m2.put("lombok.experimental.Value", "lombok.Value"); m2.put("lombok.experimental.Builder", "lombok.Builder"); + m2.put("lombok.experimental.var", "lombok.var"); m2.put("lombok.Delegate", "lombok.experimental.Delegate"); ALIASES = Collections.unmodifiableMap(m2); } diff --git a/src/core/lombok/core/configuration/AllowHelper.java b/src/core/lombok/core/configuration/AllowHelper.java index 3873b055..1146ccde 100644 --- a/src/core/lombok/core/configuration/AllowHelper.java +++ b/src/core/lombok/core/configuration/AllowHelper.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 The Project Lombok Authors. + * Copyright (C) 2018 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 @@ -24,10 +24,8 @@ package lombok.core.configuration; import java.util.Collection; import java.util.Collections; -import lombok.ConfigurationKeys; - public final class AllowHelper { - private final static Collection<? extends ConfigurationKey<?>> ALLOWABLE = Collections.singleton(ConfigurationKeys.VAR_FLAG_USAGE); + private final static Collection<? extends ConfigurationKey<?>> ALLOWABLE = Collections.emptySet(); private AllowHelper() { // Prevent instantiation diff --git a/src/core/lombok/eclipse/handlers/HandleVal.java b/src/core/lombok/eclipse/handlers/HandleVal.java index d8901067..3742ac00 100644 --- a/src/core/lombok/eclipse/handlers/HandleVal.java +++ b/src/core/lombok/eclipse/handlers/HandleVal.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2016 The Project Lombok Authors. + * Copyright (C) 2010-2018 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 @@ -25,13 +25,14 @@ import static lombok.core.handlers.HandlerUtil.handleFlagUsage; import static lombok.eclipse.handlers.EclipseHandlerUtil.typeMatches; import lombok.ConfigurationKeys; import lombok.val; +import lombok.var; import lombok.core.HandlerPriority; import lombok.eclipse.DeferUntilPostDiet; import lombok.eclipse.EclipseASTAdapter; import lombok.eclipse.EclipseASTVisitor; import lombok.eclipse.EclipseNode; -import lombok.experimental.var; +import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer; import org.eclipse.jdt.internal.compiler.ast.ForStatement; import org.eclipse.jdt.internal.compiler.ast.ForeachStatement; @@ -74,11 +75,18 @@ public class HandleVal extends EclipseASTAdapter { return; } - if (isVal && localNode.directUp().get() instanceof ForStatement) { + ASTNode parentRaw = localNode.directUp().get(); + + if (isVal && parentRaw instanceof ForStatement) { localNode.addError("'val' is not allowed in old-style for loops"); return; } + if (parentRaw instanceof ForStatement && ((ForStatement) parentRaw).initializations != null && ((ForStatement) parentRaw).initializations.length > 1) { + localNode.addError("'var' is not allowed in old-style for loops if there is more than 1 initializer"); + return; + } + if (local.initialization != null && local.initialization.getClass().getName().equals("org.eclipse.jdt.internal.compiler.ast.LambdaExpression")) { localNode.addError("'" + annotation + "' is not allowed with lambda expressions."); return; diff --git a/src/core/lombok/experimental/var.java b/src/core/lombok/experimental/var.java index d8de8b19..71cc141a 100644 --- a/src/core/lombok/experimental/var.java +++ b/src/core/lombok/experimental/var.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2013 The Project Lombok Authors. + * Copyright (C) 2010-2017 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 @@ -23,6 +23,9 @@ package lombok.experimental; /** * like val but not final + * + * @deprecated {@code var} has been promoted to the main package; use {@link lombok.var} instead. */ +@Deprecated public @interface var { } diff --git a/src/core/lombok/javac/handlers/HandleVal.java b/src/core/lombok/javac/handlers/HandleVal.java index f0f6eb2a..14130bc4 100644 --- a/src/core/lombok/javac/handlers/HandleVal.java +++ b/src/core/lombok/javac/handlers/HandleVal.java @@ -26,7 +26,7 @@ import static lombok.javac.handlers.JavacHandlerUtil.*; import lombok.ConfigurationKeys; import lombok.val; import lombok.core.HandlerPriority; -import lombok.experimental.var; +import lombok.var; import lombok.javac.JavacASTAdapter; import lombok.javac.JavacASTVisitor; import lombok.javac.JavacNode; @@ -54,10 +54,10 @@ import com.sun.tools.javac.util.List; public class HandleVal extends JavacASTAdapter { private static boolean eq(String typeTreeToString, String key) { - return (typeTreeToString.equals(key) || typeTreeToString.equals("lombok." + key)); + return typeTreeToString.equals(key) || typeTreeToString.equals("lombok." + key) || typeTreeToString.equals("lombok.experimental." + key); } - @Override + @SuppressWarnings("deprecation") @Override public void visitLocal(JavacNode localNode, JCVariableDecl local) { JCTree typeTree = local.vartype; if (typeTree == null) return; @@ -77,6 +77,11 @@ public class HandleVal extends JavacASTAdapter { return; } + if (parentRaw instanceof JCForLoop && ((JCForLoop) parentRaw).getInitializer().size() > 1) { + localNode.addError("'var' is not allowed in old-style for loops if there is more than 1 initializer"); + return; + } + JCExpression rhsOfEnhancedForLoop = null; if (local.init == null) { if (parentRaw instanceof JCEnhancedForLoop) { @@ -98,6 +103,7 @@ public class HandleVal extends JavacASTAdapter { if (localNode.shouldDeleteLombokAnnotations()) { JavacHandlerUtil.deleteImportFromCompilationUnit(localNode, val.class.getName()); + JavacHandlerUtil.deleteImportFromCompilationUnit(localNode, lombok.experimental.var.class.getName()); JavacHandlerUtil.deleteImportFromCompilationUnit(localNode, var.class.getName()); } diff --git a/src/core/lombok/var.java b/src/core/lombok/var.java new file mode 100644 index 00000000..63a70213 --- /dev/null +++ b/src/core/lombok/var.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2010-2018 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; + +/** + * Use {@code var} as the type of any local variable declaration (even in a {@code for} statement), and the type will be inferred from the initializing expression + * (any further assignments to the variable are not involved in this type inference). + * <p> + * For example: {@code var x = 10.0;} will infer {@code double}, and {@code var y = new ArrayList<String>();} will infer {@code ArrayList<String>}. + * <p> + * Note that this is an annotation type because {@code var x = 10;} will be desugared to {@code @var int x = 10;} + * <p> + * Complete documentation is found at <a href="https://projectlombok.org/features/var">the project lombok features page for @var</a>. + */ +public @interface var { +} diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java index e4dd7b26..632dd865 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchVal.java @@ -212,7 +212,7 @@ public class PatchVal { } private static boolean isVar(LocalDeclaration local, BlockScope scope) { - return is(local.type, scope, "lombok.experimental.var"); + return is(local.type, scope, "lombok.experimental.var") || is(local.type, scope, "lombok.var"); } private static boolean isVal(LocalDeclaration local, BlockScope scope) { diff --git a/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipse.java b/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipse.java index 505eb767..839fce6e 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipse.java +++ b/src/eclipseAgent/lombok/eclipse/agent/PatchValEclipse.java @@ -103,7 +103,7 @@ public class PatchValEclipse { } private static boolean couldBeVar(TypeReference type) { - return PatchVal.couldBe("lombok.experimental.var", type); + return PatchVal.couldBe("lombok.experimental.var", type) || PatchVal.couldBe("lombok.var", type); } public static void addFinalAndValAnnotationToSingleVariableDeclaration(Object converter, SingleVariableDeclaration out, LocalDeclaration in) { diff --git a/test/transform/resource/after-delombok/VarWarning.java b/test/transform/resource/after-delombok/VarWarning.java new file mode 100644 index 00000000..a333c87c --- /dev/null +++ b/test/transform/resource/after-delombok/VarWarning.java @@ -0,0 +1,6 @@ +public class VarWarning { + public void isOkay() { + java.lang.String x = "Warning"; + x.toLowerCase(); + } +}
\ No newline at end of file diff --git a/test/transform/resource/after-ecj/VarComplex.java b/test/transform/resource/after-ecj/VarComplex.java index 10c456eb..97a0a177 100644 --- a/test/transform/resource/after-ecj/VarComplex.java +++ b/test/transform/resource/after-ecj/VarComplex.java @@ -1,4 +1,4 @@ -import lombok.experimental.var; +import lombok.var; public class VarComplex { private String field = ""; private static final int CONSTANT = 20; diff --git a/test/transform/resource/after-ecj/VarInFor.java b/test/transform/resource/after-ecj/VarInFor.java index 0192aaed..1799d9b7 100644 --- a/test/transform/resource/after-ecj/VarInFor.java +++ b/test/transform/resource/after-ecj/VarInFor.java @@ -1,4 +1,4 @@ -import lombok.experimental.var; +import lombok.var; public class VarInFor { public VarInFor() { super(); diff --git a/test/transform/resource/after-ecj/VarInForOld.java b/test/transform/resource/after-ecj/VarInForOld.java index 98fedf03..065ea94d 100644 --- a/test/transform/resource/after-ecj/VarInForOld.java +++ b/test/transform/resource/after-ecj/VarInForOld.java @@ -1,4 +1,4 @@ -import lombok.experimental.var; +import lombok.var; public class VarInForOld { public VarInForOld() { super(); diff --git a/test/transform/resource/after-ecj/VarNullInit.java b/test/transform/resource/after-ecj/VarNullInit.java index 3eb2d506..848744cc 100644 --- a/test/transform/resource/after-ecj/VarNullInit.java +++ b/test/transform/resource/after-ecj/VarNullInit.java @@ -1,4 +1,4 @@ -import lombok.experimental.var; +import lombok.var; public class VarNullInit { public VarNullInit() { super(); diff --git a/test/transform/resource/after-ecj/VarWarning.java b/test/transform/resource/after-ecj/VarWarning.java new file mode 100644 index 00000000..4caf90f8 --- /dev/null +++ b/test/transform/resource/after-ecj/VarWarning.java @@ -0,0 +1,10 @@ +import lombok.var; +public class VarWarning { + public VarWarning() { + super(); + } + public void isOkay() { + @var java.lang.String x = "Warning"; + x.toLowerCase(); + } +}
\ No newline at end of file diff --git a/test/transform/resource/before/VarComplex.java b/test/transform/resource/before/VarComplex.java index bfaa8804..c93e177a 100644 --- a/test/transform/resource/before/VarComplex.java +++ b/test/transform/resource/before/VarComplex.java @@ -1,5 +1,4 @@ -//CONF: lombok.var.flagUsage = ALLOW -import lombok.experimental.var; +import lombok.var; public class VarComplex { private String field = ""; diff --git a/test/transform/resource/before/VarInFor.java b/test/transform/resource/before/VarInFor.java index cc8c387e..7f7bb7a7 100644 --- a/test/transform/resource/before/VarInFor.java +++ b/test/transform/resource/before/VarInFor.java @@ -1,5 +1,4 @@ -//CONF: lombok.var.flagUsage = ALLOW -import lombok.experimental.var; +import lombok.var; public class VarInFor { public void enhancedFor() { diff --git a/test/transform/resource/before/VarInForOld.java b/test/transform/resource/before/VarInForOld.java index f90aba7f..99e83b57 100644 --- a/test/transform/resource/before/VarInForOld.java +++ b/test/transform/resource/before/VarInForOld.java @@ -1,5 +1,4 @@ -//CONF: lombok.var.flagUsage = ALLOW -import lombok.experimental.var; +import lombok.var; public class VarInForOld { public void oldFor() { diff --git a/test/transform/resource/before/VarInForOldMulti.java b/test/transform/resource/before/VarInForOldMulti.java new file mode 100644 index 00000000..e2ea9682 --- /dev/null +++ b/test/transform/resource/before/VarInForOldMulti.java @@ -0,0 +1,10 @@ +//skip compare contents +import lombok.var; + +public class VarInForOldMulti { + public void oldForMulti() { + for (var i = 0, j = "Hey"; i < 100; ++i) { + System.out.println(i); + } + } +}
\ No newline at end of file diff --git a/test/transform/resource/before/VarModifier.java b/test/transform/resource/before/VarModifier.java index 7250c1c5..5c68caa7 100644 --- a/test/transform/resource/before/VarModifier.java +++ b/test/transform/resource/before/VarModifier.java @@ -1,4 +1,3 @@ -//CONF: lombok.var.flagUsage = ALLOW import lombok.experimental.var; public class VarModifier { diff --git a/test/transform/resource/before/VarNullInit.java b/test/transform/resource/before/VarNullInit.java index efdc9d9e..f9bb53a3 100644 --- a/test/transform/resource/before/VarNullInit.java +++ b/test/transform/resource/before/VarNullInit.java @@ -1,5 +1,4 @@ -//CONF: lombok.var.flagUsage = ALLOW -import lombok.experimental.var; +import lombok.var; public class VarNullInit { void method() { diff --git a/test/transform/resource/before/VarWarning.java b/test/transform/resource/before/VarWarning.java index 85559587..90464d30 100644 --- a/test/transform/resource/before/VarWarning.java +++ b/test/transform/resource/before/VarWarning.java @@ -1,6 +1,5 @@ //CONF: lombok.var.flagUsage = WARNING -//skip compare contents -import lombok.experimental.var; +import lombok.var; public class VarWarning { public void isOkay() { diff --git a/test/transform/resource/messages-delombok/VarInForOldMulti.java.messages b/test/transform/resource/messages-delombok/VarInForOldMulti.java.messages new file mode 100644 index 00000000..f65fe823 --- /dev/null +++ b/test/transform/resource/messages-delombok/VarInForOldMulti.java.messages @@ -0,0 +1 @@ +6 'var' is not allowed in old-style for loops if there is more than 1 initializer diff --git a/test/transform/resource/messages-delombok/VarNullInit.java.messages b/test/transform/resource/messages-delombok/VarNullInit.java.messages index 190ab7c4..5a2a6ae1 100644 --- a/test/transform/resource/messages-delombok/VarNullInit.java.messages +++ b/test/transform/resource/messages-delombok/VarNullInit.java.messages @@ -1 +1 @@ -6 variable initializer is 'null'
\ No newline at end of file +5 variable initializer is 'null'
\ No newline at end of file diff --git a/test/transform/resource/messages-delombok/VarWarning.java.messages b/test/transform/resource/messages-delombok/VarWarning.java.messages index 48c89581..886e98f4 100644 --- a/test/transform/resource/messages-delombok/VarWarning.java.messages +++ b/test/transform/resource/messages-delombok/VarWarning.java.messages @@ -1 +1 @@ -7 Use of var is flagged according to lombok configuration
\ No newline at end of file +6 Use of var is flagged according to lombok configuration
\ No newline at end of file diff --git a/test/transform/resource/messages-ecj/ValInTryWithResources.java.messages b/test/transform/resource/messages-ecj/ValInTryWithResources.java.messages new file mode 100644 index 00000000..9d0d7a6e --- /dev/null +++ b/test/transform/resource/messages-ecj/ValInTryWithResources.java.messages @@ -0,0 +1 @@ +OPTIONAL 8 Resource leak: 'i' is never closed diff --git a/test/transform/resource/messages-ecj/VarInForOldMulti.java.messages b/test/transform/resource/messages-ecj/VarInForOldMulti.java.messages new file mode 100644 index 00000000..0bfd6e65 --- /dev/null +++ b/test/transform/resource/messages-ecj/VarInForOldMulti.java.messages @@ -0,0 +1 @@ +6 'var' is not allowed in old-style for loops if there is more than 1 initializer
\ No newline at end of file diff --git a/test/transform/resource/messages-ecj/VarModifier.java.messages b/test/transform/resource/messages-ecj/VarModifier.java.messages new file mode 100644 index 00000000..051d1ad7 --- /dev/null +++ b/test/transform/resource/messages-ecj/VarModifier.java.messages @@ -0,0 +1,3 @@ +1 The type var is deprecated +7 The type var is deprecated +8 The type var is deprecated diff --git a/test/transform/resource/messages-ecj/VarNullInit.java.messages b/test/transform/resource/messages-ecj/VarNullInit.java.messages index 190ab7c4..5a2a6ae1 100644 --- a/test/transform/resource/messages-ecj/VarNullInit.java.messages +++ b/test/transform/resource/messages-ecj/VarNullInit.java.messages @@ -1 +1 @@ -6 variable initializer is 'null'
\ No newline at end of file +5 variable initializer is 'null'
\ No newline at end of file diff --git a/test/transform/resource/messages-ecj/VarWarning.java.messages b/test/transform/resource/messages-ecj/VarWarning.java.messages index 48c89581..25096b84 100644 --- a/test/transform/resource/messages-ecj/VarWarning.java.messages +++ b/test/transform/resource/messages-ecj/VarWarning.java.messages @@ -1 +1 @@ -7 Use of var is flagged according to lombok configuration
\ No newline at end of file +6 Use of var is flagged according to lombok configuration.
\ No newline at end of file diff --git a/website/templates/features/index.html b/website/templates/features/index.html index 73b5dce8..d077ab4d 100644 --- a/website/templates/features/index.html +++ b/website/templates/features/index.html @@ -12,6 +12,10 @@ Finally! Hassle-free final local variables. </@main.feature> + <@main.feature title="var" href="var"> + Mutably! Hassle-free local variables. + </@main.feature> + <@main.feature title="@NonNull" href="NonNull"> or: How I learned to stop worrying and love the NullPointerException. </@main.feature> diff --git a/website/templates/features/experimental/var.html b/website/templates/features/var.html index fa35ac5e..60e24914 100644 --- a/website/templates/features/experimental/var.html +++ b/website/templates/features/var.html @@ -1,23 +1,13 @@ -<#import "../_features.html" as f> +<#import "_features.html" as f> -<@f.scaffold title="var" logline="Modifiable local variables with a type inferred by assigning value."> +<@f.scaffold title="var" logline="Mutably! Hassle-free local variables."> <@f.history> - <p> - <code>var</code> was introduced in lombok 1.16.12 as experimental feature. - </p> + <p><ul> + <li><code>var</code> was promoted to the main package in lombok 2.0.0; given that <a href="http://openjdk.java.net/jeps/286">JEP 286</a> establishes expectations, and lombok's take on <code>var</code> follows these, we've decided to promote <code>var</code> eventhough the feature remains controversial.</li> + <li><code>var</code> was introduced in lombok 1.16.12 as experimental feature.</li> + </ul></p> </@f.history> - <@f.experimental> - <ul> - <li> - This feature is very controversial. - </li><li> - There is <a href="http://openjdk.java.net/jeps/286">JEP 286</a> that should make <code>var</code> obsolete. - </li> - </ul> - Current status: <em>uncertain</em> – Currently we feel this feature cannot move out of experimental status. - </@f.experimental> - <@f.overview> <p> <code>var</code> works exactly like <a href="/features/val"><code>val</code></a>, except the local variable is <em>not</em> marked as <code>final</code>. |