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