When building light classes don't resolve irrelevant declarations in the same package
This commit is contained in:
committed by
Nikolay Krasko
parent
783258d91a
commit
623b169d71
+14
-13
@@ -103,22 +103,23 @@ public class CliLightClassGenerationSupport extends LightClassGenerationSupport
|
|||||||
module = null;
|
module = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private LightClassConstructionContext analyzeRelevantCode() {
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public LightClassConstructionContext getContextForPackage(@NotNull Collection<JetFile> files) {
|
||||||
|
return getContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public LightClassConstructionContext getContextForClassOrObject(@NotNull JetClassOrObject classOrObject) {
|
||||||
|
return getContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private LightClassConstructionContext getContext() {
|
||||||
return new LightClassConstructionContext(getTrace().getBindingContext(), null);
|
return new LightClassConstructionContext(getTrace().getBindingContext(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public LightClassConstructionContext analyzeRelevantCode(@NotNull Collection<JetFile> files) {
|
|
||||||
return analyzeRelevantCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public LightClassConstructionContext analyzeRelevantCode(@NotNull JetClassOrObject classOrObject) {
|
|
||||||
return analyzeRelevantCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Collection<JetClassOrObject> findClassOrObjectDeclarations(@NotNull FqName fqName, @NotNull GlobalSearchScope searchScope) {
|
public Collection<JetClassOrObject> findClassOrObjectDeclarations(@NotNull FqName fqName, @NotNull GlobalSearchScope searchScope) {
|
||||||
|
|||||||
+9
-9
@@ -70,8 +70,8 @@ public class KotlinJavaFileStubProvider implements CachedValueProvider<LightClas
|
|||||||
new StubGenerationStrategy.NoDeclaredClasses() {
|
new StubGenerationStrategy.NoDeclaredClasses() {
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public LightClassConstructionContext createLightClassConstructionContext(@NotNull Collection<JetFile> files) {
|
public LightClassConstructionContext getContext(@NotNull Collection<JetFile> files) {
|
||||||
return LightClassGenerationSupport.getInstance(project).analyzeRelevantCode(files);
|
return LightClassGenerationSupport.getInstance(project).getContextForPackage(files);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -116,8 +116,8 @@ public class KotlinJavaFileStubProvider implements CachedValueProvider<LightClas
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public LightClassConstructionContext createLightClassConstructionContext(@NotNull Collection<JetFile> files) {
|
public LightClassConstructionContext getContext(@NotNull Collection<JetFile> files) {
|
||||||
return LightClassGenerationSupport.getInstance(classOrObject.getProject()).analyzeRelevantCode(classOrObject);
|
return LightClassGenerationSupport.getInstance(classOrObject.getProject()).getContextForClassOrObject(classOrObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -201,7 +201,7 @@ public class KotlinJavaFileStubProvider implements CachedValueProvider<LightClas
|
|||||||
|
|
||||||
checkForBuiltIns(packageFqName, files);
|
checkForBuiltIns(packageFqName, files);
|
||||||
|
|
||||||
LightClassConstructionContext context = stubGenerationStrategy.createLightClassConstructionContext(files);
|
LightClassConstructionContext context = stubGenerationStrategy.getContext(files);
|
||||||
Throwable error = context.getError();
|
Throwable error = context.getError();
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
throw new IllegalStateException("failed to analyze: " + error, error);
|
throw new IllegalStateException("failed to analyze: " + error, error);
|
||||||
@@ -242,7 +242,7 @@ public class KotlinJavaFileStubProvider implements CachedValueProvider<LightClas
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Result.create(
|
return Result.<LightClassStubWithData>create(
|
||||||
stubGenerationStrategy.createLightClassStubWithData(javaFileStub, bindingContext),
|
stubGenerationStrategy.createLightClassStubWithData(javaFileStub, bindingContext),
|
||||||
local ? PsiModificationTracker.MODIFICATION_COUNT : PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT
|
local ? PsiModificationTracker.MODIFICATION_COUNT : PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT
|
||||||
);
|
);
|
||||||
@@ -303,11 +303,11 @@ public class KotlinJavaFileStubProvider implements CachedValueProvider<LightClas
|
|||||||
}
|
}
|
||||||
|
|
||||||
private interface StubGenerationStrategy {
|
private interface StubGenerationStrategy {
|
||||||
@NotNull LightClassConstructionContext createLightClassConstructionContext(@NotNull Collection<JetFile> files);
|
|
||||||
@NotNull
|
|
||||||
LightClassStubWithData createLightClassStubWithData(PsiJavaFileStub javaFileStub, BindingContext bindingContext);
|
|
||||||
@NotNull Collection<JetFile> getFiles();
|
@NotNull Collection<JetFile> getFiles();
|
||||||
@NotNull FqName getPackageFqName();
|
@NotNull FqName getPackageFqName();
|
||||||
|
@NotNull LightClassConstructionContext getContext(@NotNull Collection<JetFile> files);
|
||||||
|
@NotNull LightClassStubWithData createLightClassStubWithData(PsiJavaFileStub javaFileStub, BindingContext bindingContext);
|
||||||
|
|
||||||
boolean generateDeclaredClasses();
|
boolean generateDeclaredClasses();
|
||||||
void generate(@NotNull GenerationState state, @NotNull Collection<JetFile> files);
|
void generate(@NotNull GenerationState state, @NotNull Collection<JetFile> files);
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -36,10 +36,10 @@ public abstract class LightClassGenerationSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public abstract LightClassConstructionContext analyzeRelevantCode(@NotNull Collection<JetFile> files);
|
public abstract LightClassConstructionContext getContextForPackage(@NotNull Collection<JetFile> files);
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public abstract LightClassConstructionContext analyzeRelevantCode(@NotNull JetClassOrObject classOrObject);
|
public abstract LightClassConstructionContext getContextForClassOrObject(@NotNull JetClassOrObject classOrObject);
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public abstract Collection<JetClassOrObject> findClassOrObjectDeclarations(@NotNull FqName fqName, @NotNull GlobalSearchScope searchScope);
|
public abstract Collection<JetClassOrObject> findClassOrObjectDeclarations(@NotNull FqName fqName, @NotNull GlobalSearchScope searchScope);
|
||||||
|
|||||||
@@ -97,9 +97,7 @@ public class ModuleDescriptorImpl extends DeclarationDescriptorImpl implements M
|
|||||||
@Override
|
@Override
|
||||||
public PackageViewDescriptor getPackage(@NotNull FqName fqName) {
|
public PackageViewDescriptor getPackage(@NotNull FqName fqName) {
|
||||||
List<PackageFragmentDescriptor> fragments = packageFragmentProvider.getPackageFragments(fqName);
|
List<PackageFragmentDescriptor> fragments = packageFragmentProvider.getPackageFragments(fqName);
|
||||||
return !fragments.isEmpty()
|
return !fragments.isEmpty() ? new PackageViewDescriptorImpl(this, fqName, fragments) : null;
|
||||||
? new PackageViewDescriptorImpl(this, fqName, fragments)
|
|
||||||
: null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
+36
-15
@@ -85,7 +85,7 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public LightClassConstructionContext analyzeRelevantCode(@NotNull Collection<JetFile> files) {
|
public LightClassConstructionContext getContextForPackage(@NotNull Collection<JetFile> files) {
|
||||||
if (files.isEmpty()) {
|
if (files.isEmpty()) {
|
||||||
return new LightClassConstructionContext(BindingContext.EMPTY, null);
|
return new LightClassConstructionContext(BindingContext.EMPTY, null);
|
||||||
}
|
}
|
||||||
@@ -97,7 +97,7 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport
|
|||||||
try {
|
try {
|
||||||
if (USE_LAZY) {
|
if (USE_LAZY) {
|
||||||
ResolveSessionForBodies session = AnalyzerFacadeWithCache.getLazyResolveSessionForFile(sortedFiles.get(0));
|
ResolveSessionForBodies session = AnalyzerFacadeWithCache.getLazyResolveSessionForFile(sortedFiles.get(0));
|
||||||
forceResolveRelevantDeclarations(files, session);
|
forceResolvePackageDeclarations(files, session);
|
||||||
return new LightClassConstructionContext(session.getBindingContext(), null);
|
return new LightClassConstructionContext(session.getBindingContext(), null);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -111,7 +111,36 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void forceResolveRelevantDeclarations(@NotNull Collection<JetFile> files, @NotNull KotlinCodeAnalyzer session) {
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public LightClassConstructionContext getContextForClassOrObject(@NotNull JetClassOrObject classOrObject) {
|
||||||
|
Profiler p = Profiler.create((USE_LAZY ? "lazy" : "eager") + " analyze", LOG).start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (USE_LAZY) {
|
||||||
|
ResolveSessionForBodies session = AnalyzerFacadeWithCache.getLazyResolveSessionForFile((JetFile) classOrObject.getContainingFile());
|
||||||
|
|
||||||
|
if (JetPsiUtil.isLocal(classOrObject)) {
|
||||||
|
BindingContext bindingContext = session.resolveToElement(classOrObject);
|
||||||
|
forceResolveAllContents(bindingContext.get(BindingContext.CLASS, classOrObject));
|
||||||
|
|
||||||
|
return new LightClassConstructionContext(bindingContext, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
forceResolveAllContents(session.getClassDescriptor(classOrObject));
|
||||||
|
return new LightClassConstructionContext(session.getBindingContext(), null);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
KotlinCacheManager cacheManager = KotlinCacheManager.getInstance(project);
|
||||||
|
return new LightClassConstructionContext(cacheManager.getLightClassContextCache().getLightClassContext(classOrObject), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
p.end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void forceResolvePackageDeclarations(@NotNull Collection<JetFile> files, @NotNull KotlinCodeAnalyzer session) {
|
||||||
for (JetFile file : files) {
|
for (JetFile file : files) {
|
||||||
// Scripts are not supported
|
// Scripts are not supported
|
||||||
if (file.isScript()) continue;
|
if (file.isScript()) continue;
|
||||||
@@ -127,11 +156,7 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (JetDeclaration declaration : file.getDeclarations()) {
|
for (JetDeclaration declaration : file.getDeclarations()) {
|
||||||
if (declaration instanceof JetClassOrObject) {
|
if (declaration instanceof JetFunction) {
|
||||||
ClassDescriptor descriptor = session.getClassDescriptor((JetClassOrObject) declaration);
|
|
||||||
forceResolveAllContents(descriptor);
|
|
||||||
}
|
|
||||||
else if (declaration instanceof JetFunction) {
|
|
||||||
JetFunction jetFunction = (JetFunction) declaration;
|
JetFunction jetFunction = (JetFunction) declaration;
|
||||||
Name name = jetFunction.getNameAsSafeName();
|
Name name = jetFunction.getNameAsSafeName();
|
||||||
Collection<FunctionDescriptor> functions = packageDescriptor.getMemberScope().getFunctions(name);
|
Collection<FunctionDescriptor> functions = packageDescriptor.getMemberScope().getFunctions(name);
|
||||||
@@ -147,6 +172,9 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport
|
|||||||
forceResolveAllContents(descriptor);
|
forceResolveAllContents(descriptor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (declaration instanceof JetClassOrObject) {
|
||||||
|
// Do nothing: we are not interested in classes
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
LOG.error("Unsupported declaration kind: " + declaration + " in file " + file.getName() + "\n" + file.getText());
|
LOG.error("Unsupported declaration kind: " + declaration + " in file " + file.getName() + "\n" + file.getText());
|
||||||
}
|
}
|
||||||
@@ -161,13 +189,6 @@ public class IDELightClassGenerationSupport extends LightClassGenerationSupport
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public LightClassConstructionContext analyzeRelevantCode(@NotNull JetClassOrObject classOrObject) {
|
|
||||||
KotlinCacheManager cacheManager = KotlinCacheManager.getInstance(project);
|
|
||||||
return new LightClassConstructionContext(cacheManager.getLightClassContextCache().getLightClassContext(classOrObject), null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public Collection<JetClassOrObject> findClassOrObjectDeclarations(@NotNull FqName fqName, @NotNull GlobalSearchScope searchScope) {
|
public Collection<JetClassOrObject> findClassOrObjectDeclarations(@NotNull FqName fqName, @NotNull GlobalSearchScope searchScope) {
|
||||||
|
|||||||
Reference in New Issue
Block a user