001    /*
002     * Copyright 2010-2013 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.types.lang;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.jet.descriptors.serialization.ClassId;
021    import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
022    import org.jetbrains.jet.lang.resolve.DescriptorUtils;
023    import org.jetbrains.jet.lang.resolve.name.FqName;
024    import org.jetbrains.jet.lang.resolve.name.FqNameUnsafe;
025    import org.jetbrains.jet.lang.resolve.name.Name;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    import static org.jetbrains.jet.lang.resolve.name.SpecialNames.isClassObjectName;
031    
032    public class BuiltInsSerializationUtil {
033        private static final String CLASS_METADATA_FILE_EXTENSION = "kotlin_class";
034        private static final String PACKAGE_FILE_NAME = ".kotlin_package";
035        private static final String NAME_TABLE_FILE_NAME = ".kotlin_name_table";
036        private static final String CLASS_NAMES_FILE_NAME = ".kotlin_class_names";
037        private static final String CLASS_OBJECT_NAME = "object";
038    
039        private BuiltInsSerializationUtil() {
040        }
041    
042        @NotNull
043        public static String relativeClassNameToFilePath(@NotNull FqNameUnsafe className) {
044            List<Name> segments = className.pathSegments();
045            List<String> correctedSegments = new ArrayList<String>(segments.size());
046            for (Name segment : segments) {
047                correctedSegments.add(isClassObjectName(segment) ? CLASS_OBJECT_NAME : segment.getIdentifier());
048            }
049            return FqName.fromSegments(correctedSegments).asString();
050        }
051    
052        @NotNull
053        public static String getClassMetadataPath(@NotNull ClassId classId) {
054            return packageFqNameToPath(classId.getPackageFqName().toUnsafe())
055                   + "/" + relativeClassNameToFilePath(classId.getRelativeClassName())
056                   + "." + CLASS_METADATA_FILE_EXTENSION;
057        }
058    
059        @NotNull
060        public static String getPackageFilePath(@NotNull NamespaceDescriptor packageDescriptor) {
061            FqNameUnsafe fqName = DescriptorUtils.getFQName(packageDescriptor);
062            return packageFqNameToPath(fqName) + "/" + PACKAGE_FILE_NAME;
063        }
064    
065        @NotNull
066        public static String getNameTableFilePath(@NotNull NamespaceDescriptor packageDescriptor) {
067            FqNameUnsafe fqName = DescriptorUtils.getFQName(packageDescriptor);
068            return packageFqNameToPath(fqName) + "/" + NAME_TABLE_FILE_NAME;
069        }
070    
071        @NotNull
072        public static String getClassNamesFilePath(@NotNull NamespaceDescriptor packageDescriptor) {
073            FqNameUnsafe fqName = DescriptorUtils.getFQName(packageDescriptor);
074            return packageFqNameToPath(fqName) + "/" + CLASS_NAMES_FILE_NAME;
075        }
076    
077        private static String packageFqNameToPath(FqNameUnsafe fqName) {
078            return fqName.asString().replace('.', '/');
079        }
080    }