Search in sources :

Example 31 with FqName

use of org.jetbrains.kotlin.name.FqName in project kotlin by JetBrains.

the class SourceNavigationHelper method getOriginalClass.

@Nullable
public static PsiClass getOriginalClass(@NotNull KtClassOrObject classOrObject) {
    // Copied from JavaPsiImplementationHelperImpl:getOriginalClass()
    FqName fqName = classOrObject.getFqName();
    if (fqName == null) {
        return null;
    }
    KtFile file = classOrObject.getContainingKtFile();
    VirtualFile vFile = file.getVirtualFile();
    Project project = file.getProject();
    final ProjectFileIndex idx = ProjectRootManager.getInstance(project).getFileIndex();
    if (vFile == null || !idx.isInLibrarySource(vFile))
        return null;
    final Set<OrderEntry> orderEntries = new THashSet<OrderEntry>(idx.getOrderEntriesForFile(vFile));
    return JavaPsiFacade.getInstance(project).findClass(fqName.asString(), new GlobalSearchScope(project) {

        @Override
        public int compare(@NotNull VirtualFile file1, @NotNull VirtualFile file2) {
            return 0;
        }

        @Override
        public boolean contains(@NotNull VirtualFile file) {
            List<OrderEntry> entries = idx.getOrderEntriesForFile(file);
            for (OrderEntry entry : entries) {
                if (orderEntries.contains(entry))
                    return true;
            }
            return false;
        }

        @Override
        public boolean isSearchInModuleContent(@NotNull Module aModule) {
            return false;
        }

        @Override
        public boolean isSearchInLibraries() {
            return true;
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) OrderEntry(com.intellij.openapi.roots.OrderEntry) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) FqName(org.jetbrains.kotlin.name.FqName) List(java.util.List) Module(com.intellij.openapi.module.Module) THashSet(gnu.trove.THashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with FqName

use of org.jetbrains.kotlin.name.FqName in project kotlin by JetBrains.

the class SourceNavigationHelper method getInitialTopLevelCandidates.

@NotNull
private static Collection<KtNamedDeclaration> getInitialTopLevelCandidates(@NotNull KtNamedDeclaration declaration, @NotNull NavigationKind navigationKind) {
    FqName memberFqName = declaration.getFqName();
    assert memberFqName != null;
    GlobalSearchScope librarySourcesScope = createLibraryOrSourcesScope(declaration, navigationKind);
    if (librarySourcesScope == GlobalSearchScope.EMPTY_SCOPE) {
        // .getProject() == null for EMPTY_SCOPE, and this breaks code
        return Collections.emptyList();
    }
    //noinspection unchecked
    StringStubIndexExtension<KtNamedDeclaration> index = (StringStubIndexExtension<KtNamedDeclaration>) getIndexForTopLevelPropertyOrFunction(declaration);
    return index.get(memberFqName.asString(), declaration.getProject(), librarySourcesScope);
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) FqName(org.jetbrains.kotlin.name.FqName) StringStubIndexExtension(com.intellij.psi.stubs.StringStubIndexExtension) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with FqName

use of org.jetbrains.kotlin.name.FqName in project kotlin by JetBrains.

the class ExecuteKotlinScriptMojo method executeScriptFile.

private void executeScriptFile(File scriptFile) throws MojoExecutionException {
    initCompiler();
    Disposable rootDisposable = Disposer.newDisposable();
    try {
        MavenPluginLogMessageCollector messageCollector = new MavenPluginLogMessageCollector(getLog());
        CompilerConfiguration configuration = new CompilerConfiguration();
        configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector);
        List<File> deps = new ArrayList<File>();
        deps.addAll(PathUtil.getJdkClassesRoots());
        deps.addAll(getDependenciesForScript());
        for (File item : deps) {
            if (item.exists()) {
                configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, new JvmClasspathRoot(item));
                getLog().debug("Adding to classpath: " + item.getAbsolutePath());
            } else {
                getLog().debug("Skipping non-existing dependency: " + item.getAbsolutePath());
            }
        }
        configuration.add(JVMConfigurationKeys.CONTENT_ROOTS, new KotlinSourceRoot(scriptFile.getAbsolutePath()));
        configuration.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME);
        K2JVMCompiler.Companion.configureScriptDefinitions(scriptTemplates.toArray(new String[scriptTemplates.size()]), configuration, messageCollector, new HashMap<String, Object>());
        KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
        GenerationState state = KotlinToJVMBytecodeCompiler.INSTANCE.analyzeAndGenerate(environment);
        if (state == null) {
            throw new ScriptExecutionException(scriptFile, "compile error");
        }
        GeneratedClassLoader classLoader = new GeneratedClassLoader(state.getFactory(), getClass().getClassLoader());
        KtScript script = environment.getSourceFiles().get(0).getScript();
        FqName nameForScript = script.getFqName();
        try {
            Class<?> klass = classLoader.loadClass(nameForScript.asString());
            ExecuteKotlinScriptMojo.INSTANCE = this;
            if (ReflectionUtilKt.tryConstructClassFromStringArgs(klass, scriptArguments) == null)
                throw new ScriptExecutionException(scriptFile, "unable to construct script");
        } catch (ClassNotFoundException e) {
            throw new ScriptExecutionException(scriptFile, "internal error", e);
        }
    } finally {
        rootDisposable.dispose();
        ExecuteKotlinScriptMojo.INSTANCE = null;
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) KtScript(org.jetbrains.kotlin.psi.KtScript) KotlinSourceRoot(org.jetbrains.kotlin.config.KotlinSourceRoot) FqName(org.jetbrains.kotlin.name.FqName) ArrayList(java.util.ArrayList) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) JvmClasspathRoot(org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot) GeneratedClassLoader(org.jetbrains.kotlin.codegen.GeneratedClassLoader) KotlinCoreEnvironment(org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment) CompilerConfiguration(org.jetbrains.kotlin.config.CompilerConfiguration) File(java.io.File)

Example 34 with FqName

use of org.jetbrains.kotlin.name.FqName in project kotlin by JetBrains.

the class ResolveDescriptorsFromExternalLibraries method parseLibraryFileChunk.

private static boolean parseLibraryFileChunk(File jar, String libDescription, ZipInputStream zip, int classesPerChunk) throws IOException {
    Disposable junk = new Disposable() {

        @Override
        public void dispose() {
        }
    };
    KotlinCoreEnvironment environment;
    if (jar != null) {
        environment = KotlinCoreEnvironment.createForTests(junk, KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, KotlinTestUtils.getAnnotationsJar(), jar), EnvironmentConfigFiles.JVM_CONFIG_FILES);
    } else {
        CompilerConfiguration configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.JDK_ONLY, TestJdkKind.FULL_JDK);
        environment = KotlinCoreEnvironment.createForTests(junk, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES);
        if (!findRtJar().equals(jar)) {
            throw new RuntimeException("rt.jar mismatch: " + jar + ", " + findRtJar());
        }
    }
    ModuleDescriptor module = JvmResolveUtil.analyze(environment).getModuleDescriptor();
    boolean hasErrors;
    try {
        hasErrors = false;
        for (int count = 0; count < classesPerChunk; ) {
            ZipEntry entry = zip.getNextEntry();
            if (entry == null) {
                break;
            }
            if (count == 0) {
                System.err.println("chunk from " + entry.getName());
            }
            System.err.println(entry.getName());
            String entryName = entry.getName();
            if (!entryName.endsWith(".class")) {
                continue;
            }
            if (entryName.matches("(.*/|)package-info\\.class")) {
                continue;
            }
            if (entryName.contains("$")) {
                continue;
            }
            String className = entryName.substring(0, entryName.length() - ".class".length()).replace("/", ".");
            try {
                ClassDescriptor clazz = DescriptorUtilsKt.resolveTopLevelClass(module, new FqName(className), NoLookupLocation.FROM_TEST);
                if (clazz == null) {
                    throw new IllegalStateException("class not found by name " + className + " in " + libDescription);
                }
                DescriptorUtils.getAllDescriptors(clazz.getDefaultType().getMemberScope());
            } catch (Exception e) {
                System.err.println("failed to resolve " + className);
                e.printStackTrace();
                //throw new RuntimeException("failed to resolve " + className + ": " + e, e);
                hasErrors = true;
            }
            ++count;
        }
    } finally {
        Disposer.dispose(junk);
    }
    return hasErrors;
}
Also used : Disposable(com.intellij.openapi.Disposable) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) FqName(org.jetbrains.kotlin.name.FqName) ZipEntry(java.util.zip.ZipEntry) ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) KotlinCoreEnvironment(org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment) CompilerConfiguration(org.jetbrains.kotlin.config.CompilerConfiguration)

Example 35 with FqName

use of org.jetbrains.kotlin.name.FqName in project kotlin by JetBrains.

the class TypeUnifierTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    ComponentProvider container = JvmResolveUtil.createContainer(getEnvironment());
    module = DslKt.getService(container, ModuleDescriptor.class);
    builtinsImportingScope = ScopeUtilsKt.chainImportingScopes(CollectionsKt.map(KotlinBuiltIns.BUILT_INS_PACKAGE_FQ_NAMES, new Function1<FqName, ImportingScope>() {

        @Override
        public ImportingScope invoke(FqName fqName) {
            return ScopeUtilsKt.memberScopeAsImportingScope(module.getPackage(fqName).getMemberScope());
        }
    }), null);
    typeResolver = DslKt.getService(container, TypeResolver.class);
    x = createTypeVariable("X");
    y = createTypeVariable("Y");
    variables = Sets.newHashSet(x.getTypeConstructor(), y.getTypeConstructor());
}
Also used : ModuleDescriptor(org.jetbrains.kotlin.descriptors.ModuleDescriptor) FqName(org.jetbrains.kotlin.name.FqName) TypeResolver(org.jetbrains.kotlin.resolve.TypeResolver) ComponentProvider(org.jetbrains.kotlin.container.ComponentProvider)

Aggregations

FqName (org.jetbrains.kotlin.name.FqName)74 NotNull (org.jetbrains.annotations.NotNull)25 Nullable (org.jetbrains.annotations.Nullable)15 KtFile (org.jetbrains.kotlin.psi.KtFile)10 StringRef (com.intellij.util.io.StringRef)6 File (java.io.File)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)4 Name (org.jetbrains.kotlin.name.Name)4 PsiClass (com.intellij.psi.PsiClass)3 ClassId (org.jetbrains.kotlin.name.ClassId)3 Disposable (com.intellij.openapi.Disposable)2 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2 SmartList (com.intellij.util.SmartList)2 Function1 (kotlin.jvm.functions.Function1)2 KotlinCoreEnvironment (org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment)2 CompilerConfiguration (org.jetbrains.kotlin.config.CompilerConfiguration)2 JavaClassDescriptor (org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor)2 KtClassOrObject (org.jetbrains.kotlin.psi.KtClassOrObject)2