Skip to content

Commit

Permalink
[NETBEANS-4062] Fixed issue : import RECORD from other package hint
Browse files Browse the repository at this point in the history
  • Loading branch information
singh-akhilesh authored and lkishalmi committed Sep 13, 2020
1 parent 956559f commit 99d1aa6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,25 @@ public void testJIRA2914c() throws Exception {
"",
"");
}

public void testRecordImport() throws Exception {
doTest("test/Test",
"14",
Arrays.asList(
new FileData("test/Test.java",
"package test;\n"
+ "import mytest.test.MyRecord;\n"
+ "public class Test {\n"
+ " MyRecord rec;\n"
+ "}\n"),
new FileData("mytest/test/MyRecord.java",
"package mytest.test;\n"
+ "public record MyRecord() {\n"
+ "}\n")
),
"",
"");
}

private void prepareTest(String capitalizedName, String sourceLevel, Iterable<FileData> files) throws Exception {
FileObject workFO = FileUtil.toFileObject(getWorkDir());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,12 @@ public ElementHandle create(ElementKind kind, String... descriptors) {
}
return new ElementHandle<VariableElement> (kind, descriptors);
default:
throw new IllegalArgumentException ();
if (kind.name().equals(TreeShims.RECORD) && (descriptors.length == 1)) {
return new ElementHandle<TypeElement>(kind, descriptors);
} else {
throw new IllegalArgumentException();
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
import org.netbeans.api.lexer.TokenSequence;
import org.netbeans.api.queries.FileEncodingQuery;
import org.netbeans.api.scripting.Scripting;
import org.netbeans.modules.java.source.TreeShims;
import org.netbeans.modules.java.source.builder.CommentHandlerService;
import org.netbeans.modules.java.source.builder.CommentSetImpl;
import org.netbeans.modules.java.source.parsing.AbstractSourceFileObject;
Expand Down Expand Up @@ -1142,7 +1143,13 @@ public CompilationUnitTree addImports(CompilationUnitTree cut, Set<? extends Ele
el = e.getEnclosingElement();
break;
default:
assert false : "Illegal element kind: " + e.getKind(); //NOI18N
if (TreeShims.isRecord(e)) {
if (e.getEnclosingElement().getKind() == ElementKind.PACKAGE) {
el = e.getEnclosingElement();
}
} else {
assert false : "Illegal element kind: " + e.getKind(); //NOI18N
}
}
if (el != null) {
Integer cnt = isStatic ? typeCounts.get((TypeElement)el) : pkgCounts.get((PackageElement)el);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -50,6 +52,7 @@
import org.netbeans.api.java.source.ClassIndex;
import org.netbeans.api.java.source.ElementHandle;
import org.netbeans.modules.java.source.ElementHandleAccessor;
import org.netbeans.modules.java.source.TreeShims;
import org.netbeans.modules.java.source.parsing.FileObjects;
import org.netbeans.modules.parsing.lucene.support.Convertor;
import org.netbeans.modules.parsing.lucene.support.Convertors;
Expand Down Expand Up @@ -115,12 +118,18 @@ public static Convertor<Document,FileObject> fileObjectConvertor (
}

@NonNull
public static Convertor<Document,ElementHandle<TypeElement>> typeElementConvertor() {
return new ElementHandleConvertor<> (
ElementKind.CLASS,
ElementKind.ENUM,
ElementKind.INTERFACE,
ElementKind.ANNOTATION_TYPE);
public static Convertor<Document, ElementHandle<TypeElement>> typeElementConvertor() {
ElementKind recordKind = TreeShims.getRecordKind();

List<ElementKind> eleKindList = new ArrayList<>();
ElementKind[] otherElekinds = {ElementKind.ENUM, ElementKind.INTERFACE, ElementKind.ANNOTATION_TYPE};
eleKindList.addAll(Arrays.asList(otherElekinds));
if (recordKind != null) {
eleKindList.add(recordKind);
}
return new ElementHandleConvertor<>(
ElementKind.CLASS, eleKindList.toArray(new ElementKind[0]));

}

@NonNull
Expand Down

0 comments on commit 99d1aa6

Please sign in to comment.