Repository: incubator-netbeans Updated Branches: refs/heads/jdk18_3 [created] 49a172a3e
Intial support for local variable type inference. Project: http://git-wip-us.apache.org/repos/asf/incubator-netbeans/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-netbeans/commit/49a172a3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-netbeans/tree/49a172a3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-netbeans/diff/49a172a3 Branch: refs/heads/jdk18_3 Commit: 49a172a3eef7c26bae0b08d25121a178cc38f5d0 Parents: 4e662ec Author: Jan Lahoda <[email protected]> Authored: Mon Oct 9 20:57:56 2017 +0200 Committer: Jan Lahoda <[email protected]> Committed: Mon Oct 9 20:57:56 2017 +0200 ---------------------------------------------------------------------- .../java/api/common/ui/PlatformUiSupport.java | 2 +- .../java/completion/JavaCompletionTask.java | 2 +- .../JavaCompletionTaskTest/10/emptyVar.pass | 138 +++++++++++++++++++ .../JavaCompletionTask110FeaturesTest.java | 50 +++++++ .../modules/editor/java/GoToSupportTest.java | 41 ++++++ java.lexer/manifest.mf | 2 +- .../netbeans/api/java/lexer/JavaTokenId.java | 2 + .../org/netbeans/lib/java/lexer/JavaLexer.java | 64 +++++++-- .../lib/java/lexer/JavaLexerBatchTest.java | 17 +++ 9 files changed, 300 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/49a172a3/java.api.common/src/org/netbeans/modules/java/api/common/ui/PlatformUiSupport.java ---------------------------------------------------------------------- diff --git a/java.api.common/src/org/netbeans/modules/java/api/common/ui/PlatformUiSupport.java b/java.api.common/src/org/netbeans/modules/java/api/common/ui/PlatformUiSupport.java index 26a6b13..7d49102 100644 --- a/java.api.common/src/org/netbeans/modules/java/api/common/ui/PlatformUiSupport.java +++ b/java.api.common/src/org/netbeans/modules/java/api/common/ui/PlatformUiSupport.java @@ -1050,7 +1050,7 @@ public final class PlatformUiSupport { private static int minor(@NonNull final SpecificationVersion specVer) { final String s = specVer.toString(); final int split = s.indexOf('.'); //NOI18N - return split < 0 ? 0 : Integer.parseInt(s.substring(split+1)); + return split < 0 ? -1 : Integer.parseInt(s.substring(split+1)); } private static int major(@NonNull final SpecificationVersion specVer) { http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/49a172a3/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java ---------------------------------------------------------------------- diff --git a/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java b/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java index 234e0b3..7bcf933 100644 --- a/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java +++ b/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java @@ -4910,7 +4910,7 @@ public final class JavaCompletionTask<T> extends BaseTask { Tree tree = path.getLeaf(); switch (tree.getKind()) { case VARIABLE: - TypeMirror type = controller.getTrees().getTypeMirror(new TreePath(path, ((VariableTree) tree).getType())); + TypeMirror type = controller.getTrees().getTypeMirror(path); if (type == null) { return null; } http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/49a172a3/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/emptyVar.pass ---------------------------------------------------------------------- diff --git a/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/emptyVar.pass b/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/emptyVar.pass new file mode 100644 index 0000000..2cad701 --- /dev/null +++ b/java.completion/test/unit/data/goldenfiles/org/netbeans/modules/java/completion/JavaCompletionTaskTest/10/emptyVar.pass @@ -0,0 +1,138 @@ +int a +boolean b +protected native Object clone() +public boolean equals(Object arg0) +protected void finalize() +public final native Class<?> getClass() +public native int hashCode() +public final native void notify() +public final native void notifyAll() +public void op(int a, boolean b) +public String toString() +public final void wait() +public final native void wait(long arg0) +public final void wait(long arg0, int arg1) +boolean +byte +char +double +false +float +int +long +new +null +short +super +this +true +AbstractMethodError +Appendable +ArithmeticException +ArrayIndexOutOfBoundsException +ArrayStoreException +AssertionError +AutoCloseable +Boolean +BootstrapMethodError +Byte +CharSequence +Character +Class +ClassCastException +ClassCircularityError +ClassFormatError +ClassLoader +ClassNotFoundException +ClassValue +CloneNotSupportedException +Cloneable +Comparable +Compiler +Deprecated +Double +Enum +EnumConstantNotPresentException +Error +Exception +ExceptionInInitializerError +Float +FunctionalInterface +IllegalAccessError +IllegalAccessException +IllegalArgumentException +IllegalCallerException +IllegalMonitorStateException +IllegalStateException +IllegalThreadStateException +IncompatibleClassChangeError +IndexOutOfBoundsException +InheritableThreadLocal +InstantiationError +InstantiationException +Integer +InternalError +InterruptedException +Iterable +LayerInstantiationException +LinkageError +Long +Math +Module +ModuleLayer +NegativeArraySizeException +NoClassDefFoundError +NoSuchFieldError +NoSuchFieldException +NoSuchMethodError +NoSuchMethodException +NullPointerException +Number +NumberFormatException +Object +OutOfMemoryError +Override +Package +Process +ProcessBuilder +ProcessHandle +Readable +ReflectiveOperationException +Runnable +Runtime +RuntimeException +RuntimePermission +SafeVarargs +SecurityException +SecurityManager +Short +StackOverflowError +StackTraceElement +StackWalker +StrictMath +String +StringBuffer +StringBuilder +StringIndexOutOfBoundsException +SuppressWarnings +System +Test +Thread +ThreadDeath +ThreadGroup +ThreadLocal +Throwable +TypeNotPresentException +UnknownError +UnsatisfiedLinkError +UnsupportedClassVersionError +UnsupportedOperationException +VerifyError +VirtualMachineError +Void +com +java +javax +netscape +org +sun http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/49a172a3/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask110FeaturesTest.java ---------------------------------------------------------------------- diff --git a/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask110FeaturesTest.java b/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask110FeaturesTest.java new file mode 100644 index 0000000..57a9f74 --- /dev/null +++ b/java.completion/test/unit/src/org/netbeans/modules/java/completion/JavaCompletionTask110FeaturesTest.java @@ -0,0 +1,50 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.java.completion; + +import javax.lang.model.SourceVersion; +import org.netbeans.modules.java.source.parsing.JavacParser; + +/** + * + * @author Dusan Balek + */ +public class JavaCompletionTask110FeaturesTest extends CompletionTestBase { + + public JavaCompletionTask110FeaturesTest(String testName) { + super(testName); + } + + // Java 1.10 var tests ------------------------------------------- + + public void testVar() throws Exception { + try { + SourceVersion.valueOf("RELEASE_10"); + } catch (IllegalArgumentException ex) { + //OK, no RELEASE_10, skip test: + return ; + } + //TODO: should not propose "null"! + performTest("Method", 125, "var v =", "emptyVar.pass", "1.10"); + } + + static { + JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = true; + } +} http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/49a172a3/java.editor/test/unit/src/org/netbeans/modules/editor/java/GoToSupportTest.java ---------------------------------------------------------------------- diff --git a/java.editor/test/unit/src/org/netbeans/modules/editor/java/GoToSupportTest.java b/java.editor/test/unit/src/org/netbeans/modules/editor/java/GoToSupportTest.java index bf44269..0ec9688 100644 --- a/java.editor/test/unit/src/org/netbeans/modules/editor/java/GoToSupportTest.java +++ b/java.editor/test/unit/src/org/netbeans/modules/editor/java/GoToSupportTest.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.IOException; import java.util.concurrent.CountDownLatch; import java.util.regex.Pattern; +import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.ElementKind; @@ -1008,6 +1009,46 @@ public class GoToSupportTest extends NbTestCase { assertEquals(golden, tooltip); } + public void testVar() throws Exception { + try { + SourceVersion.valueOf("RELEASE_10"); + } catch (IllegalArgumentException ex) { + //OK, no RELEASE_10, skip test: + return ; + } + final boolean[] wasCalled = new boolean[1]; + this.sourceLevel = "1.10"; + final String code = "package test;\n" + + "public class Test {\n" + + " private static void method() {\n" + + " var var = 0;\n" + + " int i = va|r;\n" + + " }\n" + + "}\n"; + + performTest(code, new UiUtilsCaller() { + @Override public boolean open(FileObject fo, int pos) { + assertTrue(source == fo); + assertEquals(code.indexOf("var var = 0;"), pos); + wasCalled[0] = true; + return true; + } + + @Override public void beep(boolean goToSource, boolean goToJavadoc) { + fail("Should not be called."); + } + @Override public boolean open(ClasspathInfo info, ElementHandle<?> el) { + fail("Should not be called."); + return true; + } + @Override public void warnCannotOpen(String displayName) { + fail("Should not be called."); + } + }, false, false); + + assertTrue(wasCalled[0]); + } + private String sourceLevel = "1.5"; private FileObject source; http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/49a172a3/java.lexer/manifest.mf ---------------------------------------------------------------------- diff --git a/java.lexer/manifest.mf b/java.lexer/manifest.mf index 0515a35..56e3b1c 100644 --- a/java.lexer/manifest.mf +++ b/java.lexer/manifest.mf @@ -1,5 +1,5 @@ OpenIDE-Module: org.netbeans.modules.java.lexer/1 OpenIDE-Module-Localizing-Bundle: org/netbeans/lib/java/lexer/Bundle.properties -OpenIDE-Module-Specification-Version: 1.35 +OpenIDE-Module-Specification-Version: 1.36 OpenIDE-Module-Layer: org/netbeans/lib/java/lexer/layer.xml http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/49a172a3/java.lexer/src/org/netbeans/api/java/lexer/JavaTokenId.java ---------------------------------------------------------------------- diff --git a/java.lexer/src/org/netbeans/api/java/lexer/JavaTokenId.java b/java.lexer/src/org/netbeans/api/java/lexer/JavaTokenId.java index c1e398b..1e08c52 100644 --- a/java.lexer/src/org/netbeans/api/java/lexer/JavaTokenId.java +++ b/java.lexer/src/org/netbeans/api/java/lexer/JavaTokenId.java @@ -113,6 +113,8 @@ public enum JavaTokenId implements TokenId { UNDERSCORE("_", "keyword"), /**@since 1.34*/ USES("uses", "keyword"), + /**@since 1.36*/ + VAR("var", "keyword"), VOID("void", "keyword"), VOLATILE("volatile", "keyword"), WHILE("while", "keyword-directive"), http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/49a172a3/java.lexer/src/org/netbeans/lib/java/lexer/JavaLexer.java ---------------------------------------------------------------------- diff --git a/java.lexer/src/org/netbeans/lib/java/lexer/JavaLexer.java b/java.lexer/src/org/netbeans/lib/java/lexer/JavaLexer.java index baa31fe..e2d714e 100644 --- a/java.lexer/src/org/netbeans/lib/java/lexer/JavaLexer.java +++ b/java.lexer/src/org/netbeans/lib/java/lexer/JavaLexer.java @@ -1023,21 +1023,55 @@ public class JavaLexer implements Lexer<JavaTokenId> { return finishIdentifier(c); case 'v': - if ((c = nextChar()) == 'o') { - switch (c = nextChar()) { - case 'i': - if ((c = nextChar()) == 'd') - return keywordOrIdentifier(JavaTokenId.VOID); - break; - case 'l': - if ((c = nextChar()) == 'a' - && (c = nextChar()) == 't' - && (c = nextChar()) == 'i' - && (c = nextChar()) == 'l' - && (c = nextChar()) == 'e') - return keywordOrIdentifier(JavaTokenId.VOLATILE); - break; - } + switch ((c = nextChar())) { + case 'a': + if ((c = nextChar()) == 'r') { + c = nextChar(); + // Check whether the given char is non-ident and if so then return keyword + if (c == EOF || !Character.isJavaIdentifierPart(c = translateSurrogates(c))) { + // For surrogate 2 chars must be backed up + backup((c >= Character.MIN_SUPPLEMENTARY_CODE_POINT) ? 2 : 1); + + int len = input.readLength(); + + Token next = nextToken(); + boolean varKeyword = false; + + if (next.id() == JavaTokenId.BLOCK_COMMENT || + next.id() == JavaTokenId.JAVADOC_COMMENT || + next.id() == JavaTokenId.LINE_COMMENT || + next.id() == JavaTokenId.WHITESPACE) { + next = nextToken(); + + varKeyword = next.id() == JavaTokenId.IDENTIFIER; + } + + input.backup(input.readLengthEOF()- len); + + assert input.readLength() == len; + + if (varKeyword) + return token(JavaTokenId.VAR); + } + } + c = nextChar(); + break; + case 'o': + switch (c = nextChar()) { + case 'i': + if ((c = nextChar()) == 'd') + return keywordOrIdentifier(JavaTokenId.VOID); + break; + case 'l': + if ((c = nextChar()) == 'a' + && (c = nextChar()) == 't' + && (c = nextChar()) == 'i' + && (c = nextChar()) == 'l' + && (c = nextChar()) == 'e') + return keywordOrIdentifier(JavaTokenId.VOLATILE); + break; + } + break; } return finishIdentifier(c); http://git-wip-us.apache.org/repos/asf/incubator-netbeans/blob/49a172a3/java.lexer/test/unit/src/org/netbeans/lib/java/lexer/JavaLexerBatchTest.java ---------------------------------------------------------------------- diff --git a/java.lexer/test/unit/src/org/netbeans/lib/java/lexer/JavaLexerBatchTest.java b/java.lexer/test/unit/src/org/netbeans/lib/java/lexer/JavaLexerBatchTest.java index 427227f..84785c4 100644 --- a/java.lexer/test/unit/src/org/netbeans/lib/java/lexer/JavaLexerBatchTest.java +++ b/java.lexer/test/unit/src/org/netbeans/lib/java/lexer/JavaLexerBatchTest.java @@ -519,4 +519,21 @@ public class JavaLexerBatchTest extends TestCase { LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.ERROR, "\\"); LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.IDENTIFIER, "u00"); } + + public void testVar() { + String text = "var var = 0;"; + InputAttributes attr = new InputAttributes(); + attr.setValue(JavaTokenId.language(), "version", Integer.valueOf(10), true); + TokenHierarchy<?> hi = TokenHierarchy.create(text, false, JavaTokenId.language(), EnumSet.noneOf(JavaTokenId.class), attr); + TokenSequence<?> ts = hi.tokenSequence(); + + LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.VAR, "var"); + LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.WHITESPACE, " "); + LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.IDENTIFIER, "var"); + LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.WHITESPACE, " "); + LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.EQ, "="); + LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.WHITESPACE, " "); + LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.INT_LITERAL, "0"); + LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.SEMICOLON, ";"); + } }
