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 = facadeFqName.shortName().asString() + "-" + replaceSpecialSymbols(fileName) + "-" + Integer.toHexString( 060 getPathHashCode(file)); 061 062 return facadeFqName.parent().child(Name.identifier(srcName)); 063 } 064 065 @NotNull 066 public static Type getPackagePartType(@NotNull JetFile file) { 067 return Type.getObjectType(getPackagePartInternalName(file)); 068 } 069 070 @NotNull 071 public static String getPackagePartInternalName(@NotNull JetFile file) { 072 FqName fqName = getPackagePartFqName(getPackageClassFqName(file.getPackageFqName()), file.getVirtualFile()); 073 return JvmClassName.byFqNameWithoutInnerClasses(fqName).getInternalName(); 074 } 075 076 @NotNull 077 public static FqName getPackagePartFqName(@NotNull DeserializedCallableMemberDescriptor callable) { 078 FqName packageFqName = ((PackageFragmentDescriptor) callable.getContainingDeclaration()).getFqName(); 079 return packageFqName.child(BaseDescriptorLoader.getPackagePartClassName(callable)); 080 } 081 082 @NotNull 083 public static List<JetFile> getPackageFilesWithCallables(@NotNull Collection<JetFile> packageFiles) { 084 return ContainerUtil.filter(packageFiles, new Condition<JetFile>() { 085 @Override 086 public boolean value(JetFile packageFile) { 087 for (JetDeclaration declaration : packageFile.getDeclarations()) { 088 if (declaration instanceof JetProperty || declaration instanceof JetNamedFunction) { 089 return true; 090 } 091 } 092 return false; 093 } 094 }); 095 } 096 }