[java-stubs] Reuse ClassReader for both passes

This reduces the necessity to parse UTF8 constant pool items

GitOrigin-RevId: 2f0ed72260579ce66874eee614c9b7b11f6eab3a
This commit is contained in:
Tagir Valeev
2023-10-20 14:26:39 +02:00
committed by intellij-monorepo-bot
parent c8befeda75
commit d2a5a885fc
2 changed files with 13 additions and 14 deletions

View File

@@ -575,7 +575,7 @@ public class ClsFileImpl extends PsiBinaryFileImpl
else {
PsiJavaFileStub stub = new PsiJavaFileStubImpl(null, getPackageName(internalName), level, true);
try {
FileContentPair source = new FileContentPair(file, bytes);
FileContentPair source = new FileContentPair(file, reader);
StubBuildingVisitor<FileContentPair> visitor = new StubBuildingVisitor<>(source, STRATEGY, stub, 0, className);
reader.accept(visitor, EMPTY_ATTRIBUTES, ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE | ClassReader.VISIT_LOCAL_VARIABLES);
if (visitor.getResult() != null) return stub;
@@ -600,12 +600,12 @@ public class ClsFileImpl extends PsiBinaryFileImpl
return p > 0 ? internalName.substring(0, p).replace('/', '.') : "";
}
static class FileContentPair extends Pair<VirtualFile, byte[]> {
FileContentPair(@NotNull VirtualFile file, byte @NotNull [] content) {
static class FileContentPair extends Pair<VirtualFile, ClassReader> {
FileContentPair(@NotNull VirtualFile file, @NotNull ClassReader content) {
super(file, content);
}
public byte @NotNull [] getContent() {
public @NotNull ClassReader getContent() {
return second;
}
@@ -625,7 +625,7 @@ public class ClsFileImpl extends PsiBinaryFileImpl
if (innerClass != null) {
try {
byte[] bytes = innerClass.contentsToByteArray(false);
return new FileContentPair(innerClass, bytes);
return new FileContentPair(innerClass, new ClassReader(bytes));
}
catch (IOException ignored) { }
}
@@ -635,7 +635,7 @@ public class ClsFileImpl extends PsiBinaryFileImpl
@Override
public void accept(FileContentPair innerClass, StubBuildingVisitor<FileContentPair> visitor) {
try {
new ClassReader(innerClass.second).accept(visitor, EMPTY_ATTRIBUTES, ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE | ClassReader.VISIT_LOCAL_VARIABLES);
innerClass.second.accept(visitor, EMPTY_ATTRIBUTES, ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE | ClassReader.VISIT_LOCAL_VARIABLES);
}
catch (Exception e) { // workaround for bug in skipping annotations when a first parameter of inner class is dropped (IDEA-204145)
VirtualFile file = innerClass.first;

View File

@@ -6,7 +6,6 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.impl.cache.TypeInfo;
import com.intellij.psi.impl.cache.TypeInfo.RefTypeInfo;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
@@ -140,26 +139,26 @@ class FirstPassData implements SignatureParsing.TypeInfoProvider {
}
static @NotNull FirstPassData create(Object classSource) {
byte[] bytes = null;
ClassReader reader = null;
if (classSource instanceof ClsFileImpl.FileContentPair) {
bytes = ((ClsFileImpl.FileContentPair)classSource).getContent();
reader = ((ClsFileImpl.FileContentPair)classSource).getContent();
}
else if (classSource instanceof VirtualFile) {
try {
bytes = ((VirtualFile)classSource).contentsToByteArray(false);
reader = new ClassReader(((VirtualFile)classSource).contentsToByteArray(false));
}
catch (IOException ignored) {
}
}
if (bytes != null) {
return fromClassBytes(bytes);
if (reader != null) {
return fromReader(reader);
}
return NO_DATA;
}
private static @NotNull FirstPassData fromClassBytes(byte[] classBytes) {
private static @NotNull FirstPassData fromReader(@NotNull ClassReader reader) {
class FirstPassVisitor extends ClassVisitor {
final Map<String, InnerClassEntry> mapping = new HashMap<>();
@@ -246,7 +245,7 @@ class FirstPassData implements SignatureParsing.TypeInfoProvider {
FirstPassVisitor visitor = new FirstPassVisitor();
try {
new ClassReader(classBytes).accept(visitor, ClsFileImpl.EMPTY_ATTRIBUTES, ClassReader.SKIP_FRAMES);
reader.accept(visitor, ClsFileImpl.EMPTY_ATTRIBUTES, ClassReader.SKIP_FRAMES);
}
catch (Exception ex) {
LOG.debug(ex);