You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2018/07/31 11:30:43 UTC
[GitHub] geertjanw closed pull request #596: [NETBEANS-860] Hints: Convert
Lambda to Use 'var' Parameter Types
geertjanw closed pull request #596: [NETBEANS-860] Hints: Convert Lambda to Use 'var' Parameter Types
URL: https://github.com/apache/incubator-netbeans/pull/596
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/java.hints/src/org/netbeans/modules/java/hints/suggestions/Bundle.properties b/java.hints/src/org/netbeans/modules/java/hints/suggestions/Bundle.properties
index 05b90ec8f3..05bfda3f8c 100644
--- a/java.hints/src/org/netbeans/modules/java/hints/suggestions/Bundle.properties
+++ b/java.hints/src/org/netbeans/modules/java/hints/suggestions/Bundle.properties
@@ -103,3 +103,9 @@ DESC_ConstantNameHint=Checks that constant names follow the prescribed naming co
and certan predefined JDK classes, such as <b>java.awt.Color</b> and the like.\
<p/>\
You may extend the list of types treated as immutable values with your own classes.
+
+DN_ConvertVarLambdaParameters=Convert Lambda Parameter Type to 'var' Type
+DESC_ConvertVarLambdaParameters=Convert lambda explicit or implicit parameter type to 'var' type. \
+ The hint will be disable for mixed or error parameter type.
+FIX_ConvertVarLambdaParameters=Use var parameter types
+ERR_ConvertVarLambdaParameters=Use var parameter types
\ No newline at end of file
diff --git a/java.hints/src/org/netbeans/modules/java/hints/suggestions/Lambda.java b/java.hints/src/org/netbeans/modules/java/hints/suggestions/Lambda.java
index a05c84d082..41f4df58a6 100644
--- a/java.hints/src/org/netbeans/modules/java/hints/suggestions/Lambda.java
+++ b/java.hints/src/org/netbeans/modules/java/hints/suggestions/Lambda.java
@@ -43,7 +43,6 @@
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import org.netbeans.api.java.source.support.ErrorAwareTreePathScanner;
-import org.netbeans.api.java.source.support.ErrorAwareTreeScanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
@@ -67,8 +66,6 @@
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import org.netbeans.api.java.source.CompilationInfo;
-import org.netbeans.api.java.source.ElementUtilities.ElementAcceptor;
-import org.netbeans.api.java.source.GeneratorUtilities;
import org.netbeans.api.java.source.SourceUtils;
import org.netbeans.api.java.source.TreeMaker;
import org.netbeans.api.java.source.TreeUtilities;
@@ -83,6 +80,7 @@
import org.netbeans.spi.java.hints.JavaFixUtilities;
import org.netbeans.spi.java.hints.TriggerPattern;
import org.netbeans.spi.java.hints.TriggerTreeKind;
+import org.openide.util.NbBundle;
import org.openide.util.NbBundle.Messages;
/**
@@ -91,6 +89,8 @@
*/
public class Lambda {
+ private static final String[] LAMBDA_PARAMETER_ERROR_CODES = {"compiler.err.invalid.lambda.parameter.declaration"};// NOI18N
+
@Hint(displayName="#DN_lambda2Class", description="#DESC_lambda2Class", category="suggestions", hintKind=Hint.Kind.ACTION,
minSourceVersion = "8")
@Messages({
@@ -238,6 +238,29 @@ public static ErrorDescription explicitParameterTypes(HintContext ctx) {
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_addExplicitLambdaParameters(), new AddExplicitLambdaParameterTypes(ctx.getInfo(), ctx.getPath()).toEditorFix());
}
+ @Hint(displayName = "#DN_ConvertVarLambdaParameters", description = "#DESC_ConvertVarLambdaParameters", category = "suggestions", hintKind = Hint.Kind.ACTION, minSourceVersion = "11")
+ @TriggerTreeKind(Kind.LAMBDA_EXPRESSION)
+ public static ErrorDescription implicitVarParameterTypes(HintContext ctx) {
+ // hint will be enable only for JDK-11 or above.
+ if (ctx.getInfo().getSourceVersion().compareTo(SourceVersion.RELEASE_9) < 2) {
+ return null;
+ }
+ // Check invalid lambda parameter declaration
+ if (ctx.getInfo().getTreeUtilities().hasError(ctx.getPath().getLeaf(), LAMBDA_PARAMETER_ERROR_CODES)) {
+ return null;
+ }
+ // Check var parameter types
+ LambdaExpressionTree let = (LambdaExpressionTree) ctx.getPath().getLeaf();
+ if (let.getParameters() != null && let.getParameters().size() > 0) {
+ VariableTree var = let.getParameters().get(0);
+ if (ctx.getInfo().getTreeUtilities().isVarType(new TreePath(ctx.getPath(), var))) {
+ return null;
+ }
+ }
+
+ return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), NbBundle.getMessage(Lambda.class, "ERR_ConvertVarLambdaParameters"), new AddVarLambdaParameterTypes(ctx.getInfo(), ctx.getPath()).toEditorFix());
+ }
+
private static ExecutableElement findAbstractMethod(CompilationInfo info, TypeMirror type) {
if (type.getKind() != TypeKind.DECLARED) {
return null;
@@ -630,4 +653,27 @@ protected void performRewrite(TransformationContext ctx) throws Exception {
}
}
}
+
+ private static final class AddVarLambdaParameterTypes extends JavaFix {
+
+ public AddVarLambdaParameterTypes(CompilationInfo info, TreePath tp) {
+ super(info, tp);
+ }
+
+ @Override
+ protected String getText() {
+ return NbBundle.getMessage(Lambda.class, "FIX_ConvertVarLambdaParameters");
+ }
+
+ @Override
+ protected void performRewrite(JavaFix.TransformationContext ctx) throws Exception {
+ if (ctx.getPath().getLeaf().getKind() == Kind.LAMBDA_EXPRESSION) {
+ LambdaExpressionTree let = (LambdaExpressionTree) ctx.getPath().getLeaf();
+ TreeMaker make = ctx.getWorkingCopy().getTreeMaker();
+ let.getParameters().forEach((var) -> {
+ ctx.getWorkingCopy().rewrite(var.getType(), make.Type("var")); // NOI18N
+ });
+ }
+ }
+ }
}
diff --git a/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/LambdaTest.java b/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/LambdaTest.java
index 2b08967327..19e277e2a5 100644
--- a/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/LambdaTest.java
+++ b/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/LambdaTest.java
@@ -552,6 +552,58 @@ public void testExplicitParameterTypes() throws Exception {
"}\n");
}
+ //Todo: Verification is pending on nb-javac of JDK11
+ @Test
+ public void testImplicitVarParameterTypes1() throws Exception {
+ HintTest.create()
+ .setCaretMarker('^')
+ .input("package test;\n" +
+ "import java.util.function.IntBinaryOperator;\n" +
+ "public class Test {\n" +
+ " public void main(String list) {\n" +
+ " IntBinaryOperator calc3 = (int x, int y)^ -> x + y;\n" +
+ " }\n" +
+ "}\n")
+ .sourceLevel("1.11")
+ .run(Lambda.class)
+ .findWarning("4:48-4:48:verifier:ERR_ConvertVarLambdaParameters")
+ .applyFix()
+ .assertCompilable()
+ .assertVerbatimOutput("package test;\n" +
+ "import java.util.function.IntBinaryOperator;\n" +
+ "public class Test {\n" +
+ " public void main(List<String> list) {\n" +
+ " IntBinaryOperator calc3 = (var x, var y) -> x + y;\n" +
+ " }\n" +
+ "}\n");
+ }
+
+ //Todo: Verification is pending on nb-javac of JDK11
+ @Test
+ public void testImplicitVarParameterTypes2() throws Exception {
+ HintTest.create()
+ .setCaretMarker('^')
+ .input("package test;\n" +
+ "import java.util.function.IntBinaryOperator;\n" +
+ "public class Test {\n" +
+ " public void main(String list) {\n" +
+ " IntBinaryOperator calc3 = (x, y)^ -> x + y;\n" +
+ " }\n" +
+ "}\n")
+ .sourceLevel("1.11")
+ .run(Lambda.class)
+ .findWarning("4:48-4:48:verifier:ERR_ConvertVarLambdaParameters")
+ .applyFix()
+ .assertCompilable()
+ .assertVerbatimOutput("package test;\n" +
+ "import java.util.function.IntBinaryOperator;\n" +
+ "public class Test {\n" +
+ " public void main(List<String> list) {\n" +
+ " IntBinaryOperator calc3 = (var x, var y) -> x + y;\n" +
+ " }\n" +
+ "}\n");
+ }
+
static {
JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = true;
}
diff --git a/java.source.base/apichanges.xml b/java.source.base/apichanges.xml
index 84c22cbf5a..c7a9dac1df 100644
--- a/java.source.base/apichanges.xml
+++ b/java.source.base/apichanges.xml
@@ -38,6 +38,18 @@
</description>
<class name="TreeUtilities" package="org.netbeans.api.java.source"/>
</change>
+ <change id="TreeUtilities.isTreeHasError">
+ <api name="javasource_base"/>
+ <summary>Check the tree has compile error in given collection of errors.</summary>
+ <version major="1" minor="2.37"/>
+ <date day="18" month="7" year="2018"/>
+ <author login="vikasprabhakar"/>
+ <compatibility addition="yes" binary="compatible" source="compatible"/>
+ <description>
+ Check the tree has compile error in given collection of errors.
+ </description>
+ <class name="TreeUtilities" package="org.netbeans.api.java.source"/>
+ </change>
<change id="TreeUtilities.isVarType">
<api name="javasource_base"/>
<summary>Check the var type variable in given tree path.</summary>
diff --git a/java.source.base/nbproject/project.properties b/java.source.base/nbproject/project.properties
index 7bcd00fca1..c5632e3cc5 100644
--- a/java.source.base/nbproject/project.properties
+++ b/java.source.base/nbproject/project.properties
@@ -23,7 +23,7 @@ javadoc.name=Java Source Base
javadoc.title=Java Source Base
javadoc.arch=${basedir}/arch.xml
javadoc.apichanges=${basedir}/apichanges.xml
-spec.version.base=2.36.0
+spec.version.base=2.37.0
test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.jar
test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
${o.n.core.dir}/lib/boot.jar:\
diff --git a/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java b/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
index b6b8d51ddd..1f89767eac 100644
--- a/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
+++ b/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
@@ -1927,6 +1927,35 @@ public boolean isPartOfCompoundVariableDeclaration(@NonNull Tree tree) {
return false;
}
+ /**
+ * Check the tree has compile error in given errors.
+ *
+ * @param tree compilation tree
+ * @param errors Array of error code
+ * @return true if tree has compile error present in list of errors.
+ * @since 2.37
+ */
+ public boolean hasError(@NonNull Tree tree, String... errors) {
+ long startPos = info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), tree);
+ long endPos = info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), tree);
+
+ List<Diagnostic> diagnosticsList = info.getDiagnostics();
+ for (Diagnostic d : diagnosticsList) {
+ if ((d.getKind() == Diagnostic.Kind.ERROR) && ((d.getStartPosition() >= startPos) && (d.getEndPosition() <= endPos))) {
+ if (errors == null || errors.length == 0) {
+ return true;
+ } else {
+ for (String error : errors) {
+ if (error.equals(d.getCode())) {
+ return true;
+ }
+ }
+ }
+ }
+ }
+ return false;
+ }
+
private static final class NBScope implements Scope {
private final JavacScope delegate;
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists