001    /*
002     * Copyright 2010-2014 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.resolve.kotlin;
018    
019    import com.intellij.openapi.util.Condition;
020    import com.intellij.openapi.util.io.FileUtil;
021    import com.intellij.openapi.vfs.VirtualFile;
022    import com.intellij.util.PathUtil;
023    import com.intellij.util.containers.ContainerUtil;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.jet.descriptors.serialization.descriptors.DeserializedCallableMemberDescriptor;
026    import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor;
027    import org.jetbrains.jet.lang.psi.JetDeclaration;
028    import org.jetbrains.jet.lang.psi.JetFile;
029    import org.jetbrains.jet.lang.psi.JetNamedFunction;
030    import org.jetbrains.jet.lang.psi.JetProperty;
031    import org.jetbrains.jet.lang.resolve.java.JvmClassName;
032    import org.jetbrains.jet.lang.resolve.name.FqName;
033    import org.jetbrains.jet.lang.resolve.name.Name;
034    import org.jetbrains.org.objectweb.asm.Type;
035    
036    import java.util.Collection;
037    import java.util.List;
038    
039    import static org.jetbrains.jet.lang.resolve.java.PackageClassUtils.getPackageClassFqName;
040    
041    public class PackagePartClassUtils {
042        public static int getPathHashCode(@NotNull VirtualFile file) {
043            // Conversion to system-dependent name seems to be unnecessary, but it's hard to check now:
044            // it was introduced when fixing KT-2839, which appeared again (KT-3639).
045            // If you try to remove it, run tests on Windows.
046            return FileUtil.toSystemDependentName(file.getPath()).hashCode();
047        }
048    
049        @NotNull
050        private static String replaceSpecialSymbols(@NotNull String str) {
051            return str.replace('.', '_');
052        }
053    
054        @NotNull
055        public static FqName getPackagePartFqName(@NotNull FqName facadeFqName, @NotNull VirtualFile file) {
056            String fileName = FileUtil.getNameWithoutExtension(PathUtil.getFileName(file.getName()));
057    
058            // path hashCode to prevent same name / different path collision
059            String srcName = String.format(
060                    "%s$%s$%08x",
061                    facadeFqName.shortName().asString(),
062                    replaceSpecialSymbols(fileName),
063                    getPathHashCode(file)
064            );
065    
066            return facadeFqName.parent().child(Name.identifier(srcName));
067        }
068    
069        @NotNull
070        public static Type getPackagePartType(@NotNull JetFile file) {
071            return Type.getObjectType(getPackagePartInternalName(file));
072        }
073    
074        @NotNull
075        public static String getPackagePartInternalName(@NotNull JetFile file) {
076            FqName fqName = getPackagePartFqName(file);
077            return JvmClassName.byFqNameWithoutInnerClasses(fqName).getInternalName();
078        }
079    
080        @NotNull
081        public static FqName getPackagePartFqName(@NotNull JetFile file) {
082            return getPackagePartFqName(getPackageClassFqName(file.getPackageFqName()), file.getVirtualFile());
083        }
084    
085        @NotNull
086        public static FqName getPackagePartFqName(@NotNull DeserializedCallableMemberDescriptor callable) {
087            FqName packageFqName = ((PackageFragmentDescriptor) callable.getContainingDeclaration()).getFqName();
088            return packageFqName.child(BaseDescriptorLoader.getPackagePartClassName(callable));
089        }
090    
091        @NotNull
092        public static List<JetFile> getPackageFilesWithCallables(@NotNull Collection<JetFile> packageFiles) {
093            return ContainerUtil.filter(packageFiles, new Condition<JetFile>() {
094                @Override
095                public boolean value(JetFile packageFile) {
096                    for (JetDeclaration declaration : packageFile.getDeclarations()) {
097                        if (declaration instanceof JetProperty || declaration instanceof JetNamedFunction) {
098                            return true;
099                        }
100                    }
101                    return false;
102                }
103            });
104        }
105    }