aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok
diff options
context:
space:
mode:
authorReinier Zwitserloot <reinier@zwitserloot.com>2018-02-06 22:12:42 +0100
committerReinier Zwitserloot <reinier@zwitserloot.com>2018-02-07 00:01:22 +0100
commit66469e04fe35e5ceb3723cb563379a03e4883101 (patch)
treea457e2973556c795a7879279b9c39f4ed5f7cb42 /src/core/lombok
parent55bcc142d08ac8a4de0c3965333e3816c496799f (diff)
downloadlombok-66469e04fe35e5ceb3723cb563379a03e4883101.tar.gz
lombok-66469e04fe35e5ceb3723cb563379a03e4883101.tar.bz2
lombok-66469e04fe35e5ceb3723cb563379a03e4883101.zip
[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.
Diffstat (limited to 'src/core/lombok')
-rw-r--r--src/core/lombok/core/LombokInternalAliasing.java1
-rw-r--r--src/core/lombok/core/configuration/AllowHelper.java6
-rw-r--r--src/core/lombok/eclipse/handlers/HandleVal.java14
-rw-r--r--src/core/lombok/experimental/var.java5
-rw-r--r--src/core/lombok/javac/handlers/HandleVal.java12
-rw-r--r--src/core/lombok/var.java35
6 files changed, 62 insertions, 11 deletions
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 &#64;var</a>.
+ */
+public @interface var {
+}