[var] various upgrades to var:

* var is promoted to the main package.
* It is no longer an opt-in thing.
* bug: var (unlike val) is allowed in old-style for loops, but if you multi-init: for (var i = 0, j="Foo";;), you now get an error that you can't do that.
* tests both for the multi-for situation and the new main package variant.
This commit is contained in:
Reinier Zwitserloot committed 2018-02-07 00:01:22 +01:00
1 parent 55bcc142d0
commit 66469e04fe
32 files changed
+120 -48

No files matched your search

+1
View File
@@ -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.
@@ -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);
}
@@ -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
@@ -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;
+4 -1
View File
@@ -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 {
}
@@ -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());
}
+35
View File
@@ -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 &#64;var</a>.
*/
public @interface var {
}
@@ -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) {
@@ -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) {
@@ -0,0 +1,6 @@
public class VarWarning {
public void isOkay() {
java.lang.String x = "Warning";
x.toLowerCase();
}
}
@@ -1,4 +1,4 @@
import lombok.experimental.var;
import lombok.var;
public class VarComplex {
private String field = "";
private static final int CONSTANT = 20;
@@ -1,4 +1,4 @@
import lombok.experimental.var;
import lombok.var;
public class VarInFor {
public VarInFor() {
super();
@@ -1,4 +1,4 @@
import lombok.experimental.var;
import lombok.var;
public class VarInForOld {
public VarInForOld() {
super();
@@ -1,4 +1,4 @@
import lombok.experimental.var;
import lombok.var;
public class VarNullInit {
public VarNullInit() {
super();
@@ -0,0 +1,10 @@
import lombok.var;
public class VarWarning {
public VarWarning() {
super();
}
public void isOkay() {
@var java.lang.String x = "Warning";
x.toLowerCase();
}
}
@@ -1,5 +1,4 @@
//CONF: lombok.var.flagUsage = ALLOW
import lombok.experimental.var;
import lombok.var;
public class VarComplex {
private String field = "";
+1 -2
View File
@@ -1,5 +1,4 @@
//CONF: lombok.var.flagUsage = ALLOW
import lombok.experimental.var;
import lombok.var;
public class VarInFor {
public void enhancedFor() {
@@ -1,5 +1,4 @@
//CONF: lombok.var.flagUsage = ALLOW
import lombok.experimental.var;
import lombok.var;
public class VarInForOld {
public void oldFor() {
@@ -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);
}
}
}
@@ -1,4 +1,3 @@
//CONF: lombok.var.flagUsage = ALLOW
import lombok.experimental.var;
public class VarModifier {
@@ -1,5 +1,4 @@
//CONF: lombok.var.flagUsage = ALLOW
import lombok.experimental.var;
import lombok.var;
public class VarNullInit {
void method() {
@@ -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() {
@@ -0,0 +1 @@
6 'var' is not allowed in old-style for loops if there is more than 1 initializer
@@ -1 +1 @@
6 variable initializer is 'null'
5 variable initializer is 'null'
@@ -1 +1 @@
7 Use of var is flagged according to lombok configuration
6 Use of var is flagged according to lombok configuration
@@ -0,0 +1 @@
OPTIONAL 8 Resource leak: 'i' is never closed
@@ -0,0 +1 @@
6 'var' is not allowed in old-style for loops if there is more than 1 initializer
@@ -0,0 +1,3 @@
1 The type var is deprecated
7 The type var is deprecated
8 The type var is deprecated
@@ -1 +1 @@
6 variable initializer is 'null'
5 variable initializer is 'null'
@@ -1 +1 @@
7 Use of var is flagged according to lombok configuration
6 Use of var is flagged according to lombok configuration.
+4
View File
@@ -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>
@@ -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>.