Continue to look for descriptors if IGNORE_KOTLIN_SOURCES was passed as DescriptorSearchRule

Add comment to DesriptorSearchRule explaining why we should get rid of it asap
Remove useless code from DesriptorSearchRule
This commit is contained in:
Pavel V. Talanov
2013-09-02 15:25:43 +04:00
parent f700266b11
commit 217312ab78
7 changed files with 94 additions and 34 deletions
@@ -0,0 +1,10 @@
package test
public object Lol {
fun c() {
}
}
@@ -54,19 +54,12 @@ public abstract class AbstractCompileKotlinAgainstCustomBinariesTest extends Tes
File ktFile = new File(ktFilePath);
String testFileWithoutExtension = FileUtil.getNameWithoutExtension(ktFile);
checkCompiledNamespace(ktFile, LoadDescriptorUtil.TEST_PACKAGE_FQNAME,
new File(ktFile.getParentFile(), testFileWithoutExtension + ".txt"));
checkNamespace(ktFile, LoadDescriptorUtil.TEST_PACKAGE_FQNAME,
new File(ktFile.getParentFile(), testFileWithoutExtension + ".txt"));
}
private void checkCompiledNamespace(File ktFile, FqName namespaceFqn, File expectedFile) throws IOException {
JetCoreEnvironment environment = getEnvironment(ktFile);
Project project = environment.getProject();
List<JetFile> jetFiles = Collections.singletonList(JetTestUtils.loadJetFile(project, ktFile));
BindingContext bindingContext = AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
project, jetFiles, Collections.<AnalyzerScriptParameter>emptyList(),
Predicates.<PsiFile>alwaysTrue()).getBindingContext();
private void checkNamespace(File ktFile, FqName namespaceFqn, File expectedFile) throws IOException {
BindingContext bindingContext = analyzeFile(ktFile);
NamespaceDescriptor namespaceDescriptor = bindingContext.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, namespaceFqn);
assertNotNull("Failed to find namespace: " + namespaceFqn, namespaceDescriptor);
@@ -75,6 +68,17 @@ public abstract class AbstractCompileKotlinAgainstCustomBinariesTest extends Tes
DescriptorValidator.ValidationVisitor.ALLOW_ERROR_TYPES), expectedFile);
}
protected BindingContext analyzeFile(File ktFile) throws IOException {
JetCoreEnvironment environment = getEnvironment(ktFile);
Project project = environment.getProject();
List<JetFile> jetFiles = Collections.singletonList(JetTestUtils.loadJetFile(project, ktFile));
return AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
project, jetFiles, Collections.<AnalyzerScriptParameter>emptyList(),
Predicates.<PsiFile>alwaysTrue()).getBindingContext();
}
private JetCoreEnvironment getEnvironment(File ktFile) {
File dir = ktFile.getParentFile();
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed 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.jetbrains.jet.jvm.compiler;
import junit.framework.Assert;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.descriptors.VariableDescriptorForObject;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.ErrorUtils;
import java.io.File;
import java.util.Collection;
import static org.jetbrains.jet.jvm.compiler.LoadDescriptorUtil.TEST_PACKAGE_FQNAME;
import static org.jetbrains.jet.lang.resolve.BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR;
public final class CompileKotlinAgainstBinariesCustomTest extends AbstractCompileKotlinAgainstCustomBinariesTest {
public void testDuplicateObjectInSourcesAndBinaries() throws Exception {
BindingContext context = analyzeFile(new File(
"compiler/testData/compileKotlinAgainstBinariesCustom/duplicateObjectInBinaryAndSources/duplicateObjectInBinaryAndSources.kt"));
NamespaceDescriptor namespaceDescriptor = context.get(FQNAME_TO_NAMESPACE_DESCRIPTOR, TEST_PACKAGE_FQNAME);
assert namespaceDescriptor != null;
Collection<DeclarationDescriptor> allDescriptors = namespaceDescriptor.getMemberScope().getAllDescriptors();
Assert.assertEquals(allDescriptors.size(), 2);
for (DeclarationDescriptor descriptor : allDescriptors) {
Assert.assertTrue(descriptor.getName().asString().equals("Lol"));
Assert.assertTrue(descriptor instanceof VariableDescriptorForObject);
Assert.assertFalse("Object property should have valid class", ErrorUtils.isError(((VariableDescriptorForObject) descriptor).getObjectClass()));
}
}
}
@@ -16,21 +16,18 @@
package org.jetbrains.jet.lang.resolve.java;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
/*
* This class indicates whether we should look into kotlin sources when searching for descriptors in JavaDescriptorResolver.
* This is a hack because it should be done via correct scopes.
* Order in which we attempt to resolve descriptors is also should be taken care of. (though it is correct for the most part now)
* */
public enum DescriptorSearchRule {
INCLUDE_KOTLIN_SOURCES {
@Override
public <T extends DeclarationDescriptor> T processFoundInKotlin(T foundDescriptor) {
return foundDescriptor;
}
},
IGNORE_KOTLIN_SOURCES {
@Override
public <T extends DeclarationDescriptor> T processFoundInKotlin(T foundDescriptor) {
return null;
}
};
public abstract <T extends DeclarationDescriptor> T processFoundInKotlin(T foundDescriptor);
//Return immediately if you found descriptor in kotlin sources, if not continue
INCLUDE_KOTLIN_SOURCES,
//Do not try to find descriptors in kotlin sources.
//This flag is mostly used when resolving descriptors from binaries or java descriptors.
//It will not prevent from looking into java sources which is often desirable behaviour.
//It is not correct because sometimes class from sources can override class from binary since it comes earlier in classpath
//and for a thousand more reasons.
IGNORE_KOTLIN_SOURCES
}
@@ -149,9 +149,11 @@ public final class JavaClassResolver {
return builtinClassDescriptor;
}
ClassDescriptor kotlinClassDescriptor = cache.getClassResolvedFromSource(qualifiedName);
if (kotlinClassDescriptor != null) {
return searchRule.processFoundInKotlin(kotlinClassDescriptor);
if (searchRule == INCLUDE_KOTLIN_SOURCES) {
ClassDescriptor kotlinClassDescriptor = cache.getClassResolvedFromSource(qualifiedName);
if (kotlinClassDescriptor != null) {
return kotlinClassDescriptor;
}
}
FqNameUnsafe fqName = javaClassToKotlinFqName(qualifiedName);
@@ -90,10 +90,11 @@ public final class JavaNamespaceResolver {
@Nullable
public NamespaceDescriptor resolveNamespace(@NotNull FqName qualifiedName, @NotNull DescriptorSearchRule searchRule) {
// First, let's check that there is no Kotlin package:
NamespaceDescriptor kotlinNamespaceDescriptor = cache.getPackageResolvedFromSource(qualifiedName);
if (kotlinNamespaceDescriptor != null) {
return searchRule.processFoundInKotlin(kotlinNamespaceDescriptor);
if (searchRule == INCLUDE_KOTLIN_SOURCES) {
NamespaceDescriptor kotlinNamespaceDescriptor = cache.getPackageResolvedFromSource(qualifiedName);
if (kotlinNamespaceDescriptor != null) {
return kotlinNamespaceDescriptor;
}
}
if (unresolvedCache.contains(qualifiedName)) {